Search in sources :

Example 6 with RequestContextBO

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

the class AccountsApiTest method createWithCredentials.

@Test
void createWithCredentials() {
    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())).thenReturn(accountResponse);
    Mockito.when(credentialsService.create(Mockito.eq(credentialsBO), Mockito.any())).thenReturn(credentialsResponse);
    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) 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 7 with RequestContextBO

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

the class AccountsApiTest method createWithCredentialsAccountExists.

@Test
void createWithCredentialsAccountExists() {
    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())).thenReturn(credentialsResponse);
    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 8 with RequestContextBO

use of com.nexblocks.authguard.service.model.RequestContextBO 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);
}
Also used : RequestContextBO(com.nexblocks.authguard.service.model.RequestContextBO) AuthRequestDTO(com.nexblocks.authguard.api.dto.requests.AuthRequestDTO) AuthRequestBO(com.nexblocks.authguard.service.model.AuthRequestBO) Test(org.junit.jupiter.api.Test)

Example 9 with RequestContextBO

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

the class ApplicationsRoute method create.

public void create(final Context context) {
    final String idempotentKey = IdempotencyHeader.getKeyOrFail(context);
    final CreateAppRequestDTO request = appRequestRequestBodyHandler.getValidated(context);
    final RequestContextBO requestContext = RequestContextBO.builder().idempotentKey(idempotentKey).source(context.ip()).build();
    final Optional<Object> created = Optional.of(restMapper.toBO(request)).map(appBO -> applicationsService.create(appBO, requestContext)).map(restMapper::toDTO);
    if (created.isPresent()) {
        context.status(201).json(created.get());
    } else {
        context.status(400).json(new Error("400", "Failed to create application"));
    }
}
Also used : ApiKeysService(com.nexblocks.authguard.service.ApiKeysService) AppDTO(com.nexblocks.authguard.api.dto.entities.AppDTO) ApplicationsApi(com.nexblocks.authguard.api.routes.ApplicationsApi) ApiKeyDTO(com.nexblocks.authguard.api.dto.entities.ApiKeyDTO) RequestContextBO(com.nexblocks.authguard.service.model.RequestContextBO) Inject(com.google.inject.Inject) Collectors(java.util.stream.Collectors) List(java.util.List) BodyHandler(com.nexblocks.authguard.rest.util.BodyHandler) Error(com.nexblocks.authguard.api.dto.entities.Error) RestJsonMapper(com.nexblocks.authguard.rest.mappers.RestJsonMapper) RestMapper(com.nexblocks.authguard.rest.mappers.RestMapper) Context(io.javalin.http.Context) Optional(java.util.Optional) CreateAppRequestDTO(com.nexblocks.authguard.api.dto.requests.CreateAppRequestDTO) IdempotencyHeader(com.nexblocks.authguard.rest.util.IdempotencyHeader) ApplicationsService(com.nexblocks.authguard.service.ApplicationsService) CreateAppRequestDTO(com.nexblocks.authguard.api.dto.requests.CreateAppRequestDTO) RequestContextBO(com.nexblocks.authguard.service.model.RequestContextBO) Error(com.nexblocks.authguard.api.dto.entities.Error)

Example 10 with RequestContextBO

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

the class AuthRoute method logout.

@Override
public void logout(final Context context) {
    final AuthRequestDTO authenticationRequest = authRequestBodyHandler.getValidated(context);
    final RequestContextBO requestContext = RequestContextExtractor.extractWithoutIdempotentKey(context);
    authenticationService.logout(restMapper.toBO(authenticationRequest), requestContext).ifPresentOrElse(tokens -> context.json(restMapper.toDTO(tokens)), () -> context.status(400).json(new Error("400", "Failed to log user out")));
}
Also used : RequestContextBO(com.nexblocks.authguard.service.model.RequestContextBO) AuthRequestDTO(com.nexblocks.authguard.api.dto.requests.AuthRequestDTO) Error(com.nexblocks.authguard.api.dto.entities.Error)

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