Search in sources :

Example 6 with AuthResponseBO

use of com.nexblocks.authguard.service.model.AuthResponseBO in project AuthGuard by AuthGuard.

the class RefreshToAccessTokenTest method exchangeInvalidToken.

@Test
void exchangeInvalidToken() {
    // data
    final String refreshToken = "refresh_token";
    final AuthRequestBO authRequest = AuthRequestBO.builder().token(refreshToken).build();
    // mock
    Mockito.when(accountTokensRepository.getByToken(authRequest.getToken())).thenReturn(CompletableFuture.completedFuture(Optional.empty()));
    // do
    final Either<Exception, AuthResponseBO> actual = refreshToAccessToken.exchange(authRequest);
    // assert
    assertThat(actual.isLeft()).isTrue();
    assertThat(actual.left().get()).isInstanceOf(ServiceAuthorizationException.class);
}
Also used : AuthResponseBO(com.nexblocks.authguard.service.model.AuthResponseBO) AuthRequestBO(com.nexblocks.authguard.service.model.AuthRequestBO) ServiceAuthorizationException(com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException) Test(org.junit.jupiter.api.Test)

Example 7 with AuthResponseBO

use of com.nexblocks.authguard.service.model.AuthResponseBO in project AuthGuard by AuthGuard.

the class RefreshToAccessTokenTest method exchange.

@Test
void exchange() {
    // 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();
    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) null)).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);
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) AccountTokenDO(com.nexblocks.authguard.dal.model.AccountTokenDO) AuthResponseBO(com.nexblocks.authguard.service.model.AuthResponseBO) AuthRequestBO(com.nexblocks.authguard.service.model.AuthRequestBO) ServiceAuthorizationException(com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException) Test(org.junit.jupiter.api.Test)

Example 8 with AuthResponseBO

use of com.nexblocks.authguard.service.model.AuthResponseBO in project AuthGuard by AuthGuard.

the class PasswordlessRoute method verify.

public void verify(final Context context) {
    final PasswordlessRequestDTO request = passwordlessRequestBodyHandler.getValidated(context);
    final RequestContextBO requestContext = RequestContextExtractor.extractWithoutIdempotentKey(context);
    final AuthResponseBO generatedTokens = passwordlessService.authenticate(request.getToken(), requestContext);
    context.json(generatedTokens);
}
Also used : RequestContextBO(com.nexblocks.authguard.service.model.RequestContextBO) PasswordlessRequestDTO(com.nexblocks.authguard.api.dto.requests.PasswordlessRequestDTO) AuthResponseBO(com.nexblocks.authguard.service.model.AuthResponseBO)

Example 9 with AuthResponseBO

use of com.nexblocks.authguard.service.model.AuthResponseBO in project AuthGuard by AuthGuard.

the class IdTokenProviderTest method generate.

@Test
void generate() {
    final IdTokenProvider idTokenProvider = newProviderInstance(jwtConfig());
    final AccountBO account = RANDOM.nextObject(AccountBO.class).withActive(true);
    final AuthResponseBO tokens = idTokenProvider.generateToken(account);
    assertThat(tokens).isNotNull();
    assertThat(tokens.getToken()).isNotNull();
    assertThat(tokens.getRefreshToken()).isNotNull();
    assertThat(tokens.getToken()).isNotEqualTo(tokens.getRefreshToken());
    verifyToken(tokens.getToken().toString(), account.getId(), null, null, null);
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) AuthResponseBO(com.nexblocks.authguard.service.model.AuthResponseBO) Test(org.junit.jupiter.api.Test)

Example 10 with AuthResponseBO

use of com.nexblocks.authguard.service.model.AuthResponseBO in project AuthGuard by AuthGuard.

the class JwtApiKeyProviderTest method generateTokenApp.

@Test
void generateTokenApp() {
    final JtiProvider jtiProvider = Mockito.mock(JtiProvider.class);
    final JwtApiKeyProvider tokenProvider = newProviderInstance(jtiProvider);
    final String jti = "tokenId";
    final AppBO app = RANDOM.nextObject(AppBO.class);
    Mockito.when(jtiProvider.next()).thenReturn(jti);
    final AuthResponseBO tokens = tokenProvider.generateToken(app);
    assertThat(tokens).isNotNull();
    assertThat(tokens.getToken()).isNotNull();
    assertThat(tokens.getRefreshToken()).isNull();
    verifyToken(tokens.getToken().toString(), app.getId(), jti);
}
Also used : AppBO(com.nexblocks.authguard.service.model.AppBO) AuthResponseBO(com.nexblocks.authguard.service.model.AuthResponseBO) Test(org.junit.jupiter.api.Test)

Aggregations

AuthResponseBO (com.nexblocks.authguard.service.model.AuthResponseBO)29 Test (org.junit.jupiter.api.Test)24 AccountBO (com.nexblocks.authguard.service.model.AccountBO)15 AuthRequestBO (com.nexblocks.authguard.service.model.AuthRequestBO)11 ServiceAuthorizationException (com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException)10 AccountTokenDO (com.nexblocks.authguard.dal.model.AccountTokenDO)7 RequestContextBO (com.nexblocks.authguard.service.model.RequestContextBO)5 OtpConfig (com.nexblocks.authguard.basic.config.OtpConfig)4 OneTimePasswordDO (com.nexblocks.authguard.dal.model.OneTimePasswordDO)4 JwtConfig (com.nexblocks.authguard.service.config.JwtConfig)4 StrategyConfig (com.nexblocks.authguard.service.config.StrategyConfig)4 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)3 AuthRequestDTO (com.nexblocks.authguard.api.dto.requests.AuthRequestDTO)3 AppBO (com.nexblocks.authguard.service.model.AppBO)2 AuthResponseDTO (com.nexblocks.authguard.api.dto.entities.AuthResponseDTO)1 Error (com.nexblocks.authguard.api.dto.entities.Error)1 PasswordlessRequestDTO (com.nexblocks.authguard.api.dto.requests.PasswordlessRequestDTO)1 AccountTokensRepository (com.nexblocks.authguard.dal.cache.AccountTokensRepository)1 ServiceMapperImpl (com.nexblocks.authguard.service.mappers.ServiceMapperImpl)1 AccountLockBO (com.nexblocks.authguard.service.model.AccountLockBO)1