Search in sources :

Example 11 with Token

use of com.auth0.json.mgmt.Token in project sda-dropwizard-commons by SDA-SE.

the class AuthRSA256ServiceTest method validTokenWithKeyIdAndNoIssuerButConfiguredRequiredIssuer.

@Test
void validTokenWithKeyIdAndNoIssuerButConfiguredRequiredIssuer() {
    final Pair<RSAPrivateKey, RSAPublicKey> keyPair = createKeyPair(RSA_PRIVATE_KEY);
    String token = createToken(keyPair, null, KEY_ID, 0, 30);
    keyLoader.addKeySource(new JwksTestKeySource(ISSUER, keyPair.getRight(), null, KEY_ID));
    final Map<String, Claim> claims = this.service.auth(token);
    assertThat(claims.get(CLAIM_ISSUER)).isNull();
    assertThat(claims.get(CLAIM_NOT_BEFORE).asLong() * 1000L).isLessThan(new Date().getTime());
    assertThat(claims.get(CLAIM_EXPIRE).asLong() * 1000L).isGreaterThan(new Date().getTime());
}
Also used : JwksTestKeySource(org.sdase.commons.server.auth.service.testsources.JwksTestKeySource) RSAPublicKey(java.security.interfaces.RSAPublicKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) Claim(com.auth0.jwt.interfaces.Claim) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 12 with Token

use of com.auth0.json.mgmt.Token in project sda-dropwizard-commons by SDA-SE.

the class AuthRSA256ServiceTest method validTokenWithKeyIdAndIssuerAndAndFutureNotBeforeFailed.

@Test
void validTokenWithKeyIdAndIssuerAndAndFutureNotBeforeFailed() throws InterruptedException {
    final Pair<RSAPrivateKey, RSAPublicKey> keyPair = createKeyPair(RSA_PRIVATE_KEY);
    String token = createToken(keyPair, ISSUER, KEY_ID, 2, 30);
    keyLoader.addKeySource(new JwksTestKeySource(ISSUER, keyPair.getRight(), ISSUER, KEY_ID));
    assertThatThrownBy(() -> this.service.auth(token)).isInstanceOf(JwtAuthException.class);
    TimeUnit.SECONDS.sleep(2);
    final Map<String, Claim> claims = this.service.auth(token);
    assertThat(claims.get(CLAIM_ISSUER).asString()).isEqualTo(ISSUER);
    assertThat(claims.get(CLAIM_NOT_BEFORE).asLong() * 1000L).isLessThan(new Date().getTime());
    assertThat(claims.get(CLAIM_EXPIRE).asLong() * 1000L).isGreaterThan(new Date().getTime());
}
Also used : JwksTestKeySource(org.sdase.commons.server.auth.service.testsources.JwksTestKeySource) RSAPublicKey(java.security.interfaces.RSAPublicKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) Claim(com.auth0.jwt.interfaces.Claim) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 13 with Token

use of com.auth0.json.mgmt.Token in project sda-dropwizard-commons by SDA-SE.

the class AuthRSA256ServiceTest method validTokenWithKeyIdAndNoIssuerAndRequiredIssuerButJwks.

@Test
void validTokenWithKeyIdAndNoIssuerAndRequiredIssuerButJwks() {
    final Pair<RSAPrivateKey, RSAPublicKey> keyPair = createKeyPair(RSA_PRIVATE_KEY);
    String token = createToken(keyPair, ISSUER, KEY_ID, 0, 30);
    keyLoader.addKeySource(new JwksTestKeySource(null, keyPair.getRight(), ISSUER, KEY_ID));
    final Map<String, Claim> claims = this.service.auth(token);
    assertThat(claims.get(CLAIM_ISSUER).asString()).isEqualTo(ISSUER);
    assertThat(claims.get(CLAIM_NOT_BEFORE).asLong() * 1000L).isLessThan(new Date().getTime());
    assertThat(claims.get(CLAIM_EXPIRE).asLong() * 1000L).isGreaterThan(new Date().getTime());
}
Also used : JwksTestKeySource(org.sdase.commons.server.auth.service.testsources.JwksTestKeySource) RSAPublicKey(java.security.interfaces.RSAPublicKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) Claim(com.auth0.jwt.interfaces.Claim) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 14 with Token

