Search in sources :

Example 21 with TokenResponse

use of com.microsoft.identity.common.internal.providers.oauth2.TokenResponse in project data-transfer-project by google.

the class GoogleAuthDataGenerator method generateAuthData.

@Override
public AuthData generateAuthData(String callbackBaseUrl, String authCode, String id, AuthData initialAuthData, String extra) {
    Preconditions.checkState(initialAuthData == null, "Earlier auth data not expected for Google flow");
    AuthorizationCodeFlow flow;
    TokenResponse response;
    try {
        flow = createFlow();
        response = flow.newTokenRequest(authCode).setRedirectUri(// TODO(chuy): Parameterize
        callbackBaseUrl + redirectPath).execute();
    } catch (IOException e) {
        throw new RuntimeException("Error calling AuthorizationCodeFlow.execute ", e);
    }
    // Figure out storage
    Credential credential = null;
    try {
        credential = flow.createAndStoreCredential(response, id);
    } catch (IOException e) {
        throw new RuntimeException("Error calling AuthorizationCodeFlow.createAndStoreCredential ", e);
    }
    // GoogleIdToken.Payload payload = ((GoogleTokenResponse) response).parseIdToken().getPayload();
    return new TokensAndUrlAuthData(credential.getAccessToken(), credential.getRefreshToken(), credential.getTokenServerEncodedUrl());
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) TokenResponse(com.google.api.client.auth.oauth2.TokenResponse) TokensAndUrlAuthData(org.dataportabilityproject.types.transfer.auth.TokensAndUrlAuthData) IOException(java.io.IOException) AuthorizationCodeFlow(com.google.api.client.auth.oauth2.AuthorizationCodeFlow) GoogleAuthorizationCodeFlow(com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow)

Example 22 with TokenResponse

use of com.microsoft.identity.common.internal.providers.oauth2.TokenResponse in project data-transfer-project by google.

the class GoogleAuth method generateAuthData.

@Override
public AuthData generateAuthData(String callbackBaseUrl, String authCode, UUID jobId, @Nullable AuthData initialAuthData, @Nullable String extra) throws IOException {
    Preconditions.checkState(initialAuthData == null, "Earlier auth data not expected for Google flow");
    AuthorizationCodeFlow flow = createFlow();
    TokenResponse response = flow.newTokenRequest(authCode).setRedirectUri(// TODO(chuy): Parameterize
    callbackBaseUrl + CALLBACK_PATH).execute();
    // Figure out storage
    Credential credential = flow.createAndStoreCredential(response, jobId.toString());
    // GoogleIdToken.Payload payload = ((GoogleTokenResponse) response).parseIdToken().getPayload();
    return toAuthData(credential);
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) TokenResponse(com.google.api.client.auth.oauth2.TokenResponse) AuthorizationCodeFlow(com.google.api.client.auth.oauth2.AuthorizationCodeFlow) GoogleAuthorizationCodeFlow(com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow)

Example 23 with TokenResponse

use of com.microsoft.identity.common.internal.providers.oauth2.TokenResponse in project data-transfer-project by google.

the class InstagramAuth method generateAuthData.

@Override
public AuthData generateAuthData(String callbackBaseUrl, String authCode, UUID jobId, AuthData initialAuthData, String extra) throws IOException {
    Preconditions.checkArgument(Strings.isNullOrEmpty(extra), "Extra data not expected for Instagram oauth flow");
    Preconditions.checkArgument(initialAuthData == null, "Earlier auth data not expected for Instagram oauth flow");
    AuthorizationCodeFlow flow = createFlow();
    TokenResponse response = flow.newTokenRequest(authCode).setRedirectUri(// TODO(chuy): Parameterize
    callbackBaseUrl + CALLBACK_PATH).execute();
    // Figure out storage
    Credential credential = flow.createAndStoreCredential(response, jobId.toString());
    return toAuthData(credential);
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) TokenResponse(com.google.api.client.auth.oauth2.TokenResponse) AuthorizationCodeFlow(com.google.api.client.auth.oauth2.AuthorizationCodeFlow)

