1+ import { VCI_LOG_COMMON } from '../index' ;
12import {
23 AuthorizationServerMetadata ,
34 CredentialConfigurationSupported ,
@@ -11,59 +12,96 @@ import {
1112 OpenId4VCIVersion ,
1213} from '../types' ;
1314
14- export function getSupportedCredentials ( options ?: {
15+ export function getSupportedCredentials ( opts ?: {
1516 issuerMetadata ?: CredentialIssuerMetadata | IssuerMetadata ;
1617 version : OpenId4VCIVersion ;
1718 types ?: string [ ] [ ] ;
1819 format ?: OID4VCICredentialFormat | string | ( OID4VCICredentialFormat | string ) [ ] ;
19- } ) : Record < string , CredentialConfigurationSupportedV1_0_13 > {
20- if ( options ?. types && Array . isArray ( options . types ) ) {
21- return options . types
22- . map ( ( typeSet ) => {
23- return getSupportedCredential ( { ...options , types : typeSet } ) ;
24- } )
25- . reduce (
26- ( acc , result ) => {
27- Object . assign ( acc , result ) ;
28- return acc ;
29- } ,
30- { } as Record < string , CredentialConfigurationSupportedV1_0_13 > ,
31- ) ;
20+ } ) : Record < string , CredentialConfigurationSupportedV1_0_13 > | Array < CredentialConfigurationSupported > {
21+ const { version = OpenId4VCIVersion . VER_1_0_13 , types} = opts ?? { }
22+ if ( types && Array . isArray ( types ) ) {
23+ if ( version < OpenId4VCIVersion . VER_1_0_13 ) {
24+ return types . flatMap ( typeSet => getSupportedCredential ( { ...opts , version, types : typeSet } ) as Array < CredentialConfigurationSupported > ) ;
25+ } else {
26+ return types
27+ . map ( ( typeSet ) => {
28+ return getSupportedCredential ( { ...opts , version, types : typeSet } ) ;
29+ } )
30+ . reduce (
31+ ( acc , result ) => {
32+ Object . assign ( acc , result ) ;
33+ return acc ;
34+ } ,
35+ { } as Record < string , CredentialConfigurationSupportedV1_0_13 > ,
36+ ) ;
37+ }
3238 }
3339
34- return getSupportedCredential ( options ? { ...options , types : undefined } : undefined ) ;
40+ return getSupportedCredential ( opts ? { ...opts , types : undefined } : undefined ) ;
3541}
3642
3743export function getSupportedCredential ( opts ?: {
3844 issuerMetadata ?: CredentialIssuerMetadata | IssuerMetadata ;
3945 version : OpenId4VCIVersion ;
4046 types ?: string | string [ ] ;
4147 format ?: OID4VCICredentialFormat | string | ( OID4VCICredentialFormat | string ) [ ] ;
42- } ) : Record < string , CredentialConfigurationSupportedV1_0_13 > {
43- const { issuerMetadata, types, format } = opts ?? { } ;
48+ } ) : Record < string , CredentialConfigurationSupportedV1_0_13 > | Array < CredentialConfigurationSupported > {
49+ const { issuerMetadata, types, format, version = OpenId4VCIVersion . VER_1_0_13 } = opts ?? { } ;
4450
45- if ( ! issuerMetadata || ! issuerMetadata . credential_configurations_supported ) {
46- return { } ;
51+ let credentialConfigurationsV11 : Array < CredentialConfigurationSupported > | undefined = undefined ;
52+ let credentialConfigurationsV13 : Record < string , CredentialConfigurationSupportedV1_0_13 > | undefined = undefined ;
53+ if ( version < OpenId4VCIVersion . VER_1_0_12 || issuerMetadata ?. credentials_supported ) {
54+ credentialConfigurationsV11 = issuerMetadata ?. credential_supported ?? [ ] ;
55+ } else {
56+ credentialConfigurationsV13 =
57+ ( issuerMetadata ?. credential_configurations_supported as Record < string , CredentialConfigurationSupportedV1_0_13 > ) ?? { } ;
58+ }
59+ if ( ! issuerMetadata || ( ! issuerMetadata . credential_configurations_supported && ! issuerMetadata . credentials_supported ) ) {
60+ VCI_LOG_COMMON . warning ( `No credential issuer metadata or supported credentials found for issuer}` ) ;
61+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
62+ return version < OpenId4VCIVersion . VER_1_0_13 ? credentialConfigurationsV11 ! : credentialConfigurationsV13 ! ;
4763 }
4864
49- const credentialConfigurations : Record < string , CredentialConfigurationSupportedV1_0_13 > =
50- issuerMetadata . credential_configurations_supported as Record < string , CredentialConfigurationSupportedV1_0_13 > ;
5165 const normalizedTypes : string [ ] = Array . isArray ( types ) ? types : types ? [ types ] : [ ] ;
5266 const normalizedFormats : string [ ] = Array . isArray ( format ) ? format : format ? [ format ] : [ ] ;
5367
54- return Object . entries ( credentialConfigurations ) . reduce (
55- ( filteredConfigs , [ id , config ] ) => {
56- const isTypeMatch = normalizedTypes . length === 0 || normalizedTypes . some ( ( type ) => config . credential_definition . type ?. includes ( type ) ) ;
57- const isFormatMatch = normalizedFormats . length === 0 || normalizedFormats . includes ( config . format ) ;
58-
59- if ( isTypeMatch && isFormatMatch ) {
60- filteredConfigs [ id ] = config ;
68+ function filterMatchingConfig ( config : CredentialConfigurationSupported ) : CredentialConfigurationSupported | undefined {
69+ let isTypeMatch = normalizedTypes . length === 0 ;
70+ if ( ! isTypeMatch ) {
71+ if ( 'credential_definition' in config ) {
72+ isTypeMatch = normalizedTypes . some ( ( type ) => config . credential_definition . type ?. includes ( type ) ) ;
73+ } else if ( 'type' in config && Array . isArray ( config . type ) ) {
74+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
75+ // @ts -ignore
76+ isTypeMatch = normalizedTypes . some ( ( type ) => config . type . includes ( type ) ) ;
77+ } else if ( 'types' in config ) {
78+ isTypeMatch = normalizedTypes . some ( ( type ) => config . types ?. includes ( type ) ) ;
6179 }
80+ }
6281
63- return filteredConfigs ;
64- } ,
65- { } as Record < string , CredentialConfigurationSupportedV1_0_13 > ,
66- ) ;
82+ const isFormatMatch = normalizedFormats . length === 0 || normalizedFormats . includes ( config . format ) ;
83+
84+ return isTypeMatch && isFormatMatch ? config : undefined ;
85+ }
86+
87+ if ( credentialConfigurationsV13 ) {
88+ return Object . entries ( credentialConfigurationsV13 ) . reduce (
89+ ( filteredConfigs , [ id , config ] ) => {
90+ if ( filterMatchingConfig ( config ) ) {
91+ filteredConfigs [ id ] = config ;
92+ // Added to enable support < 13. We basically assign the
93+ if ( ! config . id ) {
94+ config . id = id ;
95+ }
96+ }
97+ return filteredConfigs ;
98+ } ,
99+ { } as Record < string , CredentialConfigurationSupportedV1_0_13 > ,
100+ ) ;
101+ } else if ( credentialConfigurationsV11 ) {
102+ return credentialConfigurationsV11 . filter ( ( config ) => filterMatchingConfig ( config ) ) ;
103+ }
104+ throw Error ( `Either < v11 configurations or V13 configurations should have been filtered at this point` ) ;
67105}
68106
69107export function getTypesFromCredentialSupported (
0 commit comments