Skip to content

Commit ba1c540

Browse files
committed
test(auth): Assert JWT headers and claims (alg, typ, iat, exp)
This commit adds explicit assertions to verify that the generated JWS header correctly contains 'alg=RS256' and 'typ=JWT', and that the JWT payload contains the 'iat' and 'exp' claims with exactly a 3600-second (1-hour) expiration offset. This brings the Java library's test suite into alignment with the expected auth specification. Other Google Cloud client libraries like Go, Node.js, and Python natively assert the presence of these standard headers and the 1-hour expiration window during their Self-Signed JWT generation tests.
1 parent 79e26b8 commit ba1c540

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/ServiceAccountCredentialsTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,6 +1777,8 @@ private void verifyJwtAccess(Map<String, List<String>> metadata, String expected
17771777
assertNotNull(assertion, "Bearer assertion not found");
17781778
JsonWebSignature signature =
17791779
JsonWebSignature.parse(GsonFactory.getDefaultInstance(), assertion);
1780+
assertEquals("RS256", signature.getHeader().getAlgorithm());
1781+
assertEquals("JWT", signature.getHeader().getType());
17801782
assertEquals(CLIENT_EMAIL, signature.getPayload().getIssuer());
17811783
assertEquals(CLIENT_EMAIL, signature.getPayload().getSubject());
17821784
if (expectedScopeClaim != null) {
@@ -1787,6 +1789,14 @@ private void verifyJwtAccess(Map<String, List<String>> metadata, String expected
17871789
assertFalse(signature.getPayload().containsKey("scope"));
17881790
}
17891791
assertEquals(PRIVATE_KEY_ID, signature.getHeader().getKeyId());
1792+
1793+
Long iat = (Long) signature.getPayload().get("iat");
1794+
Long exp = (Long) signature.getPayload().get("exp");
1795+
assertNotNull(iat);
1796+
assertNotNull(exp);
1797+
assertEquals(3600L, exp - iat);
1798+
long currentTimeSecs = System.currentTimeMillis() / 1000;
1799+
assertTrue(Math.abs(currentTimeSecs - iat) < 60);
17901800
}
17911801

17921802
static GenericJson writeServiceAccountJson(

google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/ServiceAccountJwtAccessCredentialsTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,8 @@ private void verifyJwtAccess(Map<String, List<String>> metadata, URI expectedAud
914914
}
915915
assertNotNull(assertion, "Bearer assertion not found");
916916
JsonWebSignature signature = JsonWebSignature.parse(JSON_FACTORY, assertion);
917+
assertEquals("RS256", signature.getHeader().getAlgorithm());
918+
assertEquals("JWT", signature.getHeader().getType());
917919
assertEquals(
918920
ServiceAccountJwtAccessCredentialsTest.SA_CLIENT_EMAIL, signature.getPayload().getIssuer());
919921
assertEquals(
@@ -922,6 +924,14 @@ private void verifyJwtAccess(Map<String, List<String>> metadata, URI expectedAud
922924
assertEquals(expectedAudience.toString(), signature.getPayload().getAudience());
923925
assertEquals(
924926
ServiceAccountJwtAccessCredentialsTest.SA_PRIVATE_KEY_ID, signature.getHeader().getKeyId());
927+
928+
Long iat = (Long) signature.getPayload().get("iat");
929+
Long exp = (Long) signature.getPayload().get("exp");
930+
assertNotNull(iat);
931+
assertNotNull(exp);
932+
assertEquals(3600L, exp - iat);
933+
long currentTimeSecs = System.currentTimeMillis() / 1000;
934+
assertTrue(Math.abs(currentTimeSecs - iat) < 60);
925935
}
926936

927937
private static void testFromStreamException(InputStream stream, String expectedMessageContent) {

0 commit comments

Comments
 (0)