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);
}
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();
}
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());
}
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;
}
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());
}
Aggregations