Search in sources :

Example 6 with AuthMessage

use of com.nexblocks.authguard.service.messaging.AuthMessage in project AuthGuard by AuthGuard.

the class AccountLockerTest method onMessageAuthNotAccount.

@Test
void onMessageAuthNotAccount() {
    // data
    final AuthMessage authMessage = AuthMessage.success("basic", "session", EntityType.APPLICATION, "account");
    final Message<Object> message = Message.builder().eventType(EventType.AUTHENTICATION).bodyType(AuthMessage.class).messageBody(authMessage).timestamp(OffsetDateTime.now()).build();
    // call
    accountLocker.onMessage(message);
    // verify
    Mockito.verifyZeroInteractions(exchangeAttemptsRepository, accountLocksService);
}
Also used : AuthMessage(com.nexblocks.authguard.service.messaging.AuthMessage) Test(org.junit.jupiter.api.Test)

Example 7 with AuthMessage

use of com.nexblocks.authguard.service.messaging.AuthMessage in project AuthGuard by AuthGuard.

the class AccountLockerTest method onMessageNoLock.

@Test
void onMessageNoLock() {
    // 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(Collections.emptyList()));
    // call
    accountLocker.onMessage(message);
    // verify
    final ArgumentCaptor<OffsetDateTime> timeArgumentCaptor = ArgumentCaptor.forClass(OffsetDateTime.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.verifyZeroInteractions(accountLocksService);
}
Also used : OffsetDateTime(java.time.OffsetDateTime) AuthMessage(com.nexblocks.authguard.service.messaging.AuthMessage) Test(org.junit.jupiter.api.Test)

Example 8 with AuthMessage

use of com.nexblocks.authguard.service.messaging.AuthMessage in project AuthGuard by AuthGuard.

the class ExchangeServiceImpl method exchangeFailure.

private void exchangeFailure(final AuthRequestBO authRequest, final RequestContextBO requestContext, final Exception e, final String fromTokenType, final String toTokenType) {
    if (ServiceAuthorizationException.class.isAssignableFrom(e.getClass())) {
        final ServiceAuthorizationException sae = (ServiceAuthorizationException) e;
        final AuthMessage authMessage = AuthMessage.failure(fromTokenType, toTokenType, sae.getEntityType(), sae.getEntityId(), sae);
        if (sae.getEntityType() == EntityType.ACCOUNT) {
            final ExchangeAttemptBO attempt = createBaseAttempt(authRequest, requestContext).exchangeFrom(fromTokenType).exchangeTo(toTokenType).successful(false).entityId(sae.getEntityId()).build();
            exchangeAttemptsService.create(attempt);
        }
        emb.publish(CHANNEL, Messages.auth(authMessage));
    } else {
        final AuthMessage authMessage = AuthMessage.failure(fromTokenType, toTokenType, e);
        emb.publish(CHANNEL, Messages.auth(authMessage));
    }
}
Also used : ServiceAuthorizationException(com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException) AuthMessage(com.nexblocks.authguard.service.messaging.AuthMessage)

Aggregations

AuthMessage (com.nexblocks.authguard.service.messaging.AuthMessage)8 Test (org.junit.jupiter.api.Test)5 OffsetDateTime (java.time.OffsetDateTime)3 ImmutableAccountLockerConfig (com.nexblocks.authguard.extensions.config.ImmutableAccountLockerConfig)2 AccountLockBO (com.nexblocks.authguard.service.model.AccountLockBO)2 Inject (com.google.inject.Inject)1 Named (com.google.inject.name.Named)1 ConfigContext (com.nexblocks.authguard.config.ConfigContext)1 ExchangeAttemptsRepository (com.nexblocks.authguard.dal.persistence.ExchangeAttemptsRepository)1 MessageSubscriber (com.nexblocks.authguard.emb.MessageSubscriber)1 Channel (com.nexblocks.authguard.emb.annotations.Channel)1 EventType (com.nexblocks.authguard.emb.model.EventType)1 Message (com.nexblocks.authguard.emb.model.Message)1 AccountLocksService (com.nexblocks.authguard.service.AccountLocksService)1 ServiceAuthorizationException (com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException)1 EntityType (com.nexblocks.authguard.service.model.EntityType)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1