Search in sources :

Example 1 with OAuthTokens

use of ch.cyberduck.core.OAuthTokens in project cyberduck by iterate-ch.

the class OAuth2AuthorizationService method refresh.

public OAuthTokens refresh(final OAuthTokens tokens) throws BackgroundException {
    if (StringUtils.isBlank(tokens.getRefreshToken())) {
        log.warn("Missing refresh token");
        return tokens;
    }
    if (log.isDebugEnabled()) {
        log.debug(String.format("Refresh expired tokens %s", tokens));
    }
    try {
        final TokenResponse response = new RefreshTokenRequest(transport, json, new GenericUrl(tokenServerUrl), tokens.getRefreshToken()).setRequestInitializer(new UserAgentHttpRequestInitializer(new PreferencesUseragentProvider())).setClientAuthentication(new ClientParametersAuthentication(clientid, clientsecret)).executeUnparsed().parseAs(PermissiveTokenResponse.class).toTokenResponse();
        final long expiryInMilliseconds = System.currentTimeMillis() + response.getExpiresInSeconds() * 1000;
        if (StringUtils.isBlank(response.getRefreshToken())) {
            return new OAuthTokens(response.getAccessToken(), tokens.getRefreshToken(), expiryInMilliseconds);
        }
        return new OAuthTokens(response.getAccessToken(), response.getRefreshToken(), expiryInMilliseconds);
    } catch (TokenResponseException e) {
        throw new OAuthExceptionMappingService().map(e);
    } catch (HttpResponseException e) {
        throw new DefaultHttpResponseExceptionMappingService().map(new org.apache.http.client.HttpResponseException(e.getStatusCode(), e.getStatusMessage()));
    } catch (IOException e) {
        throw new DefaultIOExceptionMappingService().map(e);
    }
}
Also used : DefaultHttpResponseExceptionMappingService(ch.cyberduck.core.http.DefaultHttpResponseExceptionMappingService) UserAgentHttpRequestInitializer(ch.cyberduck.core.http.UserAgentHttpRequestInitializer) OAuthTokens(ch.cyberduck.core.OAuthTokens) PreferencesUseragentProvider(ch.cyberduck.core.PreferencesUseragentProvider) HttpResponseException(com.google.api.client.http.HttpResponseException) GenericUrl(com.google.api.client.http.GenericUrl) IOException(java.io.IOException) RefreshTokenRequest(com.google.api.client.auth.oauth2.RefreshTokenRequest) ClientParametersAuthentication(com.google.api.client.auth.oauth2.ClientParametersAuthentication) TokenResponse(com.google.api.client.auth.oauth2.TokenResponse) DefaultIOExceptionMappingService(ch.cyberduck.core.DefaultIOExceptionMappingService) TokenResponseException(com.google.api.client.auth.oauth2.TokenResponseException)

Example 2 with OAuthTokens

use of ch.cyberduck.core.OAuthTokens in project cyberduck by iterate-ch.

the class OAuth2AuthorizationService method authorize.

public OAuthTokens authorize(final Host bookmark, final LoginCallback prompt, final CancelCallback cancel, final FlowType type) throws BackgroundException {
    final Credentials credentials = bookmark.getCredentials();
    final OAuthTokens saved = credentials.getOauth();
    if (saved.validate()) {
        // Found existing tokens
        if (saved.isExpired()) {
            log.warn(String.format("Refresh expired access tokens %s", saved));
            // Refresh expired access key
            try {
                credentials.setSaved(true);
                return this.refresh(saved);
            } catch (LoginFailureException | InteroperabilityException e) {
                log.warn(String.format("Failure refreshing tokens from %s for %s", saved, bookmark));
            // Continue with new OAuth 2 flow
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug(String.format("Returned saved OAuth tokens %s for %s", saved, bookmark));
            }
            return saved;
        }
    }
    if (log.isDebugEnabled()) {
        log.debug(String.format("Start new OAuth flow for %s with missing access token", bookmark));
    }
    final TokenResponse response;
    switch(type) {
        case AuthorizationCode:
            response = this.authorizeWithCode(bookmark, prompt, cancel, credentials);
            break;
        case PasswordGrant:
            response = this.authorizeWithPassword(credentials);
            break;
        default:
            throw new LoginCanceledException();
    }
    // Save access key and refresh key
    final OAuthTokens tokens = new OAuthTokens(response.getAccessToken(), response.getRefreshToken(), null == response.getExpiresInSeconds() ? System.currentTimeMillis() : System.currentTimeMillis() + response.getExpiresInSeconds() * 1000);
    credentials.setOauth(tokens);
    return tokens;
}
Also used : LoginFailureException(ch.cyberduck.core.exception.LoginFailureException) InteroperabilityException(ch.cyberduck.core.exception.InteroperabilityException) OAuthTokens(ch.cyberduck.core.OAuthTokens) TokenResponse(com.google.api.client.auth.oauth2.TokenResponse) LoginCanceledException(ch.cyberduck.core.exception.LoginCanceledException) Credentials(ch.cyberduck.core.Credentials)

Aggregations

OAuthTokens (ch.cyberduck.core.OAuthTokens)2 TokenResponse (com.google.api.client.auth.oauth2.TokenResponse)2 Credentials (ch.cyberduck.core.Credentials)1 DefaultIOExceptionMappingService (ch.cyberduck.core.DefaultIOExceptionMappingService)1 PreferencesUseragentProvider (ch.cyberduck.core.PreferencesUseragentProvider)1 InteroperabilityException (ch.cyberduck.core.exception.InteroperabilityException)1 LoginCanceledException (ch.cyberduck.core.exception.LoginCanceledException)1 LoginFailureException (ch.cyberduck.core.exception.LoginFailureException)1 DefaultHttpResponseExceptionMappingService (ch.cyberduck.core.http.DefaultHttpResponseExceptionMappingService)1 UserAgentHttpRequestInitializer (ch.cyberduck.core.http.UserAgentHttpRequestInitializer)1 ClientParametersAuthentication (com.google.api.client.auth.oauth2.ClientParametersAuthentication)1 RefreshTokenRequest (com.google.api.client.auth.oauth2.RefreshTokenRequest)1 TokenResponseException (com.google.api.client.auth.oauth2.TokenResponseException)1 GenericUrl (com.google.api.client.http.GenericUrl)1 HttpResponseException (com.google.api.client.http.HttpResponseException)1 IOException (java.io.IOException)1