|
1 | | -import { VCI_LOG_COMMON } from '../index'; |
| 1 | +import { CredentialDefinitionV1_0_13, CredentialOfferFormat, JsonLdIssuerCredentialDefinition, VCI_LOG_COMMON } from '../index'; |
2 | 2 | import { |
3 | 3 | AuthorizationServerMetadata, |
4 | 4 | CredentialConfigurationSupported, |
@@ -79,17 +79,22 @@ export function getSupportedCredential(opts?: { |
79 | 79 |
|
80 | 80 | function filterMatchingConfig(config: CredentialConfigurationSupported): CredentialConfigurationSupported | undefined { |
81 | 81 | let isTypeMatch = normalizedTypes.length === 0; |
| 82 | + const types = getTypesFromObject(config); |
82 | 83 | if (!isTypeMatch) { |
83 | 84 | if (normalizedTypes.length === 1 && config.id === normalizedTypes[0]) { |
84 | 85 | isTypeMatch = true; |
85 | | - } else if ('credential_definition' in config) { |
86 | | - isTypeMatch = normalizedTypes.some((type) => config.credential_definition.type?.includes(type)); |
87 | | - } else if ('type' in config && Array.isArray(config.type)) { |
88 | | - // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
89 | | - // @ts-ignore |
90 | | - isTypeMatch = normalizedTypes.some((type) => config.type.includes(type)); |
91 | | - } else if ('types' in config) { |
92 | | - isTypeMatch = normalizedTypes.some((type) => config.types?.includes(type)); |
| 86 | + } else if (types) { |
| 87 | + isTypeMatch = normalizedTypes.some((type) => types.includes(type)); |
| 88 | + } else { |
| 89 | + if ('credential_definition' in config) { |
| 90 | + isTypeMatch = normalizedTypes.some((type) => config.credential_definition.type?.includes(type)); |
| 91 | + } else if ('type' in config && Array.isArray(config.type)) { |
| 92 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 93 | + // @ts-ignore |
| 94 | + isTypeMatch = normalizedTypes.some((type) => config.type.includes(type)); |
| 95 | + } else if ('types' in config) { |
| 96 | + isTypeMatch = normalizedTypes.some((type) => config.types?.includes(type)); |
| 97 | + } |
93 | 98 | } |
94 | 99 | } |
95 | 100 |
|
@@ -129,12 +134,7 @@ export function getTypesFromCredentialSupported( |
129 | 134 | credentialSupported.format === 'jwt_vc_json-ld' || |
130 | 135 | credentialSupported.format === 'ldp_vc' |
131 | 136 | ) { |
132 | | - types = |
133 | | - (credentialSupported.types |
134 | | - ? (credentialSupported.types as string[]) |
135 | | - : 'credential_definition' in credentialSupported |
136 | | - ? credentialSupported.credential_definition?.type |
137 | | - : []) ?? []; |
| 137 | + types = getTypesFromObject(credentialSupported) ?? []; |
138 | 138 | } else if (credentialSupported.format === 'vc+sd-jwt') { |
139 | 139 | // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
140 | 140 | // @ts-ignore |
@@ -204,3 +204,26 @@ export function getIssuerName( |
204 | 204 | } |
205 | 205 | return url; |
206 | 206 | } |
| 207 | + |
| 208 | +/** |
| 209 | + * The specs had many places where types could be expressed. This method ensures we get them in any way possible |
| 210 | + * @param subject |
| 211 | + */ |
| 212 | +export function getTypesFromObject( |
| 213 | + subject: CredentialConfigurationSupported | CredentialOfferFormat | CredentialDefinitionV1_0_13 | JsonLdIssuerCredentialDefinition | string, |
| 214 | +): string[] | undefined { |
| 215 | + if (typeof subject === 'string') { |
| 216 | + return [subject]; |
| 217 | + } else if ('credential_definition' in subject && subject.credential_definition) { |
| 218 | + return getTypesFromObject(subject.credential_definition); |
| 219 | + } else if ('types' in subject && subject.types) { |
| 220 | + return Array.isArray(subject.types) ? subject.types : [subject.types]; |
| 221 | + } else if ('type' in subject && subject.type) { |
| 222 | + return Array.isArray(subject.type) ? subject.type : [subject.type]; |
| 223 | + } else if ('vct' in subject && subject.vct) { |
| 224 | + return [subject.vct]; |
| 225 | + } |
| 226 | + |
| 227 | + VCI_LOG_COMMON.warning('Could not deduce credential types. Probably a failure down the line will happen!'); |
| 228 | + return undefined; |
| 229 | +} |
0 commit comments