Skip to content

Commit

Permalink
feat: Unify how we get types from different spec versions
Browse files Browse the repository at this point in the history
  • Loading branch information
nklomp committed Jun 16, 2024
1 parent 039b622 commit 449364b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 39 deletions.
13 changes: 2 additions & 11 deletions packages/client/lib/OpenID4VCIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
getIssuerFromCredentialOfferPayload,
getSupportedCredentials,
getTypesFromCredentialSupported,
getTypesFromObject,
JWK,
KID_JWK_X5C_ERROR,
NotificationRequest,
Expand Down Expand Up @@ -482,17 +483,7 @@ export class OpenID4VCIClient {
result[0] = types;
return result;
} else if (this.credentialOffer.version < OpenId4VCIVersion.VER_1_0_13) {
return (this.credentialOffer.credential_offer as CredentialOfferPayloadV1_0_11).credentials.map((c) => {
if (typeof c === 'string') {
return [c];
} else if ('types' in c) {
return c.types;
} else if ('vct' in c) {
return [c.vct];
} else {
return c.credential_definition.types;
}
});
return (this.credentialOffer.credential_offer as CredentialOfferPayloadV1_0_11).credentials.map((c) => getTypesFromObject(c) ?? []);
}
// we don't have this for v13. v13 only has credential_configuration_ids which is not translatable to type
return undefined;
Expand Down
17 changes: 4 additions & 13 deletions packages/client/lib/OpenID4VCIClientV1_0_11.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
getIssuerFromCredentialOfferPayload,
getSupportedCredentials,
getTypesFromCredentialSupported,
getTypesFromObject,
JWK,
KID_JWK_X5C_ERROR,
OID4VCICredentialFormat,
Expand Down Expand Up @@ -479,20 +480,10 @@ export class OpenID4VCIClientV1_0_11 {
result[0] = types;
return result;
} else if (this.credentialOffer.version < OpenId4VCIVersion.VER_1_0_13) {
return (this.credentialOffer.credential_offer as CredentialOfferPayloadV1_0_11).credentials.map((c) => {
if (typeof c === 'string') {
return [c];
} else if ('types' in c) {
return c.types;
} else if ('vct' in c) {
return [c.vct];
} else {
return c.credential_definition.types;
}
});
return (this.credentialOffer.credential_offer as CredentialOfferPayloadV1_0_11).credentials.map((c) => getTypesFromObject(c) ?? []);
}
// we don't have this for v13. v13 only has credential_configuration_ids which is not translatable to type
return [];
// we don't support > V11
throw Error(`This class only supports version 11 and lower! Version: ${this.version()}`);
}

issuerSupportedFlowTypes(): AuthzFlowType[] {
Expand Down
53 changes: 38 additions & 15 deletions packages/common/lib/functions/IssuerMetadataUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { VCI_LOG_COMMON } from '../index';
import { CredentialDefinitionV1_0_13, CredentialOfferFormat, JsonLdIssuerCredentialDefinition, VCI_LOG_COMMON } from '../index';
import {
AuthorizationServerMetadata,
CredentialConfigurationSupported,
Expand Down Expand Up @@ -79,17 +79,22 @@ export function getSupportedCredential(opts?: {

function filterMatchingConfig(config: CredentialConfigurationSupported): CredentialConfigurationSupported | undefined {
let isTypeMatch = normalizedTypes.length === 0;
const types = getTypesFromObject(config);
if (!isTypeMatch) {
if (normalizedTypes.length === 1 && config.id === normalizedTypes[0]) {
isTypeMatch = true;
} else if ('credential_definition' in config) {
isTypeMatch = normalizedTypes.some((type) => config.credential_definition.type?.includes(type));
} else if ('type' in config && Array.isArray(config.type)) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
isTypeMatch = normalizedTypes.some((type) => config.type.includes(type));
} else if ('types' in config) {
isTypeMatch = normalizedTypes.some((type) => config.types?.includes(type));
} else if (types) {
isTypeMatch = normalizedTypes.some((type) => types.includes(type));
} else {
if ('credential_definition' in config) {
isTypeMatch = normalizedTypes.some((type) => config.credential_definition.type?.includes(type));
} else if ('type' in config && Array.isArray(config.type)) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
isTypeMatch = normalizedTypes.some((type) => config.type.includes(type));
} else if ('types' in config) {
isTypeMatch = normalizedTypes.some((type) => config.types?.includes(type));
}
}
}

Expand Down Expand Up @@ -129,12 +134,7 @@ export function getTypesFromCredentialSupported(
credentialSupported.format === 'jwt_vc_json-ld' ||
credentialSupported.format === 'ldp_vc'
) {
types =
(credentialSupported.types
? (credentialSupported.types as string[])
: 'credential_definition' in credentialSupported
? credentialSupported.credential_definition?.type
: []) ?? [];
types = getTypesFromObject(credentialSupported) ?? [];
} else if (credentialSupported.format === 'vc+sd-jwt') {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Expand Down Expand Up @@ -204,3 +204,26 @@ export function getIssuerName(
}
return url;
}

/**
* The specs had many places where types could be expressed. This method ensures we get them in any way possible
* @param subject
*/
export function getTypesFromObject(
subject: CredentialConfigurationSupported | CredentialOfferFormat | CredentialDefinitionV1_0_13 | JsonLdIssuerCredentialDefinition | string,
): string[] | undefined {
if (typeof subject === 'string') {
return [subject];
} else if ('credential_definition' in subject && subject.credential_definition) {
return getTypesFromObject(subject.credential_definition);
} else if ('types' in subject && subject.types) {
return Array.isArray(subject.types) ? subject.types : [subject.types];
} else if ('type' in subject && subject.type) {
return Array.isArray(subject.type) ? subject.type : [subject.type];
} else if ('vct' in subject && subject.vct) {
return [subject.vct];
}

VCI_LOG_COMMON.warning('Could not deduce credential types. Probably a failure down the line will happen!');
return undefined;
}

0 comments on commit 449364b

Please sign in to comment.