use of com.nexblocks.authguard.service.model.AuthRequestBO in project AuthGuard by AuthGuard.
the class OtpServiceImplTest method authenticate.
@Test
void authenticate() {
final OtpConfig otpConfig = OtpConfig.builder().generateToken("accessToken").build();
setup(otpConfig);
final OneTimePasswordDO otp = random.nextObject(OneTimePasswordDO.class);
final AuthResponseBO tokens = random.nextObject(AuthResponseBO.class);
final String otpToken = otp.getId() + ":" + otp.getPassword();
final AuthRequestBO authRequest = AuthRequestBO.builder().token(otpToken).build();
final RequestContextBO requestContext = RequestContextBO.builder().build();
Mockito.when(mockExchangeService.exchange(authRequest, "otp", otpConfig.getGenerateToken(), requestContext)).thenReturn(tokens);
final AuthResponseBO generated = otpService.authenticate(otp.getId(), otp.getPassword(), requestContext);
assertThat(generated).isEqualTo(tokens);
}
use of com.nexblocks.authguard.service.model.AuthRequestBO in project AuthGuard by AuthGuard.
the class AuthRouteTest method authenticateUnsuccessful.
@Test
void authenticateUnsuccessful() {
final AuthRequestDTO requestDTO = randomObject(AuthRequestDTO.class);
final AuthRequestBO requestBO = restMapper.toBO(requestDTO);
final RequestContextBO requestContext = RequestContextBO.builder().build();
Mockito.when(authenticationService.authenticate(requestBO, requestContext)).thenReturn(Optional.empty());
given().body(requestDTO).post(url("authenticate")).then().statusCode(400);
}
use of com.nexblocks.authguard.service.model.AuthRequestBO 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.AuthRequestBO in project AuthGuard by AuthGuard.
the class RefreshToAccessTokenTest method exchangeWithRestrictions.
@Test
void exchangeWithRestrictions() {
// data
final String accountId = "account";
final String refreshToken = "refresh_token";
final String restrictionPermission = "permission.read";
final AuthRequestBO authRequest = AuthRequestBO.builder().token(refreshToken).build();
final AccountTokenDO accountToken = AccountTokenDO.builder().token(refreshToken).associatedAccountId(accountId).expiresAt(OffsetDateTime.now().plusMinutes(1)).tokenRestrictions(TokenRestrictionsDO.builder().permissions(Collections.singleton(restrictionPermission)).scopes(Collections.emptySet()).build()).build();
final AccountBO account = AccountBO.builder().id(accountId).build();
final AuthResponseBO newTokens = AuthResponseBO.builder().token("new_token").refreshToken("new_refresh_token").build();
// mock
Mockito.when(accountTokensRepository.getByToken(authRequest.getToken())).thenReturn(CompletableFuture.completedFuture(Optional.of(accountToken)));
Mockito.when(accountsService.getById(accountId)).thenReturn(Optional.of(account));
Mockito.when(accessTokenProvider.generateToken(account, TokenRestrictionsBO.builder().addPermissions(restrictionPermission).build())).thenReturn(newTokens);
// do
final Either<Exception, AuthResponseBO> actual = refreshToAccessToken.exchange(authRequest);
// assert
assertThat(actual.isRight()).isTrue();
assertThat(actual.right().get()).isEqualTo(newTokens);
Mockito.verify(accountTokensRepository).deleteToken(refreshToken);
}
use of com.nexblocks.authguard.service.model.AuthRequestBO in project AuthGuard by AuthGuard.
the class RefreshToAccessTokenTest method exchangeNoAccount.
@Test
void exchangeNoAccount() {
// data
final String accountId = "account";
final String refreshToken = "refresh_token";
final AuthRequestBO authRequest = AuthRequestBO.builder().token(refreshToken).build();
final AccountTokenDO accountToken = AccountTokenDO.builder().token(refreshToken).associatedAccountId(accountId).expiresAt(OffsetDateTime.now().plusMinutes(1)).build();
// mock
Mockito.when(accountTokensRepository.getByToken(authRequest.getToken())).thenReturn(CompletableFuture.completedFuture(Optional.of(accountToken)));
Mockito.when(accountsService.getById(accountId)).thenReturn(Optional.empty());
// do
final Either<Exception, AuthResponseBO> actual = refreshToAccessToken.exchange(authRequest);
// assert
assertThat(actual.isLeft()).isTrue();
assertThat(actual.left().get()).isInstanceOf(ServiceAuthorizationException.class);
Mockito.verify(accountTokensRepository).deleteToken(refreshToken);
}
Aggregations