use of com.nexblocks.authguard.service.model.AccountLockBO 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());
}
}
use of com.nexblocks.authguard.service.model.AccountLockBO in project AuthGuard by AuthGuard.
the class AccountLocker method processAuthMessage.
private void processAuthMessage(final AuthMessage authMessage) {
if (authMessage.getEntityType() == EntityType.ACCOUNT) {
final OffsetDateTime now = OffsetDateTime.now();
final OffsetDateTime from = now.minusMinutes(config.getCheckPeriod());
exchangeAttemptsRepository.findByEntityAndTimestamp(authMessage.getEntityId(), from).thenAccept(attempts -> {
final long failedCount = attempts.stream().filter(attempt -> !attempt.isSuccessful()).count();
if (failedCount >= config.getMaxAttempts()) {
LOG.info("Account {} had {} failed attempts in the past {} minutes; a lock will be placed", authMessage.getEntityId(), attempts.size(), config.getCheckPeriod());
final AccountLockBO lock = AccountLockBO.builder().accountId(authMessage.getEntityId()).expiresAt(now.plusMinutes(config.getLockPeriod())).build();
accountLocksService.create(lock);
}
});
} else {
LOG.info("Skipping entity auth message for entity of type {}", authMessage.getEntityType());
}
}
use of com.nexblocks.authguard.service.model.AccountLockBO in project AuthGuard by AuthGuard.
the class AccountLockerTest method onMessageLock.
@Test
void onMessageLock() {
// data
final AuthMessage authMessage = AuthMessage.success("basic", "session", EntityType.ACCOUNT, "account");
final Message<Object> message = Message.builder().eventType(EventType.AUTHENTICATION).bodyType(AuthMessage.class).messageBody(authMessage).timestamp(OffsetDateTime.now()).build();
// mocks
Mockito.when(exchangeAttemptsRepository.findByEntityAndTimestamp(Mockito.any(), Mockito.any())).thenReturn(CompletableFuture.completedFuture(Arrays.asList(ExchangeAttemptDO.builder().build(), ExchangeAttemptDO.builder().build(), ExchangeAttemptDO.builder().build())));
// call
accountLocker.onMessage(message);
// verify
final ArgumentCaptor<OffsetDateTime> timeArgumentCaptor = ArgumentCaptor.forClass(OffsetDateTime.class);
final ArgumentCaptor<AccountLockBO> accountLockArgumentCaptor = ArgumentCaptor.forClass(AccountLockBO.class);
Mockito.verify(exchangeAttemptsRepository).findByEntityAndTimestamp(Mockito.eq("account"), timeArgumentCaptor.capture());
assertThat(timeArgumentCaptor.getValue()).isBetween(OffsetDateTime.now().minusMinutes(config.getCheckPeriod()).minusMinutes(1), OffsetDateTime.now().minusMinutes(config.getCheckPeriod()).plusMinutes(1));
Mockito.verify(accountLocksService).create(accountLockArgumentCaptor.capture());
assertThat(accountLockArgumentCaptor.getValue().getAccountId()).isEqualTo(authMessage.getEntityId());
assertThat(accountLockArgumentCaptor.getValue().getExpiresAt()).isBetween(OffsetDateTime.now().plusMinutes(config.getLockPeriod()).minusMinutes(1), OffsetDateTime.now().plusMinutes(config.getLockPeriod()).plusMinutes(1));
}
use of com.nexblocks.authguard.service.model.AccountLockBO in project AuthGuard by AuthGuard.
the class AccountLocksServiceImplTest method getActiveLocksByAccountId.
@Test
void getActiveLocksByAccountId() {
final OffsetDateTime now = OffsetDateTime.now();
Mockito.when(repository.findByAccountId("account")).thenReturn(CompletableFuture.completedFuture(Arrays.asList(AccountLockDO.builder().accountId("account").expiresAt(now.plusMinutes(5)).build(), AccountLockDO.builder().accountId("account").expiresAt(now.minusMinutes(1)).build())));
final Collection<AccountLockBO> actual = service.getActiveLocksByAccountId("account");
final Collection<AccountLockBO> expected = Collections.singletonList(AccountLockBO.builder().accountId("account").expiresAt(now.plusMinutes(5)).build());
assertThat(actual).isEqualTo(expected);
}
use of com.nexblocks.authguard.service.model.AccountLockBO in project AuthGuard by AuthGuard.
the class AccountLocksServiceImplTest method delete.
@Test
void delete() {
final OffsetDateTime now = OffsetDateTime.now();
Mockito.when(repository.delete("lock")).thenReturn(CompletableFuture.completedFuture(Optional.of(AccountLockDO.builder().accountId("account").expiresAt(now.plusMinutes(5)).build())));
final Optional<AccountLockBO> actual = service.delete("lock");
final AccountLockBO expected = AccountLockBO.builder().accountId("account").expiresAt(now.plusMinutes(5)).build();
assertThat(actual).contains(expected);
}
Aggregations