Search in sources :

Example 26 with ECDSAKeyProvider

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

the class ECDSAAlgorithmTest method shouldFailECDSA512VerificationWhenProvidedPublicKeyIsNull.

@Test
public void shouldFailECDSA512VerificationWhenProvidedPublicKeyIsNull() {
    exception.expect(SignatureVerificationException.class);
    exception.expectMessage("The Token's Signature resulted invalid when verified using the Algorithm: SHA512withECDSA");
    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);
    String jwt = "eyJhbGciOiJFUzUxMiIsImtpZCI6Im15LWtleS1pZCJ9.eyJpc3MiOiJhdXRoMCJ9.AGxEwbsYa2bQ7Y7DAcTQnVD8PmLSlhJ20jg2OfdyPnqdXI8SgBaG6lGciq3_pofFhs1HEoFoJ33Jcluha24oMHIvAfwu8qbv_Wq3L2eI9Q0L0p6ul8Pd_BS8adRa2PgLc36xXGcRc7ID5YH-CYaQfsTp5YIaF0Po3h0QyCoQ6ZiYQkqm";
    Algorithm algorithm = Algorithm.ECDSA512(provider);
    algorithm.verify(JWT.decode(jwt));
}
Also used : ECDSAKeyProvider(com.auth0.jwt.interfaces.ECDSAKeyProvider) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 27 with ECDSAKeyProvider

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

the class ECDSAAlgorithmTest method shouldThrowOnSignWhenSignatureAlgorithmDoesNotExists.

@Test
public void shouldThrowOnSignWhenSignatureAlgorithmDoesNotExists() throws Exception {
    exception.expect(SignatureGenerationException.class);
    exception.expectMessage("The Token's Signature couldn't be generated when signing using the Algorithm: some-algorithm");
    exception.expectCause(isA(NoSuchAlgorithmException.class));
    CryptoHelper crypto = mock(CryptoHelper.class);
    when(crypto.createSignatureFor(anyString(), any(PrivateKey.class), any(byte[].class), any(byte[].class))).thenThrow(NoSuchAlgorithmException.class);
    ECPublicKey publicKey = mock(ECPublicKey.class);
    ECPrivateKey privateKey = mock(ECPrivateKey.class);
    ECDSAKeyProvider provider = ECDSAAlgorithm.providerForKeys(publicKey, privateKey);
    Algorithm algorithm = new ECDSAAlgorithm(crypto, "some-alg", "some-algorithm", 32, provider);
    algorithm.sign(ES256HeaderBytes, new byte[0]);
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) ECDSAKeyProvider(com.auth0.jwt.interfaces.ECDSAKeyProvider) ECPrivateKey(java.security.interfaces.ECPrivateKey) ECPublicKey(java.security.interfaces.ECPublicKey) Test(org.junit.Test)

Example 28 with ECDSAKeyProvider

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

the class ECDSAAlgorithmTest 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) Test(org.junit.Test)

Example 29 with ECDSAKeyProvider

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

the class JWTCreatorTest method shouldAddKeyIdIfAvailableFromECDSAKAlgorithms.

@Test
public void shouldAddKeyIdIfAvailableFromECDSAKAlgorithms() throws Exception {
    ECPrivateKey privateKey = (ECPrivateKey) PemUtils.readPrivateKeyFromFile(PRIVATE_KEY_FILE_EC_256K, "EC");
    ECDSAKeyProvider provider = mock(ECDSAKeyProvider.class);
    when(provider.getPrivateKeyId()).thenReturn("my-key-id");
    when(provider.getPrivateKey()).thenReturn(privateKey);
    String signed = JWTCreator.init().sign(Algorithm.ECDSA256K(provider));
    assertThat(signed, is(notNullValue()));
    String[] parts = signed.split("\\.");
    String headerJson = new String(Base64.getUrlDecoder().decode(parts[0]), StandardCharsets.UTF_8);
    assertThat(headerJson, JsonMatcher.hasEntry("kid", "my-key-id"));
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) ECDSAKeyProvider(com.auth0.jwt.interfaces.ECDSAKeyProvider) Test(org.junit.Test)

Example 30 with ECDSAKeyProvider

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

the class JWTCreatorTest method shouldNotOverwriteKeyIdIfAddedFromECDSAKAlgorithms.

@Test
public void shouldNotOverwriteKeyIdIfAddedFromECDSAKAlgorithms() throws Exception {
    ECPrivateKey privateKey = (ECPrivateKey) PemUtils.readPrivateKeyFromFile(PRIVATE_KEY_FILE_EC_256K, "EC");
    ECDSAKeyProvider provider = mock(ECDSAKeyProvider.class);
    when(provider.getPrivateKeyId()).thenReturn("my-key-id");
    when(provider.getPrivateKey()).thenReturn(privateKey);
    String signed = JWTCreator.init().withKeyId("real-key-id").sign(Algorithm.ECDSA256(provider));
    assertThat(signed, is(notNullValue()));
    String[] parts = signed.split("\\.");
    String headerJson = new String(Base64.getUrlDecoder().decode(parts[0]), StandardCharsets.UTF_8);
    assertThat(headerJson, JsonMatcher.hasEntry("kid", "my-key-id"));
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) ECDSAKeyProvider(com.auth0.jwt.interfaces.ECDSAKeyProvider) 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