Search in sources :

Example 1 with DcsResponse

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

the class JwtHelperTest method shouldCreateValidSignedJWT.

@Test
void shouldCreateValidSignedJWT() throws JOSEException, ParseException, InvalidKeySpecException, NoSuchAlgorithmException, JsonProcessingException {
    ECDSASigner ecSigner = new ECDSASigner(getPrivateKey());
    VerifiableCredential verifiableCredential = new VerifiableCredential(new CredentialSubject(new Name(List.of(new NameParts(GIVEN_NAME, NamePartType.GIVEN_NAME.getName(), VALID_FROM, VALID_UNTIL))), PASSPORT_NUMBER, new BirthDate(BIRTH_DATE), LocalDate.parse(EXPIRY_DATE).toString(), UUID.randomUUID().toString(), UUID.randomUUID().toString(), new DcsResponse()), Collections.singletonList(new Evidence()));
    JWTClaimsSet testClaimsSet = new JWTClaimsSet.Builder().claim("sub", "test-subject").claim("iss", "test-issuer").claim("nbf", Instant.now().getEpochSecond()).claim("vc", verifiableCredential).claim("exp", Instant.now().plusSeconds(100000).getEpochSecond()).build();
    SignedJWT signedJWT = JwtHelper.createSignedJwtFromClaimSet(testClaimsSet, ecSigner);
    JWTClaimsSet generatedClaims = signedJWT.getJWTClaimsSet();
    assertTrue(signedJWT.verify(new ECDSAVerifier(ECKey.parse(EC_PUBLIC_JWK_1))));
    JsonNode claimsSet = objectMapper.readTree(generatedClaims.toString());
    JsonNode vcNode = claimsSet.get("vc");
    JsonNode credentialSubjectNode = vcNode.get("credentialSubject");
    JsonNode nameNode = credentialSubjectNode.get("name");
    assertEquals(GIVEN_NAME, nameNode.get("nameParts").get(0).get("value").asText());
    assertEquals(NamePartType.GIVEN_NAME.getName(), nameNode.get("nameParts").get(0).get("type").asText());
    assertEquals(VALID_FROM, nameNode.get("nameParts").get(0).get("validFrom").asText());
    assertEquals(VALID_UNTIL, nameNode.get("nameParts").get(0).get("validUntil").asText());
    assertEquals(BIRTH_DATE, credentialSubjectNode.get("birthDate").get("value").asText());
    assertEquals(EXPIRY_DATE, credentialSubjectNode.get("expiryDate").asText());
}
Also used : DcsResponse(uk.gov.di.ipv.cri.passport.library.domain.DcsResponse) CredentialSubject(uk.gov.di.ipv.cri.passport.library.domain.verifiablecredential.CredentialSubject) ECDSASigner(com.nimbusds.jose.crypto.ECDSASigner) JsonNode(com.fasterxml.jackson.databind.JsonNode) SignedJWT(com.nimbusds.jwt.SignedJWT) Name(uk.gov.di.ipv.cri.passport.library.domain.verifiablecredential.Name) VerifiableCredential(uk.gov.di.ipv.cri.passport.library.domain.verifiablecredential.VerifiableCredential) ECDSAVerifier(com.nimbusds.jose.crypto.ECDSAVerifier) NameParts(uk.gov.di.ipv.cri.passport.library.domain.verifiablecredential.NameParts) JWTClaimsSet(com.nimbusds.jwt.JWTClaimsSet) Evidence(uk.gov.di.ipv.cri.passport.library.domain.verifiablecredential.Evidence) BirthDate(uk.gov.di.ipv.cri.passport.library.domain.verifiablecredential.BirthDate) Test(org.junit.jupiter.api.Test)

Example 2 with DcsResponse

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

the class DateStorePassportCheckIT method createPassportCheckDao.

private PassportCheckDao createPassportCheckDao() {
    String resourceId = UUID.randomUUID().toString();
    DcsResponse dcsResponse = new DcsResponse(UUID.randomUUID().toString(), UUID.randomUUID().toString(), false, true, null);
    PassportAttributes passportAttributes = new PassportAttributes("passport-number", "surname", List.of("family-name"), LocalDate.of(1900, 1, 1), LocalDate.of(2025, 2, 2));
    passportAttributes.setDcsResponse(dcsResponse);
    Evidence evidence = new Evidence(5, 5);
    createdItemIds.add(resourceId);
    return new PassportCheckDao(resourceId, passportAttributes, evidence, "test-user-id");
}
Also used : DcsResponse(uk.gov.di.ipv.cri.passport.library.domain.DcsResponse) PassportAttributes(uk.gov.di.ipv.cri.passport.library.domain.PassportAttributes) Evidence(uk.gov.di.ipv.cri.passport.library.domain.verifiablecredential.Evidence) PassportCheckDao(uk.gov.di.ipv.cri.passport.library.persistence.item.PassportCheckDao)

Example 3 with DcsResponse

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

the class VerifiableCredentialTest method shouldConvertPassportCheckDaoToPassportCredentialIssuerResponse.

