@oauth-spa-kit/server 2.7.0 → 2.7.2

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/authzen.js +46 -11
  2. package/package.json +2 -2
package/dist/authzen.js CHANGED
@@ -1,4 +1,6 @@
1
+ import { createDpopProof, importDpopKeyPair } from "@oauth-spa-kit/core";
1
2
  import { getAuthorizationHeader } from "./handlers.js";
3
+ import { readSession } from "./session.js";
2
4
  export class AuthzenError extends Error {
3
5
  status;
4
6
  body;
@@ -12,16 +14,28 @@ export class AuthzenError extends Error {
12
14
  function pdpEndpoint(pdpUrl, path) {
13
15
  return `${pdpUrl.replace(/\/+$/, "")}/${path}`;
14
16
  }
15
- async function postToPdp(authzen, path, authorizationHeader, body) {
17
+ async function postToPdp(authzen, path, authorizationHeader, dpop, body) {
16
18
  const fetchImpl = authzen.fetchImpl ?? fetch;
17
- const response = await fetchImpl(pdpEndpoint(authzen.pdpUrl, path), {
18
- method: "POST",
19
- headers: {
19
+ const url = pdpEndpoint(authzen.pdpUrl, path);
20
+ const htu = new URL(url);
21
+ async function post(nonce) {
22
+ const headers = {
20
23
  "content-type": "application/json",
21
24
  authorization: authorizationHeader,
22
- },
23
- body: JSON.stringify(body),
24
- });
25
+ };
26
+ if (dpop) {
27
+ headers.DPoP = await createDpopProof({
28
+ keyPair: dpop.keyPair, htm: "POST", htu: `${htu.origin}${htu.pathname}`, accessToken: dpop.accessToken, nonce,
29
+ });
30
+ }
31
+ return fetchImpl(url, { method: "POST", headers, body: JSON.stringify(body) });
32
+ }
33
+ let response = await post();
34
+ if (response.status === 401 && dpop) {
35
+ const nonce = response.headers.get("DPoP-Nonce");
36
+ if (nonce)
37
+ response = await post(nonce);
38
+ }
25
39
  if (!response.ok) {
26
40
  const responseBody = await response.text().catch(() => "");
27
41
  throw new AuthzenError(`AuthZEN PDP call to ${path} failed with status ${response.status}`, response.status, responseBody);
@@ -29,8 +43,8 @@ async function postToPdp(authzen, path, authorizationHeader, body) {
29
43
  return response.json();
30
44
  }
31
45
  /**
32
- * Calls the PDP with the caller's own session -- the same access token
33
- * `getAuthorizationHeader` would hand to any other upstream resource
46
+ * Calls the PDP with the caller's own session -- the same DPoP-bound access
47
+ * token `getAuthorizationHeader` would hand to any other upstream resource
34
48
  * server -- and returns `null` when there's no session to authorize with,
35
49
  * the same "not logged in" signal `getAuthorizationHeader` itself uses.
36
50
  */
@@ -38,7 +52,10 @@ async function callPdp(request, config, authzen, path, body) {
38
52
  const auth = await getAuthorizationHeader(request, config);
39
53
  if (!auth)
40
54
  return null;
41
- const result = await postToPdp(authzen, path, auth.header, body);
55
+ const session = await readSession(request, config.session);
56
+ const dpopKeyPair = session?.dpopKeyPair ? await importDpopKeyPair(session.dpopKeyPair) : undefined;
57
+ const accessToken = auth.header.slice(auth.header.indexOf(" ") + 1);
58
+ const result = await postToPdp(authzen, path, auth.header, dpopKeyPair ? { keyPair: dpopKeyPair, accessToken } : undefined, body);
42
59
  return { result, setCookie: auth.setCookie };
43
60
  }
44
61
  /** Single access decision -- `POST /access/v1/evaluation`. */
@@ -65,8 +82,26 @@ export async function resolveOwnSubject(request, config, resolver) {
65
82
  const auth = await getAuthorizationHeader(request, config);
66
83
  if (!auth)
67
84
  return null;
85
+ const session = await readSession(request, config.session);
86
+ const dpopKeyPair = session?.dpopKeyPair ? await importDpopKeyPair(session.dpopKeyPair) : undefined;
87
+ const accessToken = auth.header.slice(auth.header.indexOf(" ") + 1);
68
88
  const fetchImpl = resolver.fetchImpl ?? fetch;
69
- const response = await fetchImpl(resolver.endpoint, { headers: { authorization: auth.header } });
89
+ const endpointUrl = new URL(resolver.endpoint);
90
+ async function get(nonce) {
91
+ const headers = { authorization: auth.header };
92
+ if (dpopKeyPair) {
93
+ headers.DPoP = await createDpopProof({
94
+ keyPair: dpopKeyPair, htm: "GET", htu: `${endpointUrl.origin}${endpointUrl.pathname}`, accessToken, nonce,
95
+ });
96
+ }
97
+ return fetchImpl(resolver.endpoint, { headers });
98
+ }
99
+ let response = await get();
100
+ if (response.status === 401 && dpopKeyPair) {
101
+ const nonce = response.headers.get("DPoP-Nonce");
102
+ if (nonce)
103
+ response = await get(nonce);
104
+ }
70
105
  if (!response.ok) {
71
106
  const body = await response.text().catch(() => "");
72
107
  throw new AuthzenError(`Subject resolver call to ${resolver.endpoint} failed with status ${response.status}`, response.status, body);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oauth-spa-kit/server",
3
- "version": "2.7.0",
3
+ "version": "2.7.2",
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.7.0"
25
+ "@oauth-spa-kit/core": "2.7.2"
26
26
  },
27
27
  "devDependencies": {
28
28
  "typescript": "^5.6.0",