Search in sources :

Example 6 with AccountTokenDO

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

the class AuthorizationCodeVerifierTest method expiredToken.

@Test
void expiredToken() {
    final AccountTokensRepository accountTokensRepository = Mockito.mock(AccountTokensRepository.class);
    final AuthorizationCodeVerifier authorizationCodeVerifier = new AuthorizationCodeVerifier(accountTokensRepository);
    final String accountId = "account-id";
    final String authorizationCode = "authorization-code";
    final AccountTokenDO accountToken = AccountTokenDO.builder().expiresAt(OffsetDateTime.now().minus(Duration.ofMinutes(5))).associatedAccountId(accountId).token(authorizationCode).build();
    Mockito.when(accountTokensRepository.getByToken(authorizationCode)).thenReturn(CompletableFuture.completedFuture(Optional.of(accountToken)));
    assertThatThrownBy(() -> authorizationCodeVerifier.verifyAccountToken(authorizationCode)).isInstanceOf(ServiceAuthorizationException.class);
}
Also used : AccountTokensRepository(com.nexblocks.authguard.dal.cache.AccountTokensRepository) AccountTokenDO(com.nexblocks.authguard.dal.model.AccountTokenDO) Test(org.junit.jupiter.api.Test)

Example 7 with AccountTokenDO

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

the class PasswordlessProvider method generateToken.

@Override
public AuthResponseBO generateToken(final AccountBO account) {
    if (!account.isActive()) {
        throw new ServiceAuthorizationException(ErrorCode.ACCOUNT_INACTIVE, "Account was deactivated");
    }
    final String token = randomToken();
    final AccountTokenDO accountToken = AccountTokenDO.builder().id(ID.generate()).associatedAccountId(account.getId()).token(token).expiresAt(OffsetDateTime.now().plus(tokenTtl)).build();
    accountTokensRepository.save(accountToken);
    final PasswordlessMessageBody messageBody = new PasswordlessMessageBody(accountToken, account);
    messageBus.publish(PASSWORDLESS_CHANNEL, Messages.passwordlessGenerated(messageBody));
    return AuthResponseBO.builder().type(TOKEN_TYPE).token(accountToken.getId()).entityType(EntityType.ACCOUNT).entityId(account.getId()).build();
}
Also used : AccountTokenDO(com.nexblocks.authguard.dal.model.AccountTokenDO) ServiceAuthorizationException(com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException)

Example 8 with AccountTokenDO

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

the class ActionTokenServiceImplTest method verifyTokenWrongAction.

@Test
void verifyTokenWrongAction() {
    final AccountTokenDO accountToken = AccountTokenDO.builder().expiresAt(OffsetDateTime.now().plusMinutes(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", "else");
    assertThat(actual.isFailure());
    assertThat(((ServiceException) actual.getCause()).getErrorCode()).isEqualTo(ErrorCode.INVALID_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 9 with AccountTokenDO

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

the class ActionTokenServiceImpl method generateToken.

private AccountTokenDO generateToken(final AccountBO account, final String action) {
    final OffsetDateTime now = OffsetDateTime.now();
    final AccountTokenDO accountToken = AccountTokenDO.builder().id(ID.generate()).token(cryptographicRandom.base64Url(ACTION_TOKEN_SIZE)).associatedAccountId(account.getId()).additionalInformation(ImmutableMap.of("action", action)).expiresAt(now.plus(TOKEN_LIFETIME)).build();
    accountTokensRepository.save(accountToken).join();
    return accountToken;
}
Also used : OffsetDateTime(java.time.OffsetDateTime) AccountTokenDO(com.nexblocks.authguard.dal.model.AccountTokenDO)

Example 10 with AccountTokenDO

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

the class ActionTokenServiceImpl method generateFromBasicAuth.

@Override
public Try<ActionTokenBO> generateFromBasicAuth(final AuthRequestBO authRequest, final String action) {
    final Either<Exception, AccountBO> authResult = basicAuthProvider.getAccount(authRequest);
    if (authResult.isLeft()) {
        return Try.failure(authResult.getLeft());
    }
    final AccountBO account = authResult.get();
    final AccountTokenDO token = generateToken(account, action);
    return Try.success(ActionTokenBO.builder().accountId(account.getId()).token(token.getToken()).validFor(TOKEN_LIFETIME.toSeconds()).build());
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) AccountTokenDO(com.nexblocks.authguard.dal.model.AccountTokenDO) ServiceException(com.nexblocks.authguard.service.exceptions.ServiceException)

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