@openid4vc/oauth2 0.5.3-alpha-20260702123119 → 0.5.3

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
@@ -90,6 +90,13 @@ interface ClientAuthenticationCallbackOptions {
90
90
  * You can modify this object
91
91
  */
92
92
  body: Record<string, unknown>;
93
+ /**
94
+ * A fresh Client Attestation challenge provided by the authorization server (draft 09), e.g.
95
+ * obtained from the `OAuth-Client-Attestation-Challenge` response header when retrying after a
96
+ * `use_attestation_challenge` error. When set it takes precedence over any statically configured
97
+ * challenge for methods that include a Client Attestation PoP.
98
+ */
99
+ attestationChallenge?: string;
93
100
  }
94
101
  /**
95
102
  * Callback method to determine the client authentication for a request.
@@ -606,6 +613,12 @@ type AccessTokenErrorResponse = z.infer<typeof zAccessTokenErrorResponse>;
606
613
  interface RetrieveAccessTokenReturn {
607
614
  accessTokenResponse: AccessTokenResponse;
608
615
  dpop?: RequestDpopOptions;
616
+ /**
617
+ * A fresh Client Attestation challenge provided by the authorization server in the
618
+ * `OAuth-Client-Attestation-Challenge` response header (draft 09 §6.2). If present, the client
619
+ * should use this challenge for the next Client Attestation PoP JWT.
620
+ */
621
+ attestationChallenge?: string;
609
622
  }
610
623
  interface RetrieveAccessTokenBaseOptions {
611
624
  /**
@@ -772,6 +785,10 @@ declare const zClientAttestationJwtHeader: z.ZodObject<{
772
785
  trust_chain: z.ZodOptional<z.ZodTuple<[z.ZodString], z.ZodString>>;
773
786
  }, z.core.$loose>;
774
787
  type ClientAttestationJwtHeader = z.infer<typeof zClientAttestationJwtHeader>;
788
+ declare const zClientAttestationChallengeResponse: z.ZodObject<{
789
+ attestation_challenge: z.ZodString;
790
+ }, z.core.$loose>;
791
+ type ClientAttestationChallengeResponse = z.infer<typeof zClientAttestationChallengeResponse>;
775
792
  declare const zClientAttestationPopJwtPayload: z.ZodObject<{
776
793
  aud: z.ZodUnion<readonly [z.ZodURL, z.ZodArray<z.ZodURL>]>;
777
794
  jti: z.ZodString;
@@ -1724,6 +1741,7 @@ declare enum Oauth2ErrorCodes {
1724
1741
  InvalidScope = "invalid_scope",
1725
1742
  InvalidDpopProof = "invalid_dpop_proof",
1726
1743
  UseDpopNonce = "use_dpop_nonce",
1744
+ UseAttestationChallenge = "use_attestation_challenge",
1727
1745
  RedirectToWeb = "redirect_to_web",
1728
1746
  InvalidSession = "invalid_session",
1729
1747
  InsufficientAuthorization = "insufficient_authorization",
@@ -2107,6 +2125,65 @@ declare function verifyAuthorizationResponse({
2107
2125
  authorizationServerMetadata
2108
2126
  }: VerifyAuthorizationResponseOptions): void;
2109
2127
  //#endregion
2128
+ //#region src/client-attestation/client-attestation-challenge.d.ts
2129
+ interface RequestClientAttestationChallengeOptions {
2130
+ /**
2131
+ * Metadata of the authorization server from which to request the challenge.
2132
+ */
2133
+ authorizationServerMetadata: AuthorizationServerMetadata;
2134
+ /**
2135
+ * Callback context
2136
+ */
2137
+ callbacks: Pick<CallbackContext, 'fetch'>;
2138
+ }
2139
+ /**
2140
+ * Request a fresh Client Attestation challenge from the authorization server's `challenge_endpoint`.
2141
+ *
2142
+ * @see https://www.ietf.org/archive/id/draft-ietf-oauth-attestation-based-client-auth-09.html#section-6.1
2143
+ *
2144
+ * @throws {Oauth2Error} if the authorization server has no `challenge_endpoint`
2145
+ * @throws {InvalidFetchResponseError} if the request failed
2146
+ * @throws {ValidationError} if the response could not be validated
2147
+ */
2148
+ declare function requestClientAttestationChallenge(options: RequestClientAttestationChallengeOptions): Promise<{
2149
+ challenge: string;
2150
+ }>;
2151
+ /**
2152
+ * Extract the Client Attestation challenge from the `OAuth-Client-Attestation-Challenge` response header.
2153
+ */
2154
+ declare function extractClientAttestationChallengeFromHeaders(headers: FetchHeaders): string | null;
2155
+ interface ShouldRetryAuthorizationServerRequestWithClientAttestationChallengeOptions {
2156
+ /**
2157
+ * The error response that will be evaluated for the 'use_attestation_challenge' error to determine
2158
+ * whether the request should be retried using a fresh client attestation challenge.
2159
+ */
2160
+ errorResponse: Oauth2ErrorResponse;
2161
+ /**
2162
+ * The headers returned in the response. The 'OAuth-Client-Attestation-Challenge' header will be
2163
+ * extracted if the error response indicates so. Will throw an error if the 'error' in the response is
2164
+ * 'use_attestation_challenge' but the headers do not contain the 'OAuth-Client-Attestation-Challenge'
2165
+ * header value.
2166
+ */
2167
+ responseHeaders: FetchHeaders;
2168
+ }
2169
+ declare function shouldRetryAuthorizationServerRequestWithClientAttestationChallenge(options: ShouldRetryAuthorizationServerRequestWithClientAttestationChallengeOptions): {
2170
+ readonly retry: false;
2171
+ readonly attestationChallenge?: undefined;
2172
+ } | {
2173
+ readonly retry: true;
2174
+ readonly attestationChallenge: string;
2175
+ };
2176
+ /**
2177
+ * Wraps an authorization server request so that it is retried once with a fresh Client Attestation
2178
+ * challenge when the server responds with the 'use_attestation_challenge' error and provides a
2179
+ * challenge in the 'OAuth-Client-Attestation-Challenge' response header.
2180
+ *
2181
+ * Mirrors {@link authorizationServerRequestWithDpopRetry} for DPoP nonces.
2182
+ */
2183
+ declare function authorizationServerRequestWithClientAttestationChallengeRetry<T>(options: {
2184
+ request: (attestationChallenge?: string) => Promise<T>;
2185
+ }): Promise<T>;
2186
+ //#endregion
2110
2187
  //#region src/common/algorithm/algorithm-transform.d.ts
