@openid4vc/oauth2 0.3.0-alpha-20251107130226 → 0.3.0-alpha-20251107132439

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.mjs CHANGED
@@ -506,6 +506,149 @@ var Oauth2ServerErrorResponseError = class extends Oauth2Error {
506
506
  }
507
507
  };
508
508
 
509
+ //#endregion
510
+ //#region src/common/jwt/z-jwe.ts
511
+ const zCompactJwe = z.string().regex(/^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$/, { message: "Not a valid compact jwe" });
512
+
513
+ //#endregion
514
+ //#region src/jar/z-jar-authorization-request.ts
515
+ const zJarAuthorizationRequest = z.object({
516
+ request: z.optional(z.string()),
517
+ request_uri: z.optional(zHttpsUrl),
518
+ client_id: z.optional(z.string())
519
+ }).loose();
520
+ function validateJarRequestParams(options) {
521
+ const { jarRequestParams } = options;
522
+ if (jarRequestParams.request && jarRequestParams.request_uri) throw new Oauth2ServerErrorResponseError({
523
+ error: Oauth2ErrorCodes.InvalidRequestObject,
524
+ error_description: "request and request_uri cannot both be present in a JAR request"
525
+ });
526
+ if (!jarRequestParams.request && !jarRequestParams.request_uri) throw new Oauth2ServerErrorResponseError({
527
+ error: Oauth2ErrorCodes.InvalidRequestObject,
528
+ error_description: "request or request_uri must be present"
529
+ });
530
+ return jarRequestParams;
531
+ }
532
+ function isJarAuthorizationRequest(request) {
533
+ return "request" in request || "request_uri" in request;
534
+ }
535
+
536
+ //#endregion
537
+ //#region src/jar/z-jar-request-object.ts
538
+ const zJarRequestObjectPayload = z.object({
539
+ ...zJwtPayload.shape,
540
+ client_id: z.string()
541
+ }).loose();
542
+ const zSignedAuthorizationRequestJwtHeaderTyp = z.literal("oauth-authz-req+jwt");
543
+ const signedAuthorizationRequestJwtHeaderTyp = zSignedAuthorizationRequestJwtHeaderTyp.value;
544
+ const zJwtAuthorizationRequestJwtHeaderTyp = z.literal("jwt");
545
+ const jwtAuthorizationRequestJwtHeaderTyp = zJwtAuthorizationRequestJwtHeaderTyp.value;
546
+
547
+ //#endregion
548
+ //#region src/jar/handle-jar-request/verify-jar-request.ts
549
+ /**
550
+ * Parse a JAR (JWT Secured Authorization Request) request by validating and optionally fetch from uri.
551
+ *
552
+ * @param options - The input parameters
553
+ * @param options.jarRequestParams - The JAR authorization request parameters
554
+ * @param options.callbacks - Context containing the relevant Jose crypto operations
555
+ * @returns An object containing the transmission method ('value' or 'reference') and the JWT request object.
556
+ */
557
+ async function parseJarRequest(options) {
558
+ const { callbacks } = options;
559
+ const jarRequestParams = {
560
+ ...validateJarRequestParams(options),
561
+ ...options.jarRequestParams
562
+ };
563
+ return {
564
+ sendBy: jarRequestParams.request ? "value" : "reference",
565
+ authorizationRequestJwt: jarRequestParams.request ?? await fetchJarRequestObject({
566
+ requestUri: jarRequestParams.request_uri,
567
+ fetch: callbacks.fetch
568
+ })
569
+ };
570
+ }
571
+ /**
572
+ * Verifies a JAR (JWT Secured Authorization Request) request by validating and verifying signatures.
573
+ *
574
+ * @param options - The input parameters
575
+ * @param options.jarRequestParams - The JAR authorization request parameters
576
+ * @param options.callbacks - Context containing the relevant Jose crypto operations
577
+ * @returns The verified authorization request parameters and metadata
578
+ */
579
+ async function verifyJarRequest(options) {
580
+ const { jarRequestParams, authorizationRequestJwt, callbacks, jwtSigner } = options;
581
+ if (zCompactJwe.safeParse(authorizationRequestJwt).success) throw new Oauth2ServerErrorResponseError({
582
+ error: Oauth2ErrorCodes.InvalidRequestObject,
583
+ error_description: "Encrypted JWE request objects are not supported."
584
+ });
585
+ if (!zCompactJwt.safeParse(authorizationRequestJwt).success) throw new Oauth2ServerErrorResponseError({
586
+ error: Oauth2ErrorCodes.InvalidRequestObject,
587
+ error_description: "JAR request object is not a valid JWT."
588
+ });
589
+ const { authorizationRequestPayload, signer, jwt } = await verifyJarRequestObject({
590
+ authorizationRequestJwt,
591
+ callbacks,
592
+ jwtSigner
593
+ });
594
+ if (!authorizationRequestPayload.client_id) throw new Oauth2ServerErrorResponseError({
595
+ error: Oauth2ErrorCodes.InvalidRequestObject,
596
+ error_description: "Jar Request Object is missing the required \"client_id\" field."
597
+ });
598
+ if (jarRequestParams.client_id !== authorizationRequestPayload.client_id) throw new Oauth2ServerErrorResponseError({
599
+ error: Oauth2ErrorCodes.InvalidRequest,
600
+ error_description: "client_id does not match the request object client_id."
601
+ });
602
+ return {
603
+ jwt,
604
+ authorizationRequestPayload,
605
+ signer
606
+ };
607
+ }
608
+ async function fetchJarRequestObject(options) {
609
+ const { requestUri, fetch } = options;
610
+ const response = await createFetcher(fetch)(requestUri, {
611
+ method: "get",
612
+ headers: {
613
+ Accept: `${ContentType.OAuthAuthorizationRequestJwt}, ${ContentType.Jwt};q=0.9, text/plain`,
614
+ "Content-Type": ContentType.XWwwFormUrlencoded
615
+ }
616
+ }).catch(() => {
617
+ throw new Oauth2ServerErrorResponseError({
618
+ error_description: `Fetching request_object from request_uri '${requestUri}' failed`,
619
+ error: Oauth2ErrorCodes.InvalidRequestUri
620
+ });
621
+ });
622
+ if (!response.ok) throw new Oauth2ServerErrorResponseError({
623
+ error_description: `Fetching request_object from request_uri '${requestUri}' failed with status code '${response.status}'.`,
624
+ error: Oauth2ErrorCodes.InvalidRequestUri
625
+ });
626
+ return await response.text();
627
+ }
628
+ async function verifyJarRequestObject(options) {
629
+ const { authorizationRequestJwt, callbacks, jwtSigner } = options;
630
+ const jwt = decodeJwt({
631
+ jwt: authorizationRequestJwt,
632
+ payloadSchema: zJarRequestObjectPayload
633
+ });
634
+ const { signer } = await verifyJwt({
635
+ verifyJwtCallback: callbacks.verifyJwt,
636
+ compact: authorizationRequestJwt,
637
+ header: jwt.header,
638
+ payload: jwt.payload,
639
+ signer: jwtSigner
640
+ });
641
+ if (jwt.header.typ !== signedAuthorizationRequestJwtHeaderTyp && jwt.header.typ !== jwtAuthorizationRequestJwtHeaderTyp) throw new Oauth2ServerErrorResponseError({
642
+ error: Oauth2ErrorCodes.InvalidRequestObject,
643
+ error_description: `Invalid Jar Request Object typ header. Expected "oauth-authz-req+jwt" or "jwt", received "${jwt.header.typ}".`
644
+ });
645
+ return {
646
+ signer,
647
+ jwt,
648
+ authorizationRequestPayload: jwt.payload
649
+ };
650
+ }
651
+
509
652
  //#endregion
