From b79027fe601ecccb1373ba399419e14f5ec2d7ff Mon Sep 17 00:00:00 2001 From: Niels Klomp Date: Wed, 27 Sep 2023 04:24:33 +0200 Subject: [PATCH] fix: Fix credential offer matching against metadata --- .../lib/functions/IssuerMetadataUtils.ts | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/packages/common/lib/functions/IssuerMetadataUtils.ts b/packages/common/lib/functions/IssuerMetadataUtils.ts index 0e6a0cc3..342ade39 100644 --- a/packages/common/lib/functions/IssuerMetadataUtils.ts +++ b/packages/common/lib/functions/IssuerMetadataUtils.ts @@ -7,8 +7,8 @@ import { IssuerMetadataV1_0_08, MetadataDisplay, OID4VCICredentialFormat, - OpenId4VCIVersion, -} from '../types'; + OpenId4VCIVersion +} from '../types' export function getSupportedCredentials(opts?: { issuerMetadata?: CredentialIssuerMetadata | IssuerMetadataV1_0_08; @@ -25,7 +25,7 @@ export function getSupportedCredentials(opts?: { export function getSupportedCredential(opts?: { issuerMetadata?: CredentialIssuerMetadata | IssuerMetadataV1_0_08; version: OpenId4VCIVersion; - types?: string[]; + types?: string | string[]; format?: (OID4VCICredentialFormat | string) | (OID4VCICredentialFormat | string)[]; }): CredentialSupported[] { const { issuerMetadata } = opts ?? {}; @@ -56,20 +56,30 @@ export function getSupportedCredential(opts?: { /** * the following (not array part is a legacy code from version 1_0-08 which JFF plugfest 2 implementors used) */ - const initiationTypes = version === OpenId4VCIVersion.VER_1_0_08 && !types ? formats : types; - const supportedFormats: (CredentialOfferFormat | string)[] = formats ?? ['jwt_vc_json', 'jwt_vc_json-ld', 'ldp_vc']; + let initiationTypes :string[] | undefined + if (opts?.types) { + if (typeof opts.types === 'string') { + initiationTypes = [opts.types] + } else { + initiationTypes = opts.types + } + } + if (version === OpenId4VCIVersion.VER_1_0_08 && (!initiationTypes || initiationTypes?.length === 0)) { + initiationTypes = formats + } + const supportedFormats: (CredentialOfferFormat | string)[] = formats && formats.length > 0 ? formats : ['jwt_vc_json', 'jwt_vc_json-ld', 'ldp_vc']; const credentialSupportedOverlap: CredentialSupported[] = []; - if (initiationTypes.length === 1) { + if (initiationTypes && Array.isArray(initiationTypes) && initiationTypes.length === 1) { const supported = credentialsSupported.filter( - (sup) => sup.id === initiationTypes[0] || (arrayEqualsIgnoreOrder(sup.types, initiationTypes) && sup.types.includes(initiationTypes[0])), + (sup) => sup.id === initiationTypes![0] || (arrayEqualsIgnoreOrder(sup.types, initiationTypes!) && sup.types.includes(initiationTypes![0])), ); if (supported) { credentialSupportedOverlap.push(...supported); } } else { // Make sure we include Verifiable Credential both on the offer side as well as in the metadata side, to ensure consistency of the issuer does not. - if (!initiationTypes.includes('VerifiableCredential')) { + if (initiationTypes && !initiationTypes.includes('VerifiableCredential')) { initiationTypes.push('VerifiableCredential'); } const supported = credentialsSupported.filter((sup) => { @@ -77,7 +87,7 @@ export function getSupportedCredential(opts?: { if (!supTypes.includes('VerifiableCredential')) { supTypes.push('VerifiableCredential'); } - return arrayEqualsIgnoreOrder(supTypes, initiationTypes) && supportedFormats.includes(sup.format); + return (!initiationTypes || arrayEqualsIgnoreOrder(supTypes, initiationTypes)) && supportedFormats.includes(sup.format); }); if (supported) { credentialSupportedOverlap.push(...supported);