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);
}
}
Aggregations