Search in sources :

Example 31 with AccountTokenDO

use of com.nexblocks.authguard.dal.model.AccountTokenDO in project AuthGuard by AuthGuard.

the class ActionTokenServiceImplTest method verifyTokenExpired.

@Test
void verifyTokenExpired() {
    final AccountTokenDO accountToken = AccountTokenDO.builder().expiresAt(OffsetDateTime.now().minusMinutes(1)).additionalInformation(ImmutableMap.of("action", "something")).build();
    Mockito.when(accountTokensRepository.getByToken("action-token")).thenReturn(CompletableFuture.completedFuture(Optional.of(accountToken)));
    final Try<ActionTokenBO> actual = actionTokenService.verifyToken("action-token", "something");
    assertThat(actual.isFailure());
    assertThat(((ServiceException) actual.getCause()).getErrorCode()).isEqualTo(ErrorCode.EXPIRED_TOKEN.getCode());
}
Also used : ServiceException(com.nexblocks.authguard.service.exceptions.ServiceException) AccountTokenDO(com.nexblocks.authguard.dal.model.AccountTokenDO) ActionTokenBO(com.nexblocks.authguard.service.model.ActionTokenBO) Test(org.junit.jupiter.api.Test)

Example 32 with AccountTokenDO

use of com.nexblocks.authguard.dal.model.AccountTokenDO in project AuthGuard by AuthGuard.

the class ActionTokenServiceImpl method verifyToken.

@Override
public Try<ActionTokenBO> verifyToken(final String token, final String action) {
    final Optional<AccountTokenDO> persisted = accountTokensRepository.getByToken(token).join();
    if (persisted.isEmpty()) {
        return Try.failure(new ServiceException(ErrorCode.TOKEN_EXPIRED_OR_DOES_NOT_EXIST, "Token was not found"));
    }
    final OffsetDateTime now = OffsetDateTime.now();
    if (persisted.get().getExpiresAt().isBefore(now)) {
        return Try.failure(new ServiceException(ErrorCode.EXPIRED_TOKEN, "Token has expired"));
    }
    final String allowedAction = persisted.get().getAdditionalInformation().get("action");
    if (allowedAction == null || !allowedAction.equals(action)) {
        return Try.failure(new ServiceException(ErrorCode.INVALID_TOKEN, "Token was created for a different action"));
    }
    return Try.success(ActionTokenBO.builder().accountId(persisted.get().getAssociatedAccountId()).token(token).action(action).build());
}
Also used : ServiceException(com.nexblocks.authguard.service.exceptions.ServiceException) OffsetDateTime(java.time.OffsetDateTime) AccountTokenDO(com.nexblocks.authguard.dal.model.AccountTokenDO)

Example 33 with AccountTokenDO

use of com.nexblocks.authguard.dal.model.AccountTokenDO in project AuthGuard by AuthGuard.

the class SmsPasswordlessSubscriber method onMessage.

@Override
public void onMessage(final Message message) {
    if (message.getEventType() == EventType.PASSWORDLESS_GENERATED) {
        final PasswordlessMessageBody messageBody = (PasswordlessMessageBody) message.getMessageBody();
        final AccountBO account = messageBody.getAccount();
        final AccountTokenDO accountToken = messageBody.getAccountToken();
        sendEmail(account, accountToken);
    }
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) AccountTokenDO(com.nexblocks.authguard.dal.model.AccountTokenDO) PasswordlessMessageBody(com.nexblocks.authguard.basic.passwordless.PasswordlessMessageBody)

Example 34 with AccountTokenDO

use of com.nexblocks.authguard.dal.model.AccountTokenDO in project AuthGuard by AuthGuard.

the class EmailPasswordlessSubscriber method onMessage.

@Override
public void onMessage(final Message message) {
    if (message.getEventType() == EventType.PASSWORDLESS_GENERATED) {
        final PasswordlessMessageBody messageBody = (PasswordlessMessageBody) message.getMessageBody();
        final AccountBO account = messageBody.getAccount();
        final AccountTokenDO accountToken = messageBody.getAccountToken();
        sendEmail(account, accountToken);
    }
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) AccountTokenDO(com.nexblocks.authguard.dal.model.AccountTokenDO) PasswordlessMessageBody(com.nexblocks.authguard.basic.passwordless.PasswordlessMessageBody)

Example 35 with AccountTokenDO

use of com.nexblocks.authguard.dal.model.AccountTokenDO in project AuthGuard by AuthGuard.

the class EmailPasswordlessSubscriberTest method onWrongMessageType.

@Test
void onWrongMessageType() {
    final AccountTokenDO accountToken = AccountTokenDO.builder().token("token").build();
    final AccountBO account = AccountBO.builder().email(AccountEmailBO.builder().email("user@test.net").build()).build();
    final PasswordlessMessageBody messageBody = new PasswordlessMessageBody(accountToken, account);
    final Message message = Messages.passwordlessGenerated(messageBody).withEventType(EventType.ADMIN);
    emailPasswordlessSubscriber.onMessage(message);
    Mockito.verify(emailProvider, Mockito.never()).send(Mockito.any());
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) Message(com.nexblocks.authguard.emb.model.Message) AccountTokenDO(com.nexblocks.authguard.dal.model.AccountTokenDO) PasswordlessMessageBody(com.nexblocks.authguard.basic.passwordless.PasswordlessMessageBody) Test(org.junit.jupiter.api.Test)

Aggregations

AccountTokenDO (com.nexblocks.authguard.dal.model.AccountTokenDO)36 Test (org.junit.jupiter.api.Test)22 AccountBO (com.nexblocks.authguard.service.model.AccountBO)21 Message (com.nexblocks.authguard.emb.model.Message)9 PasswordlessMessageBody (com.nexblocks.authguard.basic.passwordless.PasswordlessMessageBody)8 ServiceException (com.nexblocks.authguard.service.exceptions.ServiceException)8 AuthResponseBO (com.nexblocks.authguard.service.model.AuthResponseBO)7 AccountTokensRepository (com.nexblocks.authguard.dal.cache.AccountTokensRepository)6 AuthRequestBO (com.nexblocks.authguard.service.model.AuthRequestBO)6 ServiceAuthorizationException (com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException)5 ResetTokenMessage (com.nexblocks.authguard.service.messaging.ResetTokenMessage)5 OffsetDateTime (java.time.OffsetDateTime)5 ImmutableEmail (com.nexblocks.authguard.external.email.ImmutableEmail)4 ImmutableTextMessage (com.nexblocks.authguard.external.sms.ImmutableTextMessage)3 ServiceNotFoundException (com.nexblocks.authguard.service.exceptions.ServiceNotFoundException)3 ActionTokenBO (com.nexblocks.authguard.service.model.ActionTokenBO)3 Optional (java.util.Optional)3 Inject (com.google.inject.Inject)2 AccountsService (com.nexblocks.authguard.service.AccountsService)2 ErrorCode (com.nexblocks.authguard.service.exceptions.codes.ErrorCode)2