Java Code Examples for com.google.api.client.auth.openidconnect.IdToken#Payload

The following examples show how to use com.google.api.client.auth.openidconnect.IdToken#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: FirebaseTokenTest.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testFirebaseTokenFromIdToken() {
  IdToken.Payload payload = new IdToken.Payload()
      .setSubject("testUser")
      .setIssuer("test-project-id")
      .set("email", "[email protected]")
      .set("email_verified", true)
      .set("name", "Test User")
      .set("picture", "https://picture.url")
      .set("custom", "claim");

  FirebaseToken firebaseToken = new FirebaseToken(payload);

  assertEquals("testUser", firebaseToken.getUid());
  assertEquals("test-project-id", firebaseToken.getIssuer());
  assertEquals("[email protected]", firebaseToken.getEmail());
  assertTrue(firebaseToken.isEmailVerified());
  assertEquals("Test User", firebaseToken.getName());
  assertEquals("https://picture.url", firebaseToken.getPicture());
  assertEquals("claim", firebaseToken.getClaims().get("custom"));
  assertEquals(7, firebaseToken.getClaims().size());
}
 
Example 2
Source File: FirebaseTokenVerifierImpl.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
private String getErrorForTokenWithoutKid(IdToken.Header header, IdToken.Payload payload) {
  if (isCustomToken(payload)) {
    return String.format("%s expects %s, but was given a custom token.",
        method, articledShortName);
  } else if (isLegacyCustomToken(header, payload)) {
    return String.format("%s expects %s, but was given a legacy custom token.",
        method, articledShortName);
  }
  return String.format("Firebase %s has no \"kid\" claim.", shortName);
}
 
Example 3
Source File: FirebaseTokenVerifierImpl.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
private boolean containsLegacyUidField(IdToken.Payload payload) {
  Object dataField = payload.get("d");
  if (dataField instanceof ArrayMap) {
    return ((ArrayMap) dataField).get("uid") != null;
  }
  return false;
}
 
Example 4
Source File: FirebaseTokenTest.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testFirebaseTokenFromMinimalIdToken() {
  IdToken.Payload payload = new IdToken.Payload()
      .setSubject("testUser");

  FirebaseToken firebaseToken = new FirebaseToken(payload);

  assertEquals("testUser", firebaseToken.getUid());
  assertNull(firebaseToken.getIssuer());
  assertNull(firebaseToken.getEmail());
  assertFalse(firebaseToken.isEmailVerified());
  assertNull(firebaseToken.getName());
  assertNull(firebaseToken.getPicture());
  assertEquals(1, firebaseToken.getClaims().size());
}
 
Example 5
Source File: PluginTest.java    From oic-auth-plugin with MIT License 5 votes vote down vote up
private String createIdToken(PrivateKey privateKey, Map<String, Object> keyValues) throws Exception {
    JsonWebSignature.Header header = new JsonWebSignature.Header()
        .setAlgorithm("RS256");
    IdToken.Payload payload = new IdToken.Payload()
        .setIssuer("issuer")
        .setSubject(TEST_USER_USERNAME)
        .setAudience(Collections.singletonList("clientId"))
        .setAudience(System.currentTimeMillis() / 60 + 5)
        .setIssuedAtTimeSeconds(System.currentTimeMillis() / 60);
    for(Map.Entry<String, Object> keyValue : keyValues.entrySet()) {
        payload.set(keyValue.getKey(), keyValue.getValue());
    }

    return JsonWebSignature.signUsingRsaSha256(privateKey, JSON_FACORY, header, payload);
}
 
Example 6
Source File: FirebaseTokenVerifierImpl.java    From firebase-admin-java with Apache License 2.0 4 votes vote down vote up
private boolean isCustomToken(IdToken.Payload payload) {
  return FIREBASE_AUDIENCE.equals(payload.getAudience());
}
 
Example 7
Source File: FirebaseTokenVerifierImpl.java    From firebase-admin-java with Apache License 2.0 4 votes vote down vote up
private boolean isLegacyCustomToken(IdToken.Header header, IdToken.Payload payload) {
  return "HS256".equals(header.getAlgorithm())
      && new BigDecimal(0).equals(payload.get("v"))
      && containsLegacyUidField(payload);
}