use of com.google.crypto.tink.proto.testing.JwtVerifyResponse in project tink by google.
the class JwtServiceImpl method publicKeyVerifyAndDecode.
/**
* Decodes and verifies a signed, compact JWT.
*/
@Override
public void publicKeyVerifyAndDecode(JwtVerifyRequest request, StreamObserver<JwtVerifyResponse> responseObserver) {
JwtVerifyResponse response;
try {
KeysetHandle keysetHandle = CleartextKeysetHandle.read(BinaryKeysetReader.withBytes(request.getKeyset().toByteArray()));
JwtValidator validator = convertProtoValidatorToValidator(request.getValidator());
JwtPublicKeyVerify verifier = keysetHandle.getPrimitive(JwtPublicKeyVerify.class);
VerifiedJwt verifiedJwt = verifier.verifyAndDecode(request.getSignedCompactJwt(), validator);
JwtToken token = convertVerifiedJwtToJwtToken(verifiedJwt);
response = JwtVerifyResponse.newBuilder().setVerifiedJwt(token).build();
} catch (GeneralSecurityException | InvalidProtocolBufferException e) {
response = JwtVerifyResponse.newBuilder().setErr(e.toString()).build();
} catch (IOException e) {
responseObserver.onError(Status.UNKNOWN.withDescription(e.getMessage()).asException());
return;
}
responseObserver.onNext(response);
responseObserver.onCompleted();
}
use of com.google.crypto.tink.proto.testing.JwtVerifyResponse in project tink by google.
the class JwtServiceImpl method verifyMacAndDecode.
/**
* Decodes and verifies a signed, compact JWT.
*/
@Override
public void verifyMacAndDecode(JwtVerifyRequest request, StreamObserver<JwtVerifyResponse> responseObserver) {
JwtVerifyResponse response;
try {
KeysetHandle keysetHandle = CleartextKeysetHandle.read(BinaryKeysetReader.withBytes(request.getKeyset().toByteArray()));
JwtValidator validator = convertProtoValidatorToValidator(request.getValidator());
JwtMac jwtMac = keysetHandle.getPrimitive(JwtMac.class);
VerifiedJwt verifiedJwt = jwtMac.verifyMacAndDecode(request.getSignedCompactJwt(), validator);
JwtToken token = convertVerifiedJwtToJwtToken(verifiedJwt);
response = JwtVerifyResponse.newBuilder().setVerifiedJwt(token).build();
} catch (GeneralSecurityException | InvalidProtocolBufferException e) {
response = JwtVerifyResponse.newBuilder().setErr(e.toString()).build();
} catch (IOException e) {
responseObserver.onError(Status.UNKNOWN.withDescription(e.getMessage()).asException());
return;
}
responseObserver.onNext(response);
responseObserver.onCompleted();
}
use of com.google.crypto.tink.proto.testing.JwtVerifyResponse in project tink by google.
the class JwtServiceImplTest method publicKeySignVerify_success.
@Test
public void publicKeySignVerify_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();
long expSecs = 1234 + 100;
int expNanos = 567000000;
JwtToken token = generateToken("audience", expSecs, expNanos);
JwtSignRequest signRequest = JwtSignRequest.newBuilder().setKeyset(ByteString.copyFrom(privateKeyset)).setRawJwt(token).build();
JwtSignResponse signResponse = jwtStub.publicKeySignAndEncode(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(publicKeyset)).setSignedCompactJwt(signResponse.getSignedCompactJwt()).setValidator(validator).build();
JwtToken expectedToken = generateToken("audience", expSecs, 0);
JwtVerifyResponse verifyResponse = jwtStub.publicKeyVerifyAndDecode(verifyRequest);
assertThat(verifyResponse.getErr()).isEmpty();
assertThat(verifyResponse.getVerifiedJwt()).isEqualTo(expectedToken);
}
use of com.google.crypto.tink.proto.testing.JwtVerifyResponse in project tink by google.
the class JwtServiceImplTest method verifyFailsWithWrongAudience.
@Test
public void verifyFailsWithWrongAudience() 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("wrong_audience", 1234 + 100, 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();
}
Aggregations