2111
2188
  /**
2112
2189
  * Algorithm transformation utilities for JWA and COSE
@@ -3591,6 +3668,14 @@ declare class Oauth2Client {
3591
3668
  authorization_code: string;
3592
3669
  };
3593
3670
  }>;
3671
+ /**
3672
+ * Request a fresh Client Attestation challenge from the authorization server's `challenge_endpoint`
3673
+ * (draft 09). The returned challenge can be passed to `clientAuthenticationClientAttestationJwt` so
3674
+ * it is included in the Client Attestation PoP JWT.
3675
+ */
3676
+ requestClientAttestationChallenge(options: Omit<RequestClientAttestationChallengeOptions, 'callbacks'>): Promise<{
3677
+ challenge: string;
3678
+ }>;
3594
3679
  createAuthorizationRequestUrl(options: Omit<CreateAuthorizationRequestUrlOptions, 'callbacks'>): Promise<{
3595
3680
  authorizationRequestUrl: string;
3596
3681
  pkce: CreatePkceReturn | undefined;
@@ -3964,5 +4049,5 @@ declare function verifyResourceRequest(options: VerifyResourceRequestOptions): P
3964
4049
  authorizationServer: string;
3965
4050
  }>;
3966
4051
  //#endregion
3967
- export { type AccessTokenErrorResponse, type AccessTokenProfileJwtPayload, type AccessTokenResponse, type AuthorizationChallengeErrorResponse, type AuthorizationChallengeRequest, type AuthorizationChallengeResponse, type AuthorizationCodeGrantIdentifier, AuthorizationErrorResponse, type AuthorizationRequest, AuthorizationResponse, type AuthorizationServerMetadata, type CalculateJwkThumbprintOptions, type CallbackContext, type ClientAttestationJwtHeader, type ClientAttestationJwtPayload, type ClientAttestationPopJwtHeader, type ClientAttestationPopJwtPayload, type ClientAuthenticationCallback, type ClientAuthenticationCallbackOptions, type ClientAuthenticationClientAttestationJwtOptions, type ClientAuthenticationClientSecretBasicOptions, type ClientAuthenticationClientSecretPostOptions, type ClientAuthenticationDynamicOptions, type ClientAuthenticationNoneOptions, type ClientCredentialsGrantIdentifier, type CreateAuthorizationRequestUrlOptions, type CreateClientAttestationJwtOptions, type CreateJarAuthorizationRequestOptions, type CreatePkceReturn, type CreatePushedAuthorizationErrorResponseOptions, type CreatePushedAuthorizationResponseOptions, type DecodeJwtHeaderResult, type DecodeJwtOptions, type DecodeJwtResult, type DecryptJweCallback, type DecryptJweCallbackOptions, type EncryptJweCallback, type GenerateRandomCallback, HashAlgorithm, type HashCallback, type HttpMethod, IdTokenJwtHeader, IdTokenJwtPayload, InvalidFetchResponseError, type JarAuthorizationRequest, type JarRequestObjectPayload, type JweEncryptor, type Jwk, type JwkSet, type JwtHeader, type JwtPayload, type JwtSigner, type JwtSignerCustom, type JwtSignerDid, type JwtSignerJwk, type JwtSignerWithJwk, type JwtSignerX5c, Oauth2AuthorizationServer, type Oauth2AuthorizationServerOptions, Oauth2Client, Oauth2ClientAuthorizationChallengeError, Oauth2ClientErrorResponseError, type Oauth2ClientOptions, Oauth2Error, Oauth2ErrorCodes, type Oauth2ErrorOptions, type Oauth2ErrorResponse, Oauth2JwtParseError, Oauth2JwtVerificationError, Oauth2ResourceServer, type Oauth2ResourceServerOptions, Oauth2ResourceUnauthorizedError, Oauth2ServerErrorResponseError, type Oid4vcTsConfig, type ParseAuthorizationChallengeRequestOptions, type ParseAuthorizationChallengeRequestResult, type ParseAuthorizationRequestOptions, type ParseAuthorizationRequestResult, ParseAuthorizationResponseOptions, type ParsePushedAuthorizationRequestOptions, type ParsePushedAuthorizationRequestResult, type ParsedJarRequest, type ParsedJarRequestOptions, PkceCodeChallengeMethod, type PreAuthorizedCodeGrantIdentifier, type PushedAuthorizationRequestUriPrefix, type RefreshTokenGrantIdentifier, type RequestClientAttestationOptions, type RequestDpopOptions, type RequestLike, type ResourceRequestOptions, type ResourceRequestResponseNotOk, type ResourceRequestResponseOk, type RetrieveAuthorizationCodeAccessTokenOptions, type RetrieveClientCredentialsAccessTokenOptions, type RetrievePreAuthorizedCodeAccessTokenOptions, type SignJwtCallback, SupportedAuthenticationScheme, SupportedClientAuthenticationMethod, type TokenIntrospectionResponse, type VerifiedClientAttestationJwt, type VerifiedJarRequest, type VerifyAccessTokenRequestReturn, type VerifyAuthorizationChallengeRequestOptions, type VerifyAuthorizationChallengeRequestReturn, type VerifyAuthorizationRequestOptions, type VerifyAuthorizationRequestReturn, VerifyAuthorizationResponseOptions, type VerifyDataIntegrityProofCallback, VerifyIdTokenJwtOptions, type VerifyJarRequestOptions, type VerifyJwtCallback, type VerifyPushedAuthorizationRequestOptions, type VerifyPushedAuthorizationRequestReturn, type VerifyResourceRequestOptions, type WwwAuthenticateHeaderChallenge, authorizationCodeGrantIdentifier, authorizationServerRequestWithDpopRetry, calculateJwkThumbprint, clientAuthenticationAnonymous, clientAuthenticationClientAttestationJwt, clientAuthenticationClientSecretBasic, clientAuthenticationClientSecretPost, clientAuthenticationDynamic, clientAuthenticationNone, clientCredentialsGrantIdentifier, createClientAttestationJwt, createDpopHeadersForRequest, createJarAuthorizationRequest, createPkce, decodeJwt, decodeJwtHeader, defaultGrantTypesSupported, extractDpopNonceFromHeaders, fetchAuthorizationServerMetadata, fetchJwks, fetchWellKnownMetadata, fullySpecifiedCoseAlgorithmArrayToJwaSignatureAlgorithmArray, fullySpecifiedCoseAlgorithmToJwaSignatureAlgorithm, getAuthorizationServerMetadataFromList, getGlobalConfig, getGrantTypesSupported, isJarAuthorizationRequest, isJwkInSet, jwaSignatureAlgorithmArrayToFullySpecifiedCoseAlgorithmArray, jwaSignatureAlgorithmToFullySpecifiedCoseAlgorithm, jwtAuthorizationRequestJwtHeaderTyp, jwtHeaderFromJwtSigner, jwtSignerFromJwt, parseAuthorizationRequest, parseAuthorizationResponseRedirectUrl, parseJarRequest, parsePushedAuthorizationRequestUriReferenceValue, preAuthorizedCodeGrantIdentifier, pushedAuthorizationRequestUriPrefix, refreshTokenGrantIdentifier, resourceRequest, setGlobalConfig, signedAuthorizationRequestJwtHeaderTyp, validateJarRequestParams, verifyAuthorizationRequest, verifyAuthorizationResponse, verifyClientAttestationJwt, verifyIdTokenJwt, verifyJarRequest, verifyJwt, verifyResourceRequest, zAlgValueNotNone, zAuthorizationCodeGrantIdentifier, zAuthorizationErrorResponse, zAuthorizationRequest, zAuthorizationResponse, zAuthorizationResponseFromUriParams, zAuthorizationServerMetadata, zClientCredentialsGrantIdentifier, zCompactJwe, zCompactJwt, zIdTokenJwtHeader, zIdTokenJwtPayload, zJarAuthorizationRequest, zJarRequestObjectPayload, zJwk, zJwkSet, zJwtHeader, zJwtPayload, zOauth2ErrorResponse, zPreAuthorizedCodeGrantIdentifier, zPushedAuthorizationRequestUriPrefix, zRefreshTokenGrantIdentifier };
4052
+ export { type AccessTokenErrorResponse, type AccessTokenProfileJwtPayload, type AccessTokenResponse, type AuthorizationChallengeErrorResponse, type AuthorizationChallengeRequest, type AuthorizationChallengeResponse, type AuthorizationCodeGrantIdentifier, AuthorizationErrorResponse, type AuthorizationRequest, AuthorizationResponse, type AuthorizationServerMetadata, type CalculateJwkThumbprintOptions, type CallbackContext, type ClientAttestationChallengeResponse, type ClientAttestationJwtHeader, type ClientAttestationJwtPayload, type ClientAttestationPopJwtHeader, type ClientAttestationPopJwtPayload, type ClientAuthenticationCallback, type ClientAuthenticationCallbackOptions, type ClientAuthenticationClientAttestationJwtOptions, type ClientAuthenticationClientSecretBasicOptions, type ClientAuthenticationClientSecretPostOptions, type ClientAuthenticationDynamicOptions, type ClientAuthenticationNoneOptions, type ClientCredentialsGrantIdentifier, type CreateAuthorizationRequestUrlOptions, type CreateClientAttestationJwtOptions, type CreateJarAuthorizationRequestOptions, type CreatePkceReturn, type CreatePushedAuthorizationErrorResponseOptions, type CreatePushedAuthorizationResponseOptions, type DecodeJwtHeaderResult, type DecodeJwtOptions, type DecodeJwtResult, type DecryptJweCallback, type DecryptJweCallbackOptions, type EncryptJweCallback, type GenerateRandomCallback, HashAlgorithm, type HashCallback, type HttpMethod, IdTokenJwtHeader, IdTokenJwtPayload, InvalidFetchResponseError, type JarAuthorizationRequest, type JarRequestObjectPayload, type JweEncryptor, type Jwk, type JwkSet, type JwtHeader, type JwtPayload, type JwtSigner, type JwtSignerCustom, type JwtSignerDid, type JwtSignerJwk, type JwtSignerWithJwk, type JwtSignerX5c, Oauth2AuthorizationServer, type Oauth2AuthorizationServerOptions, Oauth2Client, Oauth2ClientAuthorizationChallengeError, Oauth2ClientErrorResponseError, type Oauth2ClientOptions, Oauth2Error, Oauth2ErrorCodes, type Oauth2ErrorOptions, type Oauth2ErrorResponse, Oauth2JwtParseError, Oauth2JwtVerificationError, Oauth2ResourceServer, type Oauth2ResourceServerOptions, Oauth2ResourceUnauthorizedError, Oauth2ServerErrorResponseError, type Oid4vcTsConfig, type ParseAuthorizationChallengeRequestOptions, type ParseAuthorizationChallengeRequestResult, type ParseAuthorizationRequestOptions, type ParseAuthorizationRequestResult, ParseAuthorizationResponseOptions, type ParsePushedAuthorizationRequestOptions, type ParsePushedAuthorizationRequestResult, type ParsedJarRequest, type ParsedJarRequestOptions, PkceCodeChallengeMethod, type PreAuthorizedCodeGrantIdentifier, type PushedAuthorizationRequestUriPrefix, type RefreshTokenGrantIdentifier, type RequestClientAttestationChallengeOptions, type RequestClientAttestationOptions, type RequestDpopOptions, type RequestLike, type ResourceRequestOptions, type ResourceRequestResponseNotOk, type ResourceRequestResponseOk, type RetrieveAuthorizationCodeAccessTokenOptions, type RetrieveClientCredentialsAccessTokenOptions, type RetrievePreAuthorizedCodeAccessTokenOptions, type ShouldRetryAuthorizationServerRequestWithClientAttestationChallengeOptions, type SignJwtCallback, SupportedAuthenticationScheme, SupportedClientAuthenticationMethod, type TokenIntrospectionResponse, type VerifiedClientAttestationJwt, type VerifiedJarRequest, type VerifyAccessTokenRequestReturn, type VerifyAuthorizationChallengeRequestOptions, type VerifyAuthorizationChallengeRequestReturn, type VerifyAuthorizationRequestOptions, type VerifyAuthorizationRequestReturn, VerifyAuthorizationResponseOptions, type VerifyDataIntegrityProofCallback, VerifyIdTokenJwtOptions, type VerifyJarRequestOptions, type VerifyJwtCallback, type VerifyPushedAuthorizationRequestOptions, type VerifyPushedAuthorizationRequestReturn, type VerifyResourceRequestOptions, type WwwAuthenticateHeaderChallenge, authorizationCodeGrantIdentifier, authorizationServerRequestWithClientAttestationChallengeRetry, authorizationServerRequestWithDpopRetry, calculateJwkThumbprint, clientAuthenticationAnonymous, clientAuthenticationClientAttestationJwt, clientAuthenticationClientSecretBasic, clientAuthenticationClientSecretPost, clientAuthenticationDynamic, clientAuthenticationNone, clientCredentialsGrantIdentifier, createClientAttestationJwt, createDpopHeadersForRequest, createJarAuthorizationRequest, createPkce, decodeJwt, decodeJwtHeader, defaultGrantTypesSupported, extractClientAttestationChallengeFromHeaders, extractDpopNonceFromHeaders, fetchAuthorizationServerMetadata, fetchJwks, fetchWellKnownMetadata, fullySpecifiedCoseAlgorithmArrayToJwaSignatureAlgorithmArray, fullySpecifiedCoseAlgorithmToJwaSignatureAlgorithm, getAuthorizationServerMetadataFromList, getGlobalConfig, getGrantTypesSupported, isJarAuthorizationRequest, isJwkInSet, jwaSignatureAlgorithmArrayToFullySpecifiedCoseAlgorithmArray, jwaSignatureAlgorithmToFullySpecifiedCoseAlgorithm, jwtAuthorizationRequestJwtHeaderTyp, jwtHeaderFromJwtSigner, jwtSignerFromJwt, parseAuthorizationRequest, parseAuthorizationResponseRedirectUrl, parseJarRequest, parsePushedAuthorizationRequestUriReferenceValue, preAuthorizedCodeGrantIdentifier, pushedAuthorizationRequestUriPrefix, refreshTokenGrantIdentifier, requestClientAttestationChallenge, resourceRequest, setGlobalConfig, shouldRetryAuthorizationServerRequestWithClientAttestationChallenge, signedAuthorizationRequestJwtHeaderTyp, validateJarRequestParams, verifyAuthorizationRequest, verifyAuthorizationResponse, verifyClientAttestationJwt, verifyIdTokenJwt, verifyJarRequest, verifyJwt, verifyResourceRequest, zAlgValueNotNone, zAuthorizationCodeGrantIdentifier, zAuthorizationErrorResponse, zAuthorizationRequest, zAuthorizationResponse, zAuthorizationResponseFromUriParams, zAuthorizationServerMetadata, zClientAttestationChallengeResponse, zClientCredentialsGrantIdentifier, zCompactJwe, zCompactJwt, zIdTokenJwtHeader, zIdTokenJwtPayload, zJarAuthorizationRequest, zJarRequestObjectPayload, zJwk, zJwkSet, zJwtHeader, zJwtPayload, zOauth2ErrorResponse, zPreAuthorizedCodeGrantIdentifier, zPushedAuthorizationRequestUriPrefix, zRefreshTokenGrantIdentifier };
3968
4053
  //# sourceMappingURL=index.d.mts.map
