Search in sources :

Example 11 with AccountBO

use of com.nexblocks.authguard.service.model.AccountBO in project AuthGuard by AuthGuard.

the class OtpProviderTest method generateNumeric.

@Test
void generateNumeric() {
    final OtpConfig otpConfig = OtpConfig.builder().mode(OtpMode.NUMERIC).length(6).lifeTime("5m").build();
    setup(otpConfig);
    final AccountBO account = random.nextObject(AccountBO.class).withActive(true);
    final AuthResponseBO expected = AuthResponseBO.builder().type("otp").entityType(EntityType.ACCOUNT).entityId(account.getId()).build();
    final AuthResponseBO generated = otpProvider.generateToken(account);
    assertThat(generated).isEqualToIgnoringGivenFields(expected, "token");
    assertThat(generated.getToken()).isNotNull();
    final ArgumentCaptor<OneTimePasswordDO> argumentCaptor = ArgumentCaptor.forClass(OneTimePasswordDO.class);
    Mockito.verify(mockOtpRepository).save(argumentCaptor.capture());
    final OneTimePasswordDO persisted = argumentCaptor.getValue();
    assertThat(persisted.getAccountId()).isEqualTo(account.getId());
    assertThat(persisted.getExpiresAt()).isAfter(OffsetDateTime.now()).isBefore(OffsetDateTime.now().plus(Duration.ofMinutes(6)));
    assertThat(persisted.getId()).isNotNull();
    assertThat(persisted.getPassword()).isNotNull();
    assertThat(persisted.getPassword()).hasSize(6);
    for (final char ch : persisted.getPassword().toCharArray()) {
        assertThat(Character.isAlphabetic(ch)).isFalse();
    }
    Mockito.verify(messageBus, Mockito.times(1)).publish(eq("otp"), any());
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) AuthResponseBO(com.nexblocks.authguard.service.model.AuthResponseBO) OtpConfig(com.nexblocks.authguard.basic.config.OtpConfig) OneTimePasswordDO(com.nexblocks.authguard.dal.model.OneTimePasswordDO) Test(org.junit.jupiter.api.Test)

Example 12 with AccountBO

use of com.nexblocks.authguard.service.model.AccountBO in project AuthGuard by AuthGuard.

the class OtpProviderTest method generateAlphabetic.

@Test
void generateAlphabetic() {
    final OtpConfig otpConfig = OtpConfig.builder().mode(OtpMode.ALPHABETIC).length(6).lifeTime("5m").build();
    setup(otpConfig);
    final AccountBO account = random.nextObject(AccountBO.class).withActive(true);
    final AuthResponseBO expected = AuthResponseBO.builder().type("otp").entityType(EntityType.ACCOUNT).entityId(account.getId()).build();
    final AuthResponseBO generated = otpProvider.generateToken(account);
    assertThat(generated).isEqualToIgnoringGivenFields(expected, "token");
    assertThat(generated.getToken()).isNotNull();
    final ArgumentCaptor<OneTimePasswordDO> argumentCaptor = ArgumentCaptor.forClass(OneTimePasswordDO.class);
    Mockito.verify(mockOtpRepository).save(argumentCaptor.capture());
    final OneTimePasswordDO persisted = argumentCaptor.getValue();
    assertThat(persisted.getAccountId()).isEqualTo(account.getId());
    assertThat(persisted.getExpiresAt()).isAfter(OffsetDateTime.now()).isBefore(OffsetDateTime.now().plus(Duration.ofMinutes(6)));
    assertThat(persisted.getId()).isNotNull();
    assertThat(persisted.getPassword()).isNotNull();
    assertThat(persisted.getPassword()).hasSize(6);
    for (final char ch : persisted.getPassword().toCharArray()) {
        assertThat(Character.isDigit(ch)).isFalse();
    }
    Mockito.verify(messageBus, Mockito.times(1)).publish(eq("otp"), any());
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) AuthResponseBO(com.nexblocks.authguard.service.model.AuthResponseBO) OtpConfig(com.nexblocks.authguard.basic.config.OtpConfig) OneTimePasswordDO(com.nexblocks.authguard.dal.model.OneTimePasswordDO) Test(org.junit.jupiter.api.Test)

Example 13 with AccountBO

use of com.nexblocks.authguard.service.model.AccountBO in project AuthGuard by AuthGuard.

the class OtpProviderTest method generateTokenForInactiveAccount.

