com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload Java Examples

The following examples show how to use com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GoogleIdTokenVerifierTest.java    From google-api-java-client with Apache License 2.0 6 votes vote down vote up
public void testVerify() throws Exception {
  GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(
      new GooglePublicKeysManagerTest.PublicCertsMockHttpTransport(), new JacksonFactory()).build();
  Header header = new Header();
  header.setAlgorithm("RS25");
  Payload payload = newPayload(CLIENT_ID);
  Payload payload2 = newPayload(CLIENT_ID + "2");
  GoogleIdToken idToken = new GoogleIdToken(header, payload, new byte[0], new byte[0]);
  GoogleIdToken idToken2 = new GoogleIdToken(header, payload2, new byte[0], new byte[0]);
  assertFalse(verifier.verify(idToken));
  assertFalse(verifier.verify(idToken2));
  verifier = new GoogleIdTokenVerifier(
      new GooglePublicKeysManagerTest.PublicCertsMockHttpTransport(), new JacksonFactory());
  assertFalse(verifier.verify(idToken));
  assertFalse(verifier.verify(idToken2));
  // TODO(yanivi): add a unit test that returns true
}
 
Example #2
Source File: GoogleSecurityServiceSupplier.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public SpagoBIUserProfile checkAuthenticationToken(String token) {
	logger.debug("IN");
	LogMF.debug(logger, "Verifying token [{0}]...", token);
	GoogleIdToken idToken;
	try {
		idToken = verifyToken(token);
	} catch (GeneralSecurityException | IOException e) {
		logger.error("An exception occurred while verifying Google token [" + token + "]", e);
		return null;
	}
	if (idToken == null) {
		logger.error("Invalid ID token [" + token + "]");
		return null;
	}
	LogMF.debug(logger, "Token [{0}] verified successfully", token);

	Payload payload = idToken.getPayload();

	String userId = payload.getSubject();
	LogMF.debug(logger, "User ID: [{0}]", userId);
	String email = payload.getEmail();
	LogMF.debug(logger, "User email: [{0}]", email);
	String name = (String) payload.get("name");
	LogMF.debug(logger, "User name: [{0}]", name);
	LogMF.debug(logger, "Creating user profile object for user [{0}]...", email);
	SpagoBIUserProfile profile = createUserProfileObject(email);
	LogMF.debug(logger, "User profile object for user [{0}] created", email);
	return profile;
}
 
Example #3
Source File: GoogleJwtAuthenticatorTest.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #4
Source File: GoogleIdTokenTest.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #5
Source File: GoogleIdTokenTest.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #6
Source File: GoogleIdTokenVerifierTest.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
private static Payload newPayload(String clientId) {
  Payload payload = new Payload();
  payload.setIssuer("accounts.google.com");
  payload.setAudience(clientId);
  payload.setAuthorizedParty(clientId);
  payload.setExpirationTimeSeconds(100L);
  payload.setIssuedAtTimeSeconds(0L);
  return payload;
}
 
Example #7
Source File: GoogleIdTokenTest.java    From google-api-java-client with Apache License 2.0 4 votes vote down vote up
public void testEmailVerified() {
  Payload payload = newPayload(USER_ID, CLIENT_ID);
  assertNull(payload.getEmailVerified());

  payload.setEmailVerified(true);
  assertTrue(payload.getEmailVerified());

  payload.setEmailVerified(false);
  assertFalse(payload.getEmailVerified());

  payload.setEmailVerified(null);
  assertNull(payload.getEmailVerified());

  payload.set(EMAIL_VERIFIED_KEY, "true");
  assertTrue(payload.getEmailVerified());

  payload.set(EMAIL_VERIFIED_KEY, true);
  assertTrue(payload.getEmailVerified());

  payload.set(EMAIL_VERIFIED_KEY, "false");
  assertFalse(payload.getEmailVerified());

  payload.set(EMAIL_VERIFIED_KEY, false);
  assertFalse(payload.getEmailVerified());

  payload.set(EMAIL_VERIFIED_KEY, "RandomString");
  assertFalse(payload.getEmailVerified());

  payload.set(EMAIL_VERIFIED_KEY, "");
  assertFalse(payload.getEmailVerified());

  payload.set(EMAIL_VERIFIED_KEY, null);
  assertNull(payload.getEmailVerified());

  // Wrong type.
  payload.set(EMAIL_VERIFIED_KEY, new Integer(5));
  try {
    payload.getEmailVerified();
    fail();
  } catch (ClassCastException e) {
    // Expected.
  }
}