use of com.nexblocks.authguard.service.model.ActionTokenBO in project AuthGuard by AuthGuard.
the class ActionTokenServiceImplTest method generateFromOtp.
@Test
void generateFromOtp() {
final AccountBO account = AccountBO.builder().id("account").build();
final String otpToken = "password-id:otp";
Mockito.when(otpVerifier.verifyAccountToken(otpToken)).thenReturn(Either.right(account.getId()));
Mockito.when(accountsService.getById("account")).thenReturn(Optional.of(account));
Mockito.when(accountTokensRepository.save(Mockito.any())).thenReturn(CompletableFuture.completedFuture(null));
final Try<ActionTokenBO> actual = actionTokenService.generateFromOtp("password-id", "otp", "something");
final ActionTokenBO expected = ActionTokenBO.builder().accountId(account.getId()).validFor(Duration.ofMinutes(5).toSeconds()).build();
assertThat(actual.isSuccess());
assertThat(actual.get()).isEqualToIgnoringGivenFields(expected, "token");
assertThat(actual.get().getToken()).isNotNull();
}
use of com.nexblocks.authguard.service.model.ActionTokenBO 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.service.model.ActionTokenBO in project AuthGuard by AuthGuard.
the class ActionTokensRoute method createToken.
@Override
public void createToken(final Context context) {
final ActionTokenRequestDTO request = actionTokenRequestBodyHandler.getValidated(context);
final Try<ActionTokenBO> result;
if (request.getType() == ActionTokenRequestType.OTP) {
result = actionTokenService.generateFromOtp(request.getOtp().getPasswordId(), request.getOtp().getPassword(), request.getAction());
} else {
final AuthRequestBO authRequest = restMapper.toBO(request.getBasic());
result = actionTokenService.generateFromBasicAuth(authRequest, request.getAction());
}
if (result.isFailure()) {
throw (ServiceException) result.getCause();
}
context.status(201).json(restMapper.toDTO(result.get()));
}
use of com.nexblocks.authguard.service.model.ActionTokenBO in project AuthGuard by AuthGuard.
the class ActionTokenServiceImplTest method verifyToken.
@Test
void verifyToken() {
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", "something");
assertThat(actual.isSuccess());
}
use of com.nexblocks.authguard.service.model.ActionTokenBO 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());
}
Aggregations