Search in sources :

Example 16 with Context

use of io.javalin.http.Context 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

Context (io.javalin.http.Context)16 List (java.util.List)13 Optional (java.util.Optional)10 Map (java.util.Map)9 Collectors (java.util.stream.Collectors)9 Handler (io.javalin.http.Handler)5 HttpMethod (io.javalin.plugin.openapi.annotations.HttpMethod)5 OpenApi (io.javalin.plugin.openapi.annotations.OpenApi)5 OpenApiContent (io.javalin.plugin.openapi.annotations.OpenApiContent)5 OpenApiResponse (io.javalin.plugin.openapi.annotations.OpenApiResponse)5 Inject (com.google.inject.Inject)4 Error (com.nexblocks.authguard.api.dto.entities.Error)4 RestMapper (com.nexblocks.authguard.rest.mappers.RestMapper)4 BodyHandler (com.nexblocks.authguard.rest.util.BodyHandler)4 IdempotencyHeader (com.nexblocks.authguard.rest.util.IdempotencyHeader)4 File (java.io.File)4 IOException (java.io.IOException)4 Collections (java.util.Collections)3 SC_BAD_REQUEST (javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST)3 DataProvider (tech.pegasys.teku.api.DataProvider)3