@Test
void shouldConvertPassportCheckDaoToPassportCredentialIssuerResponse() {
    PassportAttributes attributes = new PassportAttributes(PASSPORT_NUMBER, FAMILY_NAME, GIVEN_NAMES, DATE_OF_BIRTH, EXPIRY_DATE);
    Evidence evidence = new Evidence(4, 4);
    attributes.setDcsResponse(new DcsResponse(UUID.randomUUID().toString(), UUID.randomUUID().toString(), true, false, Collections.emptyList()));
    PassportCheckDao passportCheckDao = new PassportCheckDao(RESOURCE_ID, attributes, evidence, "test-user-id");
    VerifiableCredential verifiableCredential = VerifiableCredential.fromPassportCheckDao(passportCheckDao);
    assertEquals(FAMILY_NAME, verifiableCredential.getCredentialSubject().getName().getNameParts().get(1).getValue());
    assertEquals(GIVEN_NAMES.get(0), verifiableCredential.getCredentialSubject().getName().getNameParts().get(0).getValue());
    assertEquals(PASSPORT_NUMBER, verifiableCredential.getCredentialSubject().getPassportNumber());
    assertEquals(DATE_OF_BIRTH.toString(), verifiableCredential.getCredentialSubject().getBirthDate().getValue());
    assertEquals(EXPIRY_DATE.toString(), verifiableCredential.getCredentialSubject().getExpiryDate());
    assertEquals(passportCheckDao.getAttributes().getRequestId().toString(), verifiableCredential.getCredentialSubject().getRequestId());
    assertEquals(passportCheckDao.getAttributes().getCorrelationId().toString(), verifiableCredential.getCredentialSubject().getCorrelationId());
    assertEquals(passportCheckDao.getAttributes().getDcsResponse(), verifiableCredential.getCredentialSubject().getDcsResponse());
}
Also used : DcsResponse(uk.gov.di.ipv.cri.passport.library.domain.DcsResponse) VerifiableCredential(uk.gov.di.ipv.cri.passport.library.domain.verifiablecredential.VerifiableCredential) PassportAttributes(uk.gov.di.ipv.cri.passport.library.domain.PassportAttributes) Evidence(uk.gov.di.ipv.cri.passport.library.domain.verifiablecredential.Evidence) PassportCheckDao(uk.gov.di.ipv.cri.passport.library.persistence.item.PassportCheckDao) Test(org.junit.jupiter.api.Test)

Example 4 with DcsResponse

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

the class StubDcsHandler method handleRequest.

@Override
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
    try {
        DcsResponse incomingPayload = verifyAndDecryptAndVerify(input.getBody());
        DcsResponse dcsResponse = new DcsResponse(incomingPayload.getCorrelationId(), incomingPayload.getRequestId(), false, true, null);
        LOGGER.info("Generated DCS response with correlationId: {} and requestId: {}", dcsResponse.getCorrelationId(), dcsResponse.getRequestId());
        return ApiGatewayResponseGenerator.proxyJoseResponse(HttpStatus.SC_OK, signAndEncryptAndSign(dcsResponse));
    } catch (StubDcsException e) {
        return ApiGatewayResponseGenerator.proxyJsonResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, e);
    }
}
Also used : DcsResponse(uk.gov.di.ipv.cri.passport.library.domain.DcsResponse) StubDcsException(uk.gov.di.ipv.cri.passport.library.exceptions.StubDcsException)

Example 5 with DcsResponse

use of uk.gov.di.ipv.cri.passport.library.domain.DcsResponse 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

DcsResponse (uk.gov.di.ipv.cri.passport.library.domain.DcsResponse)7 Test (org.junit.jupiter.api.Test)4 PassportAttributes (uk.gov.di.ipv.cri.passport.library.domain.PassportAttributes)4 DcsSignedEncryptedResponse (uk.gov.di.ipv.cri.passport.library.domain.DcsSignedEncryptedResponse)3 Evidence (uk.gov.di.ipv.cri.passport.library.domain.verifiablecredential.Evidence)3 PassportCheckDao (uk.gov.di.ipv.cri.passport.library.persistence.item.PassportCheckDao)3 JWSObject (com.nimbusds.jose.JWSObject)2 VerifiableCredential (uk.gov.di.ipv.cri.passport.library.domain.verifiablecredential.VerifiableCredential)2 APIGatewayProxyRequestEvent (com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ECDSASigner (com.nimbusds.jose.crypto.ECDSASigner)1 ECDSAVerifier (com.nimbusds.jose.crypto.ECDSAVerifier)1 JWTClaimsSet (com.nimbusds.jwt.JWTClaimsSet)1 SignedJWT (com.nimbusds.jwt.SignedJWT)1 AuthorizationCode (com.nimbusds.oauth2.sdk.AuthorizationCode)1 ParseException (com.nimbusds.oauth2.sdk.ParseException)1 AuthenticationRequest (com.nimbusds.openid.connect.sdk.AuthenticationRequest)1 HashMap (java.util.HashMap)1 List (java.util.List)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1