Search in sources :

Example 1 with SqsException

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

the class AuditService method sendAuditEvent.

public void sendAuditEvent(AuditEventTypes eventType) throws SqsException {
    try {
        SendMessageRequest sendMessageRequest = new SendMessageRequest().withQueueUrl(queueUrl).withMessageBody(generateMessageBody(eventType));
        sqs.sendMessage(sendMessageRequest);
    } catch (JsonProcessingException e) {
        throw new SqsException(e);
    }
}
Also used : SqsException(uk.gov.di.ipv.cri.passport.library.exceptions.SqsException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) SendMessageRequest(com.amazonaws.services.sqs.model.SendMessageRequest)

Example 2 with SqsException

use of uk.gov.di.ipv.cri.passport.library.exceptions.SqsException 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)

Example 3 with SqsException

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

the class IssueCredentialHandler method handleRequest.

@Override
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
    try {
        String accessTokenString = RequestHelper.getHeaderByKey(input.getHeaders(), AUTHORIZATION_HEADER_KEY);
        // Performs validation on header value and throws a ParseException if invalid
        AccessToken.parse(accessTokenString);
        String resourceId = accessTokenService.getResourceIdByAccessToken(accessTokenString);
        if (StringUtils.isBlank(resourceId)) {
            LOGGER.error("User credential could not be retrieved. The supplied access token was not found in the database.");
            return ApiGatewayResponseGenerator.proxyJsonResponse(OAuth2Error.ACCESS_DENIED.getHTTPStatusCode(), OAuth2Error.ACCESS_DENIED.appendDescription(" - The supplied access token was not found in the database").toJSONObject());
        }
        PassportCheckDao passportCheck = dcsPassportCheckService.getDcsPassportCheck(resourceId);
        VerifiableCredential verifiableCredential = VerifiableCredential.fromPassportCheckDao(passportCheck);
        SignedJWT signedJWT = generateAndSignVerifiableCredentialJwt(verifiableCredential, passportCheck.getUserId());
        auditService.sendAuditEvent(AuditEventTypes.PASSPORT_CREDENTIAL_ISSUED);
        return ApiGatewayResponseGenerator.proxyJwtResponse(HttpStatus.SC_OK, signedJWT.serialize());
    } catch (ParseException e) {
        LOGGER.error("Failed to parse access token");
        return ApiGatewayResponseGenerator.proxyJsonResponse(e.getErrorObject().getHTTPStatusCode(), e.getErrorObject().toJSONObject());
    } catch (JOSEException e) {
        LOGGER.error("Failed to sign verifiable credential: '{}'", e.getMessage());
        return ApiGatewayResponseGenerator.proxyJsonResponse(OAuth2Error.SERVER_ERROR.getHTTPStatusCode(), OAuth2Error.SERVER_ERROR.appendDescription(" " + e.getMessage()).toJSONObject());
    } 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 : VerifiableCredential(uk.gov.di.ipv.cri.passport.library.domain.verifiablecredential.VerifiableCredential) SqsException(uk.gov.di.ipv.cri.passport.library.exceptions.SqsException) SignedJWT(com.nimbusds.jwt.SignedJWT) ParseException(com.nimbusds.oauth2.sdk.ParseException) JOSEException(com.nimbusds.jose.JOSEException) PassportCheckDao(uk.gov.di.ipv.cri.passport.library.persistence.item.PassportCheckDao)

Aggregations

SqsException (uk.gov.di.ipv.cri.passport.library.exceptions.SqsException)3 ParseException (com.nimbusds.oauth2.sdk.ParseException)2 PassportCheckDao (uk.gov.di.ipv.cri.passport.library.persistence.item.PassportCheckDao)2 SendMessageRequest (com.amazonaws.services.sqs.model.SendMessageRequest)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JOSEException (com.nimbusds.jose.JOSEException)1 JWSObject (com.nimbusds.jose.JWSObject)1 SignedJWT (com.nimbusds.jwt.SignedJWT)1 AuthorizationCode (com.nimbusds.oauth2.sdk.AuthorizationCode)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 VerifiableCredential (uk.gov.di.ipv.cri.passport.library.domain.verifiablecredential.VerifiableCredential)1 HttpResponseExceptionWithErrorBody (uk.gov.di.ipv.cri.passport.library.exceptions.HttpResponseExceptionWithErrorBody)1