Search in sources :

Example 16 with AuthResponseBO

use of com.nexblocks.authguard.service.model.AuthResponseBO 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);
}
Also used : RequestContextBO(com.nexblocks.authguard.service.model.RequestContextBO) AuthResponseBO(com.nexblocks.authguard.service.model.AuthResponseBO) OtpConfig(com.nexblocks.authguard.basic.config.OtpConfig) OneTimePasswordDO(com.nexblocks.authguard.dal.model.OneTimePasswordDO) AuthRequestBO(com.nexblocks.authguard.service.model.AuthRequestBO) Test(org.junit.jupiter.api.Test)

Example 17 with AuthResponseBO

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

the class AuthRoute method exchange.

public void exchange(final Context context) {
    final AuthRequestDTO authenticationRequest = authRequestBodyHandler.getValidated(context);
    if (authenticationRequest.getDomain() != null && !ActorDomainVerifier.verifyActorDomain(context, authenticationRequest.getDomain())) {
        return;
    }
    final String from = context.queryParam("from");
    final String to = context.queryParam("to");
    final RequestContextBO requestContext = RequestContextExtractor.extractWithoutIdempotentKey(context);
    final AuthResponseBO tokens = exchangeService.exchange(restMapper.toBO(authenticationRequest), from, to, requestContext);
    context.json(restMapper.toDTO(tokens));
}
Also used : RequestContextBO(com.nexblocks.authguard.service.model.RequestContextBO) AuthResponseBO(com.nexblocks.authguard.service.model.AuthResponseBO) AuthRequestDTO(com.nexblocks.authguard.api.dto.requests.AuthRequestDTO)

Example 18 with AuthResponseBO

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

the class AuthRoute method clearToken.

@Override
public void clearToken(final Context context) {
    final AuthRequestDTO authenticationRequest = authRequestBodyHandler.getValidated(context);
    final String tokenType = context.queryParam("tokenType");
    if (tokenType == null) {
        context.status(400).json(new Error("400", "Missing 'tokenType' query parameter"));
    } else {
        final AuthResponseBO tokens = exchangeService.delete(restMapper.toBO(authenticationRequest), tokenType);
        context.json(restMapper.toDTO(tokens));
    }
}
Also used : AuthResponseBO(com.nexblocks.authguard.service.model.AuthResponseBO) AuthRequestDTO(com.nexblocks.authguard.api.dto.requests.AuthRequestDTO) Error(com.nexblocks.authguard.api.dto.entities.Error)

Example 19 with AuthResponseBO

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

the class IdTokenProviderTest method generateEncrypted.

@Test
void generateEncrypted() {
    final IdTokenProvider idTokenProvider = newProviderInstance(jwtConfigWithEncryption());
    Mockito.when(tokenEncryptor.encryptAndEncode(Mockito.any())).thenAnswer(invocation -> Either.right("encrypted"));
    final AccountBO account = RANDOM.nextObject(AccountBO.class).withActive(true);
    final AuthResponseBO tokens = idTokenProvider.generateToken(account);
    assertThat(tokens).isNotNull();
    assertThat(tokens.getToken()).isEqualTo("encrypted");
    assertThat(tokens.getRefreshToken()).isNotNull();
    assertThat(tokens.getToken()).isNotEqualTo(tokens.getRefreshToken());
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) AuthResponseBO(com.nexblocks.authguard.service.model.AuthResponseBO) Test(org.junit.jupiter.api.Test)

Example 20 with AuthResponseBO

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

the class AuthorizationCodeProviderTest method generateToken.

@Test
void generateToken() {
    final AccountTokensRepository accountTokensRepository = Mockito.mock(AccountTokensRepository.class);
    final AuthorizationCodeProvider authorizationCodeProvider = new AuthorizationCodeProvider(accountTokensRepository, new ServiceMapperImpl(), config());
    final AccountBO account = AccountBO.builder().id("account-id").build();
    final AuthResponseBO tokens = authorizationCodeProvider.generateToken(account);
    assertThat(tokens.getType()).isEqualTo("authorizationCode");
    assertThat(tokens.getToken()).isNotNull();
    assertThat(tokens.getRefreshToken()).isNull();
    final ArgumentCaptor<AccountTokenDO> argCaptor = ArgumentCaptor.forClass(AccountTokenDO.class);
    Mockito.verify(accountTokensRepository, Mockito.times(1)).save(argCaptor.capture());
    assertThat(argCaptor.getValue().getToken()).isEqualTo(tokens.getToken());
    assertThat(argCaptor.getValue().getAssociatedAccountId()).isEqualTo(account.getId());
    assertThat(argCaptor.getValue().getExpiresAt()).isAfter(OffsetDateTime.now()).isBefore(OffsetDateTime.now().plus(Duration.ofMinutes(6)));
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) AccountTokensRepository(com.nexblocks.authguard.dal.cache.AccountTokensRepository) ServiceMapperImpl(com.nexblocks.authguard.service.mappers.ServiceMapperImpl) AccountTokenDO(com.nexblocks.authguard.dal.model.AccountTokenDO) 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