@oauth-spa-kit/server 2.0.5 → 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 +6 -0
- package/dist/handlers.d.ts +15 -1
- package/dist/handlers.js +56 -5
- package/package.json +2 -2
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
|
package/dist/handlers.d.ts
CHANGED
|
@@ -29,7 +29,7 @@ export interface OAuthHandlersConfig {
|
|
|
29
29
|
export declare function createLoginHandler(config: OAuthHandlersConfig): (request: Request) => Promise<Response>;
|
|
30
30
|
export declare function createCallbackHandler(config: OAuthHandlersConfig): (request: Request) => Promise<Response>;
|
|
31
31
|
export declare function createSessionHandler(config: OAuthHandlersConfig): (request: Request) => Promise<Response>;
|
|
32
|
-
export declare function createLogoutHandler(config: OAuthHandlersConfig): (
|
|
32
|
+
export declare function createLogoutHandler(config: OAuthHandlersConfig): (request: Request) => Promise<Response>;
|
|
33
33
|
export interface AuthorizationHeaderResult {
|
|
34
34
|
header: string;
|
|
35
35
|
/** Set only when the token was refreshed -- forward this Set-Cookie on your outgoing response. */
|
|
@@ -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) {
|
|
@@ -196,10 +196,15 @@ export function createSessionHandler(config) {
|
|
|
196
196
|
};
|
|
197
197
|
}
|
|
198
198
|
export function createLogoutHandler(config) {
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
199
|
+
return async function logoutHandler(request) {
|
|
200
|
+
// Read before clearing: id_token_hint (OIDC RP-Initiated Logout 1.0 section 2) is what
|
|
201
|
+
// lets the AS resolve the requesting client and validate/honor post_logout_redirect_uri
|
|
202
|
+
// without an extra confirmation step -- a bare client_id query param proves nothing (any
|
|
203
|
+
// page could set it), so an AS that takes client identity seriously only trusts it when
|
|
204
|
+
// accompanied by the id_token_hint proving this really was that client's session. Without
|
|
205
|
+
// it, expect the AS to fall back to an interactive "are you sure?" prompt and/or drop
|
|
206
|
+
// post_logout_redirect_uri entirely instead of honoring it.
|
|
207
|
+
const session = await readSession(request, config.session);
|
|
203
208
|
const headers = new Headers({ "content-type": "application/json" });
|
|
204
209
|
headers.append("Set-Cookie", clearSessionHeader(config.session));
|
|
205
210
|
const discovery = await resolveDiscovery(config).catch(() => null);
|
|
@@ -207,6 +212,9 @@ export function createLogoutHandler(config) {
|
|
|
207
212
|
const endSessionUrl = new URL(discovery.end_session_endpoint);
|
|
208
213
|
endSessionUrl.searchParams.set("client_id", config.oauth.clientId);
|
|
209
214
|
endSessionUrl.searchParams.set("post_logout_redirect_uri", config.oauth.postLogoutRedirectUri);
|
|
215
|
+
if (session?.tokens.idToken) {
|
|
216
|
+
endSessionUrl.searchParams.set("id_token_hint", session.tokens.idToken);
|
|
217
|
+
}
|
|
210
218
|
headers.set("Location", endSessionUrl.toString());
|
|
211
219
|
return new Response(null, { status: 302, headers });
|
|
212
220
|
}
|
|
@@ -242,3 +250,46 @@ export async function getAuthorizationHeader(request, config) {
|
|
|
242
250
|
}
|
|
243
251
|
return { header: `${session.tokens.tokenType} ${session.tokens.accessToken}` };
|
|
244
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
|
|
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
|
|
25
|
+
"@oauth-spa-kit/core": "2.1.0"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
28
|
"typescript": "^5.6.0",
|