Search in sources :

Example 1 with RestMapper

use of com.nexblocks.authguard.rest.mappers.RestMapper 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 RestMapper

use of com.nexblocks.authguard.rest.mappers.RestMapper 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 3 with RestMapper

use of com.nexblocks.authguard.rest.mappers.RestMapper in project AuthGuard by AuthGuard.

the class CredentialsRoute method create.

public void create(final Context context) {
    final String idempotentKey = IdempotencyHeader.getKeyOrFail(context);
    final CreateCredentialsRequestDTO request = credentialsRequestBodyHandler.getValidated(context);
    if (!ActorDomainVerifier.verifyActorDomain(context, request.getDomain())) {
        return;
    }
    final RequestContextBO requestContext = RequestContextBO.builder().idempotentKey(idempotentKey).source(context.ip()).build();
    final CredentialsBO credentials = restMapper.toBO(request);
    final List<UserIdentifierBO> identifiers = credentials.getIdentifiers().stream().map(identifier -> identifier.withDomain(request.getDomain())).collect(Collectors.toList());
    final Optional<CredentialsDTO> created = Optional.of(credentials.withIdentifiers(identifiers)).map(credentialsBO -> credentialsService.create(credentialsBO, requestContext)).map(restMapper::toDTO);
    if (created.isPresent()) {
        context.status(201).json(created.get());
    } else {
        context.status(400).json(new Error("400", "Failed to create credentials"));
    }
}
Also used : CredentialsDTO(com.nexblocks.authguard.api.dto.entities.CredentialsDTO) Inject(com.google.inject.Inject) UserIdentifiersRequestDTO(com.nexblocks.authguard.api.dto.requests.UserIdentifiersRequestDTO) Violation(com.nexblocks.authguard.api.dto.validation.violations.Violation) CredentialsService(com.nexblocks.authguard.service.CredentialsService) CredentialsApi(com.nexblocks.authguard.api.routes.CredentialsApi) UserIdentifierDTO(com.nexblocks.authguard.api.dto.entities.UserIdentifierDTO) RestMapper(com.nexblocks.authguard.rest.mappers.RestMapper) Context(io.javalin.http.Context) ActorDomainVerifier(com.nexblocks.authguard.rest.access.ActorDomainVerifier) CreateCredentialsRequestDTO(com.nexblocks.authguard.api.dto.requests.CreateCredentialsRequestDTO) IdempotencyHeader(com.nexblocks.authguard.rest.util.IdempotencyHeader) PasswordResetRequestDTO(com.nexblocks.authguard.api.dto.requests.PasswordResetRequestDTO) RequestValidationException(com.nexblocks.authguard.rest.exceptions.RequestValidationException) com.nexblocks.authguard.service.model(com.nexblocks.authguard.service.model) AuthGuardRoles(com.nexblocks.authguard.api.access.AuthGuardRoles) PasswordResetTokenRequestDTO(com.nexblocks.authguard.api.dto.requests.PasswordResetTokenRequestDTO) 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) Optional(java.util.Optional) Collections(java.util.Collections) ViolationType(com.nexblocks.authguard.api.dto.validation.violations.ViolationType) CredentialsDTO(com.nexblocks.authguard.api.dto.entities.CredentialsDTO) Error(com.nexblocks.authguard.api.dto.entities.Error) CreateCredentialsRequestDTO(com.nexblocks.authguard.api.dto.requests.CreateCredentialsRequestDTO)

Aggregations

Inject (com.google.inject.Inject)3 Error (com.nexblocks.authguard.api.dto.entities.Error)3 RestMapper (com.nexblocks.authguard.rest.mappers.RestMapper)3 BodyHandler (com.nexblocks.authguard.rest.util.BodyHandler)3 IdempotencyHeader (com.nexblocks.authguard.rest.util.IdempotencyHeader)3 Context (io.javalin.http.Context)3 List (java.util.List)3 Optional (java.util.Optional)3 Collectors (java.util.stream.Collectors)3 AuthGuardRoles (com.nexblocks.authguard.api.access.AuthGuardRoles)2 AppDTO (com.nexblocks.authguard.api.dto.entities.AppDTO)2 ActorDomainVerifier (com.nexblocks.authguard.rest.access.ActorDomainVerifier)2 RestJsonMapper (com.nexblocks.authguard.rest.mappers.RestJsonMapper)2 ApplicationsService (com.nexblocks.authguard.service.ApplicationsService)2 CredentialsService (com.nexblocks.authguard.service.CredentialsService)2 com.nexblocks.authguard.service.model (com.nexblocks.authguard.service.model)2 AccountDTO (com.nexblocks.authguard.api.dto.entities.AccountDTO)1 AccountLockDTO (com.nexblocks.authguard.api.dto.entities.AccountLockDTO)1 ApiKeyDTO (com.nexblocks.authguard.api.dto.entities.ApiKeyDTO)1 CredentialsDTO (com.nexblocks.authguard.api.dto.entities.CredentialsDTO)1