Search in sources :

Example 11 with RequestContextBO

use of com.nexblocks.authguard.service.model.RequestContextBO 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 12 with RequestContextBO

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

the class OAuthService method getOrCreateAccount.

private AccountBO getOrCreateAccount(final OAuthServiceClient serviceClient, final String authorizationCode, final String idToken) {
    final ImmutableOAuthClientConfiguration configuration = serviceClient.getConfiguration();
    final DecodedJWT decoded = JWT.decode(idToken);
    final String externalId = decoded.getSubject();
    final Optional<AccountBO> account = accountsService.getByExternalId(externalId);
    if (account.isPresent()) {
        return account.get();
    }
    final AccountBO.Builder newAccount = AccountBO.builder().externalId(externalId).social(true).identityProvider(configuration.getProvider());
    if (configuration.getEmailField() != null) {
        final Claim emailClaim = decoded.getClaim(configuration.getEmailField());
        if (!emailClaim.isNull()) {
            newAccount.email(AccountEmailBO.builder().email(emailClaim.asString()).build());
        }
    }
    final RequestContextBO requestContext = RequestContextBO.builder().source(configuration.getProvider()).idempotentKey(authorizationCode).build();
    return accountsService.create(newAccount.build(), requestContext);
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) RequestContextBO(com.nexblocks.authguard.service.model.RequestContextBO) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) Claim(com.auth0.jwt.interfaces.Claim) ImmutableOAuthClientConfiguration(com.nexblocks.authguard.jwt.oauth.config.ImmutableOAuthClientConfiguration)

Example 13 with RequestContextBO

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

the class OAuthServiceTest method exchangeAuthorizationCodeAndCreateAccount.

@Test
void exchangeAuthorizationCodeAndCreateAccount() {
    final RequestContextBO expectedContext = RequestContextBO.builder().idempotentKey("code").source("account_test").build();
    Mockito.when(sessionsService.getByToken(Mockito.any())).thenAnswer(invocation -> {
        final SessionBO session = SessionBO.builder().sessionToken(invocation.getArgument(0)).expiresAt(OffsetDateTime.now().plus(Duration.ofMinutes(2))).build();
        return Optional.of(session);
    });
    Mockito.when(accountsService.create(Mockito.any(), Mockito.eq(expectedContext))).thenAnswer(invocation -> invocation.getArgument(0, AccountBO.class).withId("1"));
    final TokensResponse actual = oAuthService.exchangeAuthorizationCode("account_test", "random", "code").join();
    final TokensResponse expected = testIdentityServer.getSuccessResponse();
    expected.setAccountId("1");
    assertThat(actual).isEqualTo(expected);
}
Also used : RequestContextBO(com.nexblocks.authguard.service.model.RequestContextBO) TokensResponse(com.nexblocks.authguard.jwt.oauth.TokensResponse) SessionBO(com.nexblocks.authguard.service.model.SessionBO)

Example 14 with RequestContextBO

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

the class AccountsApiTest method createWithCredentialsAllExist.

@Test
void createWithCredentialsAllExist() {
    final CreateAccountRequestDTO accountRequest = CreateAccountRequestDTO.builder().externalId("external").email(AccountEmailDTO.builder().email("email@server.com").build()).domain("main").build();
    final CreateCredentialsRequestDTO credentialsRequest = CreateCredentialsRequestDTO.builder().plainPassword("password").addIdentifiers(UserIdentifierDTO.builder().identifier("username").type(UserIdentifier.Type.USERNAME).build()).build();
    final CreateCompleteAccountRequestDTO completeRequest = CreateCompleteAccountRequestDTO.builder().account(accountRequest).credentials(credentialsRequest).build();
    final RequestContextBO requestContext = RequestContextBO.builder().idempotentKey(UUID.randomUUID().toString()).build();
    final AccountBO accountBO = mapper().toBO(accountRequest);
    final AccountBO accountResponse = accountBO.withId(UUID.randomUUID().toString());
    final CredentialsBO credentialsBO = mapper().toBO(credentialsRequest).withAccountId(accountResponse.getId());
    final CredentialsBO credentialsResponse = credentialsBO.withId(UUID.randomUUID().toString());
    Mockito.when(accountsService.create(Mockito.eq(accountBO), Mockito.any())).thenThrow(new CompletionException(new IdempotencyException(IdempotentRecordBO.builder().entityId(accountResponse.getId()).build())));
    Mockito.when(credentialsService.create(Mockito.eq(credentialsBO), Mockito.any())).thenThrow(new CompletionException(new IdempotencyException(IdempotentRecordBO.builder().entityId(credentialsResponse.getId()).build())));
    LOG.info("Request {}", accountRequest);
    final ValidatableResponse httpResponse = given().body(completeRequest).contentType(ContentType.JSON).header(IdempotencyHeader.HEADER_NAME, requestContext.getIdempotentKey()).post(url("complete")).then().statusCode(201).contentType(ContentType.JSON);
    final CreateCompleteAccountResponseDTO response = httpResponse.extract().response().getBody().as(CreateCompleteAccountResponseDTO.class);
    assertThat(response.getAccountId()).isEqualTo(accountResponse.getId());
    assertThat(response.getCredentialsId()).isEqualTo(credentialsResponse.getId());
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) CredentialsBO(com.nexblocks.authguard.service.model.CredentialsBO) ValidatableResponse(io.restassured.response.ValidatableResponse) RequestContextBO(com.nexblocks.authguard.service.model.RequestContextBO) CreateAccountRequestDTO(com.nexblocks.authguard.api.dto.requests.CreateAccountRequestDTO) CompletionException(java.util.concurrent.CompletionException) IdempotencyException(com.nexblocks.authguard.service.exceptions.IdempotencyException) CreateCompleteAccountResponseDTO(com.nexblocks.authguard.api.dto.requests.CreateCompleteAccountResponseDTO) CreateCompleteAccountRequestDTO(com.nexblocks.authguard.api.dto.requests.CreateCompleteAccountRequestDTO) CreateCredentialsRequestDTO(com.nexblocks.authguard.api.dto.requests.CreateCredentialsRequestDTO) Test(org.junit.jupiter.api.Test)

