@fluixi/oauth2 1.0.0-alpha.11 → 1.0.0-alpha.110
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/api-guard.cjs +8 -3
- package/dist/api-guard.mjs +9 -135
- package/dist/backchannel.cjs +467 -0
- package/dist/backchannel.d.ts +39 -0
- package/dist/backchannel.d.ts.map +1 -0
- package/dist/backchannel.js +97 -0
- package/dist/backchannel.mjs +19 -0
- package/dist/browser.cjs +6 -1
- package/dist/browser.mjs +10 -702
- package/dist/callback.cjs +2 -2
- package/dist/callback.d.ts +2 -2
- package/dist/callback.js +3 -3
- package/dist/callback.mjs +17 -24
- package/dist/{dpop-YPGCGL7S.mjs → chunk-27BGFJIY.mjs} +6 -6
- package/dist/chunk-2GLWT7XH.mjs +143 -0
- package/dist/chunk-3EICMAF7.mjs +22 -0
- package/dist/chunk-4OHJTTX7.mjs +398 -0
- package/dist/chunk-4QRYJ4TN.mjs +86 -0
- package/dist/chunk-5WX3EV2W.mjs +78 -0
- package/dist/chunk-7AMC336S.mjs +52 -0
- package/dist/chunk-AAN3YN4S.mjs +63 -0
- package/dist/chunk-AWVECOG7.mjs +404 -0
- package/dist/chunk-GN5SERAQ.mjs +189 -0
- package/dist/chunk-KIXTZEEW.mjs +80 -0
- package/dist/chunk-KVW2S6VY.mjs +36 -0
- package/dist/chunk-MHWZTXZZ.mjs +9 -0
- package/dist/chunk-QC2FFASM.mjs +67 -0
- package/dist/chunk-SJOBQPG6.mjs +77 -0
- package/dist/chunk-VCNXQCIH.mjs +26 -0
- package/dist/chunk-XQJTQV3C.mjs +91 -0
- package/dist/client-auth.mjs +6 -78
- package/dist/discovery.cjs +6 -1
- package/dist/discovery.d.ts.map +1 -1
- package/dist/discovery.js +6 -1
- package/dist/discovery.mjs +6 -59
- package/dist/dpop-store.mjs +6 -118
- package/dist/dpop.mjs +11 -95
- package/dist/env-config.cjs +1 -1
- package/dist/env-config.mjs +5 -214
- package/dist/env-credentials.mjs +5 -47
- package/dist/index.cjs +108 -8
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.mjs +50 -912
- package/dist/module.cjs +1 -1
- package/dist/module.mjs +5 -20
- package/dist/pkce.mjs +11 -56
- package/dist/presets.cjs +1 -1
- package/dist/presets.mjs +5 -214
- package/dist/return-to.mjs +4 -22
- package/dist/roles.cjs +1 -1
- package/dist/roles.d.ts +1 -1
- package/dist/roles.js +1 -1
- package/dist/roles.mjs +4 -32
- package/dist/server-client.mjs +7 -107
- package/dist/server.cjs +795 -153
- package/dist/server.d.ts +85 -4
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +577 -75
- package/dist/server.mjs +442 -310
- package/dist/service-client.cjs +7 -2
- package/dist/service-client.mjs +16 -133
- package/dist/service.cjs +104 -5
- package/dist/service.d.ts +1 -1
- package/dist/service.d.ts.map +1 -1
- package/dist/service.js +38 -6
- package/dist/service.mjs +14 -879
- package/dist/session-monitor.cjs +98 -0
- package/dist/session-monitor.d.ts +32 -0
- package/dist/session-monitor.d.ts.map +1 -0
- package/dist/session-monitor.js +104 -0
- package/dist/session-monitor.mjs +7 -0
- package/dist/tokens.cjs +1 -1
- package/dist/tokens.d.ts +2 -2
- package/dist/tokens.d.ts.map +1 -1
- package/dist/tokens.js +1 -1
- package/dist/tokens.mjs +5 -4
- package/dist/tsconfig.lib.tsbuildinfo +1 -1
- package/dist/types.d.ts +8 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.mjs +4 -9
- package/dist/verify.cjs +15 -5
- package/dist/verify.d.ts +2 -2
- package/dist/verify.js +2 -2
- package/dist/verify.mjs +17 -580
- package/package.json +23 -17
- package/dist/browser-N432SMC3.mjs +0 -3545
- package/dist/chunk-4TPZVJSR.mjs +0 -10
- package/dist/chunk-ZVDC3JW7.mjs +0 -15
- package/dist/dpop-DYUR2CH2.mjs +0 -100
- /package/dist/{chunk-OGSFNTKA.mjs → chunk-AXPSPTNP.mjs} +0 -0
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
import {
|
|
2
|
+
claimsOf,
|
|
3
|
+
createPkcePair,
|
|
4
|
+
createStateValue,
|
|
5
|
+
expiryFrom,
|
|
6
|
+
expiryFromJwt,
|
|
7
|
+
timingSafeEqual
|
|
8
|
+
} from "./chunk-QC2FFASM.mjs";
|
|
9
|
+
import {
|
|
10
|
+
safeReturnTo
|
|
11
|
+
} from "./chunk-VCNXQCIH.mjs";
|
|
12
|
+
import {
|
|
13
|
+
applyClientAuth
|
|
14
|
+
} from "./chunk-SJOBQPG6.mjs";
|
|
15
|
+
import {
|
|
16
|
+
discover
|
|
17
|
+
} from "./chunk-AAN3YN4S.mjs";
|
|
18
|
+
import {
|
|
19
|
+
forgetKey,
|
|
20
|
+
keyFor
|
|
21
|
+
} from "./chunk-XQJTQV3C.mjs";
|
|
22
|
+
import {
|
|
23
|
+
nonceFrom,
|
|
24
|
+
proofFor
|
|
25
|
+
} from "./chunk-27BGFJIY.mjs";
|
|
26
|
+
import {
|
|
27
|
+
OAuthError
|
|
28
|
+
} from "./chunk-AXPSPTNP.mjs";
|
|
29
|
+
|
|
30
|
+
// src/browser.ts
|
|
31
|
+
var keysFor = (id) => ({
|
|
32
|
+
verifier: `fx.oauth2.${id}.verifier`,
|
|
33
|
+
state: `fx.oauth2.${id}.state`,
|
|
34
|
+
returnTo: `fx.oauth2.${id}.return`,
|
|
35
|
+
refresh: `fx.oauth2.${id}.refresh`,
|
|
36
|
+
/** Names the record in IndexedDB, not a `Storage` entry: a key is never a string here. */
|
|
37
|
+
dpop: `fx.oauth2.${id}.dpop`
|
|
38
|
+
});
|
|
39
|
+
var DEFAULT_SCOPES = ["openid", "profile", "email"];
|
|
40
|
+
function store(kind) {
|
|
41
|
+
if (typeof window === "undefined") return null;
|
|
42
|
+
try {
|
|
43
|
+
return kind === "local" ? window.localStorage : window.sessionStorage;
|
|
44
|
+
} catch {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
function createBrowserClient(config, fetcher = fetch) {
|
|
49
|
+
const scopes = config.scopes ?? DEFAULT_SCOPES;
|
|
50
|
+
const KEY = keysFor(config.storageKey ?? config.clientId);
|
|
51
|
+
const flow = () => store(config.storage ?? "session");
|
|
52
|
+
const durable = () => store(config.storage ?? "session");
|
|
53
|
+
let access = null;
|
|
54
|
+
let dpopKey = null;
|
|
55
|
+
let dpopPending = null;
|
|
56
|
+
let dpopNonce = null;
|
|
57
|
+
const keyForDpop = async () => {
|
|
58
|
+
if (!config.dpop) return null;
|
|
59
|
+
if (dpopKey) return dpopKey;
|
|
60
|
+
dpopPending ??= keyFor(KEY.dpop, config.dpopAlgorithm, (reason) => {
|
|
61
|
+
console.warn(`[fluixi/oauth2] ${reason}`);
|
|
62
|
+
}).then((key) => {
|
|
63
|
+
dpopKey = key;
|
|
64
|
+
return key;
|
|
65
|
+
});
|
|
66
|
+
return dpopPending;
|
|
67
|
+
};
|
|
68
|
+
let endpoints = config.endpoints ?? null;
|
|
69
|
+
async function resolveEndpoints() {
|
|
70
|
+
if (endpoints) return endpoints;
|
|
71
|
+
if (!config.issuer) {
|
|
72
|
+
throw new OAuthError(
|
|
73
|
+
"Give the client an `issuer` to discover from, or `endpoints` directly.",
|
|
74
|
+
"missing_issuer"
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
endpoints = await discover(config.issuer, fetcher);
|
|
78
|
+
return endpoints;
|
|
79
|
+
}
|
|
80
|
+
async function exchange(body) {
|
|
81
|
+
const { token } = await resolveEndpoints();
|
|
82
|
+
const key = await keyForDpop();
|
|
83
|
+
const headers = {
|
|
84
|
+
"content-type": "application/x-www-form-urlencoded"
|
|
85
|
+
};
|
|
86
|
+
const form = new URLSearchParams(body);
|
|
87
|
+
form.delete("client_id");
|
|
88
|
+
await applyClientAuth(
|
|
89
|
+
{ clientId: config.clientId, method: config.tokenAuthMethod ?? "none", clientSecret: "" },
|
|
90
|
+
token,
|
|
91
|
+
headers,
|
|
92
|
+
form
|
|
93
|
+
);
|
|
94
|
+
const send = async () => {
|
|
95
|
+
if (key) {
|
|
96
|
+
headers.dpop = await proofFor(key, {
|
|
97
|
+
method: "POST",
|
|
98
|
+
url: token,
|
|
99
|
+
nonce: dpopNonce ?? void 0
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
return fetcher(token, { method: "POST", headers, body: form.toString() });
|
|
103
|
+
};
|
|
104
|
+
let response = await send();
|
|
105
|
+
if (key && !response.ok) {
|
|
106
|
+
const offered = nonceFrom(response);
|
|
107
|
+
if (offered && offered !== dpopNonce) {
|
|
108
|
+
dpopNonce = offered;
|
|
109
|
+
response = await send();
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (key) dpopNonce = nonceFrom(response) ?? dpopNonce;
|
|
113
|
+
const payload = await response.json().catch(() => ({}));
|
|
114
|
+
if (!response.ok) {
|
|
115
|
+
const code = String(payload.error ?? "token_request_failed");
|
|
116
|
+
const description = typeof payload.error_description === "string" ? payload.error_description : void 0;
|
|
117
|
+
const hint = (code === "invalid_dpop_proof" || code === "use_dpop_nonce") && !config.dpop ? " This provider requires DPoP (RFC 9449), which binds a token to a key this client holds. Set `dpop: true` on the config." : "";
|
|
118
|
+
throw new OAuthError(
|
|
119
|
+
`Token request failed: ${[code, description].filter(Boolean).join(": ")}${hint}`,
|
|
120
|
+
code,
|
|
121
|
+
description
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
const accessToken = String(payload.access_token ?? "");
|
|
125
|
+
if (!accessToken) {
|
|
126
|
+
throw new OAuthError("The token response carried no access_token.", "no_access_token");
|
|
127
|
+
}
|
|
128
|
+
const tokens = {
|
|
129
|
+
accessToken,
|
|
130
|
+
refreshToken: typeof payload.refresh_token === "string" ? payload.refresh_token : void 0,
|
|
131
|
+
idToken: typeof payload.id_token === "string" ? payload.id_token : void 0,
|
|
132
|
+
tokenType: typeof payload.token_type === "string" ? payload.token_type : void 0,
|
|
133
|
+
scope: typeof payload.scope === "string" ? payload.scope : void 0,
|
|
134
|
+
// `expires_in` is what the server said; the JWT's own `exp` is the fallback for a
|
|
135
|
+
// server that omits it.
|
|
136
|
+
expiresAt: expiryFrom(payload.expires_in) ?? expiryFromJwt(accessToken) ?? void 0
|
|
137
|
+
};
|
|
138
|
+
access = tokens;
|
|
139
|
+
if (tokens.refreshToken) durable()?.setItem(KEY.refresh, tokens.refreshToken);
|
|
140
|
+
return tokens;
|
|
141
|
+
}
|
|
142
|
+
return {
|
|
143
|
+
async login(options = {}) {
|
|
144
|
+
const { authorization } = await resolveEndpoints();
|
|
145
|
+
const { verifier, challenge } = await createPkcePair();
|
|
146
|
+
const state = createStateValue();
|
|
147
|
+
const storage = flow();
|
|
148
|
+
storage?.setItem(KEY.verifier, verifier);
|
|
149
|
+
storage?.setItem(KEY.state, state);
|
|
150
|
+
storage?.setItem(
|
|
151
|
+
KEY.returnTo,
|
|
152
|
+
safeReturnTo(options.returnTo ?? `${location.pathname}${location.search}`, {
|
|
153
|
+
fallback: "/",
|
|
154
|
+
allowedOrigins: config.allowedReturnOrigins
|
|
155
|
+
})
|
|
156
|
+
);
|
|
157
|
+
const url = new URL(authorization);
|
|
158
|
+
url.searchParams.set("response_type", "code");
|
|
159
|
+
url.searchParams.set("client_id", config.clientId);
|
|
160
|
+
url.searchParams.set("redirect_uri", config.redirectUri);
|
|
161
|
+
url.searchParams.set("scope", scopes.join(" "));
|
|
162
|
+
url.searchParams.set("state", state);
|
|
163
|
+
url.searchParams.set("code_challenge", challenge);
|
|
164
|
+
url.searchParams.set("code_challenge_method", "S256");
|
|
165
|
+
for (const [key, value] of Object.entries({
|
|
166
|
+
...config.extraParams,
|
|
167
|
+
...options.params
|
|
168
|
+
})) {
|
|
169
|
+
url.searchParams.set(key, value);
|
|
170
|
+
}
|
|
171
|
+
location.assign(url.toString());
|
|
172
|
+
},
|
|
173
|
+
async handleCallback(params) {
|
|
174
|
+
if (params.error) {
|
|
175
|
+
throw new OAuthError(
|
|
176
|
+
params.errorDescription ?? params.error,
|
|
177
|
+
params.error,
|
|
178
|
+
params.errorDescription
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
if (!params.code) {
|
|
182
|
+
throw new OAuthError("The callback carried no authorization code.", "no_code");
|
|
183
|
+
}
|
|
184
|
+
const storage = flow();
|
|
185
|
+
const expected = storage?.getItem(KEY.state) ?? null;
|
|
186
|
+
const verifier = storage?.getItem(KEY.verifier) ?? null;
|
|
187
|
+
storage?.removeItem(KEY.state);
|
|
188
|
+
storage?.removeItem(KEY.verifier);
|
|
189
|
+
if (!expected || !params.state || !timingSafeEqual(expected, params.state)) {
|
|
190
|
+
throw new OAuthError(
|
|
191
|
+
"The callback state did not match the one this client sent.",
|
|
192
|
+
"state_mismatch"
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
if (!verifier) {
|
|
196
|
+
throw new OAuthError(
|
|
197
|
+
"No PKCE verifier for this callback. The login was started elsewhere, or storage was cleared.",
|
|
198
|
+
"missing_verifier"
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
return exchange({
|
|
202
|
+
grant_type: "authorization_code",
|
|
203
|
+
code: params.code,
|
|
204
|
+
redirect_uri: config.redirectUri,
|
|
205
|
+
client_id: config.clientId,
|
|
206
|
+
code_verifier: verifier
|
|
207
|
+
});
|
|
208
|
+
},
|
|
209
|
+
async refresh() {
|
|
210
|
+
const token = access?.refreshToken ?? durable()?.getItem(KEY.refresh);
|
|
211
|
+
if (!token) {
|
|
212
|
+
throw new OAuthError("No refresh token, so the session cannot be extended.", "no_refresh_token");
|
|
213
|
+
}
|
|
214
|
+
try {
|
|
215
|
+
return await exchange({
|
|
216
|
+
grant_type: "refresh_token",
|
|
217
|
+
refresh_token: token,
|
|
218
|
+
client_id: config.clientId
|
|
219
|
+
});
|
|
220
|
+
} catch (error) {
|
|
221
|
+
if (error instanceof OAuthError && error.code === "invalid_grant") {
|
|
222
|
+
durable()?.removeItem(KEY.refresh);
|
|
223
|
+
access = null;
|
|
224
|
+
}
|
|
225
|
+
throw error;
|
|
226
|
+
}
|
|
227
|
+
},
|
|
228
|
+
async fetchUser(accessToken) {
|
|
229
|
+
const custom = config.userInfo;
|
|
230
|
+
const bearer = accessToken ?? access?.accessToken;
|
|
231
|
+
if (!bearer) throw new OAuthError("No access token to call userinfo with.", "no_access_token");
|
|
232
|
+
let endpoint = custom?.url;
|
|
233
|
+
if (!endpoint) {
|
|
234
|
+
const { userinfo } = await resolveEndpoints();
|
|
235
|
+
if (!userinfo) {
|
|
236
|
+
throw new OAuthError(
|
|
237
|
+
"The provider published no userinfo endpoint. Give `userInfo.url`, or the service its own `fetchUser`.",
|
|
238
|
+
"no_userinfo"
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
endpoint = userinfo;
|
|
242
|
+
}
|
|
243
|
+
const key = await keyForDpop();
|
|
244
|
+
const headers = {
|
|
245
|
+
// A bound token is presented with the DPoP scheme, not Bearer: a service checking
|
|
246
|
+
// binding refuses `Bearer` outright, since that is the word for an unbound token.
|
|
247
|
+
authorization: `${key ? "DPoP" : "Bearer"} ${bearer}`,
|
|
248
|
+
accept: "application/json",
|
|
249
|
+
...custom?.headers
|
|
250
|
+
};
|
|
251
|
+
if (key) {
|
|
252
|
+
headers.dpop = await proofFor(key, {
|
|
253
|
+
method: custom?.method ?? "GET",
|
|
254
|
+
url: endpoint,
|
|
255
|
+
// `ath` binds the proof to this token, so one captured beside it cannot be
|
|
256
|
+
// reused with another.
|
|
257
|
+
accessToken: bearer,
|
|
258
|
+
nonce: dpopNonce ?? void 0
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
const response = await fetcher(endpoint, {
|
|
262
|
+
method: custom?.method ?? "GET",
|
|
263
|
+
headers,
|
|
264
|
+
...custom?.credentials ? { credentials: custom.credentials } : {}
|
|
265
|
+
});
|
|
266
|
+
if (!response.ok) {
|
|
267
|
+
const reason = response.headers.get("www-authenticate")?.match(/error="([^"]+)"/)?.[1];
|
|
268
|
+
throw new OAuthError(
|
|
269
|
+
`The user endpoint refused the token: ${reason ?? response.status}`,
|
|
270
|
+
reason ?? "userinfo_failed"
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
const type = response.headers.get("content-type") ?? "";
|
|
274
|
+
if (type.includes("application/jwt")) {
|
|
275
|
+
throw new OAuthError(
|
|
276
|
+
"This provider returns a signed userinfo response, which needs its own verification. Give the service its own `fetchUser`.",
|
|
277
|
+
"userinfo_signed"
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
const raw = await response.json();
|
|
281
|
+
if (!custom?.url) {
|
|
282
|
+
const expected = access?.idToken ? claimsOf(access.idToken)?.sub : void 0;
|
|
283
|
+
if (expected !== void 0 && raw.sub !== expected) {
|
|
284
|
+
throw new OAuthError(
|
|
285
|
+
"The userinfo subject does not match the id token. The response was discarded.",
|
|
286
|
+
"sub_mismatch"
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
return custom?.map ? await custom.map(raw, bearer) : raw;
|
|
291
|
+
},
|
|
292
|
+
/**
|
|
293
|
+
* Retire a token at the provider, per RFC 7009.
|
|
294
|
+
*
|
|
295
|
+
* Defaults to the refresh token, which is the one worth retiring: an access token
|
|
296
|
+
* expires on its own in minutes, a refresh token does not. A provider with no
|
|
297
|
+
* revocation endpoint resolves without doing anything, because there is nothing this
|
|
298
|
+
* can do about that and failing a logout over it helps nobody.
|
|
299
|
+
*/
|
|
300
|
+
async revoke(token, hint = "refresh_token") {
|
|
301
|
+
const target = token ?? (hint === "refresh_token" ? access?.refreshToken ?? durable()?.getItem(KEY.refresh) ?? void 0 : access?.accessToken);
|
|
302
|
+
if (!target) {
|
|
303
|
+
throw new OAuthError(
|
|
304
|
+
`No ${hint.replace("_", " ")} held by this client, so there is nothing to revoke. One is stored when the provider issues it, and cleared when the provider rejects it.`,
|
|
305
|
+
"nothing_to_revoke"
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
const { revocation } = await resolveEndpoints();
|
|
309
|
+
if (!revocation) {
|
|
310
|
+
throw new OAuthError(
|
|
311
|
+
"This provider publishes no revocation endpoint, so a token cannot be retired before it expires.",
|
|
312
|
+
"no_revocation_endpoint"
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
const headers = {
|
|
316
|
+
"content-type": "application/x-www-form-urlencoded"
|
|
317
|
+
};
|
|
318
|
+
const form = new URLSearchParams({ token: target, token_type_hint: hint });
|
|
319
|
+
await applyClientAuth(
|
|
320
|
+
{ clientId: config.clientId, method: config.tokenAuthMethod ?? "none", clientSecret: "" },
|
|
321
|
+
revocation,
|
|
322
|
+
headers,
|
|
323
|
+
form
|
|
324
|
+
);
|
|
325
|
+
const response = await fetcher(revocation, {
|
|
326
|
+
method: "POST",
|
|
327
|
+
headers,
|
|
328
|
+
body: form.toString()
|
|
329
|
+
});
|
|
330
|
+
if (!response.ok) {
|
|
331
|
+
const payload = await response.json().catch(() => ({}));
|
|
332
|
+
throw new OAuthError(
|
|
333
|
+
`Revocation was refused: ${[payload.error, payload.error_description].filter(Boolean).join(": ") || response.status}`,
|
|
334
|
+
String(payload.error ?? "revocation_failed")
|
|
335
|
+
);
|
|
336
|
+
}
|
|
337
|
+
if (hint === "refresh_token") {
|
|
338
|
+
durable()?.removeItem(KEY.refresh);
|
|
339
|
+
if (access) access = { ...access, refreshToken: void 0 };
|
|
340
|
+
}
|
|
341
|
+
},
|
|
342
|
+
async logout(options = {}) {
|
|
343
|
+
const idToken = access?.idToken;
|
|
344
|
+
await this.revoke?.().catch(() => void 0);
|
|
345
|
+
access = null;
|
|
346
|
+
durable()?.removeItem(KEY.refresh);
|
|
347
|
+
dpopKey = null;
|
|
348
|
+
dpopPending = null;
|
|
349
|
+
await forgetKey(KEY.dpop);
|
|
350
|
+
const { endSession } = await resolveEndpoints().catch(() => ({ endSession: void 0 }));
|
|
351
|
+
const target = options.returnTo ? safeReturnTo(options.returnTo, {
|
|
352
|
+
fallback: config.postLogoutRedirectUri ?? "/",
|
|
353
|
+
allowedOrigins: config.allowedReturnOrigins
|
|
354
|
+
}) : config.postLogoutRedirectUri;
|
|
355
|
+
if (!endSession) {
|
|
356
|
+
if (target && typeof location !== "undefined") location.assign(target);
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
const url = new URL(endSession);
|
|
360
|
+
if (target) url.searchParams.set("post_logout_redirect_uri", target);
|
|
361
|
+
if (idToken) url.searchParams.set("id_token_hint", idToken);
|
|
362
|
+
url.searchParams.set("client_id", config.clientId);
|
|
363
|
+
location.assign(url.toString());
|
|
364
|
+
},
|
|
365
|
+
async restore() {
|
|
366
|
+
if (access) return access;
|
|
367
|
+
if (!durable()?.getItem(KEY.refresh)) return null;
|
|
368
|
+
try {
|
|
369
|
+
return await this.refresh();
|
|
370
|
+
} catch {
|
|
371
|
+
return null;
|
|
372
|
+
}
|
|
373
|
+
},
|
|
374
|
+
/**
|
|
375
|
+
* A proof for a request a caller will send with this client's token.
|
|
376
|
+
*
|
|
377
|
+
* Null when the client does not bind its tokens, so a caller can send `Bearer`
|
|
378
|
+
* unchanged. Only this client holds the key, so nothing else can produce one.
|
|
379
|
+
*/
|
|
380
|
+
async dpopProof(method, url) {
|
|
381
|
+
const key = await keyForDpop();
|
|
382
|
+
if (!key) return null;
|
|
383
|
+
return proofFor(key, {
|
|
384
|
+
method,
|
|
385
|
+
url,
|
|
386
|
+
accessToken: access?.accessToken,
|
|
387
|
+
nonce: dpopNonce ?? void 0
|
|
388
|
+
});
|
|
389
|
+
},
|
|
390
|
+
consumeReturnTo() {
|
|
391
|
+
const storage = flow();
|
|
392
|
+
const value = storage?.getItem(KEY.returnTo) ?? null;
|
|
393
|
+
storage?.removeItem(KEY.returnTo);
|
|
394
|
+
return value === null ? null : safeReturnTo(value, {
|
|
395
|
+
fallback: "/",
|
|
396
|
+
allowedOrigins: config.allowedReturnOrigins
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
export {
|
|
403
|
+
createBrowserClient
|
|
404
|
+
};
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import {
|
|
2
|
+
rolesFrom
|
|
3
|
+
} from "./chunk-KVW2S6VY.mjs";
|
|
4
|
+
|
|
5
|
+
// src/presets.ts
|
|
6
|
+
var trim = (url) => url.replace(/\/+$/, "");
|
|
7
|
+
var presets = {
|
|
8
|
+
/**
|
|
9
|
+
* Any OIDC provider publishing a discovery document.
|
|
10
|
+
*
|
|
11
|
+
* The agnostic path, and the one to reach for first. A provider not listed below almost
|
|
12
|
+
* certainly works through this.
|
|
13
|
+
*/
|
|
14
|
+
oidc(options) {
|
|
15
|
+
return {
|
|
16
|
+
label: options.issuer,
|
|
17
|
+
client: { issuer: trim(options.issuer), scopes: options.scopes },
|
|
18
|
+
roles: options.roles ?? rolesFrom.scope,
|
|
19
|
+
verifier: (clientId) => ({
|
|
20
|
+
issuer: trim(options.issuer),
|
|
21
|
+
audience: options.audience ?? clientId,
|
|
22
|
+
...options.audience === false ? { authorizedParty: clientId } : {}
|
|
23
|
+
}),
|
|
24
|
+
constraints: []
|
|
25
|
+
};
|
|
26
|
+
},
|
|
27
|
+
/**
|
|
28
|
+
* A plain OAuth2 server with no discovery document.
|
|
29
|
+
*
|
|
30
|
+
* Every endpoint is given, because there is nothing to ask. Token verification needs an
|
|
31
|
+
* issuer regardless, since a verifier will not accept a token without one.
|
|
32
|
+
*/
|
|
33
|
+
oauth2(options) {
|
|
34
|
+
return {
|
|
35
|
+
label: options.issuer,
|
|
36
|
+
client: { endpoints: options.endpoints, scopes: options.scopes },
|
|
37
|
+
roles: options.roles ?? rolesFrom.scope,
|
|
38
|
+
verifier: (clientId) => ({
|
|
39
|
+
issuer: trim(options.issuer),
|
|
40
|
+
audience: options.audience ?? clientId,
|
|
41
|
+
...options.audience === false ? { authorizedParty: clientId } : {}
|
|
42
|
+
}),
|
|
43
|
+
constraints: ["Publishes no discovery document, so every endpoint is configured by hand."]
|
|
44
|
+
};
|
|
45
|
+
},
|
|
46
|
+
/** Keycloak. A realm is its own issuer. */
|
|
47
|
+
keycloak(options) {
|
|
48
|
+
const issuer = `${trim(options.baseUrl)}/realms/${options.realm}`;
|
|
49
|
+
return {
|
|
50
|
+
label: "Keycloak",
|
|
51
|
+
client: {
|
|
52
|
+
issuer,
|
|
53
|
+
scopes: options.scopes ?? ["openid", "profile", "email"]
|
|
54
|
+
},
|
|
55
|
+
roles: rolesFrom.keycloakRealm,
|
|
56
|
+
// No `aud` on an access token until an audience mapper is configured, and the client is
|
|
57
|
+
// named in `azp` instead. So the check moves to `authorizedParty` rather than being
|
|
58
|
+
// dropped: `audience: false` alone would accept a token minted for any client of the
|
|
59
|
+
// realm, which is the check being skipped.
|
|
60
|
+
verifier: (clientId) => ({ issuer, audience: false, authorizedParty: clientId }),
|
|
61
|
+
constraints: [
|
|
62
|
+
"Puts no `aud` on an access token until a dedicated audience mapper is configured. Configure one and switch the verifier to `audience: clientId`.",
|
|
63
|
+
"Revokes the whole session when an authorization code is replayed, so a replay test has to run last."
|
|
64
|
+
]
|
|
65
|
+
};
|
|
66
|
+
},
|
|
67
|
+
/**
|
|
68
|
+
* Auth0.
|
|
69
|
+
*
|
|
70
|
+
* `audience` is the field that decides what kind of token comes back. Without it Auth0
|
|
71
|
+
* issues an opaque token for its own userinfo endpoint and the JWT checks have nothing to
|
|
72
|
+
* work with.
|
|
73
|
+
*/
|
|
74
|
+
auth0(options) {
|
|
75
|
+
const issuer = `https://${options.domain.replace(/^https?:\/\//, "").replace(/\/+$/, "")}/`;
|
|
76
|
+
return {
|
|
77
|
+
label: "Auth0",
|
|
78
|
+
client: {
|
|
79
|
+
issuer,
|
|
80
|
+
scopes: options.scopes ?? ["openid", "profile", "email", "offline_access"],
|
|
81
|
+
...options.audience ? { extraParams: { audience: options.audience } } : {}
|
|
82
|
+
},
|
|
83
|
+
roles: options.rolesClaim ? rolesFrom.namespaced(options.rolesClaim) : rolesFrom.scope,
|
|
84
|
+
verifier: (clientId) => ({
|
|
85
|
+
issuer,
|
|
86
|
+
// The API identifier is the audience of an access token. Without one the token is
|
|
87
|
+
// opaque and there is nothing for a verifier to check.
|
|
88
|
+
audience: options.audience ?? false,
|
|
89
|
+
...options.audience ? {} : { authorizedParty: clientId }
|
|
90
|
+
}),
|
|
91
|
+
constraints: [
|
|
92
|
+
"Issues an opaque access token unless `audience` names an API, so token verification needs it.",
|
|
93
|
+
"A refresh token needs three things together: `offline_access` in the scopes, an audience whose API has Allow Offline Access on, and Refresh Token Rotation enabled on the application. Any one missing and the scope is ignored silently.",
|
|
94
|
+
"Has no roles claim of its own. An action must add a namespaced one, and Auth0 drops a custom claim that is not a URI.",
|
|
95
|
+
"The issuer carries a trailing slash. A verifier pinned without it rejects every token."
|
|
96
|
+
]
|
|
97
|
+
};
|
|
98
|
+
},
|
|
99
|
+
/**
|
|
100
|
+
* Okta.
|
|
101
|
+
*
|
|
102
|
+
* Two authorization servers that are not interchangeable: a custom one per application,
|
|
103
|
+
* and the org itself for Okta's own management API.
|
|
104
|
+
*/
|
|
105
|
+
okta(options) {
|
|
106
|
+
const host = `https://${options.domain.replace(/^https?:\/\//, "").replace(/\/+$/, "")}`;
|
|
107
|
+
const server = options.authorizationServer === void 0 ? "default" : options.authorizationServer;
|
|
108
|
+
const issuer = server === null ? host : `${host}/oauth2/${server}`;
|
|
109
|
+
return {
|
|
110
|
+
label: "Okta",
|
|
111
|
+
client: {
|
|
112
|
+
issuer,
|
|
113
|
+
scopes: options.scopes ?? ["openid", "profile", "email", "offline_access"],
|
|
114
|
+
dpop: options.dpop ?? true
|
|
115
|
+
},
|
|
116
|
+
roles: rolesFrom.groups,
|
|
117
|
+
verifier: (clientId) => ({
|
|
118
|
+
issuer,
|
|
119
|
+
// An access token from a custom server is minted for that server's audience, named in
|
|
120
|
+
// the console and `api://default` by default. It does not carry the client id.
|
|
121
|
+
audience: false,
|
|
122
|
+
authorizedParty: clientId
|
|
123
|
+
}),
|
|
124
|
+
constraints: [
|
|
125
|
+
"Two authorization servers: /oauth2/<name> for an application, the bare domain for the management API. They are not interchangeable.",
|
|
126
|
+
"Turns DPoP on by default for a new application, and an integrator org will not let you untick it.",
|
|
127
|
+
"Binds the refresh token to the DPoP key as well as the access token, so a refresh must be proved with the key that obtained it.",
|
|
128
|
+
"A public SPA cannot introspect at all. An API Services client with `private_key_jwt` has to ask instead.",
|
|
129
|
+
"Client credentials against the org server must use `private_key_jwt`, whatever the application is set to."
|
|
130
|
+
]
|
|
131
|
+
};
|
|
132
|
+
},
|
|
133
|
+
/**
|
|
134
|
+
* Google.
|
|
135
|
+
*
|
|
136
|
+
* Refuses a public client outright, so server mode is the only option: the token endpoint
|
|
137
|
+
* checks for a client secret before it looks at PKCE.
|
|
138
|
+
*/
|
|
139
|
+
google(options = {}) {
|
|
140
|
+
return {
|
|
141
|
+
label: "Google",
|
|
142
|
+
client: {
|
|
143
|
+
issuer: "https://accounts.google.com",
|
|
144
|
+
scopes: options.scopes ?? ["openid", "profile", "email"],
|
|
145
|
+
...options.hostedDomain ? { extraParams: { hd: options.hostedDomain } } : {}
|
|
146
|
+
},
|
|
147
|
+
roles: rolesFrom.scope,
|
|
148
|
+
verifier: (clientId) => ({
|
|
149
|
+
// Names itself two ways, and a service pinned to one rejects tokens carrying the
|
|
150
|
+
// other. Discovery uses the first.
|
|
151
|
+
issuer: ["https://accounts.google.com", "accounts.google.com"],
|
|
152
|
+
audience: clientId
|
|
153
|
+
}),
|
|
154
|
+
constraints: [
|
|
155
|
+
"Refuses a public client: the token endpoint reports `client_secret is missing` before checking PKCE, so browser mode cannot work.",
|
|
156
|
+
"Publishes its issuer both with and without a scheme, so a verifier needs both spellings.",
|
|
157
|
+
"Issues a refresh token only on the first consent unless `prompt=consent` and `access_type=offline` are sent."
|
|
158
|
+
]
|
|
159
|
+
};
|
|
160
|
+
},
|
|
161
|
+
/**
|
|
162
|
+
* Dex.
|
|
163
|
+
*
|
|
164
|
+
* Sends no CORS headers, on discovery or the token endpoint, so a browser cannot talk to it
|
|
165
|
+
* at all. Server mode only, and giving endpoints explicitly does not help: the token request
|
|
166
|
+
* is blocked too.
|
|
167
|
+
*/
|
|
168
|
+
dex(options) {
|
|
169
|
+
const issuer = trim(options.baseUrl);
|
|
170
|
+
return {
|
|
171
|
+
label: "Dex",
|
|
172
|
+
client: {
|
|
173
|
+
issuer,
|
|
174
|
+
scopes: options.scopes ?? ["openid", "profile", "email", "offline_access"]
|
|
175
|
+
},
|
|
176
|
+
roles: rolesFrom.groups,
|
|
177
|
+
verifier: (clientId) => ({ issuer, audience: clientId }),
|
|
178
|
+
constraints: [
|
|
179
|
+
"Sends no CORS headers on discovery or the token endpoint, so browser mode cannot work. Naming the endpoints explicitly only moves the failure to the token request.",
|
|
180
|
+
"Has no admin interface. Users are static entries in its config file, and a password is a bcrypt hash.",
|
|
181
|
+
"Publishes no revocation endpoint."
|
|
182
|
+
]
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
export {
|
|
188
|
+
presets
|
|
189
|
+
};
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createTokenVerifier
|
|
3
|
+
} from "./chunk-4OHJTTX7.mjs";
|
|
4
|
+
|
|
5
|
+
// src/backchannel.ts
|
|
6
|
+
var LOGOUT_EVENT = "http://schemas.openid.net/event/backchannel-logout";
|
|
7
|
+
function memoryLogouts(ttlMs = 14 * 24 * 36e5) {
|
|
8
|
+
const sids = /* @__PURE__ */ new Map();
|
|
9
|
+
const subs = /* @__PURE__ */ new Map();
|
|
10
|
+
const prune = (now) => {
|
|
11
|
+
for (const map of [sids, subs]) for (const [key, at] of map) if (now - at > ttlMs) map.delete(key);
|
|
12
|
+
};
|
|
13
|
+
return {
|
|
14
|
+
add(entry) {
|
|
15
|
+
prune(Date.now());
|
|
16
|
+
if (entry.sid) sids.set(entry.sid, entry.at);
|
|
17
|
+
else if (entry.sub) subs.set(entry.sub, Math.max(entry.at, subs.get(entry.sub) ?? 0));
|
|
18
|
+
},
|
|
19
|
+
ended(session) {
|
|
20
|
+
if (session.sid && sids.has(session.sid)) return true;
|
|
21
|
+
const all = session.sub ? subs.get(session.sub) : void 0;
|
|
22
|
+
return all !== void 0 && session.since <= all;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
function idTokenClaims(idToken) {
|
|
27
|
+
if (!idToken) return {};
|
|
28
|
+
try {
|
|
29
|
+
const part = idToken.split(".")[1] ?? "";
|
|
30
|
+
const json = atob(part.replace(/-/g, "+").replace(/_/g, "/").padEnd(Math.ceil(part.length / 4) * 4, "="));
|
|
31
|
+
const claims = JSON.parse(json);
|
|
32
|
+
return {
|
|
33
|
+
sid: typeof claims.sid === "string" ? claims.sid : void 0,
|
|
34
|
+
sub: typeof claims.sub === "string" ? claims.sub : void 0,
|
|
35
|
+
iat: typeof claims.iat === "number" ? claims.iat : void 0
|
|
36
|
+
};
|
|
37
|
+
} catch {
|
|
38
|
+
return {};
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function headerOf(token) {
|
|
42
|
+
try {
|
|
43
|
+
const part = token.split(".")[0] ?? "";
|
|
44
|
+
return JSON.parse(atob(part.replace(/-/g, "+").replace(/_/g, "/").padEnd(Math.ceil(part.length / 4) * 4, "=")));
|
|
45
|
+
} catch {
|
|
46
|
+
return {};
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
function logoutTokenVerifier(options) {
|
|
50
|
+
const verify = createTokenVerifier({
|
|
51
|
+
issuer: options.issuer,
|
|
52
|
+
audience: options.clientId,
|
|
53
|
+
jwksUri: options.jwksUri,
|
|
54
|
+
jwks: options.jwks,
|
|
55
|
+
require: (claims) => {
|
|
56
|
+
const events = claims.events;
|
|
57
|
+
if (!events || typeof events !== "object" || !(LOGOUT_EVENT in events)) return "no logout event";
|
|
58
|
+
if ("nonce" in claims) return "a logout token has no nonce";
|
|
59
|
+
if (typeof claims.sid !== "string" && typeof claims.sub !== "string") return "names no session and no subject";
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
return async (token) => {
|
|
64
|
+
const typ = headerOf(token).typ;
|
|
65
|
+
if (typ !== void 0 && typ !== "logout+jwt") throw new Error("not a logout token");
|
|
66
|
+
const claims = await verify(token);
|
|
67
|
+
return {
|
|
68
|
+
sid: typeof claims.sid === "string" ? claims.sid : void 0,
|
|
69
|
+
sub: typeof claims.sub === "string" ? claims.sub : void 0,
|
|
70
|
+
at: (typeof claims.iat === "number" ? claims.iat : Math.floor(Date.now() / 1e3)) * 1e3
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export {
|
|
76
|
+
LOGOUT_EVENT,
|
|
77
|
+
memoryLogouts,
|
|
78
|
+
idTokenClaims,
|
|
79
|
+
logoutTokenVerifier
|
|
80
|
+
};
|