Example 24 with TokenResponse

use of com.microsoft.identity.common.internal.providers.oauth2.TokenResponse in project repairnator by Spirals-Team.

the class ManageGoogleAccessToken method initializeCredentialFromAccessToken.

public void initializeCredentialFromAccessToken(String accessToken) {
    TokenResponse tokenResponse = new TokenResponse();
    tokenResponse.setAccessToken(accessToken);
    tokenResponse.setTokenType("offline");
    this.credential = new GoogleCredential().setFromTokenResponse(tokenResponse);
}
Also used : TokenResponse(com.google.api.client.auth.oauth2.TokenResponse) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential)

Example 25 with TokenResponse

use of com.microsoft.identity.common.internal.providers.oauth2.TokenResponse in project workbench by all-of-us.

the class DelegatedUserCredentials method refreshAccessToken.

@Override
public AccessToken refreshAccessToken() throws IOException {
    // The first step is to call the IamCredentials API to generate a signed JWT with the
    // appropriate claims. This call is authorized with application default credentials (ADCs). The
    // ADC service account may be different from `serviceAccountEmail` if the ADC account has the
    // roles/iam.serviceAccountTokenCreator role on the `serviceAccountEmail` account.
    SignJwtRequest jwtRequest = SignJwtRequest.newBuilder().setName(String.format(SERVICE_ACCOUNT_NAME_FORMAT, serviceAccountEmail)).setPayload(JSON_FACTORY.toString(createJwtPayload())).build();
    String jwt = credentialsClient.signJwt(jwtRequest).getSignedJwt();
    // With the signed JWT in hand, we call Google's OAuth2 token server to exchange the JWT for
    // an access token.
    TokenRequest tokenRequest = new TokenRequest(httpTransport, JSON_FACTORY, new GenericUrl(GoogleOAuthConstants.TOKEN_SERVER_URL), JWT_BEARER_GRANT_TYPE);
    tokenRequest.put("assertion", jwt);
    TokenResponse tokenResponse = tokenRequest.execute();
    return new AccessToken(tokenResponse.getAccessToken(), Date.from(Instant.now(clock).plusSeconds(tokenResponse.getExpiresInSeconds())));
}
Also used : TokenResponse(com.google.api.client.auth.oauth2.TokenResponse) AccessToken(com.google.auth.oauth2.AccessToken) TokenRequest(com.google.api.client.auth.oauth2.TokenRequest) SignJwtRequest(com.google.cloud.iam.credentials.v1.SignJwtRequest) GenericUrl(com.google.api.client.http.GenericUrl)

Aggregations

TokenResponse (com.google.api.client.auth.oauth2.TokenResponse)48 IOException (java.io.IOException)23 GenericUrl (com.google.api.client.http.GenericUrl)22 Credential (com.google.api.client.auth.oauth2.Credential)20 ClientParametersAuthentication (com.google.api.client.auth.oauth2.ClientParametersAuthentication)16 AuthorizationCodeFlow (com.google.api.client.auth.oauth2.AuthorizationCodeFlow)15 Map (java.util.Map)13 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)12 TokenResponse (com.microsoft.identity.common.internal.providers.oauth2.TokenResponse)11 BearerToken (com.google.api.client.auth.oauth2.BearerToken)9 TokenResult (com.microsoft.identity.common.internal.providers.oauth2.TokenResult)8 Logger (org.slf4j.Logger)8 LoggerFactory (org.slf4j.LoggerFactory)8 Test (org.junit.Test)7 URL (java.net.URL)6 HashMap (java.util.HashMap)6 List (java.util.List)6 Timed (com.codahale.metrics.annotation.Timed)5 AuthorizationCodeRequestUrl (com.google.api.client.auth.oauth2.AuthorizationCodeRequestUrl)5 Collections (java.util.Collections)5