@vitemcp/server 1.10.3 → 1.10.4

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.
@@ -383,6 +383,17 @@ var clientCodeStorageSchema = _zod.z.object({
383
383
  upstreamTokens: upstreamTokenSetStorageSchema,
384
384
  used: _zod.z.boolean().optional()
385
385
  });
386
+ var upstreamCodeStorageSchema = _zod.z.object({
387
+ code: _zod.z.string().min(1),
388
+ scope: _zod.z.array(_zod.z.string())
389
+ });
390
+ var storedClientCodeSchema = _zod.z.union([
391
+ clientCodeStorageSchema,
392
+ clientCodeStorageSchema.omit({ upstreamTokens: true }).extend({
393
+ codeChallenge: _zod.z.string().min(1),
394
+ upstreamCode: upstreamCodeStorageSchema
395
+ }).refine(({ codeChallengeMethod }) => codeChallengeMethod === "S256")
396
+ ]);
386
397
  var spentClientCodeStorageSchema = _zod.z.object({
387
398
  expiresAt: storedDateSchema,
388
399
  used: _zod.z.literal(true)
@@ -396,6 +407,7 @@ var oauthTransactionStorageSchema = _zod.z.object({
396
407
  consentGiven: _zod.z.boolean().optional(),
397
408
  createdAt: storedDateSchema,
398
409
  expiresAt: storedDateSchema,
410
+ forwardPkce: _zod.z.literal(true).optional(),
399
411
  id: _zod.z.string(),
400
412
  metadata: _zod.z.record(_zod.z.string(), _zod.z.unknown()).optional(),
401
413
  proxyCodeChallenge: _zod.z.string(),
@@ -433,7 +445,7 @@ var OAuthProxyStateStore = class {
433
445
  if (spent.success) {
434
446
  return this.isExpired(spent.data.expiresAt) ? null : { expiresAt: spent.data.expiresAt, status: "spent" };
435
447
  }
436
- const parsed = clientCodeStorageSchema.safeParse(stored);
448
+ const parsed = storedClientCodeSchema.safeParse(stored);
437
449
  if (!parsed.success || this.isExpired(parsed.data.expiresAt)) {
438
450
  return null;
439
451
  }
@@ -1744,52 +1756,57 @@ var OAuthProxy = (_class4 = class {
1744
1756
  `S256 PKCE is required for ${describeUnauthenticatedClient(client)}`
1745
1757
  );
1746
1758
  }
1759
+ const codeVerifier = request2.code_verifier;
1747
1760
  if (clientCode.codeChallenge) {
1748
- if (!request2.code_verifier) {
1761
+ if (!codeVerifier) {
1749
1762
  throw new OAuthProxyError(
1750
1763
  "invalid_request",
1751
1764
  "code_verifier required for PKCE"
1752
1765
  );
1753
1766
  }
1754
1767
  const valid = PKCEUtils.validateChallenge(
1755
- request2.code_verifier,
1768
+ codeVerifier,
1756
1769
  clientCode.codeChallenge,
1757
1770
  clientCode.codeChallengeMethod
1758
1771
  );
1759
1772
  if (!valid) {
1760
1773
  throw new OAuthProxyError("invalid_grant", "Invalid PKCE verifier");
1761
1774
  }
1762
- } else if (request2.code_verifier) {
1775
+ } else if (codeVerifier) {
1763
1776
  throw new OAuthProxyError(
1764
1777
  "invalid_request",
1765
1778
  "code_verifier provided without a code_challenge"
1766
1779
  );
1767
1780
  }
1781
+ const upstreamTokens = "upstreamTokens" in clientCode ? clientCode.upstreamTokens : await this.exchangeUpstreamCode(clientCode.upstreamCode.code, {
1782
+ clientCodeVerifier: codeVerifier,
1783
+ scope: clientCode.upstreamCode.scope
1784
+ });
1768
1785
  if (this.config.enableTokenSwap && this.jwtIssuer) {
1769
1786
  return await this.issueSwappedTokens(
1770
1787
  clientCode.clientId,
1771
- clientCode.upstreamTokens,
1788
+ upstreamTokens,
1772
1789
  authenticated
1773
1790
  );
1774
1791
  } else {
1775
1792
  const response = {
1776
- access_token: clientCode.upstreamTokens.accessToken,
1777
- expires_in: clientCode.upstreamTokens.expiresIn || 3600,
1778
- token_type: clientCode.upstreamTokens.tokenType
1793
+ access_token: upstreamTokens.accessToken,
1794
+ expires_in: upstreamTokens.expiresIn || 3600,
1795
+ token_type: upstreamTokens.tokenType
1779
1796
  };
1780
- if (clientCode.upstreamTokens.refreshToken) {
1797
+ if (upstreamTokens.refreshToken) {
1781
1798
  await this.savePassthroughRefreshBinding(
1782
- clientCode.upstreamTokens.refreshToken,
1799
+ upstreamTokens.refreshToken,
1783
1800
  { clientAuthenticated: authenticated, clientId: client.clientId },
1784
- clientCode.upstreamTokens.refreshExpiresIn
1801
+ upstreamTokens.refreshExpiresIn
1785
1802
  );
1786
- response.refresh_token = clientCode.upstreamTokens.refreshToken;
1803
+ response.refresh_token = upstreamTokens.refreshToken;
1787
1804
  }
1788
- if (clientCode.upstreamTokens.idToken) {
1789
- response.id_token = clientCode.upstreamTokens.idToken;
1805
+ if (upstreamTokens.idToken) {
1806
+ response.id_token = upstreamTokens.idToken;
1790
1807
  }
1791
- if (clientCode.upstreamTokens.scope.length > 0) {
1792
- response.scope = clientCode.upstreamTokens.scope.join(" ");
1808
+ if (upstreamTokens.scope.length > 0) {
1809
+ response.scope = upstreamTokens.scope.join(" ");
1793
1810
  }
1794
1811
  return response;
1795
1812
  }
@@ -1893,10 +1910,9 @@ var OAuthProxy = (_class4 = class {
1893
1910
  relayableError(error, description)
1894
1911
  );
1895
1912
  }
1896
- const upstreamTokens = await this.exchangeUpstreamCode(code, transaction);
1897
1913
  const clientCode = await this.generateAuthorizationCode(
1898
1914
  transaction,
1899
- upstreamTokens
1915
+ transaction.forwardPkce ? { code, scope: transaction.scope } : await this.exchangeUpstreamCode(code, transaction)
1900
1916
  );
1901
1917
  return this.redirectToClient(transaction, { code: clientCode });
1902
1918
  }
@@ -2194,6 +2210,7 @@ var OAuthProxy = (_class4 = class {
2194
2210
  expiresAt: new Date(
2195
2211
  Date.now() + (this.config.transactionTtl || 600) * 1e3
2196
2212
  ),
2213
+ ...this.config.forwardPkce && params.code_challenge && params.code_challenge_method === "S256" && { forwardPkce: true },
2197
2214
  id: transactionId,
2198
2215
  proxyCodeChallenge: proxyPkce.challenge,
2199
2216
  proxyCodeVerifier: proxyPkce.verifier,
@@ -2207,11 +2224,12 @@ var OAuthProxy = (_class4 = class {
2207
2224
  /**
2208
2225
  * Exchange authorization code with upstream provider
2209
2226
  */
2210
- async exchangeUpstreamCode(code, transaction) {
2227
+ async exchangeUpstreamCode(code, exchange) {
2228
+ const forwardsPkce = "clientCodeVerifier" in exchange;
2211
2229
  const useBasicAuth = this.config.upstreamTokenEndpointAuthMethod === "client_secret_basic";
2212
2230
  const bodyParams = {
2213
2231
  code,
2214
- code_verifier: transaction.proxyCodeVerifier,
2232
+ code_verifier: forwardsPkce ? exchange.clientCodeVerifier : exchange.proxyCodeVerifier,
2215
2233
  grant_type: "authorization_code",
2216
2234
  redirect_uri: `${this.config.baseUrl}${this.getRedirectPath()}`
2217
2235
  };
@@ -2239,7 +2257,10 @@ var OAuthProxy = (_class4 = class {
2239
2257
  throw new OAuthProxyError(errorCode, errorDescription);
2240
2258
  }
2241
2259
  const tokens = await this.parseTokenResponse(tokenResponse);
2242
- await this.verifyUpstreamAccessToken(tokens.access_token, "access_denied");
2260
+ await this.verifyUpstreamAccessToken(
2261
+ tokens.access_token,
2262
+ forwardsPkce ? "invalid_grant" : "access_denied"
2263
+ );
2243
2264
  return {
2244
2265
  accessToken: tokens.access_token,
2245
2266
  // 0 when the upstream did not say, so `accessTokenTtl` and the defaults
@@ -2250,7 +2271,7 @@ var OAuthProxy = (_class4 = class {
2250
2271
  issuedAt: /* @__PURE__ */ new Date(),
2251
2272
  refreshExpiresIn: tokens.refresh_expires_in,
2252
2273
  refreshToken: tokens.refresh_token,
2253
- scope: tokens.scope ? tokens.scope.split(" ") : transaction.scope,
2274
+ scope: tokens.scope ? tokens.scope.split(" ") : exchange.scope,
2254
2275
  tokenType: tokens.token_type || "Bearer"
2255
2276
  };
2256
2277
  }
@@ -2326,9 +2347,9 @@ var OAuthProxy = (_class4 = class {
2326
2347
  /**
2327
2348
  * Generate authorization code for client
2328
2349
  */
2329
- async generateAuthorizationCode(transaction, upstreamTokens) {
2350
+ async generateAuthorizationCode(transaction, upstream) {
2330
2351
  const code = this.generateId();
2331
- const clientCode = {
2352
+ await this.stateStore.saveClientCode({
2332
2353
  clientId: transaction.clientId,
2333
2354
  code,
2334
2355
  codeChallenge: transaction.clientCodeChallenge,
@@ -2339,9 +2360,8 @@ var OAuthProxy = (_class4 = class {
2339
2360
  ),
2340
2361
  redirectUri: transaction.clientCallbackUrl,
2341
2362
  transactionId: transaction.id,
2342
- upstreamTokens
2343
- };
2344
- await this.stateStore.saveClientCode(clientCode);
2363
+ ..."accessToken" in upstream ? { upstreamTokens: upstream } : { upstreamCode: upstream }
2364
+ });
2345
2365
  return code;
2346
2366
  }
2347
2367
  /**
@@ -2917,13 +2937,11 @@ var OAuthProxy = (_class4 = class {
2917
2937
  if (transaction.scope.length > 0) {
2918
2938
  authUrl.searchParams.set("scope", transaction.scope.join(" "));
2919
2939
  }
2920
- if (!this.config.forwardPkce) {
2921
- authUrl.searchParams.set(
2922
- "code_challenge",
2923
- transaction.proxyCodeChallenge
2924
- );
2925
- authUrl.searchParams.set("code_challenge_method", "S256");
2926
- }
2940
+ authUrl.searchParams.set(
2941
+ "code_challenge",
2942
+ transaction.forwardPkce ? transaction.clientCodeChallenge : transaction.proxyCodeChallenge
2943
+ );
2944
+ authUrl.searchParams.set("code_challenge_method", "S256");
2927
2945
  return new Response(null, {
2928
2946
  headers: {
2929
2947
  Location: authUrl.toString()
@@ -3972,4 +3990,4 @@ Original error: ${error.message}`
3972
3990
 
3973
3991
 
3974
3992
  exports.withoutTrailingSlash = withoutTrailingSlash; exports.getAuthSession = getAuthSession; exports.requireAll = requireAll; exports.requireAny = requireAny; exports.requireAuth = requireAuth; exports.requireRole = requireRole; exports.requireScopes = requireScopes; exports.DEFAULT_ACCESS_TOKEN_TTL = DEFAULT_ACCESS_TOKEN_TTL; exports.DEFAULT_ACCESS_TOKEN_TTL_NO_REFRESH = DEFAULT_ACCESS_TOKEN_TTL_NO_REFRESH; exports.DEFAULT_REFRESH_TOKEN_TTL = DEFAULT_REFRESH_TOKEN_TTL; exports.DEFAULT_AUTHORIZATION_CODE_TTL = DEFAULT_AUTHORIZATION_CODE_TTL; exports.DEFAULT_TRANSACTION_TTL = DEFAULT_TRANSACTION_TTL; exports.ConsentManager = ConsentManager; exports.JWTIssuer = JWTIssuer; exports.PKCEUtils = PKCEUtils; exports.EncryptedTokenStorage = EncryptedTokenStorage; exports.MemoryTokenStorage = MemoryTokenStorage; exports.OAuthProxy = OAuthProxy; exports.OAuthProxyError = OAuthProxyError; exports.AuthProvider = AuthProvider; exports.AzureProvider = AzureProvider; exports.GitHubProvider = GitHubProvider; exports.GoogleProvider = GoogleProvider; exports.OAuthProvider = OAuthProvider; exports.DiskStore = DiskStore; exports.JWKSVerifier = JWKSVerifier;
3975
- //# sourceMappingURL=chunk-IWNTAHGZ.cjs.map
3993
+ //# sourceMappingURL=chunk-LVPE26OW.cjs.map