Search in sources :

Example 1 with CredentialsDTO

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

the class CredentialsRoute method update.

public void update(final Context context) {
    final CredentialsDTO credentials = RestJsonMapper.asClass(context.body(), CredentialsDTO.class);
    if (credentials.getPlainPassword() != null) {
        context.status(400).json(new Error("400", "Password cannot be updated using regular update"));
        return;
    }
    final String credentialsId = context.pathParam("id");
    final Optional<CredentialsDTO> updated = Optional.of(credentials.withId(credentialsId)).map(restMapper::toBO).flatMap(credentialsService::update).map(restMapper::toDTO);
    if (updated.isPresent()) {
        context.status(200).json(updated.get());
    } else {
        context.status(404);
    }
}
Also used : CredentialsDTO(com.nexblocks.authguard.api.dto.entities.CredentialsDTO) Error(com.nexblocks.authguard.api.dto.entities.Error)

Example 2 with CredentialsDTO

use of com.nexblocks.authguard.api.dto.entities.CredentialsDTO 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)

Example 3 with CredentialsDTO

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

the class CredentialsRoute method updatePassword.

public void updatePassword(final Context context) {
    final CredentialsDTO credentials = RestJsonMapper.asClass(context.body(), CredentialsDTO.class);
    final String credentialsId = context.pathParam("id");
    final Optional<CredentialsDTO> updated = credentialsService.updatePassword(credentialsId, credentials.getPlainPassword()).map(restMapper::toDTO);
    if (updated.isPresent()) {
        context.status(200).json(updated.get());
    } else {
        context.status(404);
    }
}
Also used : CredentialsDTO(com.nexblocks.authguard.api.dto.entities.CredentialsDTO)

Example 4 with CredentialsDTO

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

the class CredentialsRouteTest method create.

@Test
void create() {
    final CreateCredentialsRequestDTO credentialsRequest = randomObject(CreateCredentialsRequestDTO.class);
    final CredentialsBO credentialsBO = mapper().toBO(credentialsRequest).withPasswordVersion(null);
    final CredentialsBO serviceResponse = credentialsBO.withPlainPassword(null).withId(UUID.randomUUID().toString()).withPasswordVersion(1);
    Mockito.when(credentialsService.create(Mockito.any(), Mockito.any())).thenReturn(serviceResponse);
    final ValidatableResponse httpResponse = given().body(credentialsRequest).contentType(ContentType.JSON).header("X-IdempotentKey", "key").post(url()).then().statusCode(201).contentType(ContentType.JSON);
    final CredentialsDTO responseBody = httpResponse.extract().response().getBody().as(CredentialsDTO.class);
    assertThat(responseBody).isEqualToIgnoringGivenFields(credentialsRequest, "id", "plainPassword", "createdAt", "lastModified", "passwordUpdatedAt", "passwordVersion");
    assertThat(responseBody.getPlainPassword()).isNull();
    assertThat(responseBody.getId()).isEqualTo(serviceResponse.getId());
    assertThat(responseBody.getPasswordVersion()).isEqualTo(1);
}
Also used : CredentialsBO(com.nexblocks.authguard.service.model.CredentialsBO) ValidatableResponse(io.restassured.response.ValidatableResponse) CredentialsDTO(com.nexblocks.authguard.api.dto.entities.CredentialsDTO) CreateCredentialsRequestDTO(com.nexblocks.authguard.api.dto.requests.CreateCredentialsRequestDTO) Test(org.junit.jupiter.api.Test)

Aggregations

CredentialsDTO (com.nexblocks.authguard.api.dto.entities.CredentialsDTO)4 Error (com.nexblocks.authguard.api.dto.entities.Error)2 CreateCredentialsRequestDTO (com.nexblocks.authguard.api.dto.requests.CreateCredentialsRequestDTO)2 Inject (com.google.inject.Inject)1 AuthGuardRoles (com.nexblocks.authguard.api.access.AuthGuardRoles)1 UserIdentifierDTO (com.nexblocks.authguard.api.dto.entities.UserIdentifierDTO)1 PasswordResetRequestDTO (com.nexblocks.authguard.api.dto.requests.PasswordResetRequestDTO)1 PasswordResetTokenRequestDTO (com.nexblocks.authguard.api.dto.requests.PasswordResetTokenRequestDTO)1 UserIdentifiersRequestDTO (com.nexblocks.authguard.api.dto.requests.UserIdentifiersRequestDTO)1 Violation (com.nexblocks.authguard.api.dto.validation.violations.Violation)1 ViolationType (com.nexblocks.authguard.api.dto.validation.violations.ViolationType)1 CredentialsApi (com.nexblocks.authguard.api.routes.CredentialsApi)1 ActorDomainVerifier (com.nexblocks.authguard.rest.access.ActorDomainVerifier)1 RequestValidationException (com.nexblocks.authguard.rest.exceptions.RequestValidationException)1 RestJsonMapper (com.nexblocks.authguard.rest.mappers.RestJsonMapper)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 CredentialsService (com.nexblocks.authguard.service.CredentialsService)1 com.nexblocks.authguard.service.model (com.nexblocks.authguard.service.model)1