use of com.google.crypto.tink.proto.testing.KeysetGenerateResponse in project tink by google.
the class TestingServicesTest method toFromJson_success.
@Test
public void toFromJson_success() throws Exception {
byte[] template = KeyTemplateProtoConverter.toByteArray(KeyTemplates.get("AES128_GCM"));
KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
assertThat(keysetResponse.getErr()).isEmpty();
byte[] keyset = keysetResponse.getKeyset().toByteArray();
KeysetToJsonResponse toResponse = keysetToJson(keysetStub, keyset);
assertThat(toResponse.getErr()).isEmpty();
String jsonKeyset = toResponse.getJsonKeyset();
KeysetFromJsonResponse fromResponse = keysetFromJson(keysetStub, jsonKeyset);
assertThat(fromResponse.getErr()).isEmpty();
byte[] output = fromResponse.getKeyset().toByteArray();
assertThat(output).isEqualTo(keyset);
}
use of com.google.crypto.tink.proto.testing.KeysetGenerateResponse in project tink by google.
the class TestingServicesTest method computeVerifyMac_success.
@Test
public void computeVerifyMac_success() throws Exception {
byte[] template = KeyTemplateProtoConverter.toByteArray(HmacKeyManager.hmacSha256HalfDigestTemplate());
byte[] data = "The quick brown fox jumps over the lazy dog".getBytes(UTF_8);
KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
assertThat(keysetResponse.getErr()).isEmpty();
byte[] keyset = keysetResponse.getKeyset().toByteArray();
ComputeMacResponse compResponse = computeMac(macStub, keyset, data);
assertThat(compResponse.getErr()).isEmpty();
byte[] macValue = compResponse.getMacValue().toByteArray();
VerifyMacResponse verifyResponse = verifyMac(macStub, keyset, macValue, data);
assertThat(verifyResponse.getErr()).isEmpty();
}
use of com.google.crypto.tink.proto.testing.KeysetGenerateResponse in project tink by google.
the class JwtServiceImplTest method verifyFailsWhenExpired.
@Test
public void verifyFailsWhenExpired() throws Exception {
byte[] template = KeyTemplateProtoConverter.toByteArray(JwtHmacKeyManager.hs256Template());
KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
assertThat(keysetResponse.getErr()).isEmpty();
byte[] keyset = keysetResponse.getKeyset().toByteArray();
JwtToken token = generateToken("audience", 1234 - 10, 0);
JwtSignRequest signRequest = JwtSignRequest.newBuilder().setKeyset(ByteString.copyFrom(keyset)).setRawJwt(token).build();
JwtSignResponse signResponse = jwtStub.computeMacAndEncode(signRequest);
assertThat(signResponse.getErr()).isEmpty();
JwtValidator validator = JwtValidator.newBuilder().setExpectedTypeHeader(StringValue.newBuilder().setValue("typeHeader")).setExpectedIssuer(StringValue.newBuilder().setValue("issuer")).setExpectedAudience(StringValue.newBuilder().setValue("audience")).setNow(Timestamp.newBuilder().setSeconds(1234)).build();
JwtVerifyRequest verifyRequest = JwtVerifyRequest.newBuilder().setKeyset(ByteString.copyFrom(keyset)).setSignedCompactJwt(signResponse.getSignedCompactJwt()).setValidator(validator).build();
JwtVerifyResponse verifyResponse = jwtStub.verifyMacAndDecode(verifyRequest);
assertThat(verifyResponse.getErr()).isNotEmpty();
}
use of com.google.crypto.tink.proto.testing.KeysetGenerateResponse in project tink by google.
the class JwtServiceImplTest method jwtEmptyTokenComputeVerifyMac_success.
@Test
public void jwtEmptyTokenComputeVerifyMac_success() throws Exception {
byte[] template = KeyTemplateProtoConverter.toByteArray(JwtHmacKeyManager.hs256Template());
KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
assertThat(keysetResponse.getErr()).isEmpty();
byte[] keyset = keysetResponse.getKeyset().toByteArray();
JwtToken token = JwtToken.getDefaultInstance();
JwtSignRequest signRequest = JwtSignRequest.newBuilder().setKeyset(ByteString.copyFrom(keyset)).setRawJwt(token).build();
JwtSignResponse signResponse = jwtStub.computeMacAndEncode(signRequest);
assertThat(signResponse.getErr()).isEmpty();
JwtValidator validator = JwtValidator.newBuilder().setAllowMissingExpiration(true).build();
JwtVerifyRequest verifyRequest = JwtVerifyRequest.newBuilder().setKeyset(ByteString.copyFrom(keyset)).setSignedCompactJwt(signResponse.getSignedCompactJwt()).setValidator(validator).build();
JwtVerifyResponse verifyResponse = jwtStub.verifyMacAndDecode(verifyRequest);
assertThat(verifyResponse.getErr()).isEmpty();
assertThat(verifyResponse.getVerifiedJwt()).isEqualTo(token);
}
use of com.google.crypto.tink.proto.testing.KeysetGenerateResponse in project tink by google.
the class JwtServiceImplTest method jwtToFromJwt_success.
@Test
public void jwtToFromJwt_success() throws Exception {
byte[] template = KeyTemplateProtoConverter.toByteArray(KeyTemplates.get("JWT_ES256"));
KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
assertThat(keysetResponse.getErr()).isEmpty();
byte[] privateKeyset = keysetResponse.getKeyset().toByteArray();
KeysetPublicResponse pubResponse = publicKeyset(keysetStub, privateKeyset);
assertThat(pubResponse.getErr()).isEmpty();
byte[] publicKeyset = pubResponse.getPublicKeyset().toByteArray();
JwtToken token = generateToken("audience", 1245, 0);
JwtSignRequest signRequest = JwtSignRequest.newBuilder().setKeyset(ByteString.copyFrom(privateKeyset)).setRawJwt(token).build();
JwtSignResponse signResponse = jwtStub.publicKeySignAndEncode(signRequest);
assertThat(signResponse.getErr()).isEmpty();
// Convert the public keyset to a JWK set
JwtToJwkSetRequest toRequest = JwtToJwkSetRequest.newBuilder().setKeyset(ByteString.copyFrom(publicKeyset)).build();
JwtToJwkSetResponse toResponse = jwtStub.toJwkSet(toRequest);
assertThat(toResponse.getErr()).isEmpty();
assertThat(toResponse.getJwkSet()).contains("{\"keys\":[{\"kty\":\"EC\",\"crv\":\"P-256\",");
// Convert the public keyset to a JWK set
JwtFromJwkSetRequest fromRequest = JwtFromJwkSetRequest.newBuilder().setJwkSet(toResponse.getJwkSet()).build();
JwtFromJwkSetResponse fromResponse = jwtStub.fromJwkSet(fromRequest);
assertThat(fromResponse.getErr()).isEmpty();
// Use that output keyset to verify the token
JwtValidator validator = JwtValidator.newBuilder().setExpectedTypeHeader(StringValue.newBuilder().setValue("typeHeader")).setExpectedIssuer(StringValue.newBuilder().setValue("issuer")).setExpectedAudience(StringValue.newBuilder().setValue("audience")).setNow(Timestamp.newBuilder().setSeconds(1234)).build();
JwtVerifyRequest verifyRequest = JwtVerifyRequest.newBuilder().setKeyset(fromResponse.getKeyset()).setSignedCompactJwt(signResponse.getSignedCompactJwt()).setValidator(validator).build();
JwtVerifyResponse verifyResponse = jwtStub.publicKeyVerifyAndDecode(verifyRequest);
assertThat(verifyResponse.getErr()).isEmpty();
}
Aggregations