510
653
  //#region src/client-attestation/z-client-attestation.ts
511
654
  const zOauthClientAttestationHeader = z$1.literal("OAuth-Client-Attestation");
@@ -873,12 +1016,29 @@ const zPushedAuthorizationResponse = z$1.object({
873
1016
  *
874
1017
  * @throws {Oauth2ServerErrorResponseError}
875
1018
  */
876
- function parsePushedAuthorizationRequest(options) {
877
- const parsedAuthorizationRequest = zAuthorizationRequest.safeParse(options.authorizationRequest);
878
- if (!parsedAuthorizationRequest.success) throw new Oauth2ServerErrorResponseError({
879
- error: Oauth2ErrorCodes.InvalidRequest,
880
- error_description: `Error occurred during validation of pushed authorization request.\n${formatZodError(parsedAuthorizationRequest.error)}`
881
- });
1019
+ async function parsePushedAuthorizationRequest(options) {
1020
+ const parsed = parseWithErrorHandling(z$1.union([zAuthorizationRequest, zJarAuthorizationRequest]), options.authorizationRequest, "Invalid authorization request. Could not parse authorization request or jar.");
1021
+ let parsedAuthorizationRequest;
1022
+ let authorizationRequestJwt;
1023
+ if (isJarAuthorizationRequest(parsed)) {
1024
+ const parsedJar = await parseJarRequest({
1025
+ jarRequestParams: parsed,
1026
+ callbacks: options.callbacks
1027
+ });
1028
+ const jwt = decodeJwt({ jwt: parsedJar.authorizationRequestJwt });
1029
+ parsedAuthorizationRequest = zAuthorizationRequest.safeParse(jwt.payload);
1030
+ if (!parsedAuthorizationRequest.success) throw new Oauth2ServerErrorResponseError({
1031
+ error: Oauth2ErrorCodes.InvalidRequest,
1032
+ error_description: `Invalid authorization request. Could not parse jar request payload.\n${formatZodError(parsedAuthorizationRequest.error)}`
1033
+ });
1034
+ authorizationRequestJwt = parsedJar.authorizationRequestJwt;
1035
+ } else {
1036
+ parsedAuthorizationRequest = zAuthorizationRequest.safeParse(options.authorizationRequest);
1037
+ if (!parsedAuthorizationRequest.success) throw new Oauth2ServerErrorResponseError({
1038
+ error: Oauth2ErrorCodes.InvalidRequest,
1039
+ error_description: `Error occurred during validation of pushed authorization request.\n${formatZodError(parsedAuthorizationRequest.error)}`
1040
+ });
1041
+ }
882
1042
  const authorizationRequest = parsedAuthorizationRequest.data;
883
1043
  const { clientAttestation, dpop } = parseAuthorizationRequest({
884
1044
  authorizationRequest,
@@ -886,6 +1046,7 @@ function parsePushedAuthorizationRequest(options) {
886
1046
  });
887
1047
  return {
888
1048
  authorizationRequest,
1049
+ authorizationRequestJwt,
889
1050
  dpop,
890
1051
  clientAttestation
891
1052
  };
@@ -1036,10 +1197,6 @@ function clientAuthenticationClientAttestationJwt(options) {
1036
1197
  };
1037
1198
  }
1038
1199
 
1039
- //#endregion
1040
- //#region src/common/jwt/z-jwe.ts
1041
- const zCompactJwe = z.string().regex(/^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$/, { message: "Not a valid compact jwe" });
1042
-
1043
1200
  //#endregion
1044
1201
  //#region src/error/Oauth2ClientErrorResponseError.ts
1045
1202
  var Oauth2ClientErrorResponseError = class extends Oauth2Error {
@@ -1173,6 +1330,57 @@ async function verifyIdTokenJwt(options) {
1173
1330
  };
1174
1331
  }
1175
1332
 
1333
+ //#endregion
1334
+ //#region src/jar/create-jar-authorization-request.ts
1335
+ /**
1336
+ * Creates a JAR (JWT Authorization Request) request object.
1337
+ *
1338
+ * @param options - The input parameters
1339
+ * @param options.authorizationRequestPayload - The authorization request parameters
1340
+ * @param options.jwtSigner - The JWT signer
1341
+ * @param options.jweEncryptor - The JWE encryptor (optional) if provided, the request object will be encrypted
1342
+ * @param options.requestUri - The request URI (optional) if provided, the request object needs to be fetched from the URI
1343
+ * @param options.callbacks - The callback context
1344
+ * @returns the requestParams, signerJwk, encryptionJwk, and requestObjectJwt
1345
+ */
1346
+ async function createJarAuthorizationRequest(options) {
1347
+ const { jwtSigner, jweEncryptor, authorizationRequestPayload, requestUri, callbacks } = options;
1348
+ let authorizationRequestJwt;
1349
+ let encryptionJwk;
1350
+ const now = options.now ?? /* @__PURE__ */ new Date();
1351
+ const { jwt, signerJwk } = await callbacks.signJwt(jwtSigner, {
1352
+ header: {
1353
+ ...jwtHeaderFromJwtSigner(jwtSigner),
1354
+ typ: "oauth-authz-req+jwt"
1355
+ },
1356
+ payload: {
1357
+ iat: dateToSeconds(now),
1358
+ exp: dateToSeconds(addSecondsToDate(now, options.expiresInSeconds)),
1359
+ ...options.additionalJwtPayload,
1360
+ ...authorizationRequestPayload
1361
+ }
1362
+ });
1363
+ authorizationRequestJwt = jwt;
1364
+ if (jweEncryptor) {
1365
+ const encryptionResult = await callbacks.encryptJwe(jweEncryptor, authorizationRequestJwt);
1366
+ authorizationRequestJwt = encryptionResult.jwe;
1367
+ encryptionJwk = encryptionResult.encryptionJwk;
1368
+ }
1369
+ const client_id = authorizationRequestPayload.client_id;
1370
+ return {
1371
+ jarAuthorizationRequest: requestUri ? {
1372
+ client_id,
1373
+ request_uri: requestUri
1374
+ } : {
1375
+ client_id,
1376
+ request: authorizationRequestJwt
1377
+ },
1378
+ signerJwk,
1379
+ encryptionJwk,
1380
+ authorizationRequestJwt
1381
+ };
1382
+ }
1383
+
1176
1384
  //#endregion
1177
1385
  //#region src/metadata/fetch-well-known-metadata.ts
1178
1386
  /**
@@ -1776,10 +1984,18 @@ function createPushedAuthorizationErrorResponse(options) {
1776
1984
  //#endregion
1777
1985
  //#region src/authorization-request/verify-pushed-authorization-request.ts
1778
1986
  async function verifyPushedAuthorizationRequest(options) {
1987
+ let jar;
1988
+ if (options.authorizationRequestJwt) jar = await verifyJarRequest({
1989
+ authorizationRequestJwt: options.authorizationRequestJwt.jwt,
1990
+ jarRequestParams: options.authorizationRequest,
1991
+ callbacks: options.callbacks,
1992
+ jwtSigner: options.authorizationRequestJwt.signer
1993
+ });
1779
1994
  const { clientAttestation, dpop } = await verifyAuthorizationRequest(options);
1780
1995
  return {
1781
1996
  dpop,
1782
- clientAttestation
1997
+ clientAttestation,
1998
+ jar
1783
1999
  };
1784
2000
  }
1785
2001
 
@@ -1858,9 +2074,14 @@ var Oauth2AuthorizationServer = class {
1858
2074
  /**
1859
2075
  * Parse a pushed authorization request
1860
2076
  */
1861
- parsePushedAuthorizationRequest(options) {
1862
- return parsePushedAuthorizationRequest(options);
2077
+ async parsePushedAuthorizationRequest(options) {
2078
+ return await parsePushedAuthorizationRequest(options);
1863
2079
  }
2080
+ /**
2081
+ * Verify pushed authorization request.
2082
+ *
2083
+ * Make sure to provide the `authorizationRequestJwt` if this was returned in the `parsePushedAuthorizationRequest`
2084
+ */
1864
2085
  verifyPushedAuthorizationRequest(options) {
1865
2086
  return verifyPushedAuthorizationRequest({
1866
2087
  ...options,
@@ -2590,5 +2811,5 @@ async function verifyResourceRequest(options) {
2590
2811
  }
2591
2812
 
2592
2813
  //#endregion
2593
- export { HashAlgorithm, InvalidFetchResponseError, Oauth2AuthorizationServer, Oauth2Client, Oauth2ClientAuthorizationChallengeError, Oauth2ClientErrorResponseError, Oauth2Error, Oauth2ErrorCodes, Oauth2JwtParseError, Oauth2JwtVerificationError, Oauth2ResourceServer, Oauth2ResourceUnauthorizedError, Oauth2ServerErrorResponseError, PkceCodeChallengeMethod, SupportedAuthenticationScheme, SupportedClientAuthenticationMethod, authorizationCodeGrantIdentifier, calculateJwkThumbprint, clientAuthenticationAnonymous, clientAuthenticationClientAttestationJwt, clientAuthenticationClientSecretBasic, clientAuthenticationClientSecretPost, clientAuthenticationDynamic, clientAuthenticationNone, createClientAttestationJwt, decodeJwt, decodeJwtHeader, fetchAuthorizationServerMetadata, fetchJwks, fetchWellKnownMetadata, getAuthorizationServerMetadataFromList, getGlobalConfig, isJwkInSet, jwtHeaderFromJwtSigner, jwtSignerFromJwt, parseAuthorizationResponseRedirectUrl, parsePushedAuthorizationRequestUriReferenceValue, preAuthorizedCodeGrantIdentifier, pushedAuthorizationRequestUriPrefix, refreshTokenGrantIdentifier, resourceRequest, setGlobalConfig, verifyClientAttestationJwt, verifyIdTokenJwt, verifyJwt, verifyResourceRequest, zAlgValueNotNone, zAuthorizationCodeGrantIdentifier, zAuthorizationErrorResponse, zAuthorizationResponse, zAuthorizationResponseFromUriParams, zAuthorizationServerMetadata, zCompactJwe, zCompactJwt, zIdTokenJwtHeader, zIdTokenJwtPayload, zJwk, zJwkSet, zJwtHeader, zJwtPayload, zOauth2ErrorResponse, zPreAuthorizedCodeGrantIdentifier, zPushedAuthorizationRequestUriPrefix, zRefreshTokenGrantIdentifier };
2814
+ export { HashAlgorithm, InvalidFetchResponseError, Oauth2AuthorizationServer, Oauth2Client, Oauth2ClientAuthorizationChallengeError, Oauth2ClientErrorResponseError, Oauth2Error, Oauth2ErrorCodes, Oauth2JwtParseError, Oauth2JwtVerificationError, Oauth2ResourceServer, Oauth2ResourceUnauthorizedError, Oauth2ServerErrorResponseError, PkceCodeChallengeMethod, SupportedAuthenticationScheme, SupportedClientAuthenticationMethod, authorizationCodeGrantIdentifier, calculateJwkThumbprint, clientAuthenticationAnonymous, clientAuthenticationClientAttestationJwt, clientAuthenticationClientSecretBasic, clientAuthenticationClientSecretPost, clientAuthenticationDynamic, clientAuthenticationNone, createClientAttestationJwt, createJarAuthorizationRequest, decodeJwt, decodeJwtHeader, fetchAuthorizationServerMetadata, fetchJwks, fetchWellKnownMetadata, getAuthorizationServerMetadataFromList, getGlobalConfig, isJwkInSet, jwtAuthorizationRequestJwtHeaderTyp, jwtHeaderFromJwtSigner, jwtSignerFromJwt, parseAuthorizationResponseRedirectUrl, parsePushedAuthorizationRequestUriReferenceValue, preAuthorizedCodeGrantIdentifier, pushedAuthorizationRequestUriPrefix, refreshTokenGrantIdentifier, resourceRequest, setGlobalConfig, signedAuthorizationRequestJwtHeaderTyp, validateJarRequestParams, verifyClientAttestationJwt, verifyIdTokenJwt, verifyJwt, verifyResourceRequest, zAlgValueNotNone, zAuthorizationCodeGrantIdentifier, zAuthorizationErrorResponse, zAuthorizationResponse, zAuthorizationResponseFromUriParams, zAuthorizationServerMetadata, zCompactJwe, zCompactJwt, zIdTokenJwtHeader, zIdTokenJwtPayload, zJarAuthorizationRequest, zJarRequestObjectPayload, zJwk, zJwkSet, zJwtHeader, zJwtPayload, zOauth2ErrorResponse, zPreAuthorizedCodeGrantIdentifier, zPushedAuthorizationRequestUriPrefix, zRefreshTokenGrantIdentifier };
2594
2815
  //# sourceMappingURL=index.mjs.map