Search in sources :

Example 1 with HttpResponseExceptionWithErrorBody

use of uk.gov.di.ipv.cri.passport.library.exceptions.HttpResponseExceptionWithErrorBody in project di-ipv-cri-uk-passport-back by alphagov.

the class AuthorizationCodeHandler method handleRequest.

@Override
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
    Map<String, List<String>> queryStringParameters = getQueryStringParametersAsMap(input);
    String userId = RequestHelper.getHeaderByKey(input.getHeaders(), "user_id");
    try {
        var validationResult = authRequestValidator.validateRequest(queryStringParameters, userId);
        if (validationResult.isPresent()) {
            return ApiGatewayResponseGenerator.proxyJsonResponse(HttpStatus.SC_BAD_REQUEST, validationResult.get());
        }
        AuthenticationRequest authenticationRequest = AuthenticationRequest.parse(queryStringParameters);
        PassportAttributes passportAttributes = parsePassportFormRequest(input.getBody());
        JWSObject preparedDcsPayload = preparePayload(passportAttributes);
        DcsSignedEncryptedResponse dcsResponse = doPassportCheck(preparedDcsPayload);
        auditService.sendAuditEvent(AuditEventTypes.PASSPORT_REQUEST_SENT_TO_DCS);
        DcsResponse unwrappedDcsResponse = unwrapDcsResponse(dcsResponse);
        passportAttributes.setDcsResponse(unwrappedDcsResponse);
        validateDcsResponse(unwrappedDcsResponse);
        PassportCheckDao passportCheckDao = new PassportCheckDao(UUID.randomUUID().toString(), passportAttributes, generateGpg45Score(unwrappedDcsResponse), userId);
        passportService.persistDcsResponse(passportCheckDao);
        AuthorizationCode authorizationCode = authorizationCodeService.generateAuthorizationCode();
        authorizationCodeService.persistAuthorizationCode(authorizationCode.getValue(), passportCheckDao.getResourceId(), authenticationRequest.getRedirectionURI().toString());
        return ApiGatewayResponseGenerator.proxyJsonResponse(HttpStatus.SC_OK, Map.of(AUTHORIZATION_CODE, authorizationCode));
    } catch (HttpResponseExceptionWithErrorBody e) {
        return ApiGatewayResponseGenerator.proxyJsonResponse(e.getStatusCode(), e.getErrorBody());
    } catch (ParseException e) {
        LOGGER.error("Authentication request could not be parsed", e);
        return ApiGatewayResponseGenerator.proxyJsonResponse(HttpStatus.SC_BAD_REQUEST, ErrorResponse.FAILED_TO_PARSE_OAUTH_QUERY_STRING_PARAMETERS);
    } catch (SqsException e) {
        LOGGER.error("Failed to send audit event to SQS queue because: {}", e.getMessage());
        return ApiGatewayResponseGenerator.proxyJsonResponse(HttpStatus.SC_BAD_REQUEST, ErrorResponse.FAILED_TO_SEND_AUDIT_MESSAGE_TO_SQS_QUEUE);
    }
}
Also used : DcsResponse(uk.gov.di.ipv.cri.passport.library.domain.DcsResponse) DcsSignedEncryptedResponse(uk.gov.di.ipv.cri.passport.library.domain.DcsSignedEncryptedResponse) AuthorizationCode(com.nimbusds.oauth2.sdk.AuthorizationCode) HttpResponseExceptionWithErrorBody(uk.gov.di.ipv.cri.passport.library.exceptions.HttpResponseExceptionWithErrorBody) SqsException(uk.gov.di.ipv.cri.passport.library.exceptions.SqsException) PassportAttributes(uk.gov.di.ipv.cri.passport.library.domain.PassportAttributes) List(java.util.List) ParseException(com.nimbusds.oauth2.sdk.ParseException) AuthenticationRequest(com.nimbusds.openid.connect.sdk.AuthenticationRequest) JWSObject(com.nimbusds.jose.JWSObject) PassportCheckDao(uk.gov.di.ipv.cri.passport.library.persistence.item.PassportCheckDao)

Aggregations

JWSObject (com.nimbusds.jose.JWSObject)1 AuthorizationCode (com.nimbusds.oauth2.sdk.AuthorizationCode)1 ParseException (com.nimbusds.oauth2.sdk.ParseException)1 AuthenticationRequest (com.nimbusds.openid.connect.sdk.AuthenticationRequest)1 List (java.util.List)1 DcsResponse (uk.gov.di.ipv.cri.passport.library.domain.DcsResponse)1 DcsSignedEncryptedResponse (uk.gov.di.ipv.cri.passport.library.domain.DcsSignedEncryptedResponse)1 PassportAttributes (uk.gov.di.ipv.cri.passport.library.domain.PassportAttributes)1 HttpResponseExceptionWithErrorBody (uk.gov.di.ipv.cri.passport.library.exceptions.HttpResponseExceptionWithErrorBody)1 SqsException (uk.gov.di.ipv.cri.passport.library.exceptions.SqsException)1 PassportCheckDao (uk.gov.di.ipv.cri.passport.library.persistence.item.PassportCheckDao)1