@oauth-spa-kit/server 2.0.6 → 2.1.0

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/README.md CHANGED
@@ -26,6 +26,7 @@ import {
26
26
  createSessionHandler,
27
27
  createLogoutHandler,
28
28
  getAuthorizationHeader,
29
+ getUserInfo,
29
30
  type OAuthHandlersConfig,
30
31
  } from "@oauth-spa-kit/server";
31
32
 
@@ -55,6 +56,11 @@ export const logout = createLogoutHandler(config);
55
56
 
56
57
  // From any other API route that needs to call an upstream resource server:
57
58
  const authHeader = await getAuthorizationHeader(request, config);
59
+
60
+ // Or, for the AS's own userinfo_endpoint specifically (DPoP proof, nonce
61
+ // retry, and token refresh all handled for you):
62
+ const info = await getUserInfo(request, config);
63
+ if (info) console.log(info.claims); // raw claims, shape depends on your granted scopes
58
64
  ```
59
65
 
60
66
  The SPA never sees an access or refresh token -- only `GET /auth/session`'s
@@ -42,3 +42,17 @@ export interface AuthorizationHeaderResult {
42
42
  * the token never crosses back into the browser.
43
43
  */
44
44
  export declare function getAuthorizationHeader(request: Request, config: OAuthHandlersConfig): Promise<AuthorizationHeaderResult | null>;
45
+ export interface UserInfoResult {
46
+ /** Whatever the AS's userinfo_endpoint returned -- raw, unmapped claims (shape varies per AS/scope grant). */
47
+ claims: Record<string, unknown>;
48
+ /** Set only when the access token was refreshed -- forward this Set-Cookie on your outgoing response. */
49
+ setCookie?: string;
50
+ }
51
+ /**
52
+ * Calls the AS's userinfo_endpoint with the session's (DPoP-bound, if
53
+ * enabled) access token, refreshing it first if it's expiring soon -- the
54
+ * id_token is meant to be a thin proof of authentication at login time, not
55
+ * a source of the current profile, which is why this hits userinfo_endpoint
56
+ * rather than just decoding the stored id_token.
57
+ */
58
+ export declare function getUserInfo(request: Request, config: OAuthHandlersConfig): Promise<UserInfoResult | null>;
package/dist/handlers.js CHANGED
@@ -1,4 +1,4 @@
1
- import { computeJwkThumbprint, createPkceParams, discoverOidcConfiguration, exchangeAuthorizationCode, exchangeRefreshToken, exportDpopKeyPair, fetchJwks, generateDpopKeyPair, importDpopKeyPair, pushAuthorizationRequest, verifyJwt, OAuthError, TokenExchangeError, } from "@oauth-spa-kit/core";
1
+ import { computeJwkThumbprint, createDpopProof, 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) {
@@ -250,3 +250,46 @@ export async function getAuthorizationHeader(request, config) {
250
250
  }
251
251
  return { header: `${session.tokens.tokenType} ${session.tokens.accessToken}` };
252
252
  }
253
+ /**
254
+ * Calls the AS's userinfo_endpoint with the session's (DPoP-bound, if
255
+ * enabled) access token, refreshing it first if it's expiring soon -- the
256
+ * id_token is meant to be a thin proof of authentication at login time, not
257
+ * a source of the current profile, which is why this hits userinfo_endpoint
258
+ * rather than just decoding the stored id_token.
259
+ */
260
+ export async function getUserInfo(request, config) {
261
+ const session = await readSession(request, config.session);
262
+ if (!session)
263
+ return null;
264
+ const auth = await getAuthorizationHeader(request, config);
265
+ if (!auth)
266
+ return null;
267
+ const discovery = await resolveDiscovery(config);
268
+ if (!discovery.userinfo_endpoint) {
269
+ throw new OAuthError("Discovery document has no userinfo_endpoint");
270
+ }
271
+ const userinfoEndpoint = discovery.userinfo_endpoint;
272
+ const dpopKeyPair = session.dpopKeyPair ? await importDpopKeyPair(session.dpopKeyPair) : undefined;
273
+ const accessToken = auth.header.slice(auth.header.indexOf(" ") + 1);
274
+ const fetchImpl = config.fetchImpl ?? fetch;
275
+ async function callUserInfo(nonce) {
276
+ const headers = { Authorization: auth.header };
277
+ if (dpopKeyPair) {
278
+ headers.DPoP = await createDpopProof({ keyPair: dpopKeyPair, htm: "GET", htu: userinfoEndpoint, accessToken, nonce });
279
+ }
280
+ return fetchImpl(userinfoEndpoint, { headers });
281
+ }
282
+ // DPoP resource endpoints can nonce-challenge the same way the token endpoint does (RFC 9449
283
+ // section 8), just via WWW-Authenticate/DPoP-Nonce response headers instead of a JSON body --
284
+ // so this retries once, mirroring the token-endpoint retry in @oauth-spa-kit/core.
285
+ let response = await callUserInfo();
286
+ if (response.status === 401 && dpopKeyPair) {
287
+ const nonce = response.headers.get("DPoP-Nonce");
288
+ if (nonce)
289
+ response = await callUserInfo(nonce);
290
+ }
291
+ if (!response.ok) {
292
+ throw new OAuthError(`userinfo_endpoint returned ${response.status}`);
293
+ }
294
+ return { claims: (await response.json()), setCookie: auth.setCookie };
295
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oauth-spa-kit/server",
3
- "version": "2.0.6",
3
+ "version": "2.1.0",
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.6"
25
+ "@oauth-spa-kit/core": "2.1.0"
26
26
  },
27
27
  "devDependencies": {
28
28
  "typescript": "^5.6.0",