Search in sources :

Example 1 with IdTokenResponse

use of com.google.api.client.auth.openidconnect.IdTokenResponse in project google-oauth-java-client by googleapis.

the class CustomTokenRequestTest method testSetResponseClass.

public void testSetResponseClass() throws IOException {
    TokenRequest request = new TokenRequest(new AccessTokenTransport(), JSON_FACTORY, AUTHORIZATION_SERVER_URL, "foo").setResponseClass(IdTokenResponse.class);
    assertEquals(IdTokenResponse.class, request.getResponseClass());
    TokenResponse response = request.execute();
    assertTrue(response instanceof IdTokenResponse);
    IdTokenResponse tokenResponse = (IdTokenResponse) response;
    IdToken idToken = tokenResponse.parseIdToken();
    assertEquals("John Doe", idToken.getPayload().get("name"));
}
Also used : IdToken(com.google.api.client.auth.openidconnect.IdToken) IdTokenResponse(com.google.api.client.auth.openidconnect.IdTokenResponse) IdTokenResponse(com.google.api.client.auth.openidconnect.IdTokenResponse)

Example 2 with IdTokenResponse

use of com.google.api.client.auth.openidconnect.IdTokenResponse in project flytekit-java by flyteorg.

the class GoogleAuthHelper method getUserToken.

private Token getUserToken(UserCredentials credentials) throws IOException {
    LOG.info("Fetching user id token for user credential: " + credentials.getClientId());
    TokenRequest request = getRefreshTokenRequest(credentials);
    Instant base = clock.instant();
    IdTokenResponse response = request.executeUnparsed().parseAs(IdTokenResponse.class);
    if (response == null || response.getIdToken() == null) {
        String errMessage = String.format("Couldn't get id token for user credential: [%s].%n" + "UserCredentials can obtain an id token only when authenticated through " + "gcloud running 'gcloud auth login --update-adc' or " + "'gcloud auth application-default login'", credentials.getClientId());
        throw new IOException(errMessage);
    }
    Instant expiryAt = base.plusSeconds(response.getExpiresInSeconds());
    return Token.builder().accessToken(response.getIdToken()).expiry(expiryAt).tokenType(response.getTokenType()).build();
}
Also used : Instant(java.time.Instant) TokenRequest(com.google.api.client.auth.oauth2.TokenRequest) RefreshTokenRequest(com.google.api.client.auth.oauth2.RefreshTokenRequest) IOException(java.io.IOException) IdTokenResponse(com.google.api.client.auth.openidconnect.IdTokenResponse)

Aggregations

IdTokenResponse (com.google.api.client.auth.openidconnect.IdTokenResponse)2 RefreshTokenRequest (com.google.api.client.auth.oauth2.RefreshTokenRequest)1 TokenRequest (com.google.api.client.auth.oauth2.TokenRequest)1 IdToken (com.google.api.client.auth.openidconnect.IdToken)1 IOException (java.io.IOException)1 Instant (java.time.Instant)1