use of com.google.crypto.tink.KeyTemplate in project tink by google.
the class JwtPublicKeySignVerifyWrappersTest method wrongIssuer_throwsInvalidException.
@Test
public void wrongIssuer_throwsInvalidException() throws Exception {
KeyTemplate template = KeyTemplates.get("JWT_ES256");
KeysetHandle keysetHandle = KeysetHandle.generateNew(template);
JwtPublicKeySign jwtSigner = keysetHandle.getPrimitive(JwtPublicKeySign.class);
KeysetHandle publicHandle = keysetHandle.getPublicKeysetHandle();
JwtPublicKeyVerify jwtVerifier = publicHandle.getPrimitive(JwtPublicKeyVerify.class);
RawJwt rawJwt = RawJwt.newBuilder().setIssuer("Justus").withoutExpiration().build();
String compact = jwtSigner.signAndEncode(rawJwt);
JwtValidator validator = JwtValidator.newBuilder().expectIssuer("Peter").allowMissingExpiration().build();
assertThrows(JwtInvalidException.class, () -> jwtVerifier.verifyAndDecode(compact, validator));
}
use of com.google.crypto.tink.KeyTemplate in project tink by google.
the class JwtPublicKeySignVerifyWrappersTest method test_wrapLegacy_throws.
@Test
public void test_wrapLegacy_throws() throws Exception {
KeyTemplate rawTemplate = KeyTemplates.get("JWT_ES256_RAW");
// Convert the normal, raw template into a template with output prefix type LEGACY
KeyTemplate tinkTemplate = KeyTemplate.create(rawTemplate.getTypeUrl(), rawTemplate.getValue(), KeyTemplate.OutputPrefixType.LEGACY);
KeysetHandle handle = KeysetHandle.generateNew(tinkTemplate);
assertThrows(GeneralSecurityException.class, () -> handle.getPrimitive(JwtPublicKeySign.class));
KeysetHandle publicHandle = handle.getPublicKeysetHandle();
assertThrows(GeneralSecurityException.class, () -> publicHandle.getPrimitive(JwtPublicKeyVerify.class));
}
use of com.google.crypto.tink.KeyTemplate in project tink by google.
the class JwtPublicKeySignVerifyWrappersTest method test_wrapSingleTinkKey_works.
@Test
public void test_wrapSingleTinkKey_works() throws Exception {
KeyTemplate tinkTemplate = KeyTemplates.get("JWT_ES256");
KeysetHandle handle = KeysetHandle.generateNew(tinkTemplate);
JwtPublicKeySign signer = handle.getPrimitive(JwtPublicKeySign.class);
JwtPublicKeyVerify verifier = handle.getPublicKeysetHandle().getPrimitive(JwtPublicKeyVerify.class);
RawJwt rawToken = RawJwt.newBuilder().setJwtId("blah").withoutExpiration().build();
String signedCompact = signer.signAndEncode(rawToken);
JwtValidator validator = JwtValidator.newBuilder().allowMissingExpiration().build();
VerifiedJwt verifiedToken = verifier.verifyAndDecode(signedCompact, validator);
assertThat(verifiedToken.getJwtId()).isEqualTo("blah");
}
use of com.google.crypto.tink.KeyTemplate in project tink by google.
the class JwtPublicKeySignVerifyWrappersTest method wrongKey_throwsInvalidSignatureException.
// Note: we use Theory as a parametrized test -- different from what the Theory framework intends.
@Theory
public void wrongKey_throwsInvalidSignatureException(@FromDataPoints("templateNames") String templateName) throws Exception {
if (TestUtil.isTsan()) {
// We do not use assume because Theories expects to find something which is not skipped.
return;
}
KeyTemplate template = KeyTemplates.get(templateName);
KeysetHandle keysetHandle = KeysetHandle.generateNew(template);
JwtPublicKeySign jwtSign = keysetHandle.getPrimitive(JwtPublicKeySign.class);
RawJwt rawJwt = RawJwt.newBuilder().withoutExpiration().build();
String compact = jwtSign.signAndEncode(rawJwt);
JwtValidator validator = JwtValidator.newBuilder().allowMissingExpiration().build();
KeysetHandle wrongKeysetHandle = KeysetHandle.generateNew(template);
KeysetHandle wrongPublicKeysetHandle = wrongKeysetHandle.getPublicKeysetHandle();
JwtPublicKeyVerify wrongJwtVerify = wrongPublicKeysetHandle.getPrimitive(JwtPublicKeyVerify.class);
assertThrows(GeneralSecurityException.class, () -> wrongJwtVerify.verifyAndDecode(compact, validator));
}
use of com.google.crypto.tink.KeyTemplate in project tink by google.
the class JwtRsaSsaPkcs1SignKeyManagerTest method signAndVerifyWithWrongCustomKid_fails.
@Test
public void signAndVerifyWithWrongCustomKid_fails() throws Exception {
if (TestUtil.isTsan()) {
// We do not use assume because Theories expects to find something which is not skipped.
return;
}
KeyTemplate template = KeyTemplates.get("JWT_RS256_2048_F4_RAW");
KeysetHandle handleWithoutKid = KeysetHandle.generateNew(template);
KeysetHandle handleWithKid = withCustomKid(handleWithoutKid, "kid");
KeysetHandle handleWithWrongKid = withCustomKid(handleWithoutKid, "wrong kid");
JwtPublicKeySign signerWithKid = handleWithKid.getPrimitive(JwtPublicKeySign.class);
RawJwt rawToken = RawJwt.newBuilder().setJwtId("jwtId").withoutExpiration().build();
String signedCompactWithKid = signerWithKid.signAndEncode(rawToken);
JwtValidator validator = JwtValidator.newBuilder().allowMissingExpiration().build();
JwtPublicKeyVerify verifierWithWrongKid = handleWithWrongKid.getPublicKeysetHandle().getPrimitive(JwtPublicKeyVerify.class);
assertThrows(JwtInvalidException.class, () -> verifierWithWrongKid.verifyAndDecode(signedCompactWithKid, validator));
}
Aggregations