001package co.codewizards.cloudstore.rest.server.service; 002 003import static co.codewizards.cloudstore.core.util.Util.*; 004 005import java.io.File; 006import java.util.UUID; 007 008import javax.ws.rs.Consumes; 009import javax.ws.rs.GET; 010import javax.ws.rs.Path; 011import javax.ws.rs.PathParam; 012import javax.ws.rs.Produces; 013import javax.ws.rs.core.MediaType; 014 015import org.slf4j.Logger; 016import org.slf4j.LoggerFactory; 017//import co.codewizards.cloudstore.core.repo.local.LocalRepoRegistry; 018 019import co.codewizards.cloudstore.core.auth.AuthToken; 020import co.codewizards.cloudstore.core.auth.AuthTokenIO; 021import co.codewizards.cloudstore.core.auth.AuthTokenSigner; 022import co.codewizards.cloudstore.core.auth.EncryptedSignedAuthToken; 023import co.codewizards.cloudstore.core.auth.SignedAuthToken; 024import co.codewizards.cloudstore.core.auth.SignedAuthTokenEncrypter; 025import co.codewizards.cloudstore.core.auth.SignedAuthTokenIO; 026import co.codewizards.cloudstore.core.repo.local.LocalRepoManager; 027import co.codewizards.cloudstore.core.repo.local.LocalRepoManagerFactory; 028import co.codewizards.cloudstore.core.repo.local.LocalRepoRegistry; 029import co.codewizards.cloudstore.rest.server.auth.TransientRepoPassword; 030import co.codewizards.cloudstore.rest.server.auth.TransientRepoPasswordManager; 031 032@Path("_EncryptedSignedAuthToken/{repositoryName}") 033@Consumes(MediaType.APPLICATION_XML) 034@Produces(MediaType.APPLICATION_XML) 035public class EncryptedSignedAuthTokenService 036{ 037 private static final Logger logger = LoggerFactory.getLogger(EncryptedSignedAuthTokenService.class); 038 039 { 040 logger.debug("<init>: created new instance"); 041 } 042 043 private @PathParam("repositoryName") String repositoryName; 044 045 @GET 046 @Path("{clientRepositoryId}") 047 public EncryptedSignedAuthToken getEncryptedSignedAuthToken(@PathParam("clientRepositoryId") UUID clientRepositoryId) 048 { 049 assertNotNull("repositoryName", repositoryName); 050 assertNotNull("clientRepositoryId", clientRepositoryId); 051 File localRoot = LocalRepoRegistry.getInstance().getLocalRootForRepositoryNameOrFail(repositoryName); 052 LocalRepoManager localRepoManager = LocalRepoManagerFactory.Helper.getInstance().createLocalRepoManagerForExistingRepository(localRoot); 053 try { 054 EncryptedSignedAuthToken result = getEncryptedSignedAuthToken( 055 localRepoManager.getRepositoryId(), clientRepositoryId, 056 localRepoManager.getPrivateKey(), localRepoManager.getRemoteRepositoryPublicKeyOrFail(clientRepositoryId)); 057 return result; 058 } finally { 059 localRepoManager.close(); 060 } 061 } 062 063 protected EncryptedSignedAuthToken getEncryptedSignedAuthToken( 064 UUID serverRepositoryId, UUID clientRepositoryId, byte[] localRepoPrivateKey, byte[] remoteRepoPublicKey) 065 { 066 TransientRepoPassword transientRepoPassword = TransientRepoPasswordManager.getInstance().getCurrentAuthRepoPassword(serverRepositoryId, clientRepositoryId); 067 068 AuthToken authToken = transientRepoPassword.getAuthToken(); 069 byte[] authTokenData = new AuthTokenIO().serialise(authToken); 070 SignedAuthToken signedAuthToken = new AuthTokenSigner(localRepoPrivateKey).sign(authTokenData); 071 072 byte[] signedAuthTokenData = new SignedAuthTokenIO().serialise(signedAuthToken); 073 EncryptedSignedAuthToken encryptedSignedAuthToken = 074 new SignedAuthTokenEncrypter(remoteRepoPublicKey).encrypt(signedAuthTokenData); 075 076 return encryptedSignedAuthToken; 077 } 078}