Search in sources :

Example 1 with AccountDTO

use of com.nexblocks.authguard.api.dto.entities.AccountDTO in project AuthGuard by AuthGuard.

the class AccountsRoute method create.

public void create(final Context context) {
    final String idempotentKey = IdempotencyHeader.getKeyOrFail(context);
    final CreateAccountRequestDTO request = accountRequestBodyHandler.getValidated(context);
    if (!canPerform(context, request)) {
        context.status(403).json(new Error("", "An auth client violated its restrictions in the request"));
        return;
    }
    final RequestContextBO requestContext = RequestContextBO.builder().idempotentKey(idempotentKey).source(context.ip()).build();
    final Optional<AccountDTO> createdAccount = Optional.of(restMapper.toBO(request)).map(accountBO -> accountsService.create(accountBO, requestContext)).map(restMapper::toDTO);
    if (createdAccount.isPresent()) {
        context.status(201).json(createdAccount.get());
    } else {
        context.status(400).json(new Error("400", "Failed to create account"));
    }
}
Also used : Inject(com.google.inject.Inject) IdempotencyException(com.nexblocks.authguard.service.exceptions.IdempotencyException) CredentialsService(com.nexblocks.authguard.service.CredentialsService) com.nexblocks.authguard.api.dto.requests(com.nexblocks.authguard.api.dto.requests) AccountsService(com.nexblocks.authguard.service.AccountsService) RestMapper(com.nexblocks.authguard.rest.mappers.RestMapper) Context(io.javalin.http.Context) ActorDomainVerifier(com.nexblocks.authguard.rest.access.ActorDomainVerifier) AccountsApi(com.nexblocks.authguard.api.routes.AccountsApi) IdempotencyHeader(com.nexblocks.authguard.rest.util.IdempotencyHeader) ApplicationsService(com.nexblocks.authguard.service.ApplicationsService) AccountLocksService(com.nexblocks.authguard.service.AccountLocksService) AccountDTO(com.nexblocks.authguard.api.dto.entities.AccountDTO) AppDTO(com.nexblocks.authguard.api.dto.entities.AppDTO) ErrorCode(com.nexblocks.authguard.service.exceptions.codes.ErrorCode) Collection(java.util.Collection) CompletionException(java.util.concurrent.CompletionException) com.nexblocks.authguard.service.model(com.nexblocks.authguard.service.model) AuthGuardRoles(com.nexblocks.authguard.api.access.AuthGuardRoles) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) BodyHandler(com.nexblocks.authguard.rest.util.BodyHandler) Error(com.nexblocks.authguard.api.dto.entities.Error) AccountLockDTO(com.nexblocks.authguard.api.dto.entities.AccountLockDTO) Optional(java.util.Optional) Error(com.nexblocks.authguard.api.dto.entities.Error) AccountDTO(com.nexblocks.authguard.api.dto.entities.AccountDTO)

Example 2 with AccountDTO

use of com.nexblocks.authguard.api.dto.entities.AccountDTO in project AuthGuard by AuthGuard.

the class AccountsRoute method getByEmail.

@Override
public void getByEmail(final Context context) {
    final String domain = context.pathParam("domain");
    final String email = context.pathParam("email");
    final Optional<AccountDTO> account = accountsService.getByEmail(email, domain).map(restMapper::toDTO);
    if (account.isPresent()) {
        context.status(200).json(account.get());
    } else {
        context.status(404).json(new Error(ErrorCode.ACCOUNT_DOES_NOT_EXIST.getCode(), "Account not found"));
    }
}
Also used : Error(com.nexblocks.authguard.api.dto.entities.Error) AccountDTO(com.nexblocks.authguard.api.dto.entities.AccountDTO)

Example 3 with AccountDTO

use of com.nexblocks.authguard.api.dto.entities.AccountDTO in project AuthGuard by AuthGuard.

the class AccountsRoute method updateRoles.

public void updateRoles(final Context context) {
    final String accountId = context.pathParam("id");
    final RolesRequestDTO request = rolesRequestBodyHandler.getValidated(context);
    final AccountDTO updatedAccount;
    if (request.getAction() == RolesRequest.Action.GRANT) {
        updatedAccount = restMapper.toDTO(accountsService.grantRoles(accountId, request.getRoles()));
    } else {
        updatedAccount = restMapper.toDTO(accountsService.revokeRoles(accountId, request.getRoles()));
    }
    context.json(updatedAccount);
}
Also used : AccountDTO(com.nexblocks.authguard.api.dto.entities.AccountDTO)

