use of com.google.crypto.tink.KeyTemplate in project tink by google.
the class JwtHmacKeyManagerTest method verify_wrongAudience_shouldThrow.
@Test
public void verify_wrongAudience_shouldThrow() throws Exception {
KeyTemplate template = KeyTemplates.get("JWT_HS256");
KeysetHandle handle = KeysetHandle.generateNew(template);
JwtMac mac = handle.getPrimitive(JwtMac.class);
RawJwt unverified = RawJwt.newBuilder().addAudience("foo").withoutExpiration().build();
String compact = mac.computeMacAndEncode(unverified);
JwtValidator validator = JwtValidator.newBuilder().allowMissingExpiration().expectAudience("bar").build();
assertThrows(JwtInvalidException.class, () -> mac.verifyMacAndDecode(compact, validator));
}
use of com.google.crypto.tink.KeyTemplate in project tink by google.
the class JwtHmacKeyManagerTest method testHs384Template.
@Test
public void testHs384Template() throws Exception {
KeyTemplate template = KeyTemplates.get("JWT_HS384");
assertThat(template.getTypeUrl()).isEqualTo(new JwtHmacKeyManager().getKeyType());
assertThat(template.getOutputPrefixType()).isEqualTo(KeyTemplate.OutputPrefixType.TINK);
JwtHmacKeyFormat format = JwtHmacKeyFormat.parseFrom(template.getValue(), ExtensionRegistryLite.getEmptyRegistry());
assertThat(format.getKeySize()).isEqualTo(48);
assertThat(format.getAlgorithm()).isEqualTo(JwtHmacAlgorithm.HS384);
}
use of com.google.crypto.tink.KeyTemplate in project tink by google.
the class JwtHmacKeyManagerTest method verify_noAudienceInValidator_shouldThrow.
@Test
public void verify_noAudienceInValidator_shouldThrow() throws Exception {
KeyTemplate template = KeyTemplates.get("JWT_HS256");
KeysetHandle handle = KeysetHandle.generateNew(template);
JwtMac mac = handle.getPrimitive(JwtMac.class);
RawJwt unverified = RawJwt.newBuilder().addAudience("foo").withoutExpiration().build();
String compact = mac.computeMacAndEncode(unverified);
JwtValidator validator = JwtValidator.newBuilder().allowMissingExpiration().build();
assertThrows(JwtInvalidException.class, () -> mac.verifyMacAndDecode(compact, validator));
}
use of com.google.crypto.tink.KeyTemplate in project tink by google.
the class JwtHmacKeyManagerTest method validate_notBefore_success.
@Test
public void validate_notBefore_success() throws Exception {
KeyTemplate template = KeyTemplates.get("JWT_HS256");
KeysetHandle handle = KeysetHandle.generateNew(template);
JwtMac mac = handle.getPrimitive(JwtMac.class);
Clock clock1 = Clock.systemUTC();
// This token cannot be used until 1 minute in the future.
Instant notBefore = clock1.instant().plus(Duration.ofMinutes(1));
RawJwt unverified = RawJwt.newBuilder().setNotBefore(notBefore).withoutExpiration().build();
String compact = mac.computeMacAndEncode(unverified);
// Move the clock to 2 minutes in the future.
Clock clock2 = Clock.offset(clock1, Duration.ofMinutes(2));
JwtValidator validator = JwtValidator.newBuilder().allowMissingExpiration().setClock(clock2).build();
VerifiedJwt token = mac.verifyMacAndDecode(compact, validator);
assertThat(token.getNotBefore()).isEqualTo(unverified.getNotBefore());
}
use of com.google.crypto.tink.KeyTemplate in project tink by google.
the class JwtHmacKeyManagerTest method createSignVerifyTink_withDifferentHeaders.
@Test
public void createSignVerifyTink_withDifferentHeaders() throws Exception {
KeyTemplate template = KeyTemplates.get("JWT_HS256");
KeysetHandle handle = KeysetHandle.generateNew(template);
Keyset keyset = CleartextKeysetHandle.getKeyset(handle);
JwtHmacKey keyProto = JwtHmacKey.parseFrom(keyset.getKey(0).getKeyData().getValue(), ExtensionRegistryLite.getEmptyRegistry());
byte[] keyValue = keyProto.getKeyValue().toByteArray();
SecretKeySpec keySpec = new SecretKeySpec(keyValue, "HMAC");
PrfHmacJce prf = new PrfHmacJce("HMACSHA256", keySpec);
PrfMac rawPrimitive = new PrfMac(prf, prf.getMaxOutputLength());
JwtMac primitive = handle.getPrimitive(JwtMac.class);
String kid = JwtFormat.getKid(keyset.getKey(0).getKeyId(), keyset.getKey(0).getOutputPrefixType()).get();
JsonObject payload = new JsonObject();
payload.addProperty("jti", "jwtId");
JwtValidator validator = JwtValidator.newBuilder().allowMissingExpiration().build();
// Normal, valid signed compact.
JsonObject normalHeader = new JsonObject();
normalHeader.addProperty("alg", "HS256");
normalHeader.addProperty("kid", kid);
String normalToken = generateSignedCompact(rawPrimitive, normalHeader, payload);
primitive.verifyMacAndDecode(normalToken, validator);
// valid token, with "typ" set in the header
JsonObject headerWithTyp = new JsonObject();
headerWithTyp.addProperty("alg", "HS256");
headerWithTyp.addProperty("typ", "typeHeader");
headerWithTyp.addProperty("kid", kid);
String tokenWithTyp = generateSignedCompact(rawPrimitive, headerWithTyp, payload);
primitive.verifyMacAndDecode(tokenWithTyp, JwtValidator.newBuilder().expectTypeHeader("typeHeader").allowMissingExpiration().build());
// invalid token without algorithm
JsonObject headerWithoutAlg = new JsonObject();
headerWithoutAlg.addProperty("kid", kid);
String tokenWithoutAlg = generateSignedCompact(rawPrimitive, headerWithoutAlg, payload);
assertThrows(GeneralSecurityException.class, () -> primitive.verifyMacAndDecode(tokenWithoutAlg, validator));
// invalid token with a valid but incorrect algorithm in the header
JsonObject headerWithBadAlg = new JsonObject();
headerWithBadAlg.addProperty("alg", "RS256");
headerWithBadAlg.addProperty("kid", kid);
String tokenWithBadAlg = generateSignedCompact(rawPrimitive, headerWithBadAlg, payload);
assertThrows(GeneralSecurityException.class, () -> primitive.verifyMacAndDecode(tokenWithBadAlg, validator));
// token with an unknown "kid" in the header is valid
JsonObject headerWithUnknownKid = new JsonObject();
headerWithUnknownKid.addProperty("alg", "HS256");
headerWithUnknownKid.addProperty("kid", "unknown");
String tokenWithUnknownKid = generateSignedCompact(rawPrimitive, headerWithUnknownKid, payload);
assertThrows(GeneralSecurityException.class, () -> primitive.verifyMacAndDecode(tokenWithUnknownKid, validator));
}
Aggregations