use of com.auth0.json.mgmt.Token in project sda-dropwizard-commons by SDA-SE.

the class AuthRSA256ServiceTest method validTokenWithKeyIdAndIssuerAndConfiguredRequiredIssuer.

@Test
void validTokenWithKeyIdAndIssuerAndConfiguredRequiredIssuer() {
    final Pair<RSAPrivateKey, RSAPublicKey> keyPair = createKeyPair(RSA_PRIVATE_KEY);
    String token = createToken(keyPair, ISSUER, KEY_ID, 0, 30);
    keyLoader.addKeySource(new JwksTestKeySource(ISSUER, keyPair.getRight(), ISSUER, KEY_ID));
    final Map<String, Claim> claims = this.service.auth(token);
    assertThat(claims.get(CLAIM_ISSUER).asString()).isEqualTo(ISSUER);
    assertThat(claims.get(CLAIM_NOT_BEFORE).asLong() * 1000L).isLessThan(new Date().getTime());
    assertThat(claims.get(CLAIM_EXPIRE).asLong() * 1000L).isGreaterThan(new Date().getTime());
}
Also used : JwksTestKeySource(org.sdase.commons.server.auth.service.testsources.JwksTestKeySource) RSAPublicKey(java.security.interfaces.RSAPublicKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) Claim(com.auth0.jwt.interfaces.Claim) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 15 with Token

use of com.auth0.json.mgmt.Token in project sda-dropwizard-commons by SDA-SE.

the class AuthRSA256ServiceTest method validTokenWithKeyIdAndIssuerAndWillExpire.

@Test
void validTokenWithKeyIdAndIssuerAndWillExpire() throws InterruptedException {
    final Pair<RSAPrivateKey, RSAPublicKey> keyPair = createKeyPair(RSA_PRIVATE_KEY);
    String token = createToken(keyPair, ISSUER, KEY_ID, 0, 2);
    keyLoader.addKeySource(new JwksTestKeySource(ISSUER, keyPair.getRight(), ISSUER, KEY_ID));
    final Map<String, Claim> claims = this.service.auth(token);
    assertThat(claims.get(CLAIM_ISSUER).asString()).isEqualTo(ISSUER);
    assertThat(claims.get(CLAIM_NOT_BEFORE).asLong() * 1000L).isLessThan(new Date().getTime());
    assertThat(claims.get(CLAIM_EXPIRE).asLong() * 1000L).isGreaterThan(new Date().getTime());
    TimeUnit.SECONDS.sleep(3);
    assertThatThrownBy(() -> this.service.auth(token)).isInstanceOf(JwtAuthException.class);
}
Also used : JwksTestKeySource(org.sdase.commons.server.auth.service.testsources.JwksTestKeySource) RSAPublicKey(java.security.interfaces.RSAPublicKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) Claim(com.auth0.jwt.interfaces.Claim) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Aggregations

DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)276 Algorithm (com.auth0.jwt.algorithms.Algorithm)147 Test (org.junit.Test)120 JWTVerifier (com.auth0.jwt.JWTVerifier)97 Date (java.util.Date)78 JWTVerificationException (com.auth0.jwt.exceptions.JWTVerificationException)62 IOException (java.io.IOException)59 Claim (com.auth0.jwt.interfaces.Claim)49 HashMap (java.util.HashMap)40 VoidRequest (com.auth0.net.VoidRequest)31 RSAPublicKey (java.security.interfaces.RSAPublicKey)31 Test (org.junit.jupiter.api.Test)30 JWTDecodeException (com.auth0.jwt.exceptions.JWTDecodeException)28 JWTCreator (com.auth0.jwt.JWTCreator)21 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)21 JWT (com.auth0.jwt.JWT)20 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)19 UnsupportedEncodingException (java.io.UnsupportedEncodingException)18 Instant (java.time.Instant)18 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)17