use of uk.gov.di.ipv.cri.passport.library.domain.verifiablecredential.VerifiableCredential 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());
}
use of uk.gov.di.ipv.cri.passport.library.domain.verifiablecredential.VerifiableCredential in project di-ipv-cri-uk-passport-back by alphagov.
the class IssueCredentialHandlerTest method shouldReturnCredentialsOnSuccessfulDcsCredentialRequest.
@Test
void shouldReturnCredentialsOnSuccessfulDcsCredentialRequest() throws JsonProcessingException, ParseException, JOSEException {
APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
AccessToken accessToken = new BearerAccessToken();
Map<String, String> headers = Collections.singletonMap("Authorization", accessToken.toAuthorizationHeader());
event.setHeaders(headers);
when(mockAccessTokenService.getResourceIdByAccessToken(anyString())).thenReturn(TEST_RESOURCE_ID);
when(mockDcsPassportCheckService.getDcsPassportCheck(anyString())).thenReturn(dcsCredential);
when(mockConfigurationService.getVerifiableCredentialIssuer()).thenReturn("test-issuer");
APIGatewayProxyResponseEvent response = issueCredentialHandler.handleRequest(event, mockContext);
SignedJWT signedJWT = SignedJWT.parse(response.getBody());
JsonNode claimsSet = objectMapper.readTree(signedJWT.getJWTClaimsSet().toString());
assertEquals(200, response.getStatusCode());
assertEquals(7, claimsSet.size());
JsonNode vcNode = claimsSet.get("vc");
VerifiableCredential verifiableCredential = objectMapper.convertValue(vcNode, VerifiableCredential.class);
assertEquals(dcsCredential.getUserId(), claimsSet.get("sub").asText());
List<NameParts> nameParts = verifiableCredential.getCredentialSubject().getName().getNameParts();
assertTrue(nameParts.stream().anyMatch(o -> isType(NamePartType.FAMILY_NAME).and(hasValue(dcsCredential.getAttributes().getSurname())).test(o)));
assertTrue(nameParts.stream().anyMatch(o -> isType(NamePartType.GIVEN_NAME).and(hasValue(dcsCredential.getAttributes().getForenames().get(0))).test(o)));
assertEquals(dcsCredential.getAttributes().getPassportNumber(), verifiableCredential.getCredentialSubject().getPassportNumber());
assertEquals(dcsCredential.getAttributes().getDateOfBirth().toString(), verifiableCredential.getCredentialSubject().getBirthDate().getValue());
assertEquals(dcsCredential.getAttributes().getExpiryDate().toString(), verifiableCredential.getCredentialSubject().getExpiryDate());
assertEquals(dcsCredential.getAttributes().getRequestId().toString(), verifiableCredential.getCredentialSubject().getRequestId());
assertEquals(dcsCredential.getAttributes().getCorrelationId().toString(), verifiableCredential.getCredentialSubject().getCorrelationId());
assertEquals(dcsCredential.getGpg45Score().getStrength(), verifiableCredential.getEvidence().get(0).getStrength());
assertEquals(dcsCredential.getGpg45Score().getValidity(), verifiableCredential.getEvidence().get(0).getValidity());
ECDSAVerifier ecVerifier = new ECDSAVerifier(ECKey.parse(EC_PUBLIC_JWK_1));
assertTrue(signedJWT.verify(ecVerifier));
}
use of uk.gov.di.ipv.cri.passport.library.domain.verifiablecredential.VerifiableCredential 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());
}
use of uk.gov.di.ipv.cri.passport.library.domain.verifiablecredential.VerifiableCredential 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);
}
}
Aggregations