Example 15 with RequestContextBO

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

the class ApplicationsServiceImplTest method create.

@Test
void create() {
    final AppBO app = random.nextObject(AppBO.class);
    final String idempotentKey = "idempotent-key";
    final RequestContextBO requestContext = RequestContextBO.builder().idempotentKey(idempotentKey).build();
    Mockito.when(accountsService.getById(app.getParentAccountId())).thenReturn(Optional.of(random.nextObject(AccountBO.class)));
    Mockito.when(applicationsRepository.save(any())).thenAnswer(invocation -> CompletableFuture.completedFuture(invocation.getArgument(0, AppDO.class)));
    Mockito.when(idempotencyService.performOperation(Mockito.any(), Mockito.eq(idempotentKey), Mockito.eq(app.getEntityType()))).thenAnswer(invocation -> {
        return CompletableFuture.completedFuture(invocation.getArgument(0, Supplier.class).get());
    });
    final AppBO created = applicationsService.create(app, requestContext);
    final List<PermissionBO> expectedPermissions = app.getPermissions().stream().map(permission -> permission.withEntityType(null)).collect(Collectors.toList());
    assertThat(created).isEqualToIgnoringGivenFields(app.withPermissions(expectedPermissions), "id", "createdAt", "lastModified", "entityType");
    Mockito.verify(messageBus, Mockito.times(1)).publish(eq("apps"), any());
}
Also used : ServiceMapper(com.nexblocks.authguard.service.mappers.ServiceMapper) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) MessageBus(com.nexblocks.authguard.emb.MessageBus) RequestContextBO(com.nexblocks.authguard.service.model.RequestContextBO) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ServiceMapperImpl(com.nexblocks.authguard.service.mappers.ServiceMapperImpl) CompletableFuture(java.util.concurrent.CompletableFuture) Supplier(java.util.function.Supplier) AccountsService(com.nexblocks.authguard.service.AccountsService) AppBO(com.nexblocks.authguard.service.model.AppBO) ApplicationsService(com.nexblocks.authguard.service.ApplicationsService) IdempotencyService(com.nexblocks.authguard.service.IdempotencyService) EasyRandomParameters(org.jeasy.random.EasyRandomParameters) AppDO(com.nexblocks.authguard.dal.model.AppDO) ApplicationsRepository(com.nexblocks.authguard.dal.persistence.ApplicationsRepository) EasyRandom(org.jeasy.random.EasyRandom) AccountBO(com.nexblocks.authguard.service.model.AccountBO) PermissionBO(com.nexblocks.authguard.service.model.PermissionBO) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) List(java.util.List) Optional(java.util.Optional) RequestContextBO(com.nexblocks.authguard.service.model.RequestContextBO) AppBO(com.nexblocks.authguard.service.model.AppBO) PermissionBO(com.nexblocks.authguard.service.model.PermissionBO) Test(org.junit.jupiter.api.Test)

Aggregations

RequestContextBO (com.nexblocks.authguard.service.model.RequestContextBO)17 Test (org.junit.jupiter.api.Test)9 AccountBO (com.nexblocks.authguard.service.model.AccountBO)6 AuthResponseBO (com.nexblocks.authguard.service.model.AuthResponseBO)5 AuthRequestDTO (com.nexblocks.authguard.api.dto.requests.AuthRequestDTO)4 CreateAccountRequestDTO (com.nexblocks.authguard.api.dto.requests.CreateAccountRequestDTO)4 AuthRequestBO (com.nexblocks.authguard.service.model.AuthRequestBO)4 Error (com.nexblocks.authguard.api.dto.entities.Error)3 CreateCompleteAccountRequestDTO (com.nexblocks.authguard.api.dto.requests.CreateCompleteAccountRequestDTO)3 CreateCompleteAccountResponseDTO (com.nexblocks.authguard.api.dto.requests.CreateCompleteAccountResponseDTO)3 CreateCredentialsRequestDTO (com.nexblocks.authguard.api.dto.requests.CreateCredentialsRequestDTO)3 CredentialsBO (com.nexblocks.authguard.service.model.CredentialsBO)3 ValidatableResponse (io.restassured.response.ValidatableResponse)3 AuthResponseDTO (com.nexblocks.authguard.api.dto.entities.AuthResponseDTO)2 ApplicationsService (com.nexblocks.authguard.service.ApplicationsService)2 IdempotencyException (com.nexblocks.authguard.service.exceptions.IdempotencyException)2 List (java.util.List)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 Claim (com.auth0.jwt.interfaces.Claim)1