@Test public void testShareFile() throws Exception { final SDSNodeIdProvider nodeid = new SDSNodeIdProvider(session).withCache(cache); final Path room = new SDSDirectoryFeature(session, nodeid).mkdir(new Path(new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory, Path.Type.volume, Path.Type.triplecrypt)), null, new TransferStatus()); final Path test = new SDSTouchFeature(session, nodeid).touch(new Path(room, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file)), new TransferStatus()); final DescriptiveUrl url = new SDSSharesUrlProvider(session, nodeid).toDownloadUrl(test, new CreateDownloadShareRequest() .expiration(new ObjectExpiration().enableExpiration(false)) .notifyCreator(false) .sendMail(false) .sendSms(false) .password(null) .mailRecipients(null) .mailSubject(null) .mailBody(null) .maxDownloads(null), new DisabledPasswordCallback()); assertNotEquals(DescriptiveUrl.EMPTY, url); assertEquals(DescriptiveUrl.Type.signed, url.getType()); assertTrue(url.getUrl().startsWith("https: new SDSDeleteFeature(session, nodeid).delete(Collections.singletonList(room), new DisabledLoginCallback(), new Delete.DisabledCallback()); }
@Override public DescriptiveUrl toDownloadUrl(final Path file, CreateDownloadShareRequest options, final PasswordCallback callback) throws BackgroundException { try { if(log.isDebugEnabled()) { log.debug(String.format("Create download share for %s", file)); } if(null == options) { options = new CreateDownloadShareRequest(); log.warn(String.format("Use default share options %s", options)); } final Long fileid = Long.parseLong(nodeid.getFileid(file, new DisabledListProgressListener())); final Host bookmark = session.getHost(); if(nodeid.isEncrypted(file)) { final FileKey key = new NodesApi(session.getClient()).requestUserFileKey(fileid, StringUtils.EMPTY); final UserPrivateKey privateKey = new UserPrivateKey(); final UserKeyPairContainer keyPairContainer = session.keyPair(); privateKey.setPrivateKey(keyPairContainer.getPrivateKeyContainer().getPrivateKey()); privateKey.setVersion(keyPairContainer.getPrivateKeyContainer().getVersion()); final UserKeyPair userKeyPair = new UserKeyPair(); userKeyPair.setUserPrivateKey(privateKey); final Credentials passphrase = new TripleCryptKeyPair().unlock(callback, bookmark, userKeyPair); final PlainFileKey plainFileKey = Crypto.decryptFileKey(TripleCryptConverter.toCryptoEncryptedFileKey(key), privateKey, passphrase.getPassword()); final UserKeyPair pair; if(null == options.getPassword()) { pair = Crypto.generateUserKeyPair(callback.prompt( bookmark, LocaleFactory.localizedString("Passphrase", "Cryptomator"), LocaleFactory.localizedString("Provide additional login credentials", "Credentials"), new LoginOptions().icon(session.getHost().getProtocol().disk()) ).getPassword()); } else { pair = Crypto.generateUserKeyPair(options.getPassword()); } final EncryptedFileKey encryptedFileKey = Crypto.encryptFileKey(plainFileKey, pair.getUserPublicKey()); options.setPassword(null); options.setKeyPair(TripleCryptConverter.toSwaggerUserKeyPairContainer(pair)); options.setFileKey(TripleCryptConverter.toSwaggerFileKey(encryptedFileKey)); } final DownloadShare share = new SharesApi(session.getClient()).createDownloadShare( options.nodeId(fileid), StringUtils.EMPTY, null); final String help; if(null == share.getExpireAt()) { help = MessageFormat.format(LocaleFactory.localizedString("{0} URL"), LocaleFactory.localizedString("Pre-Signed", "S3")); } else { final long expiry = share.getExpireAt().getMillis(); help = MessageFormat.format(LocaleFactory.localizedString("{0} URL"), LocaleFactory.localizedString("Pre-Signed", "S3")) + " (" + MessageFormat.format(LocaleFactory.localizedString("Expires {0}", "S3") + ")", UserDateFormatterFactory.get().getShortFormat(expiry * 1000) ); } return new DescriptiveUrl( URI.create(String.format("%s: bookmark.getProtocol().getScheme(), bookmark.getHostname(), share.getAccessKey()) ), DescriptiveUrl.Type.signed, help); } catch(ApiException e) { throw new SDSExceptionMappingService().map(e); } catch(CryptoException e) { throw new TripleCryptExceptionMappingService().map(e); } }
SDSSharesUrlProvider implements PromptUrlProvider<CreateDownloadShareRequest, CreateUploadShareRequest> { @Override public DescriptiveUrl toDownloadUrl(final Path file, CreateDownloadShareRequest options, final PasswordCallback callback) throws BackgroundException { try { if(log.isDebugEnabled()) { log.debug(String.format("Create download share for %s", file)); } if(null == options) { options = new CreateDownloadShareRequest(); log.warn(String.format("Use default share options %s", options)); } final Long fileid = Long.parseLong(nodeid.getFileid(file, new DisabledListProgressListener())); final Host bookmark = session.getHost(); if(nodeid.isEncrypted(file)) { final FileKey key = new NodesApi(session.getClient()).requestUserFileKey(fileid, StringUtils.EMPTY); final UserPrivateKey privateKey = new UserPrivateKey(); final UserKeyPairContainer keyPairContainer = session.keyPair(); privateKey.setPrivateKey(keyPairContainer.getPrivateKeyContainer().getPrivateKey()); privateKey.setVersion(keyPairContainer.getPrivateKeyContainer().getVersion()); final UserKeyPair userKeyPair = new UserKeyPair(); userKeyPair.setUserPrivateKey(privateKey); final Credentials passphrase = new TripleCryptKeyPair().unlock(callback, bookmark, userKeyPair); final PlainFileKey plainFileKey = Crypto.decryptFileKey(TripleCryptConverter.toCryptoEncryptedFileKey(key), privateKey, passphrase.getPassword()); final UserKeyPair pair; if(null == options.getPassword()) { pair = Crypto.generateUserKeyPair(callback.prompt( bookmark, LocaleFactory.localizedString("Passphrase", "Cryptomator"), LocaleFactory.localizedString("Provide additional login credentials", "Credentials"), new LoginOptions().icon(session.getHost().getProtocol().disk()) ).getPassword()); } else { pair = Crypto.generateUserKeyPair(options.getPassword()); } final EncryptedFileKey encryptedFileKey = Crypto.encryptFileKey(plainFileKey, pair.getUserPublicKey()); options.setPassword(null); options.setKeyPair(TripleCryptConverter.toSwaggerUserKeyPairContainer(pair)); options.setFileKey(TripleCryptConverter.toSwaggerFileKey(encryptedFileKey)); } final DownloadShare share = new SharesApi(session.getClient()).createDownloadShare( options.nodeId(fileid), StringUtils.EMPTY, null); final String help; if(null == share.getExpireAt()) { help = MessageFormat.format(LocaleFactory.localizedString("{0} URL"), LocaleFactory.localizedString("Pre-Signed", "S3")); } else { final long expiry = share.getExpireAt().getMillis(); help = MessageFormat.format(LocaleFactory.localizedString("{0} URL"), LocaleFactory.localizedString("Pre-Signed", "S3")) + " (" + MessageFormat.format(LocaleFactory.localizedString("Expires {0}", "S3") + ")", UserDateFormatterFactory.get().getShortFormat(expiry * 1000) ); } return new DescriptiveUrl( URI.create(String.format("%s: bookmark.getProtocol().getScheme(), bookmark.getHostname(), share.getAccessKey()) ), DescriptiveUrl.Type.signed, help); } catch(ApiException e) { throw new SDSExceptionMappingService().map(e); } catch(CryptoException e) { throw new TripleCryptExceptionMappingService().map(e); } } }
SDSSharesUrlProvider implements PromptUrlProvider<CreateDownloadShareRequest, CreateUploadShareRequest> { @Override public DescriptiveUrl toDownloadUrl(final Path file, CreateDownloadShareRequest options, final PasswordCallback callback) throws BackgroundException { try { if(log.isDebugEnabled()) { log.debug(String.format("Create download share for %s", file)); } if(null == options) { options = new CreateDownloadShareRequest(); log.warn(String.format("Use default share options %s", options)); } final Long fileid = Long.parseLong(nodeid.getFileid(file, new DisabledListProgressListener())); final Host bookmark = session.getHost(); if(nodeid.isEncrypted(file)) { final FileKey key = new NodesApi(session.getClient()).requestUserFileKey(fileid, StringUtils.EMPTY); final UserPrivateKey privateKey = new UserPrivateKey(); final UserKeyPairContainer keyPairContainer = session.keyPair(); privateKey.setPrivateKey(keyPairContainer.getPrivateKeyContainer().getPrivateKey()); privateKey.setVersion(keyPairContainer.getPrivateKeyContainer().getVersion()); final UserKeyPair userKeyPair = new UserKeyPair(); userKeyPair.setUserPrivateKey(privateKey); final Credentials passphrase = new TripleCryptKeyPair().unlock(callback, bookmark, userKeyPair); final PlainFileKey plainFileKey = Crypto.decryptFileKey(TripleCryptConverter.toCryptoEncryptedFileKey(key), privateKey, passphrase.getPassword()); final UserKeyPair pair; if(null == options.getPassword()) { pair = Crypto.generateUserKeyPair(callback.prompt( bookmark, LocaleFactory.localizedString("Passphrase", "Cryptomator"), LocaleFactory.localizedString("Provide additional login credentials", "Credentials"), new LoginOptions().icon(session.getHost().getProtocol().disk()) ).getPassword()); } else { pair = Crypto.generateUserKeyPair(options.getPassword()); } final EncryptedFileKey encryptedFileKey = Crypto.encryptFileKey(plainFileKey, pair.getUserPublicKey()); options.setPassword(null); options.setKeyPair(TripleCryptConverter.toSwaggerUserKeyPairContainer(pair)); options.setFileKey(TripleCryptConverter.toSwaggerFileKey(encryptedFileKey)); } final DownloadShare share = new SharesApi(session.getClient()).createDownloadShare( options.nodeId(fileid), StringUtils.EMPTY, null); final String help; if(null == share.getExpireAt()) { help = MessageFormat.format(LocaleFactory.localizedString("{0} URL"), LocaleFactory.localizedString("Pre-Signed", "S3")); } else { final long expiry = share.getExpireAt().getMillis(); help = MessageFormat.format(LocaleFactory.localizedString("{0} URL"), LocaleFactory.localizedString("Pre-Signed", "S3")) + " (" + MessageFormat.format(LocaleFactory.localizedString("Expires {0}", "S3") + ")", UserDateFormatterFactory.get().getShortFormat(expiry * 1000) ); } return new DescriptiveUrl( URI.create(String.format("%s: bookmark.getProtocol().getScheme(), bookmark.getHostname(), share.getAccessKey()) ), DescriptiveUrl.Type.signed, help); } catch(ApiException e) { throw new SDSExceptionMappingService().map(e); } catch(CryptoException e) { throw new TripleCryptExceptionMappingService().map(e); } } SDSSharesUrlProvider(final SDSSession session, final SDSNodeIdProvider nodeid); }
SDSSharesUrlProvider implements PromptUrlProvider<CreateDownloadShareRequest, CreateUploadShareRequest> { @Override public DescriptiveUrl toDownloadUrl(final Path file, CreateDownloadShareRequest options, final PasswordCallback callback) throws BackgroundException { try { if(log.isDebugEnabled()) { log.debug(String.format("Create download share for %s", file)); } if(null == options) { options = new CreateDownloadShareRequest(); log.warn(String.format("Use default share options %s", options)); } final Long fileid = Long.parseLong(nodeid.getFileid(file, new DisabledListProgressListener())); final Host bookmark = session.getHost(); if(nodeid.isEncrypted(file)) { final FileKey key = new NodesApi(session.getClient()).requestUserFileKey(fileid, StringUtils.EMPTY); final UserPrivateKey privateKey = new UserPrivateKey(); final UserKeyPairContainer keyPairContainer = session.keyPair(); privateKey.setPrivateKey(keyPairContainer.getPrivateKeyContainer().getPrivateKey()); privateKey.setVersion(keyPairContainer.getPrivateKeyContainer().getVersion()); final UserKeyPair userKeyPair = new UserKeyPair(); userKeyPair.setUserPrivateKey(privateKey); final Credentials passphrase = new TripleCryptKeyPair().unlock(callback, bookmark, userKeyPair); final PlainFileKey plainFileKey = Crypto.decryptFileKey(TripleCryptConverter.toCryptoEncryptedFileKey(key), privateKey, passphrase.getPassword()); final UserKeyPair pair; if(null == options.getPassword()) { pair = Crypto.generateUserKeyPair(callback.prompt( bookmark, LocaleFactory.localizedString("Passphrase", "Cryptomator"), LocaleFactory.localizedString("Provide additional login credentials", "Credentials"), new LoginOptions().icon(session.getHost().getProtocol().disk()) ).getPassword()); } else { pair = Crypto.generateUserKeyPair(options.getPassword()); } final EncryptedFileKey encryptedFileKey = Crypto.encryptFileKey(plainFileKey, pair.getUserPublicKey()); options.setPassword(null); options.setKeyPair(TripleCryptConverter.toSwaggerUserKeyPairContainer(pair)); options.setFileKey(TripleCryptConverter.toSwaggerFileKey(encryptedFileKey)); } final DownloadShare share = new SharesApi(session.getClient()).createDownloadShare( options.nodeId(fileid), StringUtils.EMPTY, null); final String help; if(null == share.getExpireAt()) { help = MessageFormat.format(LocaleFactory.localizedString("{0} URL"), LocaleFactory.localizedString("Pre-Signed", "S3")); } else { final long expiry = share.getExpireAt().getMillis(); help = MessageFormat.format(LocaleFactory.localizedString("{0} URL"), LocaleFactory.localizedString("Pre-Signed", "S3")) + " (" + MessageFormat.format(LocaleFactory.localizedString("Expires {0}", "S3") + ")", UserDateFormatterFactory.get().getShortFormat(expiry * 1000) ); } return new DescriptiveUrl( URI.create(String.format("%s: bookmark.getProtocol().getScheme(), bookmark.getHostname(), share.getAccessKey()) ), DescriptiveUrl.Type.signed, help); } catch(ApiException e) { throw new SDSExceptionMappingService().map(e); } catch(CryptoException e) { throw new TripleCryptExceptionMappingService().map(e); } } SDSSharesUrlProvider(final SDSSession session, final SDSNodeIdProvider nodeid); @Override boolean isSupported(final Path file, final Type type); @Override DescriptiveUrl toDownloadUrl(final Path file, CreateDownloadShareRequest options, final PasswordCallback callback); @Override DescriptiveUrl toUploadUrl(final Path file, CreateUploadShareRequest options, final PasswordCallback callback); }
SDSSharesUrlProvider implements PromptUrlProvider<CreateDownloadShareRequest, CreateUploadShareRequest> { @Override public DescriptiveUrl toDownloadUrl(final Path file, CreateDownloadShareRequest options, final PasswordCallback callback) throws BackgroundException { try { if(log.isDebugEnabled()) { log.debug(String.format("Create download share for %s", file)); } if(null == options) { options = new CreateDownloadShareRequest(); log.warn(String.format("Use default share options %s", options)); } final Long fileid = Long.parseLong(nodeid.getFileid(file, new DisabledListProgressListener())); final Host bookmark = session.getHost(); if(nodeid.isEncrypted(file)) { final FileKey key = new NodesApi(session.getClient()).requestUserFileKey(fileid, StringUtils.EMPTY); final UserPrivateKey privateKey = new UserPrivateKey(); final UserKeyPairContainer keyPairContainer = session.keyPair(); privateKey.setPrivateKey(keyPairContainer.getPrivateKeyContainer().getPrivateKey()); privateKey.setVersion(keyPairContainer.getPrivateKeyContainer().getVersion()); final UserKeyPair userKeyPair = new UserKeyPair(); userKeyPair.setUserPrivateKey(privateKey); final Credentials passphrase = new TripleCryptKeyPair().unlock(callback, bookmark, userKeyPair); final PlainFileKey plainFileKey = Crypto.decryptFileKey(TripleCryptConverter.toCryptoEncryptedFileKey(key), privateKey, passphrase.getPassword()); final UserKeyPair pair; if(null == options.getPassword()) { pair = Crypto.generateUserKeyPair(callback.prompt( bookmark, LocaleFactory.localizedString("Passphrase", "Cryptomator"), LocaleFactory.localizedString("Provide additional login credentials", "Credentials"), new LoginOptions().icon(session.getHost().getProtocol().disk()) ).getPassword()); } else { pair = Crypto.generateUserKeyPair(options.getPassword()); } final EncryptedFileKey encryptedFileKey = Crypto.encryptFileKey(plainFileKey, pair.getUserPublicKey()); options.setPassword(null); options.setKeyPair(TripleCryptConverter.toSwaggerUserKeyPairContainer(pair)); options.setFileKey(TripleCryptConverter.toSwaggerFileKey(encryptedFileKey)); } final DownloadShare share = new SharesApi(session.getClient()).createDownloadShare( options.nodeId(fileid), StringUtils.EMPTY, null); final String help; if(null == share.getExpireAt()) { help = MessageFormat.format(LocaleFactory.localizedString("{0} URL"), LocaleFactory.localizedString("Pre-Signed", "S3")); } else { final long expiry = share.getExpireAt().getMillis(); help = MessageFormat.format(LocaleFactory.localizedString("{0} URL"), LocaleFactory.localizedString("Pre-Signed", "S3")) + " (" + MessageFormat.format(LocaleFactory.localizedString("Expires {0}", "S3") + ")", UserDateFormatterFactory.get().getShortFormat(expiry * 1000) ); } return new DescriptiveUrl( URI.create(String.format("%s: bookmark.getProtocol().getScheme(), bookmark.getHostname(), share.getAccessKey()) ), DescriptiveUrl.Type.signed, help); } catch(ApiException e) { throw new SDSExceptionMappingService().map(e); } catch(CryptoException e) { throw new TripleCryptExceptionMappingService().map(e); } } SDSSharesUrlProvider(final SDSSession session, final SDSNodeIdProvider nodeid); @Override boolean isSupported(final Path file, final Type type); @Override DescriptiveUrl toDownloadUrl(final Path file, CreateDownloadShareRequest options, final PasswordCallback callback); @Override DescriptiveUrl toUploadUrl(final Path file, CreateUploadShareRequest options, final PasswordCallback callback);  }