|
| 1 | +import { |
| 2 | + convertJsonToURI, |
| 3 | + convertURIToJsonObject, |
| 4 | + CredentialOfferRequestWithBaseUrl, |
| 5 | + CredentialOfferV1_0_13, |
| 6 | + determineSpecVersionFromURI, |
| 7 | + getClientIdFromCredentialOfferPayload, |
| 8 | + OpenId4VCIVersion, |
| 9 | + toUniformCredentialOfferRequest, |
| 10 | +} from '@sphereon/oid4vci-common'; |
| 11 | +import Debug from 'debug'; |
| 12 | + |
| 13 | +const debug = Debug('sphereon:oid4vci:offer'); |
| 14 | + |
| 15 | +export class CredentialOfferClientV1_0_13 { |
| 16 | + public static async fromURI(uri: string, opts?: { resolve?: boolean }): Promise<CredentialOfferRequestWithBaseUrl> { |
| 17 | + debug(`Credential Offer URI: ${uri}`); |
| 18 | + if (!uri.includes('?') || !uri.includes('://')) { |
| 19 | + debug(`Invalid Credential Offer URI: ${uri}`); |
| 20 | + throw Error(`Invalid Credential Offer Request`); |
| 21 | + } |
| 22 | + const scheme = uri.split('://')[0]; |
| 23 | + const baseUrl = uri.split('?')[0]; |
| 24 | + const version = determineSpecVersionFromURI(uri); |
| 25 | + const credentialOffer = convertURIToJsonObject(uri, { |
| 26 | + // It must have the '=' sign after credential_offer otherwise the uri will get split at openid_credential_offer |
| 27 | + arrayTypeProperties: uri.includes('credential_offer_uri=') |
| 28 | + ? ['credential_configuration_ids', 'credential_offer_uri='] |
| 29 | + : ['credential_configuration_ids', 'credential_offer='], |
| 30 | + requiredProperties: uri.includes('credential_offer_uri=') ? ['credential_offer_uri='] : ['credential_offer='], |
| 31 | + }) as CredentialOfferV1_0_13; |
| 32 | + if (credentialOffer?.credential_offer_uri === undefined && !credentialOffer?.credential_offer) { |
| 33 | + throw Error('Either a credential_offer or credential_offer_uri should be present in ' + uri); |
| 34 | + } |
| 35 | + |
| 36 | + const request = await toUniformCredentialOfferRequest(credentialOffer, { |
| 37 | + ...opts, |
| 38 | + version, |
| 39 | + }); |
| 40 | + const clientId = getClientIdFromCredentialOfferPayload(request.credential_offer); |
| 41 | + const grants = request.credential_offer?.grants; |
| 42 | + |
| 43 | + return { |
| 44 | + scheme, |
| 45 | + baseUrl, |
| 46 | + ...(clientId && { clientId }), |
| 47 | + ...request, |
| 48 | + ...(grants?.authorization_code?.issuer_state && { issuerState: grants.authorization_code.issuer_state }), |
| 49 | + ...(grants?.['urn:ietf:params:oauth:grant-type:pre-authorized_code']?.['pre-authorized_code'] && { |
| 50 | + preAuthorizedCode: grants['urn:ietf:params:oauth:grant-type:pre-authorized_code']['pre-authorized_code'], |
| 51 | + }), |
| 52 | + userPinRequired: !!request.credential_offer?.grants?.['urn:ietf:params:oauth:grant-type:pre-authorized_code']?.tx_code ?? false, |
| 53 | + ...(request.credential_offer?.grants?.['urn:ietf:params:oauth:grant-type:pre-authorized_code']?.tx_code && |
| 54 | + { |
| 55 | + // txCode: request.credential_offer?.grants?.['urn:ietf:params:oauth:grant-type:pre-authorized_code']?.tx_code, |
| 56 | + }), |
| 57 | + }; |
| 58 | + } |
| 59 | + |
| 60 | + public static toURI( |
| 61 | + requestWithBaseUrl: CredentialOfferRequestWithBaseUrl, |
| 62 | + opts?: { |
| 63 | + version?: OpenId4VCIVersion; |
| 64 | + }, |
| 65 | + ): string { |
| 66 | + debug(`Credential Offer Request with base URL: ${JSON.stringify(requestWithBaseUrl)}`); |
| 67 | + const version = opts?.version ?? requestWithBaseUrl.version; |
| 68 | + let baseUrl = requestWithBaseUrl.baseUrl.includes(requestWithBaseUrl.scheme) |
| 69 | + ? requestWithBaseUrl.baseUrl |
| 70 | + : `${requestWithBaseUrl.scheme.replace('://', '')}://${requestWithBaseUrl.baseUrl}`; |
| 71 | + let param: string | undefined; |
| 72 | + |
| 73 | + const isUri = requestWithBaseUrl.credential_offer_uri !== undefined; |
| 74 | + |
| 75 | + if (version.valueOf() >= OpenId4VCIVersion.VER_1_0_11.valueOf()) { |
| 76 | + // v11 changed from encoding every param to a encoded json object with a credential_offer param key |
| 77 | + if (!baseUrl.includes('?')) { |
| 78 | + param = isUri ? 'credential_offer_uri' : 'credential_offer'; |
| 79 | + } else { |
| 80 | + const split = baseUrl.split('?'); |
| 81 | + if (split.length > 1 && split[1] !== '') { |
| 82 | + if (baseUrl.endsWith('&')) { |
| 83 | + param = isUri ? 'credential_offer_uri' : 'credential_offer'; |
| 84 | + } else if (!baseUrl.endsWith('=')) { |
| 85 | + baseUrl += `&`; |
| 86 | + param = isUri ? 'credential_offer_uri' : 'credential_offer'; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + return convertJsonToURI(requestWithBaseUrl.credential_offer_uri ?? requestWithBaseUrl.original_credential_offer, { |
| 92 | + baseUrl, |
| 93 | + arrayTypeProperties: isUri ? [] : ['credential_type'], |
| 94 | + uriTypeProperties: isUri |
| 95 | + ? ['credential_offer_uri'] |
| 96 | + : version >= OpenId4VCIVersion.VER_1_0_13 |
| 97 | + ? ['credential_issuer', 'credential_type'] |
| 98 | + : ['issuer', 'credential_type'], |
| 99 | + param, |
| 100 | + version, |
| 101 | + }); |
| 102 | + } |
| 103 | +} |
0 commit comments