use of com.google.crypto.tink.KeyTemplate in project tink by google.
the class JwtHmacKeyManagerTest method testRawHs256Template.
@Test
public void testRawHs256Template() throws Exception {
KeyTemplate template = KeyTemplates.get("JWT_HS256_RAW");
assertThat(template.getTypeUrl()).isEqualTo(manager.getKeyType());
assertThat(template.getOutputPrefixType()).isEqualTo(KeyTemplate.OutputPrefixType.RAW);
JwtHmacKeyFormat format = JwtHmacKeyFormat.parseFrom(template.getValue(), ExtensionRegistryLite.getEmptyRegistry());
assertThat(format.getKeySize()).isEqualTo(32);
assertThat(format.getAlgorithm()).isEqualTo(JwtHmacAlgorithm.HS256);
}
use of com.google.crypto.tink.KeyTemplate in project tink by google.
the class JwtHmacKeyManagerTest method verify_expired_shouldThrow.
@Test
public void verify_expired_shouldThrow() 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 expires in 1 minute in the future.
RawJwt token = RawJwt.newBuilder().setExpiration(clock1.instant().plus(Duration.ofMinutes(1))).build();
String compact = mac.computeMacAndEncode(token);
// Move the clock to 2 minutes in the future.
Clock clock2 = Clock.offset(clock1, Duration.ofMinutes(2));
JwtValidator validator = JwtValidator.newBuilder().setClock(clock2).build();
assertThrows(JwtInvalidException.class, () -> mac.verifyMacAndDecode(compact, validator));
}
use of com.google.crypto.tink.KeyTemplate in project tink by google.
the class JwtHmacKeyManagerTest method verify_noAudienceInJwt_shouldThrow.
@Test
public void verify_noAudienceInJwt_shouldThrow() throws Exception {
KeyTemplate template = KeyTemplates.get("JWT_HS256");
KeysetHandle handle = KeysetHandle.generateNew(template);
JwtMac mac = handle.getPrimitive(JwtMac.class);
RawJwt unverified = RawJwt.newBuilder().withoutExpiration().build();
String compact = mac.computeMacAndEncode(unverified);
JwtValidator validator = JwtValidator.newBuilder().allowMissingExpiration().expectAudience("foo").build();
assertThrows(JwtInvalidException.class, () -> mac.verifyMacAndDecode(compact, validator));
}
use of com.google.crypto.tink.KeyTemplate in project tink by google.
the class JwtHmacKeyManagerTest method createSignVerifyDifferentKey_throw.
// Note: we use Theory as a parametrized test -- different from what the Theory framework intends.
@Theory
public void createSignVerifyDifferentKey_throw(String templateNames) throws Exception {
KeyTemplate template = KeyTemplates.get(templateNames);
KeysetHandle handle = KeysetHandle.generateNew(template);
JwtMac primitive = handle.getPrimitive(JwtMac.class);
RawJwt rawToken = RawJwt.newBuilder().setJwtId("jwtId").withoutExpiration().build();
String compact = primitive.computeMacAndEncode(rawToken);
KeysetHandle otherHandle = KeysetHandle.generateNew(template);
JwtMac otherPrimitive = otherHandle.getPrimitive(JwtMac.class);
JwtValidator validator = JwtValidator.newBuilder().allowMissingExpiration().build();
assertThrows(GeneralSecurityException.class, () -> otherPrimitive.verifyMacAndDecode(compact, validator));
}
use of com.google.crypto.tink.KeyTemplate in project tink by google.
the class JwtHmacKeyManagerTest method computeVerify_canGetData.
@Test
public void computeVerify_canGetData() throws Exception {
KeyTemplate template = KeyTemplates.get("JWT_HS256");
KeysetHandle handle = KeysetHandle.generateNew(template);
JwtMac mac = handle.getPrimitive(JwtMac.class);
String issuer = "google";
String audience = "mybank";
String jwtId = "user123";
double amount = 0.1;
RawJwt unverified = RawJwt.newBuilder().setTypeHeader("myType").setIssuer(issuer).addAudience(audience).setJwtId(jwtId).addNumberClaim("amount", amount).withoutExpiration().build();
String compact = mac.computeMacAndEncode(unverified);
JwtValidator validator = JwtValidator.newBuilder().expectTypeHeader("myType").expectIssuer(issuer).expectAudience(audience).allowMissingExpiration().build();
VerifiedJwt token = mac.verifyMacAndDecode(compact, validator);
assertThat(token.getTypeHeader()).isEqualTo("myType");
assertThat(token.getNumberClaim("amount")).isEqualTo(amount);
assertThat(token.getIssuer()).isEqualTo(issuer);
assertThat(token.getAudiences()).containsExactly(audience);
assertThat(token.getJwtId()).isEqualTo(jwtId);
}
Aggregations