Search in sources :

Example 6 with JwtConfig

use of com.nexblocks.authguard.service.config.JwtConfig in project AuthGuard by AuthGuard.

the class JwtSignatureAlgorithmsTest method createVerifier.

private JwtTokenVerifier createVerifier(final String algorithm, final String publicKey, final String privateKey) {
    final JwtConfig jwtConfig = jwtConfig(algorithm, publicKey, privateKey);
    final Algorithm parsedAlgorithm = JwtConfigParser.parseAlgorithm(jwtConfig.getAlgorithm(), jwtConfig.getPublicKey(), jwtConfig.getPrivateKey());
    return new JwtTokenVerifier(strategyConfig(), null, parsedAlgorithm);
}
Also used : JwtConfig(com.nexblocks.authguard.service.config.JwtConfig) Algorithm(com.auth0.jwt.algorithms.Algorithm)

Example 7 with JwtConfig

use of com.nexblocks.authguard.service.config.JwtConfig in project AuthGuard by AuthGuard.

the class JwtTokenVerifierTest method validate.

@Test
void validate() {
    final StrategyConfig strategyConfig = strategyConfig(false);
    final JwtConfig jwtConfig = jwtConfig();
    final JwtTokenVerifier jwtTokenVerifier = newVerifierInstance(strategyConfig);
    final AccountBO account = RANDOM.nextObject(AccountBO.class);
    final AuthResponseBO tokens = generateToken(jwtConfig, account, null);
    final Either<Exception, DecodedJWT> validatedToken = jwtTokenVerifier.verify(tokens.getToken().toString());
    assertThat(validatedToken.isRight()).isTrue();
    verifyToken(validatedToken.get(), account.getId(), null, null, null);
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) JwtConfig(com.nexblocks.authguard.service.config.JwtConfig) StrategyConfig(com.nexblocks.authguard.service.config.StrategyConfig) AuthResponseBO(com.nexblocks.authguard.service.model.AuthResponseBO) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) ServiceAuthorizationException(com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException) Test(org.junit.jupiter.api.Test)

Example 8 with JwtConfig

use of com.nexblocks.authguard.service.config.JwtConfig in project AuthGuard by AuthGuard.

the class JwtTokenVerifierTest method validateWithJti.

@Test
void validateWithJti() {
    final StrategyConfig strategyConfig = strategyConfig(true);
    final JwtConfig jwtConfig = jwtConfig();
    final JwtTokenVerifier jwtTokenVerifier = newVerifierInstance(strategyConfig);
    final String jti = UUID.randomUUID().toString();
    Mockito.when(jtiProvider.next()).thenReturn(jti);
    Mockito.when(jtiProvider.validate(jti)).thenReturn(true);
    final AccountBO account = RANDOM.nextObject(AccountBO.class);
    final AuthResponseBO tokens = generateToken(jwtConfig, account, jti);
    final Either<Exception, DecodedJWT> validatedToken = jwtTokenVerifier.verify(tokens.getToken().toString());
    assertThat(validatedToken.isRight()).isTrue();
    verifyToken(validatedToken.get(), account.getId(), jti, null, null);
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) JwtConfig(com.nexblocks.authguard.service.config.JwtConfig) StrategyConfig(com.nexblocks.authguard.service.config.StrategyConfig) AuthResponseBO(com.nexblocks.authguard.service.model.AuthResponseBO) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) ServiceAuthorizationException(com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException) Test(org.junit.jupiter.api.Test)

Example 9 with JwtConfig

use of com.nexblocks.authguard.service.config.JwtConfig in project AuthGuard by AuthGuard.

the class TokenEncryptorAdapterTest method encryptAndDecryptEllipticCurve.

@Test
void encryptAndDecryptEllipticCurve() {
    final JwtConfig jwtConfig = JwtConfig.builder().encryption(EncryptionConfig.builder().algorithm("EC").publicKey("src/test/resources/ec256-public.pem").privateKey("src/test/resources/ec256-private.pem").build()).build();
    final TokenEncryptorAdapter encryptor = new TokenEncryptorAdapter(jwtConfig);
    final String encrypted = encryptor.encryptAndEncode(TOKEN).get();
    final String decrypted = encryptor.decryptEncoded(encrypted).get();
    assertThat(decrypted).isEqualTo(TOKEN);
}
Also used : JwtConfig(com.nexblocks.authguard.service.config.JwtConfig) Test(org.junit.jupiter.api.Test)

Example 10 with JwtConfig

use of com.nexblocks.authguard.service.config.JwtConfig in project AuthGuard by AuthGuard.

the class TokenEncryptorAdapterTest method initializeWithWrongKeys.

@Test
void initializeWithWrongKeys() {
    final JwtConfig jwtConfig = JwtConfig.builder().encryption(EncryptionConfig.builder().algorithm("EC").publicKey("src/test/resources/hmac256.pem").privateKey("src/test/resources/hmac256.pem").build()).build();
    assertThatThrownBy(() -> new TokenEncryptorAdapter(jwtConfig)).isInstanceOf(ConfigurationException.class);
}
Also used : JwtConfig(com.nexblocks.authguard.service.config.JwtConfig) Test(org.junit.jupiter.api.Test)

Aggregations

JwtConfig (com.nexblocks.authguard.service.config.JwtConfig)13 Test (org.junit.jupiter.api.Test)11 StrategyConfig (com.nexblocks.authguard.service.config.StrategyConfig)5 AccountBO (com.nexblocks.authguard.service.model.AccountBO)5 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)4 ServiceAuthorizationException (com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException)4 AuthResponseBO (com.nexblocks.authguard.service.model.AuthResponseBO)4 Algorithm (com.auth0.jwt.algorithms.Algorithm)3