@oauth-spa-kit/server 2.0.2 → 2.0.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.
Files changed (2) hide show
  1. package/dist/handlers.js +26 -6
  2. package/package.json +2 -2
package/dist/handlers.js CHANGED
@@ -1,4 +1,4 @@
1
- import { computeJwkThumbprint, createPkceParams, discoverOidcConfiguration, exchangeAuthorizationCode, exchangeRefreshToken, exportDpopKeyPair, fetchJwks, generateDpopKeyPair, importDpopKeyPair, pushAuthorizationRequest, verifyJwt, OAuthError, } from "@oauth-spa-kit/core";
1
+ import { computeJwkThumbprint, createPkceParams, discoverOidcConfiguration, exchangeAuthorizationCode, exchangeRefreshToken, exportDpopKeyPair, fetchJwks, generateDpopKeyPair, importDpopKeyPair, pushAuthorizationRequest, verifyJwt, OAuthError, TokenExchangeError, } from "@oauth-spa-kit/core";
2
2
  import { clearPkceStateHeader, readPkceState, writePkceStateHeader } from "./pkceState.js";
3
3
  import { clearSessionHeader, readSession, writeSessionHeader } from "./session.js";
4
4
  async function resolveDiscovery(config) {
@@ -81,17 +81,34 @@ export function createLoginHandler(config) {
81
81
  });
82
82
  };
83
83
  }
84
+ /**
85
+ * Redirects off the (code-bearing, single-use) callback URL on failure, the
86
+ * same way the success path already redirects to `returnTo` -- never render
87
+ * an error directly on `/auth/callback?code=...`. Leaving the browser parked
88
+ * there means a plain reload resubmits the already-spent `code`, which the
89
+ * AS then (correctly) rejects a second time as `invalid_grant`, forever,
90
+ * until the user manually navigates elsewhere.
91
+ */
92
+ function redirectToReturnToWithError(requestUrl, returnTo, errorCode) {
93
+ const target = new URL(returnTo, requestUrl);
94
+ target.searchParams.set("login_error", errorCode);
95
+ return new Response(null, {
96
+ status: 302,
97
+ headers: { Location: target.toString(), "Set-Cookie": clearPkceStateHeader() },
98
+ });
99
+ }
84
100
  export function createCallbackHandler(config) {
85
101
  return async function callbackHandler(request) {
86
102
  const url = new URL(request.url);
103
+ const pkce = await readPkceState(request, config.session.password);
104
+ const fallbackReturnTo = pkce?.returnTo ?? config.defaultReturnTo ?? "/";
87
105
  const error = url.searchParams.get("error");
88
106
  if (error)
89
- return new Response(`OAuth error: ${error}`, { status: 400 });
107
+ return redirectToReturnToWithError(url, fallbackReturnTo, error);
90
108
  const code = url.searchParams.get("code");
91
109
  const state = url.searchParams.get("state");
92
- const pkce = await readPkceState(request, config.session.password);
93
110
  if (!code || !state || !pkce || state !== pkce.state) {
94
- return new Response("Invalid or expired OAuth state -- restart login.", { status: 400 });
111
+ return redirectToReturnToWithError(url, fallbackReturnTo, "invalid_state");
95
112
  }
96
113
  const discovery = await resolveDiscovery(config);
97
114
  const dpopKeyPair = pkce.dpopKeyPair ? await importDpopKeyPair(pkce.dpopKeyPair) : undefined;
@@ -118,8 +135,11 @@ export function createCallbackHandler(config) {
118
135
  return new Response(null, { status: 302, headers });
119
136
  }
120
137
  catch (cause) {
121
- const message = cause instanceof Error ? cause.message : "Unknown error";
122
- return new Response(`Login failed: ${message}`, { status: 400, headers: { "Set-Cookie": clearPkceStateHeader() } });
138
+ // Prefer the AS's own OAuth error code (e.g. "invalid_grant") over the
139
+ // full exception message -- short, stable, safe to put in a URL query
140
+ // param; the full message may embed the AS's free-text error_description.
141
+ const errorCode = cause instanceof TokenExchangeError ? cause.error ?? "login_failed" : "login_failed";
142
+ return redirectToReturnToWithError(url, pkce.returnTo, errorCode);
123
143
  }
124
144
  };
125
145
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oauth-spa-kit/server",
3
- "version": "2.0.2",
3
+ "version": "2.0.3",
4
4
  "description": "BFF-pattern OAuth handlers: sealed HttpOnly-cookie sessions + login/callback/refresh/logout route factories, built on Web-standard Request/Response so they drop into Nitro, Next.js route handlers, Cloudflare Workers, or plain Node (via a small adapter).",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -22,7 +22,7 @@
22
22
  "node": ">=18"
23
23
  },
24
24
  "dependencies": {
25
- "@oauth-spa-kit/core": "2.0.2"
25
+ "@oauth-spa-kit/core": "2.0.3"
26
26
  },
27
27
  "devDependencies": {
28
28
  "typescript": "^5.6.0",