Search in sources :

Example 1 with DownloadShare

use of ch.cyberduck.core.sds.io.swagger.client.model.DownloadShare in project cyberduck by iterate-ch.

the class SDSSharesUrlProvider method toDownloadUrl.

@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.getVersionId(file, new DisabledListProgressListener()));
        final Host bookmark = session.getHost();
        if (SDSNodeIdProvider.isEncrypted(file)) {
            // get existing file key associated with the sharing user
            final FileKey key = new NodesApi(session.getClient()).requestUserFileKey(fileid, null, null);
            final EncryptedFileKey encFileKey = TripleCryptConverter.toCryptoEncryptedFileKey(key);
            final UserKeyPairContainer keyPairContainer = session.getKeyPairForFileKey(encFileKey.getVersion());
            final UserKeyPair userKeyPair = TripleCryptConverter.toCryptoUserKeyPair(keyPairContainer);
            final Credentials passphrase = new TripleCryptKeyPair().unlock(callback, bookmark, userKeyPair);
            final PlainFileKey plainFileKey = Crypto.decryptFileKey(encFileKey, userKeyPair.getUserPrivateKey(), passphrase.getPassword());
            // encrypt file key with a new key pair
            final UserKeyPair pair;
            if (null == options.getPassword()) {
                pair = Crypto.generateUserKeyPair(session.requiredKeyPairVersion(), 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(session.requiredKeyPairVersion(), 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://%s/#/public/shares-downloads/%s", bookmark.getProtocol().getScheme(), bookmark.getHostname(), share.getAccessKey())), DescriptiveUrl.Type.signed, help);
    } catch (ApiException e) {
        throw new SDSExceptionMappingService(nodeid).map(e);
    } catch (CryptoException e) {
        throw new TripleCryptExceptionMappingService().map(e);
    }
}
Also used : UserKeyPair(com.dracoon.sdk.crypto.model.UserKeyPair) EncryptedFileKey(com.dracoon.sdk.crypto.model.EncryptedFileKey) PlainFileKey(com.dracoon.sdk.crypto.model.PlainFileKey) FileKey(ch.cyberduck.core.sds.io.swagger.client.model.FileKey) DownloadShare(ch.cyberduck.core.sds.io.swagger.client.model.DownloadShare) UserKeyPairContainer(ch.cyberduck.core.sds.io.swagger.client.model.UserKeyPairContainer) DisabledListProgressListener(ch.cyberduck.core.DisabledListProgressListener) EncryptedFileKey(com.dracoon.sdk.crypto.model.EncryptedFileKey) Host(ch.cyberduck.core.Host) CreateDownloadShareRequest(ch.cyberduck.core.sds.io.swagger.client.model.CreateDownloadShareRequest) TripleCryptKeyPair(ch.cyberduck.core.sds.triplecrypt.TripleCryptKeyPair) LoginOptions(ch.cyberduck.core.LoginOptions) NodesApi(ch.cyberduck.core.sds.io.swagger.client.api.NodesApi) DescriptiveUrl(ch.cyberduck.core.DescriptiveUrl) SharesApi(ch.cyberduck.core.sds.io.swagger.client.api.SharesApi) TripleCryptExceptionMappingService(ch.cyberduck.core.sds.triplecrypt.TripleCryptExceptionMappingService) CryptoException(com.dracoon.sdk.crypto.error.CryptoException) Credentials(ch.cyberduck.core.Credentials) PlainFileKey(com.dracoon.sdk.crypto.model.PlainFileKey) ApiException(ch.cyberduck.core.sds.io.swagger.client.ApiException)

Aggregations

Credentials (ch.cyberduck.core.Credentials)1 DescriptiveUrl (ch.cyberduck.core.DescriptiveUrl)1 DisabledListProgressListener (ch.cyberduck.core.DisabledListProgressListener)1 Host (ch.cyberduck.core.Host)1 LoginOptions (ch.cyberduck.core.LoginOptions)1 ApiException (ch.cyberduck.core.sds.io.swagger.client.ApiException)1 NodesApi (ch.cyberduck.core.sds.io.swagger.client.api.NodesApi)1 SharesApi (ch.cyberduck.core.sds.io.swagger.client.api.SharesApi)1 CreateDownloadShareRequest (ch.cyberduck.core.sds.io.swagger.client.model.CreateDownloadShareRequest)1 DownloadShare (ch.cyberduck.core.sds.io.swagger.client.model.DownloadShare)1 FileKey (ch.cyberduck.core.sds.io.swagger.client.model.FileKey)1 UserKeyPairContainer (ch.cyberduck.core.sds.io.swagger.client.model.UserKeyPairContainer)1 TripleCryptExceptionMappingService (ch.cyberduck.core.sds.triplecrypt.TripleCryptExceptionMappingService)1 TripleCryptKeyPair (ch.cyberduck.core.sds.triplecrypt.TripleCryptKeyPair)1 CryptoException (com.dracoon.sdk.crypto.error.CryptoException)1 EncryptedFileKey (com.dracoon.sdk.crypto.model.EncryptedFileKey)1 PlainFileKey (com.dracoon.sdk.crypto.model.PlainFileKey)1 UserKeyPair (com.dracoon.sdk.crypto.model.UserKeyPair)1