Search in sources :

Example 6 with Tokeninfo

use of com.google.api.services.oauth2.model.Tokeninfo in project dockstore by dockstore.

the class GoogleHelper method tokenInfoFromToken.

private static Optional<Tokeninfo> tokenInfoFromToken(String googleToken) {
    GoogleCredential cred = new GoogleCredential().setAccessToken(googleToken);
    try {
        Oauth2 oauth2 = new Oauth2.Builder(TokenResource.HTTP_TRANSPORT, TokenResource.JSON_FACTORY, cred).setApplicationName("").build();
        Tokeninfo tokenInfo = oauth2.tokeninfo().setAccessToken(googleToken).execute();
        return Optional.ofNullable(tokenInfo);
    } catch (RuntimeException | IOException e) {
        // If token is invalid, Google client throws exception. See https://github.com/google/google-api-java-client/issues/970
        LOG.info(MessageFormat.format("Error getting token info: {0}", e.getMessage()));
        LOG.debug("Error getting token info", e);
        return Optional.empty();
    }
}
Also used : Oauth2(com.google.api.services.oauth2.Oauth2) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) IOException(java.io.IOException) Tokeninfo(com.google.api.services.oauth2.model.Tokeninfo)

Example 7 with Tokeninfo

use of com.google.api.services.oauth2.model.Tokeninfo in project dockstore by dockstore.

the class GoogleHelper method getValidAccessToken.

/**
 * Gets a non-expired access token.
 *
 * Google access tokens expire. This method returns
 * an active access token, either returning the one
 * that is in <code>token</code>, or generating a new
 * one with the refresh token, if necessary.
 *
 * This method does NOT update the <code>token</code> with the new token,
 * if there is one. It is the responsibility of the caller to update
 * the token if they want the new token to be persisted.
 *
 * @param token
 * @return
 */
public static Optional<String> getValidAccessToken(Token token) {
    final String googleToken = token.getToken();
    return tokenInfoFromToken(googleToken).map(tokenInfo -> {
        // The user has a non-expired Google token -- also make sure that the audience is valid.
        return isValidAudience(tokenInfo) ? Optional.of(googleToken) : Optional.<String>empty();
    }).orElseGet(() -> {
        // The token expired; try to refresh it
        if (token.getRefreshToken() != null) {
            TokenResponse tokenResponse = new TokenResponse();
            try {
                tokenResponse.setRefreshToken(token.getRefreshToken());
                GoogleCredential credential = new GoogleCredential.Builder().setTransport(TokenResource.HTTP_TRANSPORT).setJsonFactory(TokenResource.JSON_FACTORY).setClientSecrets(config.getGoogleClientID(), config.getGoogleClientSecret()).build().setFromTokenResponse(tokenResponse);
                credential.refreshToken();
                return Optional.ofNullable(credential.getAccessToken());
            } catch (IOException e) {
                LOG.error("Error refreshing token", e);
            }
        }
        return Optional.empty();
    });
}
Also used : GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) Logger(org.slf4j.Logger) BearerToken(com.google.api.client.auth.oauth2.BearerToken) Tokeninfo(com.google.api.services.oauth2.model.Tokeninfo) LoggerFactory(org.slf4j.LoggerFactory) CustomWebApplicationException(io.dockstore.webservice.CustomWebApplicationException) HttpStatus(org.apache.http.HttpStatus) IOException(java.io.IOException) DockstoreWebserviceConfiguration(io.dockstore.webservice.DockstoreWebserviceConfiguration) Userinfoplus(com.google.api.services.oauth2.model.Userinfoplus) MessageFormat(java.text.MessageFormat) TokenResource(io.dockstore.webservice.resources.TokenResource) ClientParametersAuthentication(com.google.api.client.auth.oauth2.ClientParametersAuthentication) AuthorizationCodeFlow(com.google.api.client.auth.oauth2.AuthorizationCodeFlow) Map(java.util.Map) TokenResponse(com.google.api.client.auth.oauth2.TokenResponse) GenericUrl(com.google.api.client.http.GenericUrl) Optional(java.util.Optional) TokenType(io.dockstore.webservice.core.TokenType) User(io.dockstore.webservice.core.User) Oauth2(com.google.api.services.oauth2.Oauth2) Token(io.dockstore.webservice.core.Token) TokenResponse(com.google.api.client.auth.oauth2.TokenResponse) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) IOException(java.io.IOException)

Aggregations

Tokeninfo (com.google.api.services.oauth2.model.Tokeninfo)7 Oauth2 (com.google.api.services.oauth2.Oauth2)5 IOException (java.io.IOException)5 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)3 DockstoreWebserviceConfiguration (io.dockstore.webservice.DockstoreWebserviceConfiguration)2 AuthorizationCodeFlow (com.google.api.client.auth.oauth2.AuthorizationCodeFlow)1 BearerToken (com.google.api.client.auth.oauth2.BearerToken)1 ClientParametersAuthentication (com.google.api.client.auth.oauth2.ClientParametersAuthentication)1 TokenResponse (com.google.api.client.auth.oauth2.TokenResponse)1 GoogleTokenResponse (com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse)1 GenericUrl (com.google.api.client.http.GenericUrl)1 HttpResponseException (com.google.api.client.http.HttpResponseException)1 Userinfoplus (com.google.api.services.oauth2.model.Userinfoplus)1 AccessToken (com.google.auth.oauth2.AccessToken)1 CacheBuilder (com.google.common.cache.CacheBuilder)1 CustomWebApplicationException (io.dockstore.webservice.CustomWebApplicationException)1 Token (io.dockstore.webservice.core.Token)1 TokenType (io.dockstore.webservice.core.TokenType)1 User (io.dockstore.webservice.core.User)1 TokenResource (io.dockstore.webservice.resources.TokenResource)1