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);
}
}
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;
}
Aggregations