use of com.google.crypto.tink.jwt.JwtMac in project tink by google.
the class JwtServiceImpl method computeMacAndEncode.
/**
* Creates a signed compact JWT.
*/
@Override
public void computeMacAndEncode(JwtSignRequest request, StreamObserver<JwtSignResponse> responseObserver) {
JwtSignResponse response;
try {
KeysetHandle keysetHandle = CleartextKeysetHandle.read(BinaryKeysetReader.withBytes(request.getKeyset().toByteArray()));
RawJwt rawJwt = convertJwtTokenToRawJwt(request.getRawJwt());
JwtMac jwtMac = keysetHandle.getPrimitive(JwtMac.class);
String signedCompactJwt = jwtMac.computeMacAndEncode(rawJwt);
response = JwtSignResponse.newBuilder().setSignedCompactJwt(signedCompactJwt).build();
} catch (GeneralSecurityException | InvalidProtocolBufferException e) {
response = JwtSignResponse.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.jwt.JwtMac 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();
}
Aggregations