Search in sources :

Example 1 with JwtMac

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();
}
Also used : KeysetHandle(com.google.crypto.tink.KeysetHandle) CleartextKeysetHandle(com.google.crypto.tink.CleartextKeysetHandle) JwtMac(com.google.crypto.tink.jwt.JwtMac) RawJwt(com.google.crypto.tink.jwt.RawJwt) GeneralSecurityException(java.security.GeneralSecurityException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) JwtSignResponse(com.google.crypto.tink.proto.testing.JwtSignResponse) ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException)

Example 2 with JwtMac

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();
}
Also used : KeysetHandle(com.google.crypto.tink.KeysetHandle) CleartextKeysetHandle(com.google.crypto.tink.CleartextKeysetHandle) JwtToken(com.google.crypto.tink.proto.testing.JwtToken) JwtMac(com.google.crypto.tink.jwt.JwtMac) JwtValidator(com.google.crypto.tink.jwt.JwtValidator) VerifiedJwt(com.google.crypto.tink.jwt.VerifiedJwt) GeneralSecurityException(java.security.GeneralSecurityException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) IOException(java.io.IOException) JwtVerifyResponse(com.google.crypto.tink.proto.testing.JwtVerifyResponse)

Aggregations

CleartextKeysetHandle (com.google.crypto.tink.CleartextKeysetHandle)2 KeysetHandle (com.google.crypto.tink.KeysetHandle)2 JwtMac (com.google.crypto.tink.jwt.JwtMac)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 IOException (java.io.IOException)2 GeneralSecurityException (java.security.GeneralSecurityException)2 JwtValidator (com.google.crypto.tink.jwt.JwtValidator)1 RawJwt (com.google.crypto.tink.jwt.RawJwt)1 VerifiedJwt (com.google.crypto.tink.jwt.VerifiedJwt)1 JwtSignResponse (com.google.crypto.tink.proto.testing.JwtSignResponse)1 JwtToken (com.google.crypto.tink.proto.testing.JwtToken)1 JwtVerifyResponse (com.google.crypto.tink.proto.testing.JwtVerifyResponse)1 ByteString (com.google.protobuf.ByteString)1