@Test
void generateTokenForInactiveAccount() {
    final OtpConfig otpConfig = OtpConfig.builder().mode(OtpMode.ALPHANUMERIC).length(6).lifeTime("5m").build();
    setup(otpConfig);
    final AccountBO account = random.nextObject(AccountBO.class).withActive(false);
    assertThatThrownBy(() -> otpProvider.generateToken(account)).isInstanceOf(ServiceAuthorizationException.class);
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) OtpConfig(com.nexblocks.authguard.basic.config.OtpConfig) Test(org.junit.jupiter.api.Test)

Example 14 with AccountBO

use of com.nexblocks.authguard.service.model.AccountBO in project AuthGuard by AuthGuard.

the class ActionTokenServiceImplTest method generateOtp.

@Test
void generateOtp() {
    final AccountBO accountBO = AccountBO.builder().id("account").build();
    final AuthResponseBO otpResponse = AuthResponseBO.builder().token("password-id").build();
    Mockito.when(accountsService.getById("account")).thenReturn(Optional.of(accountBO));
    Mockito.when(otpProvider.generateToken(accountBO)).thenReturn(otpResponse);
    final Try<AuthResponseBO> response = actionTokenService.generateOtp("account");
    assertThat(response.isSuccess());
    assertThat(response.get()).isEqualTo(otpResponse);
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) AuthResponseBO(com.nexblocks.authguard.service.model.AuthResponseBO) Test(org.junit.jupiter.api.Test)

Example 15 with AccountBO

use of com.nexblocks.authguard.service.model.AccountBO in project AuthGuard by AuthGuard.

the class ActionTokenServiceImplTest method generateFromOtp.

@Test
void generateFromOtp() {
    final AccountBO account = AccountBO.builder().id("account").build();
    final String otpToken = "password-id:otp";
    Mockito.when(otpVerifier.verifyAccountToken(otpToken)).thenReturn(Either.right(account.getId()));
    Mockito.when(accountsService.getById("account")).thenReturn(Optional.of(account));
    Mockito.when(accountTokensRepository.save(Mockito.any())).thenReturn(CompletableFuture.completedFuture(null));
    final Try<ActionTokenBO> actual = actionTokenService.generateFromOtp("password-id", "otp", "something");
    final ActionTokenBO expected = ActionTokenBO.builder().accountId(account.getId()).validFor(Duration.ofMinutes(5).toSeconds()).build();
    assertThat(actual.isSuccess());
    assertThat(actual.get()).isEqualToIgnoringGivenFields(expected, "token");
    assertThat(actual.get().getToken()).isNotNull();
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) ActionTokenBO(com.nexblocks.authguard.service.model.ActionTokenBO) Test(org.junit.jupiter.api.Test)

Aggregations

AccountBO (com.nexblocks.authguard.service.model.AccountBO)55 Test (org.junit.jupiter.api.Test)43 AccountTokenDO (com.nexblocks.authguard.dal.model.AccountTokenDO)21 Message (com.nexblocks.authguard.emb.model.Message)15 AuthResponseBO (com.nexblocks.authguard.service.model.AuthResponseBO)15 OtpMessageBody (com.nexblocks.authguard.basic.otp.OtpMessageBody)8 PasswordlessMessageBody (com.nexblocks.authguard.basic.passwordless.PasswordlessMessageBody)8 OneTimePasswordBO (com.nexblocks.authguard.service.model.OneTimePasswordBO)8 ServiceAuthorizationException (com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException)7 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)6 ImmutableTextMessage (com.nexblocks.authguard.external.sms.ImmutableTextMessage)6 RequestContextBO (com.nexblocks.authguard.service.model.RequestContextBO)6 ImmutableEmail (com.nexblocks.authguard.external.email.ImmutableEmail)5 JwtConfig (com.nexblocks.authguard.service.config.JwtConfig)5 StrategyConfig (com.nexblocks.authguard.service.config.StrategyConfig)5 ServiceException (com.nexblocks.authguard.service.exceptions.ServiceException)5 AuthRequestBO (com.nexblocks.authguard.service.model.AuthRequestBO)5 CreateAccountRequestDTO (com.nexblocks.authguard.api.dto.requests.CreateAccountRequestDTO)4 OtpConfig (com.nexblocks.authguard.basic.config.OtpConfig)4 CreateCompleteAccountRequestDTO (com.nexblocks.authguard.api.dto.requests.CreateCompleteAccountRequestDTO)3