use of com.nexblocks.authguard.service.config.StrategyConfig in project AuthGuard by AuthGuard.
the class JwtTokenVerifierTest method validateWithAlgNone.
@Test
void validateWithAlgNone() {
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 String payload = tokens.getToken().toString().split("\\.")[1];
final String maliciousToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9." + payload + ".signature";
assertThat(jwtTokenVerifier.verify(maliciousToken)).isEmpty();
}
use of com.nexblocks.authguard.service.config.StrategyConfig in project AuthGuard by AuthGuard.
the class JwtTokenVerifierTest method validateWithJtiBlacklisted.
@Test
void validateWithJtiBlacklisted() {
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(false);
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.isLeft());
}
use of com.nexblocks.authguard.service.config.StrategyConfig in project AuthGuard by AuthGuard.
the class JwtTokenVerifierTest method validateExpired.
@Test
void validateExpired() {
final StrategyConfig strategyConfig = strategyConfig(false);
final JwtConfig jwtConfig = jwtConfig();
final AccountBO account = RANDOM.nextObject(AccountBO.class);
final Algorithm algorithm = JwtConfigParser.parseAlgorithm(jwtConfig.getAlgorithm(), jwtConfig.getPublicKey(), jwtConfig.getPrivateKey());
final JwtGenerator jwtGenerator = new JwtGenerator(jwtConfig);
final String token = jwtGenerator.generateUnsignedToken(account, Duration.ofMinutes(5)).withExpiresAt(Date.from(Instant.now().minusSeconds(60))).sign(algorithm);
final JwtTokenVerifier jwtTokenVerifier = newVerifierInstance(strategyConfig);
final Either<Exception, DecodedJWT> validatedToken = jwtTokenVerifier.verify(token);
assertThat(validatedToken.isLeft()).isTrue();
assertThat(validatedToken.getLeft()).isInstanceOf(ServiceAuthorizationException.class);
}
use of com.nexblocks.authguard.service.config.StrategyConfig 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);
}
use of com.nexblocks.authguard.service.config.StrategyConfig 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);
}
Aggregations