@openid4vc/openid4vci 0.4.5 → 0.5.0-alpha-20260202131209

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1732,6 +1732,40 @@ declare const zCredentialRequestProofs: z.ZodObject<{
1732
1732
  type CredentialRequestProof = z.infer<typeof zCredentialRequestProof>;
1733
1733
  type CredentialRequestProofsFormatSpecific = z.infer<typeof zCredentialRequestProofs>;
1734
1734
  type CredentialRequestProofs = z.infer<typeof zCredentialRequestProofs>;
1735
+ declare const zCredentialResponseEncryption: z.ZodObject<{
1736
+ jwk: z.ZodObject<{
1737
+ kty: z.ZodString;
1738
+ crv: z.ZodOptional<z.ZodString>;
1739
+ x: z.ZodOptional<z.ZodString>;
1740
+ y: z.ZodOptional<z.ZodString>;
1741
+ e: z.ZodOptional<z.ZodString>;
1742
+ n: z.ZodOptional<z.ZodString>;
1743
+ alg: z.ZodOptional<z.ZodString>;
1744
+ d: z.ZodOptional<z.ZodString>;
1745
+ dp: z.ZodOptional<z.ZodString>;
1746
+ dq: z.ZodOptional<z.ZodString>;
1747
+ ext: z.ZodOptional<z.ZodBoolean>;
1748
+ k: z.ZodOptional<z.ZodString>;
1749
+ key_ops: z.ZodOptional<z.ZodArray<z.ZodString>>;
1750
+ kid: z.ZodOptional<z.ZodString>;
1751
+ oth: z.ZodOptional<z.ZodArray<z.ZodObject<{
1752
+ d: z.ZodOptional<z.ZodString>;
1753
+ r: z.ZodOptional<z.ZodString>;
1754
+ t: z.ZodOptional<z.ZodString>;
1755
+ }, z.core.$loose>>>;
1756
+ p: z.ZodOptional<z.ZodString>;
1757
+ q: z.ZodOptional<z.ZodString>;
1758
+ qi: z.ZodOptional<z.ZodString>;
1759
+ use: z.ZodOptional<z.ZodString>;
1760
+ x5c: z.ZodOptional<z.ZodArray<z.ZodString>>;
1761
+ x5t: z.ZodOptional<z.ZodString>;
1762
+ 'x5t#S256': z.ZodOptional<z.ZodString>;
1763
+ x5u: z.ZodOptional<z.ZodString>;
1764
+ }, z.core.$loose>;
1765
+ alg: z.ZodString;
1766
+ enc: z.ZodString;
1767
+ }, z.core.$loose>;
1768
+ type CredentialResponseEncryption = z.infer<typeof zCredentialResponseEncryption>;
1735
1769
  declare const zCredentialRequestCommon: z.ZodObject<{
1736
1770
  proof: z.ZodOptional<z.ZodUnion<readonly [z.ZodObject<{
1737
1771
  proof_type: z.ZodString;
@@ -2243,6 +2277,15 @@ interface ParseCredentialRequestReturn {
2243
2277
  * undefined you can still handle the request by using this object directly.
2244
2278
  */
2245
2279
  credentialRequest: CredentialRequest;
2280
+ /**
2281
+ * If the request includes `credential_response_encryption`, this contains the
2282
+ * encryption parameters the client expects the issuer to use for encrypting the response.
2283
+ *
2284
+ * When defined, the issuer should encrypt the credential response using the provided
2285
+ * JWK, algorithm, and content encryption algorithm, and return it with
2286
+ * `Content-Type: application/jwt`.
2287
+ */
2288
+ credentialResponseEncryption?: CredentialResponseEncryption;
2246
2289
  }
2247
2290
  //#endregion
2248
2291
  //#region ../oauth2/src/common/z-oauth2-error.d.ts
@@ -2323,9 +2366,9 @@ interface RetrieveCredentialsBaseOptions {
2323
2366
  */
2324
2367
  issuerMetadata: IssuerMetadataResult;
2325
2368
  /**
2326
- * Callback used in retrieve credentials endpoints
2369
+ * Callbacks used in retrieve credentials endpoints.
2327
2370
  */
2328
- callbacks: Pick<CallbackContext, 'fetch' | 'generateRandom' | 'hash' | 'signJwt'>;
2371
+ callbacks: Pick<CallbackContext, 'fetch' | 'generateRandom' | 'hash' | 'signJwt' | 'decryptJwe'>;
2329
2372
  /**
2330
2373
  * Access token authorized to retrieve the credential(s)
2331
2374
  */
@@ -2334,6 +2377,14 @@ interface RetrieveCredentialsBaseOptions {
2334
2377
  * DPoP options
2335
2378
  */
2336
2379
  dpop?: RequestDpopOptions;
2380
+ /**
2381
+ * Request encryption of the credential response. When provided, the client
2382
+ * includes `credential_response_encryption` in the request and expects
2383
+ * an encrypted JWE response.
2384
+ *
2385
+ * The `decryptJwe` callback must be provided to decrypt the response.
2386
+ */
2387
+ credentialResponseEncryption?: CredentialResponseEncryption;
2337
2388
  }
2338
2389
  interface RetrieveCredentialsWithFormatOptions extends RetrieveCredentialsBaseOptions {
2339
2390
  /**
@@ -3077,9 +3128,9 @@ declare enum AuthorizationFlow {
3077
3128
  }
3078
3129
  interface Openid4vciClientOptions {
3079
3130
  /**
3080
- * Callbacks required for the openid4vc client
3131
+ * Callbacks required for the openid4vc client.
3081
3132
  */
3082
- callbacks: Omit<CallbackContext, 'verifyJwt' | 'decryptJwe' | 'encryptJwe'>;
3133
+ callbacks: Omit<CallbackContext, 'verifyJwt' | 'encryptJwe'>;
3083
3134
  }
3084
3135
  declare class Openid4vciClient {
3085
3136
  private options;
@@ -3256,15 +3307,16 @@ declare class Openid4vciClient {
3256
3307
  credentialConfigurationId,
3257
3308
  additionalRequestPayload,
3258
3309
  accessToken,
3259
- dpop
3260
- }: Pick<RetrieveCredentialsWithFormatOptions, 'accessToken' | 'additionalRequestPayload' | 'issuerMetadata' | 'proof' | 'proofs' | 'dpop'> & {
3310
+ dpop,
3311
+ credentialResponseEncryption
3312
+ }: Pick<RetrieveCredentialsWithFormatOptions, 'accessToken' | 'additionalRequestPayload' | 'issuerMetadata' | 'proof' | 'proofs' | 'dpop' | 'credentialResponseEncryption'> & {
3261
3313
  credentialConfigurationId: string;
3262
3314
  }): Promise<RetrieveCredentialsResponseOk>;
3263
3315
  /**
3264
3316
  * @throws Openid4vciRetrieveCredentialsError - if an unsuccessful response or the response couldn't be parsed as credential response
3265
3317
  * @throws ValidationError - if validation of the credential request failed
3266
3318
  */
3267
- retrieveDeferredCredentials(options: Pick<RetrieveDeferredCredentialsOptions, 'issuerMetadata' | 'accessToken' | 'transactionId' | 'dpop' | 'additionalRequestPayload'>): Promise<RetrieveDeferredCredentialsResponseOk>;
3319
+ retrieveDeferredCredentials(options: Pick<RetrieveDeferredCredentialsOptions, 'issuerMetadata' | 'accessToken' | 'transactionId' | 'dpop' | 'additionalRequestPayload' | 'credentialResponseEncryption'>): Promise<RetrieveDeferredCredentialsResponseOk>;
3268
3320
  /**
3269
3321
  * @throws Openid4vciSendNotificationError - if an unsuccessful response
3270
3322
  * @throws ValidationError - if validation of the notification request failed
@@ -3278,6 +3330,89 @@ declare class Openid4vciClient {
3278
3330
  }: Pick<SendNotificationOptions, 'accessToken' | 'additionalRequestPayload' | 'issuerMetadata' | 'dpop' | 'notification'>): Promise<_openid4vc_oauth20.ResourceRequestResponseOk>;
3279
3331
  }
3280
3332
  //#endregion
3333
+ //#region src/credential-request/credential-response.d.ts
3334
+ interface CreateCredentialResponseOptions {
3335
+ credentialRequest: ParseCredentialRequestReturn;
3336
+ credential?: CredentialResponse['credential'];
3337
+ credentials?: CredentialResponse['credentials'];
3338
+ transactionId?: string;
3339
+ interval?: number;
3340
+ cNonce?: string;
3341
+ cNonceExpiresInSeconds?: number;
3342
+ notificationId?: string;
3343
+ /**
3344
+ * Additional payload to include in the credential response
3345
+ */
3346
+ additionalPayload?: Record<string, unknown>;
3347
+ /**
3348
+ * Encryption parameters from the credential request. When provided along with
3349
+ * the `encryptJwe` callback, the credential response will be encrypted as a JWE.
3350
+ *
3351
+ * You can pass `credentialRequest.credentialResponseEncryption` from the parsed
3352
+ * credential request directly to this option.
3353
+ */
3354
+ credentialResponseEncryption?: CredentialResponseEncryption;
3355
+ /**
3356
+ * Callbacks for credential response operations.
3357
+ * Required when `credentialResponseEncryption` is provided.
3358
+ */
3359
+ callbacks?: Pick<CallbackContext, 'encryptJwe'>;
3360
+ }
3361
+ interface CreateCredentialResponseReturn {
3362
+ /**
3363
+ * The credential response object (before encryption).
3364
+ */
3365
+ credentialResponse: CredentialResponse;
3366
+ /**
3367
+ * The credential response as a JWE string if encryption was requested.
3368
+ * When this is defined, the response should be returned with `Content-Type: application/jwt`.
3369
+ */
3370
+ credentialResponseJwt?: string;
3371
+ }
3372
+ type CreateDeferredCredentialResponseOptions = ({
3373
+ credentials: DeferredCredentialResponse['credentials'];
3374
+ notificationId?: string;
3375
+ transactionId?: never;
3376
+ interval?: never;
3377
+ } | {
3378
+ /**
3379
+ * The `transaction_id` used to identify the deferred issuance transaction.
3380
+ */
3381
+ transactionId: string;
3382
+ interval: number;
3383
+ credentials?: never;
3384
+ notificationId?: never;
3385
+ }) & {
3386
+ /**
3387
+ * Additional payload to include in the deferred credential response
3388
+ */
3389
+ additionalPayload?: Record<string, unknown>;
3390
+ /**
3391
+ * Encryption parameters from the deferred credential request. When provided along with
3392
+ * the `encryptJwe` callback, the deferred credential response will be encrypted as a JWE.
3393
+ *
3394
+ * You can pass `deferredCredentialRequest.credential_response_encryption` from the parsed
3395
+ * deferred credential request directly to this option.
3396
+ */
3397
+ credentialResponseEncryption?: CredentialResponseEncryption;
3398
+ /**
3399
+ * Callbacks for credential response operations.
3400
+ * Required when `credentialResponseEncryption` is provided.
3401
+ */
3402
+ callbacks?: Pick<CallbackContext, 'encryptJwe'>;
3403
+ };
3404
+ interface CreateDeferredCredentialResponseReturn {
3405
+ /**
3406
+ * The deferred credential response object (before encryption).
3407
+ */
3408
+ deferredCredentialResponse: DeferredCredentialResponse;
3409
+ /**
3410
+ * The deferred credential response as a JWE string if encryption was requested.
3411
+ * When this is defined, the response should be returned with `Content-Type: application/jwt`.
3412
+ */
3413
+ deferredCredentialResponseJwt?: string;
3414
+ }
3415
+ //#endregion
3281
3416
  //#region ../oauth2/src/common/jwk/z-jwk.d.ts
3282
3417
  declare const zJwk$1: z.ZodObject<{
3283
3418
  kty: z.ZodString;
@@ -3646,41 +3781,6 @@ interface VerifyClientAttestationOptions {
3646
3781
  now?: Date;
3647
3782
  }
3648
3783
  //#endregion
3649
- //#region src/credential-request/credential-response.d.ts
3650
- interface CreateCredentialResponseOptions {
3651
- credentialRequest: ParseCredentialRequestReturn;
3652
- credential?: CredentialResponse['credential'];
3653
- credentials?: CredentialResponse['credentials'];
3654
- transactionId?: string;
3655
- interval?: number;
3656
- cNonce?: string;
3657
- cNonceExpiresInSeconds?: number;
3658
- notificationId?: string;
3659
- /**
3660
- * Additional payload to include in the credential response
3661
- */
3662
- additionalPayload?: Record<string, unknown>;
3663
- }
3664
- type CreateDeferredCredentialResponseOptions = ({
3665
- credentials: DeferredCredentialResponse['credentials'];
3666
- notificationId?: string;
3667
- transactionId?: never;
3668
- interval?: never;
3669
- } | {
3670
- /**
3671
- * The `transaction_id` used to identify the deferred issuance transaction.
3672
- */
3673
- transactionId: string;
3674
- interval: number;
3675
- credentials?: never;
3676
- notificationId?: never;
3677
- }) & {
3678
- /**
3679
- * Additional payload to include in the deferred credential response
3680
- */
3681
- additionalPayload?: Record<string, unknown>;
3682
- };
3683
- //#endregion
3684
3784
  //#region src/credential-request/parse-deferred-credential-request.d.ts
3685
3785
  interface ParseDeferredCredentialRequestOptions {
3686
3786
  deferredCredentialRequest: Record<string, unknown>;
@@ -3732,9 +3832,9 @@ interface CreateSignedCredentialIssuerMetadataJwtOptions {
3732
3832
  //#region src/Openid4vciIssuer.d.ts
3733
3833
  interface Openid4vciIssuerOptions {
3734
3834
  /**
3735
- * Callbacks required for the openid4vc issuer
3835
+ * Callbacks required for the openid4vc issuer.
3736
3836
  */
3737
- callbacks: Omit<CallbackContext, 'decryptJwe' | 'encryptJwe'>;
3837
+ callbacks: Omit<CallbackContext, 'decryptJwe'>;
3738
3838
  }
3739
3839
  declare class Openid4vciIssuer {
3740
3840
  private options;
@@ -4229,31 +4329,14 @@ declare class Openid4vciIssuer {
4229
4329
  parseDeferredCredentialRequest(options: ParseDeferredCredentialRequestOptions): ParseDeferredCredentialRequestReturn;
4230
4330
  /**
4231
4331
  * @throws ValidationError - when validation of the credential response fails
4332
+ * @throws Openid4vciError - when encryption is requested but no encryptJwe callback is available
4232
4333
  */
4233
- createCredentialResponse(options: CreateCredentialResponseOptions): {
4234
- [x: string]: unknown;
4235
- credentials?: {
4236
- credential: string | Record<string, any>;
4237
- }[] | (string | Record<string, any>)[] | undefined;
4238
- notification_id?: string | undefined;
4239
- transaction_id?: string | undefined;
4240
- interval?: number | undefined;
4241
- credential?: string | Record<string, any> | undefined;
4242
- c_nonce?: string | undefined;
4243
- c_nonce_expires_in?: number | undefined;
4244
- };
4334
+ createCredentialResponse(options: Omit<CreateCredentialResponseOptions, 'callbacks'>): Promise<CreateCredentialResponseReturn>;
4245
4335
  /**
4246
4336
  * @throws ValidationError - when validation of the credential response fails
4337
+ * @throws Openid4vciError - when encryption is requested but no encryptJwe callback is available
4247
4338
  */
4248
- createDeferredCredentialResponse(options: CreateDeferredCredentialResponseOptions): {
4249
- [x: string]: unknown;
4250
- credentials?: {
4251
- credential: string | Record<string, any>;
4252
- }[] | (string | Record<string, any>)[] | undefined;
4253
- notification_id?: string | undefined;
4254
- transaction_id?: string | undefined;
4255
- interval?: number | undefined;
4256
- };
4339
+ createDeferredCredentialResponse(options: Omit<CreateDeferredCredentialResponseOptions, 'callbacks'>): Promise<CreateDeferredCredentialResponseReturn>;
4257
4340
  /**
4258
4341
  * @throws ValidationError - when validation of the nonce response fails
4259
4342
  */
@@ -4464,5 +4547,5 @@ declare class Openid4vciWalletProvider {
4464
4547
  createKeyAttestationJwt(options: Omit<CreateKeyAttestationJwtOptions, 'callbacks'>): Promise<string>;
4465
4548
  }
4466
4549
  //#endregion
4467
- export { AuthorizationFlow, type CreateKeyAttestationJwtOptions, type CredentialConfigurationSupported, type CredentialConfigurationSupportedWithFormats, type CredentialConfigurationsSupported, type CredentialConfigurationsSupportedWithFormats, type CredentialErrorResponse, type CredentialFormatIdentifier, type CredentialIssuerMetadata, type CredentialIssuerMetadataDisplayEntry, type CredentialOfferAuthorizationCodeGrant, type CredentialOfferGrants, type CredentialOfferObject, type CredentialOfferPreAuthorizedCodeGrant, type CredentialOfferPreAuthorizedCodeGrantTxCode, type CredentialRequest, type CredentialRequestFormatSpecific, type CredentialRequestJwtProofTypeHeader, type CredentialRequestJwtProofTypePayload, type CredentialRequestWithFormats, type CredentialResponse, type DeferredCredentialRequest, type DeferredCredentialResponse, type ExtractScopesForCredentialConfigurationIdsOptions, type GetCredentialConfigurationsMatchingRequestFormatOptions, type IssuerMetadataResult, type JwtProofTypeIdentifier, type JwtVcJsonFormatIdentifier, type JwtVcJsonLdFormatIdentifier, type LdpVcFormatIdentifier, type LegacySdJwtVcFormatIdentifier, type MsoMdocFormatIdentifier, type NonceResponse, type NotificationErrorResponse, type NotificationEvent, type Oid4vcTsConfig, Openid4vciClient, type Openid4vciClientOptions, Openid4vciDraftVersion, Openid4vciError, type Openid4vciErrorOptions, Openid4vciIssuer, type Openid4vciIssuerOptions, Openid4vciRetrieveCredentialsError, Openid4vciSendNotificationError, Openid4vciVersion, Openid4vciWalletProvider, type Openid4vciWalletProviderOptions, type ParseCredentialRequestReturn, type ParseKeyAttestationJwtOptions, type ProofTypeIdentifier, type RetrieveCredentialsResponseNotOk, type RetrieveCredentialsResponseOk, type SendNotificationResponseNotOk, type SendNotificationResponseOk, type VerifyKeyAttestationJwtOptions, type VerifyKeyAttestationJwtReturn, createKeyAttestationJwt, credentialsSupportedToCredentialConfigurationsSupported, determineAuthorizationServerForCredentialOffer, extractScopesForCredentialConfigurationIds, getCredentialConfigurationsMatchingRequestFormat, getGlobalConfig, parseKeyAttestationJwt, setGlobalConfig, verifyKeyAttestationJwt };
4550
+ export { AuthorizationFlow, type CreateKeyAttestationJwtOptions, type CredentialConfigurationSupported, type CredentialConfigurationSupportedWithFormats, type CredentialConfigurationsSupported, type CredentialConfigurationsSupportedWithFormats, type CredentialErrorResponse, type CredentialFormatIdentifier, type CredentialIssuerMetadata, type CredentialIssuerMetadataDisplayEntry, type CredentialOfferAuthorizationCodeGrant, type CredentialOfferGrants, type CredentialOfferObject, type CredentialOfferPreAuthorizedCodeGrant, type CredentialOfferPreAuthorizedCodeGrantTxCode, type CredentialRequest, type CredentialRequestFormatSpecific, type CredentialRequestJwtProofTypeHeader, type CredentialRequestJwtProofTypePayload, type CredentialRequestWithFormats, type CredentialResponse, type CredentialResponseEncryption, type DeferredCredentialRequest, type DeferredCredentialResponse, type ExtractScopesForCredentialConfigurationIdsOptions, type GetCredentialConfigurationsMatchingRequestFormatOptions, type IssuerMetadataResult, type JwtProofTypeIdentifier, type JwtVcJsonFormatIdentifier, type JwtVcJsonLdFormatIdentifier, type LdpVcFormatIdentifier, type LegacySdJwtVcFormatIdentifier, type MsoMdocFormatIdentifier, type NonceResponse, type NotificationErrorResponse, type NotificationEvent, type Oid4vcTsConfig, Openid4vciClient, type Openid4vciClientOptions, Openid4vciDraftVersion, Openid4vciError, type Openid4vciErrorOptions, Openid4vciIssuer, type Openid4vciIssuerOptions, Openid4vciRetrieveCredentialsError, Openid4vciSendNotificationError, Openid4vciVersion, Openid4vciWalletProvider, type Openid4vciWalletProviderOptions, type ParseCredentialRequestReturn, type ParseKeyAttestationJwtOptions, type ProofTypeIdentifier, type RetrieveCredentialsResponseNotOk, type RetrieveCredentialsResponseOk, type SendNotificationResponseNotOk, type SendNotificationResponseOk, type VerifyKeyAttestationJwtOptions, type VerifyKeyAttestationJwtReturn, createKeyAttestationJwt, credentialsSupportedToCredentialConfigurationsSupported, determineAuthorizationServerForCredentialOffer, extractScopesForCredentialConfigurationIds, getCredentialConfigurationsMatchingRequestFormat, getGlobalConfig, parseKeyAttestationJwt, setGlobalConfig, verifyKeyAttestationJwt };
4468
4551
  //# sourceMappingURL=index.d.mts.map
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { ContentType, OpenId4VcBaseError, URL, URLSearchParams, ValidationError, arrayEqualsIgnoreOrder, createZodFetcher, dateToSeconds, encodeToBase64Url, formatZodError, getGlobalConfig, getQueryParams, isResponseContentType, joinUriParts, objectToQueryParams, parseWithErrorHandling, setGlobalConfig, zDataUrl, zHttpsUrl, zInteger, zIs, zNumericDate } from "@openid4vc/utils";
1
+ import { ContentType, OpenId4VcBaseError, URL, URLSearchParams, ValidationError, arrayEqualsIgnoreOrder, createZodFetcher, dateToSeconds, encodeToBase64Url, formatZodError, getGlobalConfig, getQueryParams, isResponseContentType, joinUriParts, objectToQueryParams, parseWithErrorHandling, setGlobalConfig, stringToJsonWithErrorHandling, zDataUrl, zHttpsUrl, zInteger, zIs, zNumericDate } from "@openid4vc/utils";
2
2
  import { InvalidFetchResponseError, Oauth2AuthorizationServer, Oauth2Client, Oauth2ClientAuthorizationChallengeError, Oauth2Error, Oauth2ErrorCodes, Oauth2JwtVerificationError, Oauth2ServerErrorResponseError, authorizationCodeGrantIdentifier, createClientAttestationJwt, decodeJwt, fetchAuthorizationServerMetadata, fetchWellKnownMetadata, fullySpecifiedCoseAlgorithmArrayToJwaSignatureAlgorithmArray, getAuthorizationServerMetadataFromList, isJwkInSet, jwaSignatureAlgorithmArrayToFullySpecifiedCoseAlgorithmArray, jwtHeaderFromJwtSigner, jwtSignerFromJwt, parseAuthorizationResponseRedirectUrl, preAuthorizedCodeGrantIdentifier, resourceRequest, verifyAuthorizationResponse, verifyJwt, zAuthorizationServerMetadata, zCompactJwt, zJwk, zJwtHeader, zJwtPayload } from "@openid4vc/oauth2";
3
3
  import z from "zod";
4
4
 
@@ -1114,14 +1114,15 @@ const zCredentialRequestProofs = z.object({
1114
1114
  [zJwtProofTypeIdentifier.value]: z.optional(z.array(zCredentialRequestProofJwt.shape.jwt)),
1115
1115
  [zAttestationProofTypeIdentifier.value]: z.optional(z.array(zCredentialRequestProofAttestation.shape.attestation))
1116
1116
  });
1117
+ const zCredentialResponseEncryption = z.object({
1118
+ jwk: zJwk,
1119
+ alg: z.string(),
1120
+ enc: z.string()
1121
+ }).loose();
1117
1122
  const zCredentialRequestCommon = z.object({
1118
1123
  proof: zCredentialRequestProof.optional(),
1119
1124
  proofs: z.optional(z.intersection(zCredentialRequestProofsCommon, zCredentialRequestProofs).refine((proofs) => Object.values(proofs).length === 1, { message: `The 'proofs' object in a credential request should contain exactly one attribute` })),
1120
- credential_response_encryption: z.object({
1121
- jwk: zJwk,
1122
- alg: z.string(),
1123
- enc: z.string()
1124
- }).loose().optional()
1125
+ credential_response_encryption: zCredentialResponseEncryption.optional()
1125
1126
  }).loose().refine(({ proof, proofs }) => !(proof !== void 0 && proofs !== void 0), { message: `Both 'proof' and 'proofs' are defined. Only one is allowed` });
1126
1127
 
1127
1128
  //#endregion
@@ -1206,11 +1207,7 @@ const zCredentialRequest = z.union([
1206
1207
  ]);
1207
1208
  const zDeferredCredentialRequest = z.object({
1208
1209
  transaction_id: z.string().nonempty(),
1209
- credential_response_encryption: z.object({
1210
- jwk: zJwk,
1211
- alg: z.string(),
1212
- enc: z.string()
1213
- }).loose().optional()
1210
+ credential_response_encryption: zCredentialResponseEncryption.optional()
1214
1211
  });
1215
1212
 
1216
1213
  //#endregion
@@ -1311,6 +1308,39 @@ const zDeferredCredentialResponse = zBaseCredentialResponse.superRefine((value,
1311
1308
 
1312
1309
  //#endregion
1313
1310
  //#region src/credential-request/retrieve-credentials.ts
1311
+ /**
1312
+ * Handles credential response by detecting content type, decrypting if needed, and parsing.
1313
+ */
1314
+ async function handleCredentialResponse(options) {
1315
+ const { response, decryptJwe, credentialResponseEncryption, schema, responseType } = options;
1316
+ if (isResponseContentType(ContentType.Jwt, response)) {
1317
+ const decryptResult = await decryptJwe(await response.clone().text());
1318
+ if (!decryptResult.decrypted) return {
1319
+ ok: false,
1320
+ parseResult: void 0
1321
+ };
1322
+ const jsonPayload = stringToJsonWithErrorHandling(decryptResult.payload, `Unable to parse decrypted ${responseType} as JSON`);
1323
+ const parseResult$1 = schema.safeParse(jsonPayload);
1324
+ if (!parseResult$1.success) return {
1325
+ ok: false,
1326
+ parseResult: parseResult$1
1327
+ };
1328
+ return {
1329
+ ok: true,
1330
+ data: parseResult$1.data
1331
+ };
1332
+ }
1333
+ if (credentialResponseEncryption) throw new Openid4vciError(`Encryption was requested via 'credential_response_encryption' but the ${responseType} was not encrypted. Expected Content-Type 'application/jwt' but received '${response.headers.get("Content-Type")}'.`);
1334
+ const parseResult = isResponseContentType(ContentType.Json, response) ? schema.safeParse(await response.clone().json()) : void 0;
1335
+ if (!parseResult?.success) return {
1336
+ ok: false,
1337
+ parseResult
1338
+ };
1339
+ return {
1340
+ ok: true,
1341
+ data: parseResult.data
1342
+ };
1343
+ }
1314
1344
  async function retrieveCredentialsWithCredentialConfigurationId(options) {
1315
1345
  if (options.issuerMetadata.originalDraftVersion !== Openid4vciVersion.Draft15 && options.issuerMetadata.originalDraftVersion !== Openid4vciVersion.V1) throw new Openid4vciError("Requesting credentials based on credential configuration ID is not supported in OpenID4VCI below draft 15. Make sure to provide the format and format specific claims in the request.");
1316
1346
  getKnownCredentialConfigurationSupportedById(options.issuerMetadata, options.credentialConfigurationId);
@@ -1318,14 +1348,16 @@ async function retrieveCredentialsWithCredentialConfigurationId(options) {
1318
1348
  ...options.additionalRequestPayload,
1319
1349
  credential_configuration_id: options.credentialConfigurationId,
1320
1350
  proof: options.proof,
1321
- proofs: options.proofs
1351
+ proofs: options.proofs,
1352
+ credential_response_encryption: options.credentialResponseEncryption
1322
1353
  };
1323
1354
  return retrieveCredentials({
1324
1355
  callbacks: options.callbacks,
1325
1356
  credentialRequest,
1326
1357
  issuerMetadata: options.issuerMetadata,
1327
1358
  accessToken: options.accessToken,
1328
- dpop: options.dpop
1359
+ dpop: options.dpop,
1360
+ credentialResponseEncryption: options.credentialResponseEncryption
1329
1361
  });
1330
1362
  }
1331
1363
  async function retrieveCredentialsWithFormat(options) {
@@ -1334,14 +1366,16 @@ async function retrieveCredentialsWithFormat(options) {
1334
1366
  ...options.formatPayload,
1335
1367
  ...options.additionalRequestPayload,
1336
1368
  proof: options.proof,
1337
- proofs: options.proofs
1369
+ proofs: options.proofs,
1370
+ credential_response_encryption: options.credentialResponseEncryption
1338
1371
  };
1339
1372
  return retrieveCredentials({
1340
1373
  callbacks: options.callbacks,
1341
1374
  credentialRequest,
1342
1375
  issuerMetadata: options.issuerMetadata,
1343
1376
  accessToken: options.accessToken,
1344
- dpop: options.dpop
1377
+ dpop: options.dpop,
1378
+ credentialResponseEncryption: options.credentialResponseEncryption
1345
1379
  });
1346
1380
  }
1347
1381
  /**
@@ -1375,15 +1409,21 @@ async function retrieveCredentials(options) {
1375
1409
  credentialErrorResponseResult
1376
1410
  };
1377
1411
  }
1378
- const credentialResponseResult = isResponseContentType(ContentType.Json, resourceResponse.response) ? zCredentialResponse.safeParse(await resourceResponse.response.clone().json()) : void 0;
1379
- if (!credentialResponseResult?.success) return {
1412
+ const result = await handleCredentialResponse({
1413
+ response: resourceResponse.response,
1414
+ decryptJwe: options.callbacks.decryptJwe,
1415
+ credentialResponseEncryption: options.credentialResponseEncryption,
1416
+ schema: zCredentialResponse,
1417
+ responseType: "credential response"
1418
+ });
1419
+ if (!result.ok) return {
1380
1420
  ...resourceResponse,
1381
1421
  ok: false,
1382
- credentialResponseResult
1422
+ credentialResponseResult: result.parseResult
1383
1423
  };
1384
1424
  return {
1385
1425
  ...resourceResponse,
1386
- credentialResponse: credentialResponseResult.data
1426
+ credentialResponse: result.data
1387
1427
  };
1388
1428
  }
1389
1429
  async function retrieveDeferredCredentials(options) {
@@ -1391,6 +1431,7 @@ async function retrieveDeferredCredentials(options) {
1391
1431
  if (!credentialEndpoint) throw new Openid4vciError(`Credential issuer '${options.issuerMetadata.credentialIssuer.credential_issuer}' does not support deferred credential retrieval.`);
1392
1432
  const deferredCredentialRequest = parseWithErrorHandling(zDeferredCredentialRequest, {
1393
1433
  transaction_id: options.transactionId,
1434
+ credential_response_encryption: options.credentialResponseEncryption,
1394
1435
  ...options.additionalRequestPayload
1395
1436
  }, "Error validating deferred credential request");
1396
1437
  const resourceResponse = await resourceRequest({
@@ -1411,15 +1452,22 @@ async function retrieveDeferredCredentials(options) {
1411
1452
  deferredCredentialErrorResponseResult
1412
1453
  };
1413
1454
  }
1414
- const deferredCredentialResponseResult = isResponseContentType(ContentType.Json, resourceResponse.response) ? zDeferredCredentialResponse.refine((response) => response.credentials || response.transaction_id === options.transactionId, { error: `Transaction id in deferred credential response does not match transaction id in deferred credential request '${options.transactionId}'` }).safeParse(await resourceResponse.response.clone().json()) : void 0;
1415
- if (!deferredCredentialResponseResult?.success) return {
1455
+ const deferredResponseSchema = zDeferredCredentialResponse.refine((response) => response.credentials || response.transaction_id === options.transactionId, { message: `Transaction id in deferred credential response does not match transaction id in deferred credential request '${options.transactionId}'` });
1456
+ const result = await handleCredentialResponse({
1457
+ response: resourceResponse.response,
1458
+ decryptJwe: options.callbacks.decryptJwe,
1459
+ credentialResponseEncryption: options.credentialResponseEncryption,
1460
+ schema: deferredResponseSchema,
1461
+ responseType: "deferred credential response"
1462
+ });
1463
+ if (!result.ok) return {
1416
1464
  ...resourceResponse,
1417
1465
  ok: false,
1418
- deferredCredentialResponseResult
1466
+ deferredCredentialResponseResult: result.parseResult
1419
1467
  };
1420
1468
  return {
1421
1469
  ...resourceResponse,
1422
- deferredCredentialResponse: deferredCredentialResponseResult.data
1470
+ deferredCredentialResponse: result.data
1423
1471
  };
1424
1472
  }
1425
1473
 
@@ -1845,7 +1893,7 @@ var Openid4vciClient = class {
1845
1893
  * @throws ValidationError - if validation of the credential request failed
1846
1894
  * @throws Openid4vciError - if the `credentialConfigurationId` couldn't be found, or if the the format specific request couldn't be constructed
1847
1895
  */
1848
- async retrieveCredentials({ issuerMetadata, proof, proofs, credentialConfigurationId, additionalRequestPayload, accessToken, dpop }) {
1896
+ async retrieveCredentials({ issuerMetadata, proof, proofs, credentialConfigurationId, additionalRequestPayload, accessToken, dpop, credentialResponseEncryption }) {
1849
1897
  let credentialResponse;
1850
1898
  if (issuerMetadata.originalDraftVersion === Openid4vciVersion.Draft15 || issuerMetadata.originalDraftVersion === Openid4vciVersion.V1) credentialResponse = await retrieveCredentialsWithCredentialConfigurationId({
1851
1899
  accessToken,
@@ -1855,7 +1903,8 @@ var Openid4vciClient = class {
1855
1903
  proof,
1856
1904
  proofs,
1857
1905
  callbacks: this.options.callbacks,
1858
- dpop
1906
+ dpop,
1907
+ credentialResponseEncryption
1859
1908
  });
1860
1909
  else credentialResponse = await retrieveCredentialsWithFormat({
1861
1910
  accessToken,
@@ -1868,7 +1917,8 @@ var Openid4vciClient = class {
1868
1917
  proof,
1869
1918
  proofs,
1870
1919
  callbacks: this.options.callbacks,
1871
- dpop
1920
+ dpop,
1921
+ credentialResponseEncryption
1872
1922
  });
1873
1923
  if (!credentialResponse.ok) throw new Openid4vciRetrieveCredentialsError(`Error retrieving credentials from '${issuerMetadata.credentialIssuer.credential_issuer}'`, credentialResponse, await credentialResponse.response.clone().text());
1874
1924
  return credentialResponse;
@@ -1905,8 +1955,8 @@ var Openid4vciClient = class {
1905
1955
 
1906
1956
  //#endregion
1907
1957
  //#region src/credential-request/credential-response.ts
1908
- function createCredentialResponse(options) {
1909
- return parseWithErrorHandling(zCredentialResponse, {
1958
+ async function createCredentialResponse(options) {
1959
+ const credentialResponse = parseWithErrorHandling(zCredentialResponse, {
1910
1960
  c_nonce: options.cNonce,
1911
1961
  c_nonce_expires_in: options.cNonceExpiresInSeconds,
1912
1962
  credential: options.credential,
@@ -1917,15 +1967,45 @@ function createCredentialResponse(options) {
1917
1967
  format: options.credentialRequest.format?.format,
1918
1968
  ...options.additionalPayload
1919
1969
  });
1970
+ if (options.credentialResponseEncryption) {
1971
+ if (!options.callbacks?.encryptJwe) throw new Openid4vciError("'credentialResponseEncryption' was provided but 'callbacks.encryptJwe' is not defined. Provide the 'encryptJwe' callback to encrypt the credential response.");
1972
+ const jweEncryptor = {
1973
+ method: "jwk",
1974
+ publicJwk: options.credentialResponseEncryption.jwk,
1975
+ alg: options.credentialResponseEncryption.alg,
1976
+ enc: options.credentialResponseEncryption.enc
1977
+ };
1978
+ const { jwe } = await options.callbacks.encryptJwe(jweEncryptor, JSON.stringify(credentialResponse));
1979
+ return {
1980
+ credentialResponse,
1981
+ credentialResponseJwt: jwe
1982
+ };
1983
+ }
1984
+ return { credentialResponse };
1920
1985
  }
1921
- function createDeferredCredentialResponse(options) {
1922
- return parseWithErrorHandling(zDeferredCredentialResponse, {
1986
+ async function createDeferredCredentialResponse(options) {
1987
+ const deferredCredentialResponse = parseWithErrorHandling(zDeferredCredentialResponse, {
1923
1988
  credentials: options.credentials,
1924
1989
  notification_id: options.notificationId,
1925
1990
  transaction_id: options.transactionId,
1926
1991
  interval: options.interval,
1927
1992
  ...options.additionalPayload
1928
1993
  });
1994
+ if (options.credentialResponseEncryption) {
1995
+ if (!options.callbacks?.encryptJwe) throw new Openid4vciError("'credentialResponseEncryption' was provided but 'callbacks.encryptJwe' is not defined. Provide the 'encryptJwe' callback to encrypt the deferred credential response.");
1996
+ const jweEncryptor = {
1997
+ method: "jwk",
1998
+ publicJwk: options.credentialResponseEncryption.jwk,
1999
+ alg: options.credentialResponseEncryption.alg,
2000
+ enc: options.credentialResponseEncryption.enc
2001
+ };
2002
+ const { jwe } = await options.callbacks.encryptJwe(jweEncryptor, JSON.stringify(deferredCredentialResponse));
2003
+ return {
2004
+ deferredCredentialResponse,
2005
+ deferredCredentialResponseJwt: jwe
2006
+ };
2007
+ }
2008
+ return { deferredCredentialResponse };
1929
2009
  }
1930
2010
 
1931
2011
  //#endregion
@@ -1938,28 +2018,33 @@ function parseCredentialRequest(options) {
1938
2018
  const knownProof = z.union(allCredentialRequestProofs).safeParse(credentialRequest.proof);
1939
2019
  if (knownProof.success && knownProof.data.proof_type === jwtProofTypeIdentifier) proofs = { [jwtProofTypeIdentifier]: [knownProof.data.jwt] };
1940
2020
  else if (knownProof.success && knownProof.data.proof_type === attestationProofTypeIdentifier) proofs = { [attestationProofTypeIdentifier]: [knownProof.data.attestation] };
2021
+ const credentialResponseEncryption = credentialRequest.credential_response_encryption;
1941
2022
  if (credentialRequest.credential_configuration_id) {
1942
2023
  getKnownCredentialConfigurationSupportedById(options.issuerMetadata, credentialRequest.credential_configuration_id);
1943
2024
  return {
1944
2025
  credentialConfiguration: options.issuerMetadata.knownCredentialConfigurations[credentialRequest.credential_configuration_id],
1945
2026
  credentialConfigurationId: credentialRequest.credential_configuration_id,
1946
2027
  credentialRequest,
1947
- proofs
2028
+ proofs,
2029
+ credentialResponseEncryption
1948
2030
  };
1949
2031
  }
1950
2032
  if (credentialRequest.credential_identifier) return {
1951
2033
  credentialIdentifier: credentialRequest.credential_identifier,
1952
2034
  credentialRequest,
1953
- proofs
2035
+ proofs,
2036
+ credentialResponseEncryption
1954
2037
  };
1955
2038
  if (credentialRequest.format && allCredentialRequestFormatIdentifiers.includes(credentialRequest.format)) return {
1956
2039
  format: parseWithErrorHandling(z.union(allCredentialRequestFormats), credentialRequest, "Unable to validate format specific properties from credential request"),
1957
2040
  credentialRequest,
1958
- proofs
2041
+ proofs,
2042
+ credentialResponseEncryption
1959
2043
  };
1960
2044
  return {
1961
2045
  credentialRequest,
1962
- proofs
2046
+ proofs,
2047
+ credentialResponseEncryption
1963
2048
  };
1964
2049
  }
1965
2050
 
@@ -2121,15 +2206,23 @@ var Openid4vciIssuer = class {
2121
2206
  }
2122
2207
  /**
2123
2208
  * @throws ValidationError - when validation of the credential response fails
2209
+ * @throws Openid4vciError - when encryption is requested but no encryptJwe callback is available
2124
2210
  */
2125
2211
  createCredentialResponse(options) {
2126
- return createCredentialResponse(options);
2212
+ return createCredentialResponse({
2213
+ ...options,
2214
+ callbacks: options.credentialResponseEncryption ? { encryptJwe: this.options.callbacks.encryptJwe } : void 0
2215
+ });
2127
2216
  }
2128
2217
  /**
2129
2218
  * @throws ValidationError - when validation of the credential response fails
2219
+ * @throws Openid4vciError - when encryption is requested but no encryptJwe callback is available
2130
2220
  */
2131
2221
  createDeferredCredentialResponse(options) {
2132
- return createDeferredCredentialResponse(options);
2222
+ return createDeferredCredentialResponse({
2223
+ ...options,
2224
+ callbacks: options.credentialResponseEncryption ? { encryptJwe: this.options.callbacks.encryptJwe } : void 0
2225
+ });
2133
2226
  }
2134
2227
  /**
2135
2228
  * @throws ValidationError - when validation of the nonce response fails