Search in sources :

Example 6 with Payload

use of com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload in project google-api-java-client by google.

the class GoogleIdTokenTest method testDeprecatedMethods.

@SuppressWarnings("deprecation")
public void testDeprecatedMethods() {
    Payload payload = newPayload(USER_ID, CLIENT_ID);
    assertEquals(USER_ID, payload.getUserId());
    assertEquals(CLIENT_ID, payload.getIssuee());
    payload.setUserId(ANOTHER_USER_ID);
    payload.setIssuee(ANOTHER_CLIENT_ID);
    assertEquals(ANOTHER_USER_ID, payload.getUserId());
    assertEquals(ANOTHER_CLIENT_ID, payload.getIssuee());
    assertEquals(ANOTHER_USER_ID, payload.getSubject());
    assertEquals(ANOTHER_CLIENT_ID, payload.getAuthorizedParty());
}
Also used : Payload(com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload)

Example 7 with Payload

use of com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload in project pratilipi by Pratilipi.

the class GoogleApi method getUserData.

public static UserData getUserData(String googleIdToken) throws InvalidArgumentException, UnexpectedServerException {
    try {
        GoogleIdToken idToken = UxModeFilter.isAndroidApp() ? getAndroidIdTokenVerifier().verify(googleIdToken) : getWebIdTokenVerifier().verify(googleIdToken);
        String authorisedParty = UxModeFilter.isAndroidApp() ? getAppClientId() : getWebClientId();
        if (idToken == null || idToken.getPayload() == null || !idToken.getPayload().getAuthorizedParty().equals(authorisedParty)) {
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("googleIdToken", "Invalid GoogleIdToken !");
            throw new InvalidArgumentException(jsonObject);
        }
        Payload payload = idToken.getPayload();
        logger.log(Level.INFO, "GoogleApi Payload : " + new Gson().toJson(payload));
        if (payload.get("given_name") == null || ((String) payload.get("given_name")).isEmpty()) {
            logger.log(Level.SEVERE, "Google given_name is missing for GoogleUser: " + payload.getSubject());
            throw new UnexpectedServerException();
        }
        UserData userData = new UserData();
        userData.setGoogleId(payload.getSubject());
        userData.setFirstName((String) payload.get("given_name"));
        userData.setLastName((String) payload.get("family_name"));
        userData.setEmail(payload.getEmail());
        return userData;
    } catch (GeneralSecurityException | IOException e) {
        logger.log(Level.SEVERE, "Google id token verification failed: " + e);
        throw new UnexpectedServerException();
    }
}
Also used : InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) UserData(com.pratilipi.data.client.UserData) GeneralSecurityException(java.security.GeneralSecurityException) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) Payload(com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload) IOException(java.io.IOException) GoogleIdToken(com.google.api.client.googleapis.auth.oauth2.GoogleIdToken)

Example 8 with Payload

use of com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload in project google-api-java-client by google.

the class GoogleIdTokenTest method newPayload.

private static Payload newPayload(String userId, String clientId) {
    Payload payload = new Payload();
    payload.setIssuer("accounts.google.com");
    payload.setAudience(clientId);
    payload.setAuthorizedParty(clientId);
    payload.setSubject(userId);
    payload.setExpirationTimeSeconds(100L);
    payload.setIssuedAtTimeSeconds(0L);
    return payload;
}
Also used : Payload(com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload)

Example 9 with Payload

use of com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload in project endpoints-java by cloudendpoints.

the class GoogleJwtAuthenticatorTest method setUp.

@Before
public void setUp() throws Exception {
    payload = new Payload();
    payload.setAuthorizedParty(CLIENT_ID);
    payload.setAudience(AUDIENCE);
    payload.setEmail(EMAIL);
    payload.setSubject(USER_ID);
    authenticator = new GoogleJwtAuthenticator(verifier);
    request = new MockHttpServletRequest();
    attr = Attribute.from(request);
    attr.set(Attribute.API_METHOD_CONFIG, config);
    attr.set(Attribute.ENABLE_CLIENT_ID_WHITELIST, true);
    request.addHeader(GoogleAuth.AUTHORIZATION_HEADER, "Bearer " + TOKEN);
    when(token.getPayload()).thenReturn(payload);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Payload(com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload) Before(org.junit.Before)

Aggregations

Payload (com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload)7 AuthorizationCodeFlow (com.google.api.client.auth.oauth2.AuthorizationCodeFlow)2 Credential (com.google.api.client.auth.oauth2.Credential)2 TokenResponse (com.google.api.client.auth.oauth2.TokenResponse)2 GoogleAuthorizationCodeFlow (com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow)2 IOException (java.io.IOException)2 GoogleIdToken (com.google.api.client.googleapis.auth.oauth2.GoogleIdToken)1 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)1 Header (com.google.api.client.json.webtoken.JsonWebSignature.Header)1 Gson (com.google.gson.Gson)1 JsonObject (com.google.gson.JsonObject)1 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)1 UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)1 UserData (com.pratilipi.data.client.UserData)1 GeneralSecurityException (java.security.GeneralSecurityException)1 TokensAndUrlAuthData (org.dataportabilityproject.types.transfer.auth.TokensAndUrlAuthData)1 Before (org.junit.Before)1 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1