Search in sources :

Example 1 with AuthRequestDTO

use of com.nexblocks.authguard.api.dto.requests.AuthRequestDTO in project AuthGuard by AuthGuard.

the class ApiKeysRoute method verify.

@Override
public void verify(final Context context) {
    final AuthRequestDTO authenticationRequest = authRequestBodyHandler.getValidated(context);
    final Optional<AppBO> app = apiKeysService.validateApiKey(authenticationRequest.getToken());
    if (app.isPresent()) {
        context.status(200).json(app.get());
    } else {
        context.status(404).json(new Error(ErrorCode.API_KEY_DOES_NOT_EXIST.getCode(), "API key does not exist"));
    }
}
Also used : AppBO(com.nexblocks.authguard.service.model.AppBO) AuthRequestDTO(com.nexblocks.authguard.api.dto.requests.AuthRequestDTO) Error(com.nexblocks.authguard.api.dto.entities.Error)

Example 2 with AuthRequestDTO

use of com.nexblocks.authguard.api.dto.requests.AuthRequestDTO in project AuthGuard by AuthGuard.

the class AuthRoute method authenticate.

public void authenticate(final Context context) {
    final AuthRequestDTO authenticationRequest = authRequestBodyHandler.getValidated(context);
    if (authenticationRequest.getDomain() != null && !ActorDomainVerifier.verifyActorDomain(context, authenticationRequest.getDomain())) {
        return;
    }
    final RequestContextBO requestContext = RequestContextExtractor.extractWithoutIdempotentKey(context);
    final Optional<AuthResponseDTO> tokens = authenticationService.authenticate(restMapper.toBO(authenticationRequest), requestContext).map(restMapper::toDTO);
    if (tokens.isPresent()) {
        context.json(tokens.get());
    } else {
        context.status(400).json(new Error("400", "Failed to authenticate user"));
    }
}
Also used : RequestContextBO(com.nexblocks.authguard.service.model.RequestContextBO) AuthResponseDTO(com.nexblocks.authguard.api.dto.entities.AuthResponseDTO) AuthRequestDTO(com.nexblocks.authguard.api.dto.requests.AuthRequestDTO) Error(com.nexblocks.authguard.api.dto.entities.Error)

Example 3 with AuthRequestDTO

use of com.nexblocks.authguard.api.dto.requests.AuthRequestDTO in project AuthGuard by AuthGuard.

the class AuthRequestValidatorTest method validateNoViolationsNoRestrictions.

@Test
void validateNoViolationsNoRestrictions() {
    final AuthRequestDTO valid = AuthRequestDTO.builder().build();
    final Validator<AuthRequestDTO> validator = Validators.getForClass(AuthRequestDTO.class);
    final List<Violation> violations = validator.validate(valid);
    assertThat(violations).isEmpty();
}
Also used : Violation(com.nexblocks.authguard.api.dto.validation.violations.Violation) AuthRequestDTO(com.nexblocks.authguard.api.dto.requests.AuthRequestDTO) Test(org.junit.jupiter.api.Test)

Example 4 with AuthRequestDTO

use of com.nexblocks.authguard.api.dto.requests.AuthRequestDTO 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 5 with AuthRequestDTO

use of com.nexblocks.authguard.api.dto.requests.AuthRequestDTO 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

AuthRequestDTO (com.nexblocks.authguard.api.dto.requests.AuthRequestDTO)11 Test (org.junit.jupiter.api.Test)6 Error (com.nexblocks.authguard.api.dto.entities.Error)4 Violation (com.nexblocks.authguard.api.dto.validation.violations.Violation)4 RequestContextBO (com.nexblocks.authguard.service.model.RequestContextBO)4 AuthResponseBO (com.nexblocks.authguard.service.model.AuthResponseBO)3 AuthResponseDTO (com.nexblocks.authguard.api.dto.entities.AuthResponseDTO)2 AuthRequestBO (com.nexblocks.authguard.service.model.AuthRequestBO)2 AppBO (com.nexblocks.authguard.service.model.AppBO)1 ValidatableResponse (io.restassured.response.ValidatableResponse)1