@pontive/pontkit-server 1.3.0 → 1.4.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/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +263 -196
- package/dist/index.js.map +1 -1
- package/dist/oidc.d.ts +2 -1
- package/dist/organization.d.ts +32 -0
- package/dist/server-auth.d.ts +7 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { jwtVerify as
|
|
2
|
-
const
|
|
3
|
-
function
|
|
1
|
+
import { jwtVerify as O, createLocalJWKSet as ie, decodeJwt as ae, jwtDecrypt as ce, EncryptJWT as ue } from "jose";
|
|
2
|
+
const b = 3500, v = 16;
|
|
3
|
+
function P(t) {
|
|
4
4
|
const e = /* @__PURE__ */ new Map();
|
|
5
5
|
if (!t) return e;
|
|
6
6
|
for (const r of t.split(";")) {
|
|
@@ -11,160 +11,186 @@ function U(t) {
|
|
|
11
11
|
}
|
|
12
12
|
return e;
|
|
13
13
|
}
|
|
14
|
-
function
|
|
14
|
+
function M(t) {
|
|
15
15
|
return [...t].map(([e, r]) => `${e}=${r}`).join("; ");
|
|
16
16
|
}
|
|
17
|
-
function
|
|
17
|
+
function E(t, e, r) {
|
|
18
18
|
const n = [`${t}=${e}`, "Path=/", "HttpOnly", "SameSite=Lax", `Max-Age=${r.maxAge}`];
|
|
19
19
|
return r.secure && n.push("Secure"), n.join("; ");
|
|
20
20
|
}
|
|
21
|
-
function
|
|
21
|
+
function R(t, e) {
|
|
22
22
|
return e === 0 ? t : `${t}.${e}`;
|
|
23
23
|
}
|
|
24
|
-
function
|
|
24
|
+
function de(t, e) {
|
|
25
25
|
const r = t.get(e);
|
|
26
26
|
if (r === void 0) return null;
|
|
27
27
|
let n = r;
|
|
28
|
-
for (let s = 1; s <
|
|
29
|
-
const
|
|
30
|
-
if (
|
|
31
|
-
n +=
|
|
28
|
+
for (let s = 1; s < v; s++) {
|
|
29
|
+
const u = t.get(R(e, s));
|
|
30
|
+
if (u === void 0) break;
|
|
31
|
+
n += u;
|
|
32
32
|
}
|
|
33
33
|
return n;
|
|
34
34
|
}
|
|
35
|
-
function
|
|
36
|
-
const s = [],
|
|
37
|
-
for (let c = 0; c <
|
|
38
|
-
const
|
|
39
|
-
t.set(
|
|
35
|
+
function he(t, e, r, n) {
|
|
36
|
+
const s = [], u = Math.max(1, Math.ceil(r.length / b));
|
|
37
|
+
for (let c = 0; c < u; c++) {
|
|
38
|
+
const a = r.slice(c * b, (c + 1) * b);
|
|
39
|
+
t.set(R(e, c), a), s.push(E(R(e, c), a, n));
|
|
40
40
|
}
|
|
41
|
-
for (let c =
|
|
42
|
-
const
|
|
43
|
-
if (!t.has(
|
|
44
|
-
t.delete(
|
|
41
|
+
for (let c = u; c < v; c++) {
|
|
42
|
+
const a = R(e, c);
|
|
43
|
+
if (!t.has(a)) break;
|
|
44
|
+
t.delete(a), s.push(E(a, "", { ...n, maxAge: 0 }));
|
|
45
45
|
}
|
|
46
46
|
return s;
|
|
47
47
|
}
|
|
48
|
-
function
|
|
48
|
+
function N(t, e, r) {
|
|
49
49
|
const n = [];
|
|
50
|
-
for (let s = 0; s <
|
|
51
|
-
const
|
|
52
|
-
if (!t.has(
|
|
50
|
+
for (let s = 0; s < v; s++) {
|
|
51
|
+
const u = R(e, s);
|
|
52
|
+
if (!t.has(u)) {
|
|
53
53
|
if (s > 0) break;
|
|
54
54
|
continue;
|
|
55
55
|
}
|
|
56
|
-
t.delete(
|
|
56
|
+
t.delete(u), n.push(E(u, "", { maxAge: 0, secure: r }));
|
|
57
57
|
}
|
|
58
58
|
return n;
|
|
59
59
|
}
|
|
60
|
-
class
|
|
60
|
+
class x extends Error {
|
|
61
61
|
constructor(e, r, n) {
|
|
62
62
|
super(`token endpoint ${e} ${r}: ${n}`), this.status = e, this.code = r;
|
|
63
63
|
}
|
|
64
64
|
status;
|
|
65
65
|
code;
|
|
66
66
|
}
|
|
67
|
-
const
|
|
68
|
-
function
|
|
67
|
+
const le = 600 * 1e3, C = /* @__PURE__ */ new Map(), H = /* @__PURE__ */ new Map();
|
|
68
|
+
function fe(t) {
|
|
69
69
|
return t.replace(/\/+$/, "");
|
|
70
70
|
}
|
|
71
|
-
function
|
|
72
|
-
const r =
|
|
73
|
-
let n =
|
|
71
|
+
function I(t, e) {
|
|
72
|
+
const r = fe(t);
|
|
73
|
+
let n = C.get(r);
|
|
74
74
|
return n || (n = e(`${r}/.well-known/openid-configuration`).then(async (s) => {
|
|
75
75
|
if (!s.ok) throw new Error(`OIDC discovery failed for ${r}: ${s.status}`);
|
|
76
76
|
return await s.json();
|
|
77
77
|
}).catch((s) => {
|
|
78
|
-
throw
|
|
79
|
-
}),
|
|
78
|
+
throw C.delete(r), s;
|
|
79
|
+
}), C.set(r, n)), n;
|
|
80
80
|
}
|
|
81
|
-
async function
|
|
82
|
-
const n =
|
|
83
|
-
if (n && !r && Date.now() - n.at <
|
|
81
|
+
async function D(t, e, r) {
|
|
82
|
+
const n = H.get(t);
|
|
83
|
+
if (n && !r && Date.now() - n.at < le) return n.set;
|
|
84
84
|
const s = await e(t);
|
|
85
85
|
if (!s.ok) throw new Error(`JWKS fetch failed for ${t}: ${s.status}`);
|
|
86
|
-
const
|
|
87
|
-
return
|
|
86
|
+
const u = ie(await s.json());
|
|
87
|
+
return H.set(t, { at: Date.now(), set: u }), u;
|
|
88
88
|
}
|
|
89
|
-
function
|
|
89
|
+
function Z(t) {
|
|
90
90
|
let e = "";
|
|
91
91
|
for (const r of t) e += String.fromCharCode(r);
|
|
92
92
|
return btoa(e).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
93
93
|
}
|
|
94
|
-
function
|
|
95
|
-
return
|
|
94
|
+
function $() {
|
|
95
|
+
return Z(crypto.getRandomValues(new Uint8Array(32)));
|
|
96
96
|
}
|
|
97
|
-
async function
|
|
97
|
+
async function we(t) {
|
|
98
98
|
const e = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(t));
|
|
99
|
-
return
|
|
99
|
+
return Z(new Uint8Array(e));
|
|
100
100
|
}
|
|
101
|
-
async function
|
|
101
|
+
async function Y(t) {
|
|
102
102
|
return [...new Uint8Array(await crypto.subtle.digest("SHA-256", new TextEncoder().encode(t)))].map((r) => r.toString(16).padStart(2, "0")).join("");
|
|
103
103
|
}
|
|
104
|
-
async function
|
|
105
|
-
const n = await
|
|
106
|
-
return s.searchParams.set("response_type", "code"), s.searchParams.set("client_id", t.clientId), s.searchParams.set("redirect_uri", r.redirectUri), s.searchParams.set("scope", r.scopes), s.searchParams.set("state", r.state), s.searchParams.set("nonce", r.nonce), s.searchParams.set("code_challenge", r.codeChallenge), s.searchParams.set("code_challenge_method", "S256"), r.prompt && s.searchParams.set("prompt", r.prompt), s.toString();
|
|
104
|
+
async function pe(t, e, r) {
|
|
105
|
+
const n = await I(t.issuer, e), s = new URL(n.authorization_endpoint);
|
|
106
|
+
return s.searchParams.set("response_type", "code"), s.searchParams.set("client_id", t.clientId), s.searchParams.set("redirect_uri", r.redirectUri), s.searchParams.set("scope", r.scopes), s.searchParams.set("state", r.state), s.searchParams.set("nonce", r.nonce), s.searchParams.set("code_challenge", r.codeChallenge), s.searchParams.set("code_challenge_method", "S256"), r.prompt && s.searchParams.set("prompt", r.prompt), r.organizationId && s.searchParams.set("organization_id", r.organizationId), s.toString();
|
|
107
107
|
}
|
|
108
|
-
async function
|
|
109
|
-
const n = await
|
|
108
|
+
async function Q(t, e, r) {
|
|
109
|
+
const n = await I(t.issuer, e), s = new URLSearchParams(r);
|
|
110
110
|
s.set("client_id", t.clientId);
|
|
111
|
-
const
|
|
111
|
+
const u = { "content-type": "application/x-www-form-urlencoded" };
|
|
112
112
|
if (t.clientSecret) {
|
|
113
|
-
const
|
|
114
|
-
|
|
113
|
+
const a = `${encodeURIComponent(t.clientId)}:${encodeURIComponent(t.clientSecret)}`;
|
|
114
|
+
u.authorization = `Basic ${btoa(a)}`;
|
|
115
115
|
}
|
|
116
|
-
const c = await e(n.token_endpoint, { method: "POST", headers:
|
|
116
|
+
const c = await e(n.token_endpoint, { method: "POST", headers: u, body: s });
|
|
117
117
|
if (!c.ok) {
|
|
118
|
-
let
|
|
118
|
+
let a = "unknown_error", h = "";
|
|
119
119
|
try {
|
|
120
120
|
const f = await c.json();
|
|
121
|
-
|
|
121
|
+
a = f.error ?? a, h = f.error_description ?? "";
|
|
122
122
|
} catch {
|
|
123
123
|
}
|
|
124
|
-
throw new
|
|
124
|
+
throw new x(c.status, a, h);
|
|
125
125
|
}
|
|
126
126
|
return await c.json();
|
|
127
127
|
}
|
|
128
|
-
function
|
|
129
|
-
return
|
|
128
|
+
function ge(t, e, r) {
|
|
129
|
+
return Q(t, e, {
|
|
130
130
|
grant_type: "authorization_code",
|
|
131
131
|
code: r.code,
|
|
132
132
|
code_verifier: r.codeVerifier,
|
|
133
133
|
redirect_uri: r.redirectUri
|
|
134
134
|
});
|
|
135
135
|
}
|
|
136
|
-
function
|
|
137
|
-
return
|
|
136
|
+
function W(t, e, r, n) {
|
|
137
|
+
return Q(t, e, {
|
|
138
|
+
grant_type: "refresh_token",
|
|
139
|
+
refresh_token: r,
|
|
140
|
+
// Switches the session to that organization; see switchOrganization.
|
|
141
|
+
...n ? { organization_id: n } : {}
|
|
142
|
+
});
|
|
138
143
|
}
|
|
139
|
-
async function
|
|
140
|
-
const n = await
|
|
144
|
+
async function q(t, e, r) {
|
|
145
|
+
const n = await I(t.issuer, e), s = { issuer: n.issuer, audience: t.clientId };
|
|
141
146
|
try {
|
|
142
|
-
return (await
|
|
143
|
-
} catch (
|
|
144
|
-
if (
|
|
145
|
-
return (await
|
|
147
|
+
return (await O(r, await D(n.jwks_uri, e, !1), s)).payload;
|
|
148
|
+
} catch (u) {
|
|
149
|
+
if (u.code !== "ERR_JWKS_NO_MATCHING_KEY") throw u;
|
|
150
|
+
return (await O(r, await D(n.jwks_uri, e, !0), s)).payload;
|
|
146
151
|
}
|
|
147
152
|
}
|
|
148
|
-
async function
|
|
149
|
-
const s = await
|
|
153
|
+
async function me(t, e, r, n) {
|
|
154
|
+
const s = await q(t, e, r);
|
|
150
155
|
if (s.nonce !== n) throw new Error("id_token nonce does not match the sign-in request");
|
|
151
156
|
return s;
|
|
152
157
|
}
|
|
153
|
-
async function
|
|
154
|
-
const n = await
|
|
158
|
+
async function ke(t, e, r) {
|
|
159
|
+
const n = await I(t.issuer, e);
|
|
155
160
|
if (!n.userinfo_endpoint) return null;
|
|
156
161
|
const s = await e(n.userinfo_endpoint, { headers: { authorization: `Bearer ${r}` } });
|
|
157
162
|
if (!s.ok) throw new Error(`userinfo ${s.status}`);
|
|
158
163
|
return await s.json();
|
|
159
164
|
}
|
|
160
|
-
async function
|
|
161
|
-
const n = await
|
|
165
|
+
async function _e(t, e, r) {
|
|
166
|
+
const n = await I(t.issuer, e);
|
|
162
167
|
if (!n.end_session_endpoint) return null;
|
|
163
168
|
const s = new URL(n.end_session_endpoint);
|
|
164
169
|
return s.searchParams.set("client_id", t.clientId), s.searchParams.set("post_logout_redirect_uri", r.postLogoutRedirectUri), r.sessionId && s.searchParams.set("logout_hint", r.sessionId), s.toString();
|
|
165
170
|
}
|
|
166
|
-
const
|
|
167
|
-
function
|
|
171
|
+
const L = "switch-organization", Se = "organization_error", j = (t) => Array.isArray(t) ? t.filter((e) => typeof e == "string") : [];
|
|
172
|
+
function ee(t) {
|
|
173
|
+
const e = t.org_id;
|
|
174
|
+
return typeof e != "string" || e === "" ? null : {
|
|
175
|
+
id: e,
|
|
176
|
+
role: typeof t.role == "string" ? t.role : void 0,
|
|
177
|
+
roles: j(t.roles),
|
|
178
|
+
permissions: j(t.permissions)
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
function ye(t) {
|
|
182
|
+
try {
|
|
183
|
+
return ee(ae(t));
|
|
184
|
+
} catch {
|
|
185
|
+
return null;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
function Le(t, e, r = "/") {
|
|
189
|
+
const n = new URLSearchParams({ organization_id: e, returnTo: r });
|
|
190
|
+
return `${t}/${L}?${n.toString()}`;
|
|
191
|
+
}
|
|
192
|
+
const Pe = "__Host-pontive_access", Ue = "__Secure-pontive_session_state";
|
|
193
|
+
function Te(t) {
|
|
168
194
|
const e = (r) => typeof t[r] == "string" ? t[r] : void 0;
|
|
169
195
|
return {
|
|
170
196
|
id: e("sub") ?? "",
|
|
@@ -175,55 +201,72 @@ function we(t) {
|
|
|
175
201
|
emailVerified: t.email_verified === !0
|
|
176
202
|
};
|
|
177
203
|
}
|
|
178
|
-
function
|
|
204
|
+
function Ae(t) {
|
|
179
205
|
const e = { issuer: t.issuer, clientId: t.clientId }, r = /* @__PURE__ */ new Map();
|
|
180
206
|
function n(c) {
|
|
181
|
-
for (const [
|
|
182
|
-
h.expiresAt <= c && r.delete(
|
|
207
|
+
for (const [a, h] of r)
|
|
208
|
+
h.expiresAt <= c && r.delete(a);
|
|
183
209
|
}
|
|
184
|
-
async function s(c,
|
|
210
|
+
async function s(c, a) {
|
|
185
211
|
const h = Date.now();
|
|
186
212
|
n(h);
|
|
187
|
-
const f = await
|
|
213
|
+
const f = await Y(c), w = r.get(f);
|
|
188
214
|
if (w) return w.profile;
|
|
189
|
-
const
|
|
190
|
-
return r.set(f, { expiresAt:
|
|
215
|
+
const m = ke(e, t.fetch, c).catch((T) => (r.delete(f), t.logger.warn("[pontive] could not read the user's profile:", T instanceof Error ? T.message : String(T)), null));
|
|
216
|
+
return r.set(f, { expiresAt: a, profile: m }), m;
|
|
191
217
|
}
|
|
192
|
-
async function
|
|
193
|
-
const
|
|
194
|
-
if (!
|
|
218
|
+
async function u(c) {
|
|
219
|
+
const a = P(c).get(Pe);
|
|
220
|
+
if (!a) return null;
|
|
195
221
|
let h;
|
|
196
222
|
try {
|
|
197
|
-
h = await
|
|
198
|
-
} catch (
|
|
199
|
-
return
|
|
223
|
+
h = await q(e, t.fetch, a);
|
|
224
|
+
} catch (m) {
|
|
225
|
+
return m.code !== "ERR_JWT_EXPIRED" && t.logger.warn("[pontive] access token cookie refused:", m instanceof Error ? m.message : String(m)), null;
|
|
200
226
|
}
|
|
201
|
-
const f = (h.exp ?? 0) * 1e3, w = await s(
|
|
227
|
+
const f = (h.exp ?? 0) * 1e3, w = await s(a, f);
|
|
202
228
|
return {
|
|
203
229
|
// The token's own claims win for sub: the profile describes the user the
|
|
204
230
|
// token names, never another one.
|
|
205
|
-
user:
|
|
206
|
-
accessToken:
|
|
231
|
+
user: Te({ ...w, ...h, sub: h.sub }),
|
|
232
|
+
accessToken: a,
|
|
207
233
|
expiresAt: f,
|
|
208
|
-
sessionId: typeof h.sid == "string" ? h.sid : void 0
|
|
234
|
+
sessionId: typeof h.sid == "string" ? h.sid : void 0,
|
|
235
|
+
organization: ee(h)
|
|
209
236
|
};
|
|
210
237
|
}
|
|
211
238
|
return {
|
|
212
239
|
// Sign-in and sign-out belong to the browser SDK's widgets in this mode.
|
|
213
|
-
|
|
214
|
-
|
|
240
|
+
// Switching organization is the one route: the browser is sent through
|
|
241
|
+
// the auth server's session handshake with the organization, which renews
|
|
242
|
+
// its cookies into it — the browser stays the only thing that spends the
|
|
243
|
+
// refresh token. A refused switch comes back with organization_error.
|
|
244
|
+
async handle(c) {
|
|
245
|
+
const a = new URL(c.url);
|
|
246
|
+
if (a.pathname !== `${t.basePath}/${L}`) return null;
|
|
247
|
+
const h = a.searchParams.get("organization_id"), f = Re(a.searchParams.get("returnTo"));
|
|
248
|
+
if (!h)
|
|
249
|
+
return F(new URL(`${f}`, a.origin).toString());
|
|
250
|
+
const w = new URL(`${t.authPath}/oauth2/session`, a.origin);
|
|
251
|
+
return w.searchParams.set("client_id", t.clientId), w.searchParams.set("return_to", f), w.searchParams.set("organization_id", h), F(w.toString());
|
|
215
252
|
},
|
|
216
|
-
getSession:
|
|
253
|
+
getSession: u,
|
|
217
254
|
async refresh(c) {
|
|
218
|
-
const
|
|
219
|
-
if (h || !
|
|
220
|
-
const w = new URL(c.url),
|
|
221
|
-
return
|
|
255
|
+
const a = c.headers.get("cookie"), h = await u(a), f = { session: h, setCookies: [], cookieHeader: a ?? "" };
|
|
256
|
+
if (h || !P(a).has(Ue)) return f;
|
|
257
|
+
const w = new URL(c.url), m = new URL(`${t.authPath}/oauth2/session`, w.origin);
|
|
258
|
+
return m.searchParams.set("client_id", t.clientId), m.searchParams.set("return_to", w.pathname + w.search), { ...f, handshake: m.pathname + m.search };
|
|
222
259
|
}
|
|
223
260
|
};
|
|
224
261
|
}
|
|
225
|
-
|
|
226
|
-
|
|
262
|
+
function Re(t) {
|
|
263
|
+
return t && t.startsWith("/") && !t.startsWith("//") && !t.startsWith("/\\") ? t : "/";
|
|
264
|
+
}
|
|
265
|
+
function F(t) {
|
|
266
|
+
return new Response(null, { status: 302, headers: { location: t, "cache-control": "no-store" } });
|
|
267
|
+
}
|
|
268
|
+
const Ee = 10080 * 60;
|
|
269
|
+
function Ie(t) {
|
|
227
270
|
const e = t.clientSecret ? "server" : "browser", r = ["issuer", "clientId"];
|
|
228
271
|
e === "server" && r.push("sessionSecret");
|
|
229
272
|
for (const n of r)
|
|
@@ -252,27 +295,27 @@ function ke(t) {
|
|
|
252
295
|
prompt: t.prompt,
|
|
253
296
|
cookieName: t.cookieName ?? "pontive_session",
|
|
254
297
|
secureCookies: t.secureCookies,
|
|
255
|
-
sessionMaxAgeSeconds: t.sessionMaxAgeSeconds ??
|
|
298
|
+
sessionMaxAgeSeconds: t.sessionMaxAgeSeconds ?? Ee,
|
|
256
299
|
refreshSkewSeconds: t.refreshSkewSeconds ?? 60,
|
|
257
300
|
fetch: t.fetch ?? ((...n) => fetch(...n)),
|
|
258
301
|
logger: t.logger ?? console
|
|
259
302
|
};
|
|
260
303
|
}
|
|
261
|
-
const
|
|
262
|
-
class
|
|
263
|
-
constructor(e =
|
|
304
|
+
const be = 3e4;
|
|
305
|
+
class Ce {
|
|
306
|
+
constructor(e = be) {
|
|
264
307
|
this.recentMs = e;
|
|
265
308
|
}
|
|
266
309
|
recentMs;
|
|
267
310
|
inFlight = /* @__PURE__ */ new Map();
|
|
268
311
|
recent = /* @__PURE__ */ new Map();
|
|
269
312
|
async run(e, r) {
|
|
270
|
-
const n = await
|
|
313
|
+
const n = await Y(e);
|
|
271
314
|
this.sweep();
|
|
272
315
|
const s = this.recent.get(n);
|
|
273
316
|
if (s) return s.result;
|
|
274
|
-
let
|
|
275
|
-
return
|
|
317
|
+
let u = this.inFlight.get(n);
|
|
318
|
+
return u || (u = r().then((c) => (this.recent.set(n, { at: Date.now(), result: c }), c)).finally(() => this.inFlight.delete(n)), this.inFlight.set(n, u)), u;
|
|
276
319
|
}
|
|
277
320
|
sweep() {
|
|
278
321
|
const e = Date.now() - this.recentMs;
|
|
@@ -280,24 +323,24 @@ class Se {
|
|
|
280
323
|
n.at < e && this.recent.delete(r);
|
|
281
324
|
}
|
|
282
325
|
}
|
|
283
|
-
const
|
|
284
|
-
function
|
|
285
|
-
let e =
|
|
286
|
-
return e || (e = crypto.subtle.digest("SHA-256", new TextEncoder().encode(t)).then((r) => new Uint8Array(r)),
|
|
326
|
+
const K = /* @__PURE__ */ new Map();
|
|
327
|
+
function te(t) {
|
|
328
|
+
let e = K.get(t);
|
|
329
|
+
return e || (e = crypto.subtle.digest("SHA-256", new TextEncoder().encode(t)).then((r) => new Uint8Array(r)), K.set(t, e)), e;
|
|
287
330
|
}
|
|
288
|
-
async function
|
|
289
|
-
return new
|
|
331
|
+
async function G(t, e, r) {
|
|
332
|
+
return new ue(t).setProtectedHeader({ alg: "dir", enc: "A256GCM" }).setIssuedAt().setExpirationTime(`${r}s`).encrypt(await te(e));
|
|
290
333
|
}
|
|
291
|
-
async function
|
|
334
|
+
async function J(t, e) {
|
|
292
335
|
try {
|
|
293
|
-
const { payload: r } = await
|
|
336
|
+
const { payload: r } = await ce(t, await te(e));
|
|
294
337
|
return r;
|
|
295
338
|
} catch {
|
|
296
339
|
return null;
|
|
297
340
|
}
|
|
298
341
|
}
|
|
299
|
-
const
|
|
300
|
-
function
|
|
342
|
+
const $e = 900, V = 600;
|
|
343
|
+
function B(t, e) {
|
|
301
344
|
return t && t.startsWith("/") && !t.startsWith("//") && !t.startsWith("/\\") ? t : e;
|
|
302
345
|
}
|
|
303
346
|
function y(t, e = []) {
|
|
@@ -305,7 +348,7 @@ function y(t, e = []) {
|
|
|
305
348
|
for (const n of e) r.append("set-cookie", n);
|
|
306
349
|
return new Response(null, { status: 302, headers: r });
|
|
307
350
|
}
|
|
308
|
-
function
|
|
351
|
+
function A(t, e) {
|
|
309
352
|
if (t.baseUrl) return t.baseUrl;
|
|
310
353
|
const r = e.headers.get("x-forwarded-host")?.split(",")[0]?.trim() || e.headers.get("host")?.trim();
|
|
311
354
|
if (r) {
|
|
@@ -317,7 +360,7 @@ function P(t, e) {
|
|
|
317
360
|
}
|
|
318
361
|
return new URL(e.url).origin;
|
|
319
362
|
}
|
|
320
|
-
function
|
|
363
|
+
function xe(t) {
|
|
321
364
|
const e = (n) => typeof t[n] == "string" ? t[n] : void 0, r = e("name") ?? ([e("given_name"), e("family_name")].filter(Boolean).join(" ") || void 0) ?? e("email");
|
|
322
365
|
return {
|
|
323
366
|
id: t.sub ?? "",
|
|
@@ -328,97 +371,118 @@ function Ue(t) {
|
|
|
328
371
|
emailVerified: t.email_verified === !0
|
|
329
372
|
};
|
|
330
373
|
}
|
|
331
|
-
function
|
|
332
|
-
return Date.now() + (t.expires_in ??
|
|
374
|
+
function X(t) {
|
|
375
|
+
return Date.now() + (t.expires_in ?? $e) * 1e3;
|
|
333
376
|
}
|
|
334
|
-
function
|
|
335
|
-
const e =
|
|
336
|
-
if (e.mode === "browser") return
|
|
337
|
-
const r = { issuer: e.issuer, clientId: e.clientId, clientSecret: e.clientSecret }, n = `${e.cookieName}_tx`, s = new
|
|
338
|
-
const d = new URL(e.signInPath,
|
|
377
|
+
function ze(t) {
|
|
378
|
+
const e = Ie(t);
|
|
379
|
+
if (e.mode === "browser") return Ae(e);
|
|
380
|
+
const r = { issuer: e.issuer, clientId: e.clientId, clientSecret: e.clientSecret }, n = `${e.cookieName}_tx`, s = new Ce(), u = (o) => e.secureCookies ?? A(e, o).startsWith("https:"), c = (o) => e.redirectUri ?? `${A(e, o)}${e.basePath}/callback`, a = (o, i) => {
|
|
381
|
+
const d = new URL(e.signInPath, A(e, o));
|
|
339
382
|
return i && d.searchParams.set("error", i), d.toString();
|
|
340
383
|
};
|
|
341
384
|
async function h(o) {
|
|
342
|
-
const i =
|
|
385
|
+
const i = de(o, e.cookieName);
|
|
343
386
|
if (i === null) return null;
|
|
344
|
-
const d = await
|
|
387
|
+
const d = await J(i, e.sessionSecret);
|
|
345
388
|
return d || e.logger.warn("[pontive] session cookie could not be read (sealed with another secret, or truncated)"), d;
|
|
346
389
|
}
|
|
347
390
|
async function f(o, i, d) {
|
|
348
|
-
const l = await
|
|
349
|
-
return
|
|
391
|
+
const l = await G({ ...i }, e.sessionSecret, e.sessionMaxAgeSeconds);
|
|
392
|
+
return he(o, e.cookieName, l, { maxAge: e.sessionMaxAgeSeconds, secure: u(d) });
|
|
350
393
|
}
|
|
351
394
|
function w(o) {
|
|
352
395
|
const { refreshToken: i, ...d } = o;
|
|
353
|
-
return d;
|
|
396
|
+
return { ...d, organization: ye(o.accessToken) };
|
|
354
397
|
}
|
|
355
|
-
async function
|
|
398
|
+
async function m(o) {
|
|
356
399
|
const i = new URL(o.url), d = {
|
|
357
|
-
state:
|
|
358
|
-
verifier:
|
|
359
|
-
nonce:
|
|
360
|
-
returnTo:
|
|
361
|
-
}, l = await
|
|
400
|
+
state: $(),
|
|
401
|
+
verifier: $(),
|
|
402
|
+
nonce: $(),
|
|
403
|
+
returnTo: B(i.searchParams.get("returnTo") ?? i.searchParams.get("next"), e.afterSignInPath)
|
|
404
|
+
}, l = await pe(r, e.fetch, {
|
|
362
405
|
redirectUri: c(o),
|
|
363
406
|
scopes: e.scopes,
|
|
364
407
|
state: d.state,
|
|
365
408
|
nonce: d.nonce,
|
|
366
|
-
codeChallenge: await
|
|
367
|
-
prompt: e.prompt
|
|
368
|
-
|
|
369
|
-
|
|
409
|
+
codeChallenge: await we(d.verifier),
|
|
410
|
+
prompt: e.prompt,
|
|
411
|
+
// The organization the app wants this sign-in to start in; the user is
|
|
412
|
+
// never asked to pick one.
|
|
413
|
+
organizationId: i.searchParams.get("organization_id") ?? void 0
|
|
414
|
+
}), k = await G({ ...d }, e.sessionSecret, V);
|
|
415
|
+
return y(l, [E(n, k, { maxAge: V, secure: u(o) })]);
|
|
370
416
|
}
|
|
371
|
-
async function
|
|
372
|
-
const i = new URL(o.url), d =
|
|
373
|
-
if (
|
|
374
|
-
const
|
|
375
|
-
if (!
|
|
376
|
-
return y(
|
|
417
|
+
async function T(o) {
|
|
418
|
+
const i = new URL(o.url), d = P(o.headers.get("cookie")), l = [E(n, "", { maxAge: 0, secure: u(o) })], k = i.searchParams.get("error");
|
|
419
|
+
if (k) return y(a(o, k), l);
|
|
420
|
+
const g = d.get(n), S = g ? await J(g, e.sessionSecret) : null, _ = i.searchParams.get("code");
|
|
421
|
+
if (!S || !_ || i.searchParams.get("state") !== S.state)
|
|
422
|
+
return y(a(o, "invalid_state"), l);
|
|
377
423
|
try {
|
|
378
|
-
const
|
|
379
|
-
code:
|
|
380
|
-
codeVerifier:
|
|
424
|
+
const p = await ge(r, e.fetch, {
|
|
425
|
+
code: _,
|
|
426
|
+
codeVerifier: S.verifier,
|
|
381
427
|
redirectUri: c(o)
|
|
382
428
|
});
|
|
383
|
-
if (!
|
|
384
|
-
const
|
|
385
|
-
user:
|
|
386
|
-
accessToken:
|
|
387
|
-
refreshToken:
|
|
388
|
-
expiresAt:
|
|
389
|
-
sessionId: typeof
|
|
429
|
+
if (!p.id_token) throw new Error("the token response carried no id_token; is the openid scope requested?");
|
|
430
|
+
const U = await me(r, e.fetch, p.id_token, S.nonce), oe = {
|
|
431
|
+
user: xe(U),
|
|
432
|
+
accessToken: p.access_token,
|
|
433
|
+
refreshToken: p.refresh_token,
|
|
434
|
+
expiresAt: X(p),
|
|
435
|
+
sessionId: typeof U.sid == "string" ? U.sid : void 0
|
|
390
436
|
};
|
|
391
|
-
l.push(...await f(d,
|
|
392
|
-
} catch (
|
|
393
|
-
return e.logger.warn("[pontive] sign-in failed:",
|
|
437
|
+
l.push(...await f(d, oe, o));
|
|
438
|
+
} catch (p) {
|
|
439
|
+
return e.logger.warn("[pontive] sign-in failed:", p instanceof Error ? p.message : String(p)), y(a(o, "exchange_failed"), l);
|
|
394
440
|
}
|
|
395
|
-
return y(new URL(
|
|
441
|
+
return y(new URL(S.returnTo, A(e, o)).toString(), l);
|
|
396
442
|
}
|
|
397
|
-
async function
|
|
398
|
-
const i =
|
|
399
|
-
let
|
|
443
|
+
async function re(o) {
|
|
444
|
+
const i = P(o.headers.get("cookie")), d = await h(i), l = N(i, e.cookieName, u(o));
|
|
445
|
+
let k = a(o);
|
|
400
446
|
try {
|
|
401
|
-
|
|
402
|
-
postLogoutRedirectUri:
|
|
447
|
+
k = await _e(r, e.fetch, {
|
|
448
|
+
postLogoutRedirectUri: a(o),
|
|
403
449
|
sessionId: d?.sessionId
|
|
404
|
-
}) ??
|
|
405
|
-
} catch (
|
|
406
|
-
e.logger.warn("[pontive] could not reach the issuer to end its session:",
|
|
450
|
+
}) ?? k;
|
|
451
|
+
} catch (g) {
|
|
452
|
+
e.logger.warn("[pontive] could not reach the issuer to end its session:", g instanceof Error ? g.message : String(g));
|
|
453
|
+
}
|
|
454
|
+
return y(k, l);
|
|
455
|
+
}
|
|
456
|
+
function z(o, i) {
|
|
457
|
+
return {
|
|
458
|
+
...o,
|
|
459
|
+
accessToken: i.access_token,
|
|
460
|
+
// A response without one means rotation is off: the token in hand is still current.
|
|
461
|
+
refreshToken: i.refresh_token ?? o.refreshToken,
|
|
462
|
+
expiresAt: X(i)
|
|
463
|
+
};
|
|
464
|
+
}
|
|
465
|
+
async function ne(o) {
|
|
466
|
+
const i = new URL(o.url), d = P(o.headers.get("cookie")), l = B(i.searchParams.get("returnTo"), e.afterSignInPath), k = i.searchParams.get("organization_id"), g = (p) => {
|
|
467
|
+
const U = new URL(l, A(e, o));
|
|
468
|
+
return p && U.searchParams.set(Se, p), U.toString();
|
|
469
|
+
}, S = await h(d);
|
|
470
|
+
if (!S?.refreshToken) return y(a(o));
|
|
471
|
+
if (!k) return y(g("invalid_request"));
|
|
472
|
+
let _;
|
|
473
|
+
try {
|
|
474
|
+
_ = await W(r, e.fetch, S.refreshToken, k);
|
|
475
|
+
} catch (p) {
|
|
476
|
+
return p instanceof x && p.code === "invalid_request" ? y(g("not_a_member")) : (e.logger.warn("[pontive] organization switch failed:", p instanceof Error ? p.message : String(p)), y(g("switch_failed")));
|
|
407
477
|
}
|
|
408
|
-
return y(g,
|
|
478
|
+
return y(g(), await f(d, z(S, _), o));
|
|
409
479
|
}
|
|
410
|
-
async function
|
|
480
|
+
async function se(o) {
|
|
411
481
|
try {
|
|
412
|
-
const i = await
|
|
413
|
-
return
|
|
414
|
-
...o,
|
|
415
|
-
accessToken: i.access_token,
|
|
416
|
-
// A response without one means rotation is off: the token in hand is still current.
|
|
417
|
-
refreshToken: i.refresh_token ?? o.refreshToken,
|
|
418
|
-
expiresAt: F(i)
|
|
419
|
-
};
|
|
482
|
+
const i = await W(r, e.fetch, o.refreshToken);
|
|
483
|
+
return z(o, i);
|
|
420
484
|
} catch (i) {
|
|
421
|
-
if (i instanceof
|
|
485
|
+
if (i instanceof x && i.status >= 400 && i.status < 500)
|
|
422
486
|
return e.logger.warn("[pontive] refresh refused; signing out:", i.message), null;
|
|
423
487
|
throw i;
|
|
424
488
|
}
|
|
@@ -428,17 +492,17 @@ function Ce(t) {
|
|
|
428
492
|
const { pathname: i } = new URL(o.url);
|
|
429
493
|
if (!i.startsWith(`${e.basePath}/`)) return null;
|
|
430
494
|
const d = i.slice(e.basePath.length + 1);
|
|
431
|
-
return d === "login" && o.method === "GET" ?
|
|
495
|
+
return d === "login" && o.method === "GET" ? m(o) : d === "callback" && o.method === "GET" ? T(o) : d === "logout" && (o.method === "GET" || o.method === "POST") ? re(o) : d === L && (o.method === "GET" || o.method === "POST") ? ne(o) : null;
|
|
432
496
|
},
|
|
433
497
|
async getSession(o) {
|
|
434
|
-
const i = await h(
|
|
498
|
+
const i = await h(P(o));
|
|
435
499
|
return i ? Date.now() >= i.expiresAt ? (e.logger.warn(
|
|
436
500
|
"[pontive] the session's access token has expired and getSession cannot refresh it. Run the SDK's middleware on this path so it refreshes before the request is handled."
|
|
437
501
|
), null) : w(i) : null;
|
|
438
502
|
},
|
|
439
503
|
async refresh(o) {
|
|
440
|
-
const i =
|
|
441
|
-
session:
|
|
504
|
+
const i = P(o.headers.get("cookie")), d = (_) => ({
|
|
505
|
+
session: _,
|
|
442
506
|
setCookies: [],
|
|
443
507
|
cookieHeader: o.headers.get("cookie") ?? ""
|
|
444
508
|
}), l = await h(i);
|
|
@@ -446,23 +510,26 @@ function Ce(t) {
|
|
|
446
510
|
if (new URL(o.url).pathname.startsWith(`${e.basePath}/`))
|
|
447
511
|
return d(Date.now() < l.expiresAt ? w(l) : null);
|
|
448
512
|
if (Date.now() < l.expiresAt - e.refreshSkewSeconds * 1e3) return d(w(l));
|
|
449
|
-
const
|
|
450
|
-
if (!l.refreshToken) return
|
|
451
|
-
let
|
|
513
|
+
const k = () => ({ session: null, setCookies: N(i, e.cookieName, u(o)), cookieHeader: M(i) });
|
|
514
|
+
if (!l.refreshToken) return k();
|
|
515
|
+
let g;
|
|
452
516
|
try {
|
|
453
|
-
|
|
454
|
-
} catch (
|
|
455
|
-
return e.logger.warn("[pontive] refresh failed, will retry:",
|
|
517
|
+
g = await s.run(l.refreshToken, () => se(l));
|
|
518
|
+
} catch (_) {
|
|
519
|
+
return e.logger.warn("[pontive] refresh failed, will retry:", _ instanceof Error ? _.message : String(_)), d(Date.now() < l.expiresAt ? w(l) : null);
|
|
456
520
|
}
|
|
457
|
-
if (!
|
|
458
|
-
const
|
|
459
|
-
return { session: w(
|
|
521
|
+
if (!g) return k();
|
|
522
|
+
const S = await f(i, g, o);
|
|
523
|
+
return { session: w(g), setCookies: S, cookieHeader: M(i) };
|
|
460
524
|
}
|
|
461
525
|
};
|
|
462
526
|
}
|
|
463
527
|
export {
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
528
|
+
Pe as ACCESS_COOKIE,
|
|
529
|
+
Se as ORGANIZATION_ERROR_PARAM,
|
|
530
|
+
Ue as SESSION_HINT_COOKIE,
|
|
531
|
+
L as SWITCH_ORGANIZATION_ROUTE,
|
|
532
|
+
ze as createServerAuth,
|
|
533
|
+
Le as switchOrganizationPath
|
|
467
534
|
};
|
|
468
535
|
//# sourceMappingURL=index.js.map
|