Example 4 with AccountDTO

use of com.nexblocks.authguard.api.dto.entities.AccountDTO in project AuthGuard by AuthGuard.

the class AccountsApiTest method create.

@Test
void create() {
    final CreateAccountRequestDTO requestDTO = CreateAccountRequestDTO.builder().externalId("external").email(AccountEmailDTO.builder().email("email@server.com").build()).domain("main").build();
    final RequestContextBO requestContext = RequestContextBO.builder().idempotentKey(UUID.randomUUID().toString()).build();
    final AccountBO accountBO = mapper().toBO(requestDTO);
    final AccountBO serviceResponse = accountBO.withId(UUID.randomUUID().toString());
    Mockito.when(accountsService.create(Mockito.eq(accountBO), Mockito.any())).thenReturn(serviceResponse);
    LOG.info("Request {}", requestDTO);
    final ValidatableResponse httpResponse = given().body(requestDTO).contentType(ContentType.JSON).header(IdempotencyHeader.HEADER_NAME, requestContext.getIdempotentKey()).post(url()).then().statusCode(201).contentType(ContentType.JSON);
    final AccountDTO response = httpResponse.extract().response().getBody().as(AccountDTO.class);
    assertThat(response).isEqualToIgnoringGivenFields(requestDTO, "id", "deleted", "createdAt", "lastModified", "social", "identityProvider");
    assertThat(response.getId()).isEqualTo(serviceResponse.getId());
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) ValidatableResponse(io.restassured.response.ValidatableResponse) RequestContextBO(com.nexblocks.authguard.service.model.RequestContextBO) CreateAccountRequestDTO(com.nexblocks.authguard.api.dto.requests.CreateAccountRequestDTO) AccountDTO(com.nexblocks.authguard.api.dto.entities.AccountDTO) Test(org.junit.jupiter.api.Test)

Example 5 with AccountDTO

use of com.nexblocks.authguard.api.dto.entities.AccountDTO in project AuthGuard by AuthGuard.

the class AccountsRoute method getById.

public void getById(final Context context) {
    final String accountId = context.pathParam("id");
    final Optional<AccountDTO> account = accountsService.getById(accountId).map(restMapper::toDTO);
    if (account.isPresent()) {
        context.status(200).json(account.get());
    } else {
        context.status(404).json(new Error(ErrorCode.ACCOUNT_DOES_NOT_EXIST.getCode(), "Account not found"));
    }
}
Also used : Error(com.nexblocks.authguard.api.dto.entities.Error) AccountDTO(com.nexblocks.authguard.api.dto.entities.AccountDTO)

Aggregations

AccountDTO (com.nexblocks.authguard.api.dto.entities.AccountDTO)11 Error (com.nexblocks.authguard.api.dto.entities.Error)8 Inject (com.google.inject.Inject)1 AuthGuardRoles (com.nexblocks.authguard.api.access.AuthGuardRoles)1 AccountLockDTO (com.nexblocks.authguard.api.dto.entities.AccountLockDTO)1 AppDTO (com.nexblocks.authguard.api.dto.entities.AppDTO)1 com.nexblocks.authguard.api.dto.requests (com.nexblocks.authguard.api.dto.requests)1 CreateAccountRequestDTO (com.nexblocks.authguard.api.dto.requests.CreateAccountRequestDTO)1 AccountsApi (com.nexblocks.authguard.api.routes.AccountsApi)1 ActorDomainVerifier (com.nexblocks.authguard.rest.access.ActorDomainVerifier)1 RestMapper (com.nexblocks.authguard.rest.mappers.RestMapper)1 BodyHandler (com.nexblocks.authguard.rest.util.BodyHandler)1 IdempotencyHeader (com.nexblocks.authguard.rest.util.IdempotencyHeader)1 AccountLocksService (com.nexblocks.authguard.service.AccountLocksService)1 AccountsService (com.nexblocks.authguard.service.AccountsService)1 ApplicationsService (com.nexblocks.authguard.service.ApplicationsService)1 CredentialsService (com.nexblocks.authguard.service.CredentialsService)1 IdempotencyException (com.nexblocks.authguard.service.exceptions.IdempotencyException)1 ErrorCode (com.nexblocks.authguard.service.exceptions.codes.ErrorCode)1 com.nexblocks.authguard.service.model (com.nexblocks.authguard.service.model)1