Search in sources :

Example 11 with AuthResponseBO

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

the class OtpProviderTest method generateToken.

@Test
void generateToken() {
    final OtpConfig otpConfig = OtpConfig.builder().mode(OtpMode.ALPHANUMERIC).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);
    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 AuthResponseBO

use of com.nexblocks.authguard.service.model.AuthResponseBO 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 13 with AuthResponseBO

use of com.nexblocks.authguard.service.model.AuthResponseBO 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 14 with AuthResponseBO

use of com.nexblocks.authguard.service.model.AuthResponseBO 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 AuthResponseBO

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

the class AuthenticationServiceImpl method authenticate.

@Override
public Optional<AuthResponseBO> authenticate(final AuthRequestBO authRequest, final RequestContextBO requestContext) {
    final AuthResponseBO tokens = exchangeService.exchange(authRequest, FROM_TOKEN_TYPE, generateTokenType, requestContext);
    final Collection<AccountLockBO> locks = accountLocksService.getActiveLocksByAccountId(tokens.getEntityId());
    if (locks == null || locks.isEmpty()) {
        return Optional.of(tokens);
    } else {
        throw new ServiceAuthorizationException(ErrorCode.ACCOUNT_IS_LOCKED, "There is an active lock on account " + tokens.getEntityId());
    }
}
Also used : AccountLockBO(com.nexblocks.authguard.service.model.AccountLockBO) AuthResponseBO(com.nexblocks.authguard.service.model.AuthResponseBO) ServiceAuthorizationException(com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException)

Aggregations

AuthResponseBO (com.nexblocks.authguard.service.model.AuthResponseBO)29 Test (org.junit.jupiter.api.Test)24 AccountBO (com.nexblocks.authguard.service.model.AccountBO)15 AuthRequestBO (com.nexblocks.authguard.service.model.AuthRequestBO)11 ServiceAuthorizationException (com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException)10 AccountTokenDO (com.nexblocks.authguard.dal.model.AccountTokenDO)7 RequestContextBO (com.nexblocks.authguard.service.model.RequestContextBO)5 OtpConfig (com.nexblocks.authguard.basic.config.OtpConfig)4 OneTimePasswordDO (com.nexblocks.authguard.dal.model.OneTimePasswordDO)4 JwtConfig (com.nexblocks.authguard.service.config.JwtConfig)4 StrategyConfig (com.nexblocks.authguard.service.config.StrategyConfig)4 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)3 AuthRequestDTO (com.nexblocks.authguard.api.dto.requests.AuthRequestDTO)3 AppBO (com.nexblocks.authguard.service.model.AppBO)2 AuthResponseDTO (com.nexblocks.authguard.api.dto.entities.AuthResponseDTO)1 Error (com.nexblocks.authguard.api.dto.entities.Error)1 PasswordlessRequestDTO (com.nexblocks.authguard.api.dto.requests.PasswordlessRequestDTO)1 AccountTokensRepository (com.nexblocks.authguard.dal.cache.AccountTokensRepository)1 ServiceMapperImpl (com.nexblocks.authguard.service.mappers.ServiceMapperImpl)1 AccountLockBO (com.nexblocks.authguard.service.model.AccountLockBO)1