package/dist/index.mjs CHANGED
@@ -977,6 +977,7 @@ let Oauth2ErrorCodes = /* @__PURE__ */ function(Oauth2ErrorCodes) {
977
977
  Oauth2ErrorCodes["InvalidScope"] = "invalid_scope";
978
978
  Oauth2ErrorCodes["InvalidDpopProof"] = "invalid_dpop_proof";
979
979
  Oauth2ErrorCodes["UseDpopNonce"] = "use_dpop_nonce";
980
+ Oauth2ErrorCodes["UseAttestationChallenge"] = "use_attestation_challenge";
980
981
  Oauth2ErrorCodes["RedirectToWeb"] = "redirect_to_web";
981
982
  Oauth2ErrorCodes["InvalidSession"] = "invalid_session";
982
983
  Oauth2ErrorCodes["InsufficientAuthorization"] = "insufficient_authorization";
@@ -1032,7 +1033,8 @@ const zClientAttestationJwtHeader = z.object({
1032
1033
  typ: z.literal("oauth-client-attestation+jwt")
1033
1034
  }).loose();
1034
1035
  const oauthClientAttestationPopHeader = z.literal("OAuth-Client-Attestation-PoP").value;
1035
- z.literal("OAuth-Client-Attestation-Challenge").value;
1036
+ const oauthClientAttestationChallengeHeader = z.literal("OAuth-Client-Attestation-Challenge").value;
1037
+ const zClientAttestationChallengeResponse = z.object({ attestation_challenge: z.string() }).loose();
1036
1038
  const zClientAttestationPopJwtPayload = z.object({
1037
1039
  ...zJwtPayload.shape,
1038
1040
  aud: z.union([zHttpsUrl, z.array(zHttpsUrl)]),
@@ -1677,6 +1679,72 @@ function verifyAuthorizationResponse({ authorizationResponse, authorizationServe
1677
1679
  });
1678
1680
  }
1679
1681
  //#endregion
1682
+ //#region src/error/Oauth2ClientErrorResponseError.ts
1683
+ var Oauth2ClientErrorResponseError = class extends Oauth2Error {
1684
+ constructor(message, errorResponse, response) {
1685
+ super(`${message}\n${JSON.stringify(errorResponse, null, 2)}`);
1686
+ this.errorResponse = errorResponse;
1687
+ this.response = response.clone();
1688
+ }
1689
+ };
1690
+ //#endregion
1691
+ //#region src/client-attestation/client-attestation-challenge.ts
1692
+ /**
1693
+ * Request a fresh Client Attestation challenge from the authorization server's `challenge_endpoint`.
1694
+ *
1695
+ * @see https://www.ietf.org/archive/id/draft-ietf-oauth-attestation-based-client-auth-09.html#section-6.1
1696
+ *
1697
+ * @throws {Oauth2Error} if the authorization server has no `challenge_endpoint`
1698
+ * @throws {InvalidFetchResponseError} if the request failed
1699
+ * @throws {ValidationError} if the response could not be validated
1700
+ */
1701
+ async function requestClientAttestationChallenge(options) {
1702
+ const fetchWithZod = createZodFetcher(options.callbacks.fetch);
1703
+ const { authorizationServerMetadata } = options;
1704
+ const challengeEndpoint = authorizationServerMetadata.challenge_endpoint;
1705
+ if (!challengeEndpoint) throw new Oauth2Error(`Unable to request client attestation challenge. Authorization server '${authorizationServerMetadata.issuer}' has no 'challenge_endpoint'`);
1706
+ const { response, result } = await fetchWithZod(zClientAttestationChallengeResponse, ContentType.Json, challengeEndpoint, { method: "POST" });
1707
+ if (!response.ok || !result) throw new InvalidFetchResponseError$1(`Unable to request client attestation challenge from challenge endpoint '${challengeEndpoint}'. Received response with status ${response.status}`, await response.clone().text(), response);
1708
+ if (!result.success) throw new ValidationError("Error validating client attestation challenge response", result.error);
1709
+ return { challenge: result.data.attestation_challenge };
1710
+ }
1711
+ /**
1712
+ * Extract the Client Attestation challenge from the `OAuth-Client-Attestation-Challenge` response header.
1713
+ */
1714
+ function extractClientAttestationChallengeFromHeaders(headers) {
1715
+ return headers.get(oauthClientAttestationChallengeHeader);
1716
+ }
1717
+ function shouldRetryAuthorizationServerRequestWithClientAttestationChallenge(options) {
1718
+ if (options.errorResponse.error !== "use_attestation_challenge") return { retry: false };
1719
+ const attestationChallenge = extractClientAttestationChallengeFromHeaders(options.responseHeaders);
1720
+ if (!attestationChallenge) throw new Oauth2Error(`Error response error contains error 'use_attestation_challenge' but the response headers do not include a valid '${oauthClientAttestationChallengeHeader}' header value.`);
1721
+ return {
1722
+ retry: true,
1723
+ attestationChallenge
1724
+ };
1725
+ }
1726
+ /**
1727
+ * Wraps an authorization server request so that it is retried once with a fresh Client Attestation
1728
+ * challenge when the server responds with the 'use_attestation_challenge' error and provides a
1729
+ * challenge in the 'OAuth-Client-Attestation-Challenge' response header.
1730
+ *
1731
+ * Mirrors {@link authorizationServerRequestWithDpopRetry} for DPoP nonces.
1732
+ */
1733
+ async function authorizationServerRequestWithClientAttestationChallengeRetry(options) {
1734
+ try {
1735
+ return await options.request();
1736
+ } catch (error) {
1737
+ if (error instanceof Oauth2ClientErrorResponseError) {
1738
+ const challengeRetry = shouldRetryAuthorizationServerRequestWithClientAttestationChallenge({
1739
+ responseHeaders: error.response.headers,
1740
+ errorResponse: error.errorResponse
1741
+ });
1742
+ if (challengeRetry.retry) return options.request(challengeRetry.attestationChallenge);
1743
+ }
1744
+ throw error;
1745
+ }
1746
+ }
1747
+ //#endregion
1680
1748
  //#region src/z-grant-type.ts
1681
1749
  const zPreAuthorizedCodeGrantIdentifier = z.literal("urn:ietf:params:oauth:grant-type:pre-authorized_code");
1682
1750
  const preAuthorizedCodeGrantIdentifier = zPreAuthorizedCodeGrantIdentifier.value;
@@ -1780,12 +1848,12 @@ function clientAuthenticationAnonymous() {
1780
1848
  * Client authentication using `attest_jwt_client_auth` option.
1781
1849
  */
1782
1850
  function clientAuthenticationClientAttestationJwt(options) {
1783
- return async ({ headers, authorizationServerMetadata }) => {
1851
+ return async ({ headers, authorizationServerMetadata, attestationChallenge }) => {
1784
1852
  const clientAttestationPop = await createClientAttestationPopJwt({
1785
1853
  authorizationServer: authorizationServerMetadata.issuer,
1786
1854
  callbacks: options.callbacks,
1787
1855
  clientAttestation: options.clientAttestationJwt,
1788
- challenge: options.challenge
1856
+ challenge: attestationChallenge ?? options.challenge
1789
1857
  });
1790
1858
  headers.set(oauthClientAttestationHeader, options.clientAttestationJwt);
1791
1859
  headers.set(oauthClientAttestationPopHeader, clientAttestationPop);
@@ -1929,15 +1997,6 @@ function fullySpecifiedCoseAlgorithmArrayToJwaSignatureAlgorithmArray(coseAlgs,
1929
1997
  }).filter((alg) => alg !== void 0);
1930
1998
  }
1931
1999
  //#endregion
1932
- //#region src/error/Oauth2ClientErrorResponseError.ts
1933
- var Oauth2ClientErrorResponseError = class extends Oauth2Error {
1934
- constructor(message, errorResponse, response) {
1935
- super(`${message}\n${JSON.stringify(errorResponse, null, 2)}`);
1936
- this.errorResponse = errorResponse;
1937
- this.response = response.clone();
1938
- }
1939
- };
1940
- //#endregion
1941
2000
  //#region src/dpop/dpop-retry.ts
1942
2001
  async function authorizationServerRequestWithDpopRetry(options) {
1943
2002
  try {
@@ -2923,7 +2982,7 @@ async function retrieveAccessToken(options) {
2923
2982
  const supportedGrantTypes = getGrantTypesSupported(options.authorizationServerMetadata.grant_types_supported);
2924
2983
  if (!supportedGrantTypes.includes(accessTokenRequest.grant_type)) throw new Oauth2Error(`The authorization server '${options.authorizationServerMetadata.issuer}' does not support the '${accessTokenRequest.grant_type}' grant type. Supported grant types are: ${supportedGrantTypes.join(", ")}`);
2925
2984
  if (accessTokenRequest.tx_code) accessTokenRequest.user_pin = accessTokenRequest.tx_code;
2926
- return await authorizationServerRequestWithDpopRetry({
2985
+ return await authorizationServerRequestWithClientAttestationChallengeRetry({ request: (attestationChallenge) => authorizationServerRequestWithDpopRetry({
2927
2986
  dpop: options.dpop,
2928
2987
  request: async (dpop) => {
2929
2988
  const dpopHeaders = dpop ? await createDpopHeadersForRequest({
@@ -2945,7 +3004,8 @@ async function retrieveAccessToken(options) {
2945
3004
  authorizationServerMetadata: options.authorizationServerMetadata,
2946
3005
  body: accessTokenRequest,
2947
3006
  contentType: ContentType.XWwwFormUrlencoded,
2948
- headers
3007
+ headers,
3008
+ attestationChallenge
2949
3009
  });
2950
3010
  const { response, result } = await fetchWithZod(zAccessTokenResponse, ContentType.Json, options.authorizationServerMetadata.token_endpoint, {
2951
3011
  body: objectToQueryParams(accessTokenRequest).toString(),
@@ -2964,10 +3024,11 @@ async function retrieveAccessToken(options) {
2964
3024
  ...dpop,
2965
3025
  nonce: dpopNonce
2966
3026
  } : void 0,
3027
+ attestationChallenge: extractClientAttestationChallengeFromHeaders(response.headers) ?? void 0,
2967
3028
  accessTokenResponse: result.data
2968
3029
  };
2969
3030
  }
2970
- });
3031
+ }) });
2971
3032
  }
2972
3033
  //#endregion
2973
3034
  //#region src/authorization-challenge/send-authorization-challenge.ts
@@ -3284,6 +3345,17 @@ var Oauth2Client = class {
3284
3345
  callbacks: this.options.callbacks
3285
3346
  });
3286
3347
  }
3348
+ /**
3349
+ * Request a fresh Client Attestation challenge from the authorization server's `challenge_endpoint`
3350
+ * (draft 09). The returned challenge can be passed to `clientAuthenticationClientAttestationJwt` so
3351
+ * it is included in the Client Attestation PoP JWT.
3352
+ */
3353
+ requestClientAttestationChallenge(options) {
3354
+ return requestClientAttestationChallenge({
3355
+ ...options,
3356
+ callbacks: this.options.callbacks
3357
+ });
3358
+ }
3287
3359
  async createAuthorizationRequestUrl(options) {
3288
3360
  return createAuthorizationRequestUrl({
3289
3361
  authorizationServerMetadata: options.authorizationServerMetadata,
@@ -3510,6 +3582,6 @@ async function verifyResourceRequest(options) {
3510
3582
  };
3511
3583
  }
3512
3584
  //#endregion
3513
- export { HashAlgorithm, InvalidFetchResponseError, Oauth2AuthorizationServer, Oauth2Client, Oauth2ClientAuthorizationChallengeError, Oauth2ClientErrorResponseError, Oauth2Error, Oauth2ErrorCodes, Oauth2JwtParseError, Oauth2JwtVerificationError, Oauth2ResourceServer, Oauth2ResourceUnauthorizedError, Oauth2ServerErrorResponseError, PkceCodeChallengeMethod, SupportedAuthenticationScheme, SupportedClientAuthenticationMethod, authorizationCodeGrantIdentifier, authorizationServerRequestWithDpopRetry, calculateJwkThumbprint, clientAuthenticationAnonymous, clientAuthenticationClientAttestationJwt, clientAuthenticationClientSecretBasic, clientAuthenticationClientSecretPost, clientAuthenticationDynamic, clientAuthenticationNone, clientCredentialsGrantIdentifier, createClientAttestationJwt, createDpopHeadersForRequest, createJarAuthorizationRequest, createPkce, decodeJwt, decodeJwtHeader, defaultGrantTypesSupported, extractDpopNonceFromHeaders, fetchAuthorizationServerMetadata, fetchJwks, fetchWellKnownMetadata, fullySpecifiedCoseAlgorithmArrayToJwaSignatureAlgorithmArray, fullySpecifiedCoseAlgorithmToJwaSignatureAlgorithm, getAuthorizationServerMetadataFromList, getGlobalConfig, getGrantTypesSupported, isJarAuthorizationRequest, isJwkInSet, jwaSignatureAlgorithmArrayToFullySpecifiedCoseAlgorithmArray, jwaSignatureAlgorithmToFullySpecifiedCoseAlgorithm, jwtAuthorizationRequestJwtHeaderTyp, jwtHeaderFromJwtSigner, jwtSignerFromJwt, parseAuthorizationRequest, parseAuthorizationResponseRedirectUrl, parseJarRequest, parsePushedAuthorizationRequestUriReferenceValue, preAuthorizedCodeGrantIdentifier, pushedAuthorizationRequestUriPrefix, refreshTokenGrantIdentifier, resourceRequest, setGlobalConfig, signedAuthorizationRequestJwtHeaderTyp, validateJarRequestParams, verifyAuthorizationRequest, verifyAuthorizationResponse, verifyClientAttestationJwt, verifyIdTokenJwt, verifyJarRequest, verifyJwt, verifyResourceRequest, zAlgValueNotNone, zAuthorizationCodeGrantIdentifier, zAuthorizationErrorResponse, zAuthorizationRequest, zAuthorizationResponse, zAuthorizationResponseFromUriParams, zAuthorizationServerMetadata, zClientCredentialsGrantIdentifier, zCompactJwe, zCompactJwt, zIdTokenJwtHeader, zIdTokenJwtPayload, zJarAuthorizationRequest, zJarRequestObjectPayload, zJwk, zJwkSet, zJwtHeader, zJwtPayload, zOauth2ErrorResponse, zPreAuthorizedCodeGrantIdentifier, zPushedAuthorizationRequestUriPrefix, zRefreshTokenGrantIdentifier };
3585
+ export { HashAlgorithm, InvalidFetchResponseError, Oauth2AuthorizationServer, Oauth2Client, Oauth2ClientAuthorizationChallengeError, Oauth2ClientErrorResponseError, Oauth2Error, Oauth2ErrorCodes, Oauth2JwtParseError, Oauth2JwtVerificationError, Oauth2ResourceServer, Oauth2ResourceUnauthorizedError, Oauth2ServerErrorResponseError, PkceCodeChallengeMethod, SupportedAuthenticationScheme, SupportedClientAuthenticationMethod, authorizationCodeGrantIdentifier, authorizationServerRequestWithClientAttestationChallengeRetry, authorizationServerRequestWithDpopRetry, calculateJwkThumbprint, clientAuthenticationAnonymous, clientAuthenticationClientAttestationJwt, clientAuthenticationClientSecretBasic, clientAuthenticationClientSecretPost, clientAuthenticationDynamic, clientAuthenticationNone, clientCredentialsGrantIdentifier, createClientAttestationJwt, createDpopHeadersForRequest, createJarAuthorizationRequest, createPkce, decodeJwt, decodeJwtHeader, defaultGrantTypesSupported, extractClientAttestationChallengeFromHeaders, extractDpopNonceFromHeaders, fetchAuthorizationServerMetadata, fetchJwks, fetchWellKnownMetadata, fullySpecifiedCoseAlgorithmArrayToJwaSignatureAlgorithmArray, fullySpecifiedCoseAlgorithmToJwaSignatureAlgorithm, getAuthorizationServerMetadataFromList, getGlobalConfig, getGrantTypesSupported, isJarAuthorizationRequest, isJwkInSet, jwaSignatureAlgorithmArrayToFullySpecifiedCoseAlgorithmArray, jwaSignatureAlgorithmToFullySpecifiedCoseAlgorithm, jwtAuthorizationRequestJwtHeaderTyp, jwtHeaderFromJwtSigner, jwtSignerFromJwt, parseAuthorizationRequest, parseAuthorizationResponseRedirectUrl, parseJarRequest, parsePushedAuthorizationRequestUriReferenceValue, preAuthorizedCodeGrantIdentifier, pushedAuthorizationRequestUriPrefix, refreshTokenGrantIdentifier, requestClientAttestationChallenge, resourceRequest, setGlobalConfig, shouldRetryAuthorizationServerRequestWithClientAttestationChallenge, signedAuthorizationRequestJwtHeaderTyp, validateJarRequestParams, verifyAuthorizationRequest, verifyAuthorizationResponse, verifyClientAttestationJwt, verifyIdTokenJwt, verifyJarRequest, verifyJwt, verifyResourceRequest, zAlgValueNotNone, zAuthorizationCodeGrantIdentifier, zAuthorizationErrorResponse, zAuthorizationRequest, zAuthorizationResponse, zAuthorizationResponseFromUriParams, zAuthorizationServerMetadata, zClientAttestationChallengeResponse, zClientCredentialsGrantIdentifier, zCompactJwe, zCompactJwt, zIdTokenJwtHeader, zIdTokenJwtPayload, zJarAuthorizationRequest, zJarRequestObjectPayload, zJwk, zJwkSet, zJwtHeader, zJwtPayload, zOauth2ErrorResponse, zPreAuthorizedCodeGrantIdentifier, zPushedAuthorizationRequestUriPrefix, zRefreshTokenGrantIdentifier };
3514
3586
 
3515
3587
  //# sourceMappingURL=index.mjs.map