Search in sources :

Example 31 with ECDSAKeyProvider

use of com.auth0.jwt.interfaces.ECDSAKeyProvider in project java-jwt by auth0.

the class ECDSAAlgorithmTest method shouldFailECDSA256KVerificationWhenProvidedPublicKeyIsNull.

@Test
public void shouldFailECDSA256KVerificationWhenProvidedPublicKeyIsNull() throws Exception {
    exception.expect(SignatureVerificationException.class);
    exception.expectMessage("The Token's Signature resulted invalid when verified using the Algorithm: SHA256withECDSA");
    exception.expectCause(isA(IllegalStateException.class));
    exception.expectCause(hasMessage(is("The given Public Key is null.")));
    ECDSAKeyProvider provider = mock(ECDSAKeyProvider.class);
    when(provider.getPublicKeyById("my-key-id")).thenReturn(null);
    Algorithm algorithm = Algorithm.ECDSA256K(provider);
    algorithm.verify(JWT.decode(ES256K_JWT));
}
Also used : ECDSAKeyProvider(com.auth0.jwt.interfaces.ECDSAKeyProvider) Test(org.junit.Test)

Example 32 with ECDSAKeyProvider

use of com.auth0.jwt.interfaces.ECDSAKeyProvider in project java-jwt by auth0.

the class AlgorithmTest method shouldCreateECDSA256KAlgorithmWithProvider.

@Test
public void shouldCreateECDSA256KAlgorithmWithProvider() throws Exception {
    ECDSAKeyProvider provider = mock(ECDSAKeyProvider.class);
    Algorithm algorithm = Algorithm.ECDSA256K(provider);
    assertThat(algorithm, is(notNullValue()));
    assertThat(algorithm, is(instanceOf(ECDSAAlgorithm.class)));
    assertThat(algorithm.getDescription(), is("SHA256withECDSA"));
    assertThat(algorithm.getName(), is("ES256K"));
}
Also used : ECDSAKeyProvider(com.auth0.jwt.interfaces.ECDSAKeyProvider) Test(org.junit.Test)

Example 33 with ECDSAKeyProvider

use of com.auth0.jwt.interfaces.ECDSAKeyProvider in project java-jwt by auth0.

the class ECDSABouncyCastleProviderTests method shouldThrowOnVerifyWhenTheSignatureIsNotPrepared.

@Test
public void shouldThrowOnVerifyWhenTheSignatureIsNotPrepared() throws Exception {
    exception.expect(SignatureVerificationException.class);
    exception.expectMessage("The Token's Signature resulted invalid when verified using the Algorithm: some-alg");
    exception.expectCause(isA(SignatureException.class));
    CryptoHelper crypto = mock(CryptoHelper.class);
    when(crypto.verifySignatureFor(anyString(), any(PublicKey.class), any(String.class), any(String.class), any(byte[].class))).thenThrow(SignatureException.class);
    ECPublicKey publicKey = mock(ECPublicKey.class);
    when(publicKey.getParams()).thenReturn(mock(ECParameterSpec.class));
    byte[] a = new byte[64];
    Arrays.fill(a, Byte.MAX_VALUE);
    when(publicKey.getParams().getOrder()).thenReturn(new BigInteger(a));
    ECPrivateKey privateKey = mock(ECPrivateKey.class);
    ECDSAKeyProvider provider = ECDSAAlgorithm.providerForKeys(publicKey, privateKey);
    Algorithm algorithm = new ECDSAAlgorithm(crypto, "some-alg", "some-algorithm", 32, provider);
    String jwt = "eyJhbGciOiJFUzI1NiJ9.eyJpc3MiOiJhdXRoMCJ9.4iVk3-Y0v4RT4_9IaQlp-8dZ_4fsTzIylgrPTDLrEvTHBTyVS3tgPbr2_IZfLETtiKRqCg0aQ5sh9eIsTTwB1g";
    algorithm.verify(JWT.decode(jwt));
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) ECDSAKeyProvider(com.auth0.jwt.interfaces.ECDSAKeyProvider) ECPublicKey(java.security.interfaces.ECPublicKey) ECPublicKey(java.security.interfaces.ECPublicKey) ECParameterSpec(java.security.spec.ECParameterSpec) BigInteger(java.math.BigInteger) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ECDSAAlgorithmTest(com.auth0.jwt.algorithms.ECDSAAlgorithmTest) Test(org.junit.Test)

Example 34 with ECDSAKeyProvider

use of com.auth0.jwt.interfaces.ECDSAKeyProvider in project java-jwt by auth0.

the class ECDSABouncyCastleProviderTests method shouldDoECDSA256SigningWithProvidedPrivateKey.

@Test
public void shouldDoECDSA256SigningWithProvidedPrivateKey() throws Exception {
    ECDSAKeyProvider provider = mock(ECDSAKeyProvider.class);
    PrivateKey privateKey = readPrivateKeyFromFile(PRIVATE_KEY_FILE_256, "EC");
    PublicKey publicKey = readPublicKeyFromFile(PUBLIC_KEY_FILE_256, "EC");
    when(provider.getPrivateKey()).thenReturn((ECPrivateKey) privateKey);
    when(provider.getPublicKeyById(null)).thenReturn((ECPublicKey) publicKey);
    Algorithm algorithm = Algorithm.ECDSA256(provider);
    String jwt = asJWT(algorithm, ES256Header, auth0IssPayload);
    assertSignaturePresent(jwt);
    algorithm.verify(JWT.decode(jwt));
}
Also used : ECDSAKeyProvider(com.auth0.jwt.interfaces.ECDSAKeyProvider) ECPrivateKey(java.security.interfaces.ECPrivateKey) ECPublicKey(java.security.interfaces.ECPublicKey) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ECDSAAlgorithmTest(com.auth0.jwt.algorithms.ECDSAAlgorithmTest) Test(org.junit.Test)

Example 35 with ECDSAKeyProvider

use of com.auth0.jwt.interfaces.ECDSAKeyProvider in project java-jwt by auth0.

the class ECDSABouncyCastleProviderTests method shouldFailOnECDSA512SigningWhenProvidedPrivateKeyIsNull.

@Test
public void shouldFailOnECDSA512SigningWhenProvidedPrivateKeyIsNull() {
    exception.expect(SignatureGenerationException.class);
    exception.expectMessage("The Token's Signature couldn't be generated when signing using the Algorithm: SHA512withECDSA");
    exception.expectCause(isA(IllegalStateException.class));
    exception.expectCause(hasMessage(is("The given Private Key is null.")));
    ECDSAKeyProvider provider = mock(ECDSAKeyProvider.class);
    when(provider.getPrivateKey()).thenReturn(null);
    Algorithm algorithm = Algorithm.ECDSA512(provider);
    algorithm.sign(new byte[0], new byte[0]);
}
Also used : ECDSAKeyProvider(com.auth0.jwt.interfaces.ECDSAKeyProvider) ECDSAAlgorithmTest(com.auth0.jwt.algorithms.ECDSAAlgorithmTest) Test(org.junit.Test)

Aggregations

ECDSAKeyProvider (com.auth0.jwt.interfaces.ECDSAKeyProvider)58 Test (org.junit.Test)58 ECPublicKey (java.security.interfaces.ECPublicKey)30 ECPrivateKey (java.security.interfaces.ECPrivateKey)26 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)26 ECDSAAlgorithmTest (com.auth0.jwt.algorithms.ECDSAAlgorithmTest)23 BigInteger (java.math.BigInteger)6 ECParameterSpec (java.security.spec.ECParameterSpec)6