Search in sources :

Example 16 with Algorithm

use of com.auth0.jwt.Algorithm in project java-jwt by auth0.

the class ECDSAAlgorithmTest method shouldPassECDSA256KVerificationWithProvidedPublicKey.

@Test
public void shouldPassECDSA256KVerificationWithProvidedPublicKey() throws Exception {
    ECDSAKeyProvider provider = mock(ECDSAKeyProvider.class);
    PublicKey publicKey = readPublicKeyFromFile(PUBLIC_KEY_FILE_256K, "EC");
    when(provider.getPublicKeyById("my-key-id")).thenReturn((ECPublicKey) publicKey);
    Algorithm algorithm = Algorithm.ECDSA256K(provider);
    algorithm.verify(JWT.decode(ES256K_JWT));
}
Also used : ECDSAKeyProvider(com.auth0.jwt.interfaces.ECDSAKeyProvider) ECPublicKey(java.security.interfaces.ECPublicKey) Test(org.junit.Test)

Example 17 with Algorithm

use of com.auth0.jwt.Algorithm in project java-jwt by auth0.

the class JWTVerifierTest method shouldRemoveAudienceWhenPassingNullReference.

@Test
public void shouldRemoveAudienceWhenPassingNullReference() throws Exception {
    Algorithm algorithm = mock(Algorithm.class);
    JWTVerifier verifier = JWTVerifier.init(algorithm).withAudience((String) null).build();
    assertThat(verifier.claims, is(notNullValue()));
    assertThat(verifier.claims, not(hasKey(JWTVerifier.AUDIENCE_EXACT)));
    verifier = JWTVerifier.init(algorithm).withAudience((String[]) null).build();
    assertThat(verifier.claims, is(notNullValue()));
    assertThat(verifier.claims, not(hasKey(JWTVerifier.AUDIENCE_EXACT)));
    verifier = JWTVerifier.init(algorithm).withAudience().build();
    assertThat(verifier.claims, is(notNullValue()));
    assertThat(verifier.claims, not(hasKey(JWTVerifier.AUDIENCE_EXACT)));
    String emptyAud = "   ";
    verifier = JWTVerifier.init(algorithm).withAudience(emptyAud).build();
    assertThat(verifier.claims, is(notNullValue()));
    assertThat(verifier.claims, hasEntry(JWTVerifier.AUDIENCE_EXACT, Collections.singletonList(emptyAud)));
}
Also used : Algorithm(com.auth0.jwt.algorithms.Algorithm) Test(org.junit.Test)

Example 18 with Algorithm

use of com.auth0.jwt.Algorithm in project java-jwt by auth0.

the class JWTVerifierTest method shouldRemoveIssuerWhenPassingNullReference.

@Test
public void shouldRemoveIssuerWhenPassingNullReference() throws Exception {
    Algorithm algorithm = mock(Algorithm.class);
    JWTVerifier verifier = JWTVerifier.init(algorithm).withIssuer((String) null).build();
    assertThat(verifier.claims, is(notNullValue()));
    assertThat(verifier.claims, not(hasKey("iss")));
    verifier = JWTVerifier.init(algorithm).withIssuer((String[]) null).build();
    assertThat(verifier.claims, is(notNullValue()));
    assertThat(verifier.claims, not(hasKey("iss")));
    verifier = JWTVerifier.init(algorithm).withIssuer().build();
    assertThat(verifier.claims, is(notNullValue()));
    assertThat(verifier.claims, not(hasKey("iss")));
    String emptyIss = "  ";
    verifier = JWTVerifier.init(algorithm).withIssuer(emptyIss).build();
    assertThat(verifier.claims, is(notNullValue()));
    assertThat(verifier.claims, hasEntry("iss", Collections.singletonList(emptyIss)));
}
Also used : Algorithm(com.auth0.jwt.algorithms.Algorithm) Test(org.junit.Test)

Example 19 with Algorithm

use of com.auth0.jwt.Algorithm in project java-jwt by auth0.

the class ECDSABouncyCastleProviderTests method shouldFailECDSA256KVerificationOnInvalidJOSESignatureLength.

@Test
public void shouldFailECDSA256KVerificationOnInvalidJOSESignatureLength() throws Exception {
    exception.expect(SignatureVerificationException.class);
    exception.expectMessage("The Token's Signature resulted invalid when verified using the Algorithm: SHA256withECDSA");
    exception.expectCause(isA(IllegalArgumentException.class));
    exception.expectCause(hasMessage(is("Last unit does not have enough valid bits")));
    String jwt = ES256K_JWT.substring(0, ES256K_JWT.length() - 1);
    Algorithm algorithm = Algorithm.ECDSA256K((ECPublicKey) readPublicKeyFromFile(INVALID_PUBLIC_KEY_FILE_256K, "EC"), null);
    algorithm.verify(JWT.decode(jwt));
}
Also used : ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ECDSAAlgorithmTest(com.auth0.jwt.algorithms.ECDSAAlgorithmTest) Test(org.junit.Test)

Example 20 with Algorithm

use of com.auth0.jwt.Algorithm in project java-jwt by auth0.

the class ECDSABouncyCastleProviderTests method shouldThrowOnECDSA256KVerificationWithDERSignature.

@Test
public void shouldThrowOnECDSA256KVerificationWithDERSignature() throws Exception {
    exception.expect(SignatureVerificationException.class);
    exception.expectMessage("The Token's Signature resulted invalid when verified using the Algorithm: SHA256withECDSA");
    exception.expectCause(isA(SignatureException.class));
    exception.expectCause(hasMessage(is("Invalid JOSE signature format.")));
    String jwt = "eyJraWQiOiJteS1rZXktaWQiLCJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NksifQ.e30.MEUCIQDaCA-xzjHBCFhyAm56je5DXgylpUncBsQTxQT7AD19zwIgEjIm3lueII2W4pC_iQR6oRMHNtgqfAzTrWnV7DPNURk";
    ECPublicKey key = (ECPublicKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_256K, "EC");
    Algorithm algorithm = Algorithm.ECDSA256K(key, null);
    algorithm.verify(JWT.decode(jwt));
}
Also used : ECPublicKey(java.security.interfaces.ECPublicKey) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ECDSAAlgorithmTest(com.auth0.jwt.algorithms.ECDSAAlgorithmTest) Test(org.junit.Test)

Aggregations

Algorithm (com.auth0.jwt.algorithms.Algorithm)206 Test (org.junit.Test)160 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)90 JWTVerifier (com.auth0.jwt.JWTVerifier)79 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)79 ECDSAAlgorithmTest (com.auth0.jwt.algorithms.ECDSAAlgorithmTest)61 Date (java.util.Date)57 ECDSAKeyProvider (com.auth0.jwt.interfaces.ECDSAKeyProvider)51 RSAPublicKey (java.security.interfaces.RSAPublicKey)36 ECPublicKey (java.security.interfaces.ECPublicKey)34 RSAKeyProvider (com.auth0.jwt.interfaces.RSAKeyProvider)31 IOException (java.io.IOException)30 JWTCreator (com.auth0.jwt.JWTCreator)28 JWTVerificationException (com.auth0.jwt.exceptions.JWTVerificationException)25 ECPrivateKey (java.security.interfaces.ECPrivateKey)23 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)21 HashMap (java.util.HashMap)17 UnsupportedEncodingException (java.io.UnsupportedEncodingException)16 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)15 JsonObject (com.google.gson.JsonObject)15