Search in sources :

Example 6 with JwtSignResponse

use of com.google.crypto.tink.proto.testing.JwtSignResponse in project tink by google.

the class JwtServiceImplTest method verifyFailsWithWrongKey.

@Test
public void verifyFailsWithWrongKey() 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 + 100, 0);
    JwtSignRequest signRequest = JwtSignRequest.newBuilder().setKeyset(ByteString.copyFrom(keyset)).setRawJwt(token).build();
    JwtSignResponse signResponse = jwtStub.computeMacAndEncode(signRequest);
    assertThat(signResponse.getErr()).isEmpty();
    KeysetGenerateResponse wrongKeysetResponse = generateKeyset(keysetStub, template);
    assertThat(wrongKeysetResponse.getErr()).isEmpty();
    byte[] wrongKeyset = wrongKeysetResponse.getKeyset().toByteArray();
    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(wrongKeyset)).setSignedCompactJwt(signResponse.getSignedCompactJwt()).setValidator(validator).build();
    JwtVerifyResponse verifyResponse = jwtStub.verifyMacAndDecode(verifyRequest);
    assertThat(verifyResponse.getErr()).isNotEmpty();
}
Also used : JwtToken(com.google.crypto.tink.proto.testing.JwtToken) JwtSignRequest(com.google.crypto.tink.proto.testing.JwtSignRequest) JwtValidator(com.google.crypto.tink.proto.testing.JwtValidator) JwtSignResponse(com.google.crypto.tink.proto.testing.JwtSignResponse) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) JwtVerifyRequest(com.google.crypto.tink.proto.testing.JwtVerifyRequest) JwtVerifyResponse(com.google.crypto.tink.proto.testing.JwtVerifyResponse) Test(org.junit.Test)

Example 7 with JwtSignResponse

use of com.google.crypto.tink.proto.testing.JwtSignResponse in project tink by google.

the class JwtServiceImpl method publicKeySignAndEncode.

/**
 * Creates a signed compact JWT.
 */
@Override
public void publicKeySignAndEncode(JwtSignRequest request, StreamObserver<JwtSignResponse> responseObserver) {
    JwtSignResponse response;
    try {
        KeysetHandle keysetHandle = CleartextKeysetHandle.read(BinaryKeysetReader.withBytes(request.getKeyset().toByteArray()));
        RawJwt rawJwt = convertJwtTokenToRawJwt(request.getRawJwt());
        JwtPublicKeySign signer = keysetHandle.getPrimitive(JwtPublicKeySign.class);
        String signedCompactJwt = signer.signAndEncode(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) RawJwt(com.google.crypto.tink.jwt.RawJwt) GeneralSecurityException(java.security.GeneralSecurityException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) JwtPublicKeySign(com.google.crypto.tink.jwt.JwtPublicKeySign) JwtSignResponse(com.google.crypto.tink.proto.testing.JwtSignResponse) ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException)

Example 8 with JwtSignResponse

use of com.google.crypto.tink.proto.testing.JwtSignResponse 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 9 with JwtSignResponse

use of com.google.crypto.tink.proto.testing.JwtSignResponse 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);
}
Also used : JwtToken(com.google.crypto.tink.proto.testing.JwtToken) JwtSignRequest(com.google.crypto.tink.proto.testing.JwtSignRequest) JwtValidator(com.google.crypto.tink.proto.testing.JwtValidator) JwtSignResponse(com.google.crypto.tink.proto.testing.JwtSignResponse) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) JwtVerifyRequest(com.google.crypto.tink.proto.testing.JwtVerifyRequest) JwtVerifyResponse(com.google.crypto.tink.proto.testing.JwtVerifyResponse) KeysetPublicResponse(com.google.crypto.tink.proto.testing.KeysetPublicResponse) Test(org.junit.Test)

Example 10 with JwtSignResponse

use of com.google.crypto.tink.proto.testing.JwtSignResponse 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();
}
Also used : JwtToken(com.google.crypto.tink.proto.testing.JwtToken) JwtSignRequest(com.google.crypto.tink.proto.testing.JwtSignRequest) JwtValidator(com.google.crypto.tink.proto.testing.JwtValidator) JwtSignResponse(com.google.crypto.tink.proto.testing.JwtSignResponse) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) JwtVerifyRequest(com.google.crypto.tink.proto.testing.JwtVerifyRequest) JwtVerifyResponse(com.google.crypto.tink.proto.testing.JwtVerifyResponse) Test(org.junit.Test)

Aggregations

JwtSignResponse (com.google.crypto.tink.proto.testing.JwtSignResponse)10 JwtSignRequest (com.google.crypto.tink.proto.testing.JwtSignRequest)8 JwtToken (com.google.crypto.tink.proto.testing.JwtToken)8 Test (org.junit.Test)8 JwtValidator (com.google.crypto.tink.proto.testing.JwtValidator)7 JwtVerifyRequest (com.google.crypto.tink.proto.testing.JwtVerifyRequest)7 JwtVerifyResponse (com.google.crypto.tink.proto.testing.JwtVerifyResponse)7 KeysetGenerateResponse (com.google.crypto.tink.proto.testing.KeysetGenerateResponse)7 CleartextKeysetHandle (com.google.crypto.tink.CleartextKeysetHandle)2 KeysetHandle (com.google.crypto.tink.KeysetHandle)2 RawJwt (com.google.crypto.tink.jwt.RawJwt)2 KeysetPublicResponse (com.google.crypto.tink.proto.testing.KeysetPublicResponse)2 ByteString (com.google.protobuf.ByteString)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 IOException (java.io.IOException)2 GeneralSecurityException (java.security.GeneralSecurityException)2 JwtMac (com.google.crypto.tink.jwt.JwtMac)1 JwtPublicKeySign (com.google.crypto.tink.jwt.JwtPublicKeySign)1 JwtFromJwkSetRequest (com.google.crypto.tink.proto.testing.JwtFromJwkSetRequest)1 JwtFromJwkSetResponse (com.google.crypto.tink.proto.testing.JwtFromJwkSetResponse)1