Search in sources :

Example 1 with ActionTokenBO

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();
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) ActionTokenBO(com.nexblocks.authguard.service.model.ActionTokenBO) Test(org.junit.jupiter.api.Test)

Example 2 with ActionTokenBO

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());
}
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 3 with ActionTokenBO

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()));
}
Also used : ServiceException(com.nexblocks.authguard.service.exceptions.ServiceException) ActionTokenRequestDTO(com.nexblocks.authguard.api.dto.requests.ActionTokenRequestDTO) ActionTokenBO(com.nexblocks.authguard.service.model.ActionTokenBO) AuthRequestBO(com.nexblocks.authguard.service.model.AuthRequestBO)

Example 4 with ActionTokenBO

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());
}
Also used : AccountTokenDO(com.nexblocks.authguard.dal.model.AccountTokenDO) ActionTokenBO(com.nexblocks.authguard.service.model.ActionTokenBO) Test(org.junit.jupiter.api.Test)

Example 5 with ActionTokenBO

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

Aggregations

ActionTokenBO (com.nexblocks.authguard.service.model.ActionTokenBO)6 Test (org.junit.jupiter.api.Test)5 AccountTokenDO (com.nexblocks.authguard.dal.model.AccountTokenDO)3 ServiceException (com.nexblocks.authguard.service.exceptions.ServiceException)3 AccountBO (com.nexblocks.authguard.service.model.AccountBO)2 AuthRequestBO (com.nexblocks.authguard.service.model.AuthRequestBO)2 ActionTokenRequestDTO (com.nexblocks.authguard.api.dto.requests.ActionTokenRequestDTO)1