Search in sources :

Example 6 with OtpConfig

use of com.nexblocks.authguard.basic.config.OtpConfig in project AuthGuard by AuthGuard.

the class OtpVerifierTest method verifyInvalidOtpFormat.

@Test
void verifyInvalidOtpFormat() {
    final OtpConfig otpConfig = OtpConfig.builder().mode(OtpMode.ALPHANUMERIC).length(6).lifeTime("5m").build();
    setup(otpConfig);
    final Either<Exception, String> result = otpVerifier.verifyAccountToken("not a valid OTP");
    assertThat(result.isLeft()).isTrue();
    assertThat(result.getLeft()).isInstanceOf(ServiceAuthorizationException.class);
}
Also used : OtpConfig(com.nexblocks.authguard.basic.config.OtpConfig) ServiceAuthorizationException(com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException) Test(org.junit.jupiter.api.Test)

Example 7 with OtpConfig

use of com.nexblocks.authguard.basic.config.OtpConfig in project AuthGuard by AuthGuard.

the class OtpVerifierTest method verifyWrongPassword.

@Test
void verifyWrongPassword() {
    final OtpConfig otpConfig = OtpConfig.builder().mode(OtpMode.ALPHANUMERIC).length(6).lifeTime("5m").build();
    setup(otpConfig);
    final OneTimePasswordDO otp = random.nextObject(OneTimePasswordDO.class);
    Mockito.when(mockOtpRepository.getById(otp.getId())).thenReturn(CompletableFuture.completedFuture(Optional.of(otp)));
    assertThat(otpVerifier.verifyAccountToken(otp.getId() + ":" + "wrong")).isEmpty();
}
Also used : OtpConfig(com.nexblocks.authguard.basic.config.OtpConfig) OneTimePasswordDO(com.nexblocks.authguard.dal.model.OneTimePasswordDO) Test(org.junit.jupiter.api.Test)

Example 8 with OtpConfig

use of com.nexblocks.authguard.basic.config.OtpConfig in project AuthGuard by AuthGuard.

the class OtpServiceImplTest method authenticate.

@Test
void authenticate() {
    final OtpConfig otpConfig = OtpConfig.builder().generateToken("accessToken").build();
    setup(otpConfig);
    final OneTimePasswordDO otp = random.nextObject(OneTimePasswordDO.class);
    final AuthResponseBO tokens = random.nextObject(AuthResponseBO.class);
    final String otpToken = otp.getId() + ":" + otp.getPassword();
    final AuthRequestBO authRequest = AuthRequestBO.builder().token(otpToken).build();
    final RequestContextBO requestContext = RequestContextBO.builder().build();
    Mockito.when(mockExchangeService.exchange(authRequest, "otp", otpConfig.getGenerateToken(), requestContext)).thenReturn(tokens);
    final AuthResponseBO generated = otpService.authenticate(otp.getId(), otp.getPassword(), requestContext);
    assertThat(generated).isEqualTo(tokens);
}
Also used : RequestContextBO(com.nexblocks.authguard.service.model.RequestContextBO) AuthResponseBO(com.nexblocks.authguard.service.model.AuthResponseBO) OtpConfig(com.nexblocks.authguard.basic.config.OtpConfig) OneTimePasswordDO(com.nexblocks.authguard.dal.model.OneTimePasswordDO) AuthRequestBO(com.nexblocks.authguard.service.model.AuthRequestBO) Test(org.junit.jupiter.api.Test)

Example 9 with OtpConfig

use of com.nexblocks.authguard.basic.config.OtpConfig in project AuthGuard by AuthGuard.

the class OtpVerifierTest method verify.

@Test
void verify() {
    final OtpConfig otpConfig = OtpConfig.builder().mode(OtpMode.ALPHANUMERIC).length(6).lifeTime("5m").build();
    setup(otpConfig);
    final OneTimePasswordDO otp = random.nextObject(OneTimePasswordDO.class);
    Mockito.when(mockOtpRepository.getById(otp.getId())).thenReturn(CompletableFuture.completedFuture(Optional.of(otp)));
    final Either<Exception, String> generated = otpVerifier.verifyAccountToken(otp.getId() + ":" + otp.getPassword());
    assertThat(generated.get()).isEqualTo(otp.getAccountId());
}
Also used : OtpConfig(com.nexblocks.authguard.basic.config.OtpConfig) OneTimePasswordDO(com.nexblocks.authguard.dal.model.OneTimePasswordDO) ServiceAuthorizationException(com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException) Test(org.junit.jupiter.api.Test)

Aggregations

OtpConfig (com.nexblocks.authguard.basic.config.OtpConfig)9 Test (org.junit.jupiter.api.Test)9 OneTimePasswordDO (com.nexblocks.authguard.dal.model.OneTimePasswordDO)7 AccountBO (com.nexblocks.authguard.service.model.AccountBO)4 AuthResponseBO (com.nexblocks.authguard.service.model.AuthResponseBO)4 ServiceAuthorizationException (com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException)3 AuthRequestBO (com.nexblocks.authguard.service.model.AuthRequestBO)1 RequestContextBO (com.nexblocks.authguard.service.model.RequestContextBO)1