diff --git a/src/domain/verifier/useCases/index.ts b/src/domain/verifier/useCases/index.ts index 6115548f..9e6b8425 100644 --- a/src/domain/verifier/useCases/index.ts +++ b/src/domain/verifier/useCases/index.ts @@ -7,6 +7,7 @@ import getVerificationMap from './getVerificationMap'; import lookForTx from './lookForTx'; import parseIssuerKeys from './parseIssuerKeys'; import parseRevocationKey from './parseRevocationKey'; +import validateVerifiableCredential from './validateVerifiableCredential'; export { convertToVerificationSubsteps, @@ -17,5 +18,6 @@ export { getVerificationMap, lookForTx, parseIssuerKeys, - parseRevocationKey + parseRevocationKey, + validateVerifiableCredential }; diff --git a/src/domain/verifier/useCases/validateVerifiableCredential.ts b/src/domain/verifier/useCases/validateVerifiableCredential.ts index 72567a0d..0a4bd28d 100644 --- a/src/domain/verifier/useCases/validateVerifiableCredential.ts +++ b/src/domain/verifier/useCases/validateVerifiableCredential.ts @@ -129,7 +129,7 @@ export default function validateVerifiableCredential (credential: BlockcertsV3): validateDateRFC3339StringFormat(credential.validFrom, 'validFrom'); } if (credential.validUntil) { - validateDateRFC3339StringFormat(credential.validUntil, 'validUntil') + validateDateRFC3339StringFormat(credential.validUntil, 'validUntil'); } } diff --git a/src/parsers/parseV3.ts b/src/parsers/parseV3.ts index f8be22d8..524f588b 100644 --- a/src/parsers/parseV3.ts +++ b/src/parsers/parseV3.ts @@ -38,6 +38,11 @@ export default async function parseV3 (certificateJson: BlockcertsV3, locale: st description, credentialSubject } = certificateJson; + try { + domain.verifier.validateVerifiableCredential(certificateJson); + } catch (error) { + throw new Error(`Document presented is not a valid Verifiable Credential: ${error.message}`); + } let { validFrom } = certificateJson; const certificateMetadata = metadata ?? metadataJson; const issuer: Issuer = await domain.verifier.getIssuerProfile(issuerProfileUrl); diff --git a/test/application/parsers/parser-v3.0.test.ts b/test/application/parsers/parser-v3.0.test.ts index 8f45d07a..363ccd30 100644 --- a/test/application/parsers/parser-v3.0.test.ts +++ b/test/application/parsers/parser-v3.0.test.ts @@ -19,6 +19,16 @@ describe('Parser v3 test suite', function () { const parsedCertificate = await parseJSON(fixtureCopy); expect(parsedCertificate.isFormatValid).toBe(false); }); + + describe('when the certificate is not a valid Verifiable Credential', function () { + it('should throw an error', async function () { + const fixtureCopy = JSON.parse(JSON.stringify(fixture)); + fixtureCopy.issuanceDate = '2022-05-22'; + const parsedCertificate = await parseJSON(fixtureCopy); + expect(parsedCertificate.isFormatValid).toBe(false); + expect(parsedCertificate.error).toBe('Document presented is not a valid Verifiable Credential: issuanceDate must be a valid RFC3339 string. Received: `2022-05-22`'); + }); + }); }); describe('given it is called with valid v3 certificate data', function () {