@fluixi/oauth2 1.0.0-alpha.11 → 1.0.0-alpha.111
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,36 @@
|
|
|
1
|
+
// src/roles.ts
|
|
2
|
+
var rolesFrom = {
|
|
3
|
+
/** Keycloak, realm roles. Where it puts them unless a client mapper is configured. */
|
|
4
|
+
keycloakRealm: (claims) => {
|
|
5
|
+
const realm = claims.realm_access;
|
|
6
|
+
return realm?.roles ?? [];
|
|
7
|
+
},
|
|
8
|
+
/** Keycloak, roles scoped to one client rather than the realm. */
|
|
9
|
+
keycloakClient: (clientId) => (claims) => {
|
|
10
|
+
const all = claims.resource_access;
|
|
11
|
+
return all?.[clientId]?.roles ?? [];
|
|
12
|
+
},
|
|
13
|
+
/** Okta, and anything else using a groups claim. Needs a groups claim on the token. */
|
|
14
|
+
groups: (claims) => Array.isArray(claims.groups) ? claims.groups : [],
|
|
15
|
+
/** Cognito, which namespaces the claim. */
|
|
16
|
+
cognito: (claims) => {
|
|
17
|
+
const groups = claims["cognito:groups"];
|
|
18
|
+
return Array.isArray(groups) ? groups : [];
|
|
19
|
+
},
|
|
20
|
+
/**
|
|
21
|
+
* Auth0, which requires a namespaced custom claim added by an action.
|
|
22
|
+
*
|
|
23
|
+
* `rolesFrom.namespaced('https://acme.com/roles')`. Auth0 silently drops a custom claim
|
|
24
|
+
* that is not a URI, so an empty list here usually means the namespace, not the action.
|
|
25
|
+
*/
|
|
26
|
+
namespaced: (claim) => (claims) => {
|
|
27
|
+
const value = claims[claim];
|
|
28
|
+
return Array.isArray(value) ? value : [];
|
|
29
|
+
},
|
|
30
|
+
/** The `scope` claim read as a role list, for a provider issuing no roles at all. */
|
|
31
|
+
scope: (claims) => typeof claims.scope === "string" ? claims.scope.split(" ") : []
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export {
|
|
35
|
+
rolesFrom
|
|
36
|
+
};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// src/pkce.ts
|
|
2
|
+
function base64url(bytes) {
|
|
3
|
+
let binary = "";
|
|
4
|
+
for (const byte of bytes) binary += String.fromCharCode(byte);
|
|
5
|
+
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
6
|
+
}
|
|
7
|
+
function randomBytes(length) {
|
|
8
|
+
const bytes = new Uint8Array(length);
|
|
9
|
+
crypto.getRandomValues(bytes);
|
|
10
|
+
return bytes;
|
|
11
|
+
}
|
|
12
|
+
function createVerifier() {
|
|
13
|
+
return base64url(randomBytes(32));
|
|
14
|
+
}
|
|
15
|
+
function createStateValue() {
|
|
16
|
+
return base64url(randomBytes(16));
|
|
17
|
+
}
|
|
18
|
+
async function createChallenge(verifier) {
|
|
19
|
+
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(verifier));
|
|
20
|
+
return base64url(new Uint8Array(digest));
|
|
21
|
+
}
|
|
22
|
+
async function createPkcePair() {
|
|
23
|
+
const verifier = createVerifier();
|
|
24
|
+
return { verifier, challenge: await createChallenge(verifier) };
|
|
25
|
+
}
|
|
26
|
+
function timingSafeEqual(a, b) {
|
|
27
|
+
if (a.length !== b.length) return false;
|
|
28
|
+
let diff = 0;
|
|
29
|
+
for (let i = 0; i < a.length; i++) diff |= a.charCodeAt(i) ^ b.charCodeAt(i);
|
|
30
|
+
return diff === 0;
|
|
31
|
+
}
|
|
32
|
+
function expiryFrom(expiresIn, now = Date.now()) {
|
|
33
|
+
const seconds = typeof expiresIn === "number" ? expiresIn : Number(expiresIn);
|
|
34
|
+
return Number.isFinite(seconds) && seconds > 0 ? now + seconds * 1e3 : void 0;
|
|
35
|
+
}
|
|
36
|
+
function claimsOf(token) {
|
|
37
|
+
const parts = token.split(".");
|
|
38
|
+
if (parts.length < 2) return null;
|
|
39
|
+
try {
|
|
40
|
+
const payload = parts[1].replace(/-/g, "+").replace(/_/g, "/");
|
|
41
|
+
return JSON.parse(atob(payload.padEnd(Math.ceil(payload.length / 4) * 4, "=")));
|
|
42
|
+
} catch {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function expiryFromJwt(token) {
|
|
47
|
+
const parts = token.split(".");
|
|
48
|
+
if (parts.length < 2) return null;
|
|
49
|
+
try {
|
|
50
|
+
const payload = parts[1].replace(/-/g, "+").replace(/_/g, "/");
|
|
51
|
+
const json = JSON.parse(atob(payload.padEnd(Math.ceil(payload.length / 4) * 4, "=")));
|
|
52
|
+
return typeof json.exp === "number" ? json.exp * 1e3 : null;
|
|
53
|
+
} catch {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export {
|
|
59
|
+
createVerifier,
|
|
60
|
+
createStateValue,
|
|
61
|
+
createChallenge,
|
|
62
|
+
createPkcePair,
|
|
63
|
+
timingSafeEqual,
|
|
64
|
+
expiryFrom,
|
|
65
|
+
claimsOf,
|
|
66
|
+
expiryFromJwt
|
|
67
|
+
};
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import {
|
|
2
|
+
OAuthError
|
|
3
|
+
} from "./chunk-AXPSPTNP.mjs";
|
|
4
|
+
|
|
5
|
+
// src/client-auth.ts
|
|
6
|
+
var CANONICAL = {
|
|
7
|
+
basic: "client_secret_basic",
|
|
8
|
+
body: "client_secret_post",
|
|
9
|
+
client_secret_basic: "client_secret_basic",
|
|
10
|
+
client_secret_post: "client_secret_post",
|
|
11
|
+
private_key_jwt: "private_key_jwt",
|
|
12
|
+
none: "none"
|
|
13
|
+
};
|
|
14
|
+
function resolveMethod(auth) {
|
|
15
|
+
if (auth.method) return CANONICAL[auth.method] ?? "client_secret_basic";
|
|
16
|
+
if (auth.privateKey) return "private_key_jwt";
|
|
17
|
+
return auth.clientSecret ? "client_secret_basic" : "none";
|
|
18
|
+
}
|
|
19
|
+
function assertUsable(auth, who) {
|
|
20
|
+
if (!auth.clientId) throw new Error(`[fluixi/oauth2] ${who} needs a clientId.`);
|
|
21
|
+
const method = resolveMethod(auth);
|
|
22
|
+
if (method === "private_key_jwt" && !auth.privateKey) {
|
|
23
|
+
throw new Error(
|
|
24
|
+
`[fluixi/oauth2] ${who}: private_key_jwt needs \`privateKey\`, the JWK whose public half the provider holds.`
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
if ((method === "client_secret_basic" || method === "client_secret_post") && !auth.clientSecret) {
|
|
28
|
+
throw new Error(
|
|
29
|
+
`[fluixi/oauth2] ${who}: ${method} needs a \`clientSecret\`. A public client should use \`method: 'none'\`, which PKCE is what protects.`
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
return method;
|
|
33
|
+
}
|
|
34
|
+
async function loadJose() {
|
|
35
|
+
try {
|
|
36
|
+
return await import("./browser-FDUOEUM2.mjs");
|
|
37
|
+
} catch {
|
|
38
|
+
throw new OAuthError(
|
|
39
|
+
"private_key_jwt needs `jose`. Install it: npm i jose. It is an optional peer, so a client using a secret does not carry it.",
|
|
40
|
+
"jose_missing"
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
async function signAssertion(auth, endpoint) {
|
|
45
|
+
const jose = await loadJose();
|
|
46
|
+
const alg = auth.assertionAlgorithm ?? "RS256";
|
|
47
|
+
const key = await jose.importJWK(auth.privateKey, alg);
|
|
48
|
+
const kid = auth.keyId ?? auth.privateKey.kid;
|
|
49
|
+
return new jose.SignJWT({}).setProtectedHeader(kid ? { alg, kid } : { alg }).setIssuer(auth.clientId).setSubject(auth.clientId).setAudience(endpoint).setJti(crypto.randomUUID()).setIssuedAt().setExpirationTime("1m").sign(key);
|
|
50
|
+
}
|
|
51
|
+
async function applyClientAuth(auth, endpoint, headers, form) {
|
|
52
|
+
switch (resolveMethod(auth)) {
|
|
53
|
+
case "client_secret_basic": {
|
|
54
|
+
const pair = `${encodeURIComponent(auth.clientId)}:${encodeURIComponent(auth.clientSecret)}`;
|
|
55
|
+
headers.authorization = `Basic ${btoa(pair)}`;
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
case "client_secret_post":
|
|
59
|
+
form.set("client_id", auth.clientId);
|
|
60
|
+
form.set("client_secret", auth.clientSecret);
|
|
61
|
+
break;
|
|
62
|
+
case "private_key_jwt":
|
|
63
|
+
form.set("client_id", auth.clientId);
|
|
64
|
+
form.set("client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer");
|
|
65
|
+
form.set("client_assertion", await signAssertion(auth, endpoint));
|
|
66
|
+
break;
|
|
67
|
+
case "none":
|
|
68
|
+
form.set("client_id", auth.clientId);
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export {
|
|
74
|
+
resolveMethod,
|
|
75
|
+
assertUsable,
|
|
76
|
+
applyClientAuth
|
|
77
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// src/return-to.ts
|
|
2
|
+
function safeReturnTo(candidate, options = {}) {
|
|
3
|
+
const fallback = options.fallback ?? "/";
|
|
4
|
+
if (!candidate || typeof candidate !== "string") return fallback;
|
|
5
|
+
const value = candidate.trim();
|
|
6
|
+
if (!value) return fallback;
|
|
7
|
+
if (/[\u0000-\u001f\u007f]/.test(value)) return fallback;
|
|
8
|
+
if (/^[/\\]{2}/.test(value)) return fallback;
|
|
9
|
+
if (value.startsWith("/")) return value;
|
|
10
|
+
const here = options.origin ?? (typeof location === "undefined" ? void 0 : location.origin);
|
|
11
|
+
const allowed = [here, ...options.allowedOrigins ?? []].filter(Boolean);
|
|
12
|
+
if (!allowed.length) return fallback;
|
|
13
|
+
let url;
|
|
14
|
+
try {
|
|
15
|
+
url = new URL(value);
|
|
16
|
+
} catch {
|
|
17
|
+
return fallback;
|
|
18
|
+
}
|
|
19
|
+
if (!allowed.includes(url.origin)) return fallback;
|
|
20
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") return fallback;
|
|
21
|
+
return url.toString();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export {
|
|
25
|
+
safeReturnTo
|
|
26
|
+
};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createDpopKey
|
|
3
|
+
} from "./chunk-27BGFJIY.mjs";
|
|
4
|
+
|
|
5
|
+
// src/dpop-store.ts
|
|
6
|
+
var DB = "fluixi-oauth2-dpop";
|
|
7
|
+
var STORE = "keys";
|
|
8
|
+
function available() {
|
|
9
|
+
return typeof indexedDB !== "undefined";
|
|
10
|
+
}
|
|
11
|
+
function open() {
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
const request = indexedDB.open(DB, 1);
|
|
14
|
+
request.onupgradeneeded = () => {
|
|
15
|
+
const db = request.result;
|
|
16
|
+
if (!db.objectStoreNames.contains(STORE)) db.createObjectStore(STORE);
|
|
17
|
+
};
|
|
18
|
+
request.onsuccess = () => resolve(request.result);
|
|
19
|
+
request.onerror = () => reject(request.error ?? new Error("indexedDB.open failed"));
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
function act(mode, run) {
|
|
23
|
+
return open().then(
|
|
24
|
+
(db) => new Promise((resolve, reject) => {
|
|
25
|
+
const tx = db.transaction(STORE, mode);
|
|
26
|
+
const request = run(tx.objectStore(STORE));
|
|
27
|
+
request.onsuccess = () => resolve(request.result);
|
|
28
|
+
request.onerror = () => reject(request.error ?? new Error("DPoP key request failed"));
|
|
29
|
+
tx.oncomplete = () => db.close();
|
|
30
|
+
})
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
function usable(value) {
|
|
34
|
+
const stored = value;
|
|
35
|
+
return !!stored && typeof stored.thumbprint === "string" && typeof stored.alg === "string" && !!stored.publicJwk && // A CryptoKey survives the round trip as a CryptoKey. Anything else means the record was
|
|
36
|
+
// written by something other than this, and binding to it would fail at the provider.
|
|
37
|
+
typeof stored.privateKey?.type === "string";
|
|
38
|
+
}
|
|
39
|
+
async function keyFor(clientKey, alg = "ES256", onEphemeral) {
|
|
40
|
+
if (!available()) {
|
|
41
|
+
onEphemeral?.("This environment has no IndexedDB, so the key lasts only as long as this document.");
|
|
42
|
+
return createDpopKey(alg);
|
|
43
|
+
}
|
|
44
|
+
try {
|
|
45
|
+
const found = await act("readonly", (store) => store.get(clientKey));
|
|
46
|
+
if (usable(found) && found.alg === alg) {
|
|
47
|
+
return {
|
|
48
|
+
privateKey: found.privateKey,
|
|
49
|
+
publicJwk: found.publicJwk,
|
|
50
|
+
thumbprint: found.thumbprint,
|
|
51
|
+
alg: found.alg
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
} catch (cause) {
|
|
55
|
+
onEphemeral?.(
|
|
56
|
+
`The stored DPoP key could not be read (${cause instanceof Error ? cause.message : String(cause)}), so a new one was made.`
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
const key = await createDpopKey(alg);
|
|
60
|
+
try {
|
|
61
|
+
await act(
|
|
62
|
+
"readwrite",
|
|
63
|
+
(store) => store.put(
|
|
64
|
+
{
|
|
65
|
+
privateKey: key.privateKey,
|
|
66
|
+
publicJwk: key.publicJwk,
|
|
67
|
+
thumbprint: key.thumbprint,
|
|
68
|
+
alg: key.alg
|
|
69
|
+
},
|
|
70
|
+
clientKey
|
|
71
|
+
)
|
|
72
|
+
);
|
|
73
|
+
} catch (cause) {
|
|
74
|
+
onEphemeral?.(
|
|
75
|
+
`The DPoP key could not be stored (${cause instanceof Error ? cause.message : String(cause)}), so a refresh after the next navigation will be refused.`
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
return key;
|
|
79
|
+
}
|
|
80
|
+
async function forgetKey(clientKey) {
|
|
81
|
+
if (!available()) return;
|
|
82
|
+
try {
|
|
83
|
+
await act("readwrite", (store) => store.delete(clientKey));
|
|
84
|
+
} catch {
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export {
|
|
89
|
+
keyFor,
|
|
90
|
+
forgetKey
|
|
91
|
+
};
|
package/dist/client-auth.mjs
CHANGED
|
@@ -1,82 +1,10 @@
|
|
|
1
|
+
import {
|
|
2
|
+
applyClientAuth,
|
|
3
|
+
assertUsable,
|
|
4
|
+
resolveMethod
|
|
5
|
+
} from "./chunk-SJOBQPG6.mjs";
|
|
6
|
+
import "./chunk-AXPSPTNP.mjs";
|
|
1
7
|
import "./chunk-7P6ASYW6.mjs";
|
|
2
|
-
|
|
3
|
-
// src/types.ts
|
|
4
|
-
var OAuthError = class extends Error {
|
|
5
|
-
constructor(message, code, description) {
|
|
6
|
-
super(message);
|
|
7
|
-
this.code = code;
|
|
8
|
-
this.description = description;
|
|
9
|
-
this.name = "OAuthError";
|
|
10
|
-
}
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
// src/client-auth.ts
|
|
14
|
-
var CANONICAL = {
|
|
15
|
-
basic: "client_secret_basic",
|
|
16
|
-
body: "client_secret_post",
|
|
17
|
-
client_secret_basic: "client_secret_basic",
|
|
18
|
-
client_secret_post: "client_secret_post",
|
|
19
|
-
private_key_jwt: "private_key_jwt",
|
|
20
|
-
none: "none"
|
|
21
|
-
};
|
|
22
|
-
function resolveMethod(auth) {
|
|
23
|
-
if (auth.method) return CANONICAL[auth.method] ?? "client_secret_basic";
|
|
24
|
-
if (auth.privateKey) return "private_key_jwt";
|
|
25
|
-
return auth.clientSecret ? "client_secret_basic" : "none";
|
|
26
|
-
}
|
|
27
|
-
function assertUsable(auth, who) {
|
|
28
|
-
if (!auth.clientId) throw new Error(`[fluixi/oauth2] ${who} needs a clientId.`);
|
|
29
|
-
const method = resolveMethod(auth);
|
|
30
|
-
if (method === "private_key_jwt" && !auth.privateKey) {
|
|
31
|
-
throw new Error(
|
|
32
|
-
`[fluixi/oauth2] ${who}: private_key_jwt needs \`privateKey\`, the JWK whose public half the provider holds.`
|
|
33
|
-
);
|
|
34
|
-
}
|
|
35
|
-
if ((method === "client_secret_basic" || method === "client_secret_post") && !auth.clientSecret) {
|
|
36
|
-
throw new Error(
|
|
37
|
-
`[fluixi/oauth2] ${who}: ${method} needs a \`clientSecret\`. A public client should use \`method: 'none'\`, which PKCE is what protects.`
|
|
38
|
-
);
|
|
39
|
-
}
|
|
40
|
-
return method;
|
|
41
|
-
}
|
|
42
|
-
async function loadJose() {
|
|
43
|
-
try {
|
|
44
|
-
return await import("./browser-FDUOEUM2.mjs");
|
|
45
|
-
} catch {
|
|
46
|
-
throw new OAuthError(
|
|
47
|
-
"private_key_jwt needs `jose`. Install it: npm i jose. It is an optional peer, so a client using a secret does not carry it.",
|
|
48
|
-
"jose_missing"
|
|
49
|
-
);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
async function signAssertion(auth, endpoint) {
|
|
53
|
-
const jose = await loadJose();
|
|
54
|
-
const alg = auth.assertionAlgorithm ?? "RS256";
|
|
55
|
-
const key = await jose.importJWK(auth.privateKey, alg);
|
|
56
|
-
const kid = auth.keyId ?? auth.privateKey.kid;
|
|
57
|
-
return new jose.SignJWT({}).setProtectedHeader(kid ? { alg, kid } : { alg }).setIssuer(auth.clientId).setSubject(auth.clientId).setAudience(endpoint).setJti(crypto.randomUUID()).setIssuedAt().setExpirationTime("1m").sign(key);
|
|
58
|
-
}
|
|
59
|
-
async function applyClientAuth(auth, endpoint, headers, form) {
|
|
60
|
-
switch (resolveMethod(auth)) {
|
|
61
|
-
case "client_secret_basic": {
|
|
62
|
-
const pair = `${encodeURIComponent(auth.clientId)}:${encodeURIComponent(auth.clientSecret)}`;
|
|
63
|
-
headers.authorization = `Basic ${btoa(pair)}`;
|
|
64
|
-
break;
|
|
65
|
-
}
|
|
66
|
-
case "client_secret_post":
|
|
67
|
-
form.set("client_id", auth.clientId);
|
|
68
|
-
form.set("client_secret", auth.clientSecret);
|
|
69
|
-
break;
|
|
70
|
-
case "private_key_jwt":
|
|
71
|
-
form.set("client_id", auth.clientId);
|
|
72
|
-
form.set("client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer");
|
|
73
|
-
form.set("client_assertion", await signAssertion(auth, endpoint));
|
|
74
|
-
break;
|
|
75
|
-
case "none":
|
|
76
|
-
form.set("client_id", auth.clientId);
|
|
77
|
-
break;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
8
|
export {
|
|
81
9
|
applyClientAuth,
|
|
82
10
|
assertUsable,
|
package/dist/discovery.cjs
CHANGED
|
@@ -48,8 +48,12 @@ async function discover(issuer, fetcher = fetch) {
|
|
|
48
48
|
try {
|
|
49
49
|
response = await fetcher(documentUrl(issuer));
|
|
50
50
|
} catch (cause) {
|
|
51
|
+
const why = cause instanceof Error ? cause.message : String(cause);
|
|
51
52
|
throw new OAuthError(
|
|
52
|
-
|
|
53
|
+
typeof window === "undefined" ? (
|
|
54
|
+
// On a server nothing blocks the read: the provider is down, or the address is wrong.
|
|
55
|
+
`Could not reach the provider at ${documentUrl(issuer)}: is it running, and is the issuer right? (${why})`
|
|
56
|
+
) : `Could not read the discovery document at ${documentUrl(issuer)}. In a browser this is usually the provider refusing a cross-origin read: give \`endpoints\` directly, or resolve them on a server. (${why})`,
|
|
53
57
|
"discovery_unreachable"
|
|
54
58
|
);
|
|
55
59
|
}
|
|
@@ -74,6 +78,7 @@ async function discover(issuer, fetcher = fetch) {
|
|
|
74
78
|
jwks: doc.jwks_uri,
|
|
75
79
|
introspection: doc.introspection_endpoint,
|
|
76
80
|
revocation: doc.revocation_endpoint,
|
|
81
|
+
checkSession: doc.check_session_iframe,
|
|
77
82
|
issuer: doc.issuer ?? issuer
|
|
78
83
|
};
|
|
79
84
|
})();
|
package/dist/discovery.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"discovery.d.ts","sourceRoot":"","sources":["../src/discovery.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"discovery.d.ts","sourceRoot":"","sources":["../src/discovery.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAuB5C,wBAAsB,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,GAAE,OAAO,KAAa,GAAG,OAAO,CAAC,SAAS,CAAC,CAuDhG;AAED,2CAA2C;AAC3C,wBAAgB,mBAAmB,IAAI,IAAI,CAE1C"}
|
package/dist/discovery.js
CHANGED
|
@@ -18,7 +18,11 @@ export async function discover(issuer, fetcher = fetch) {
|
|
|
18
18
|
// `fetch` reports only "Failed to fetch". Not every provider allows a page to read
|
|
19
19
|
// its discovery document: Keycloak does once an origin is configured, Dex and
|
|
20
20
|
// Google do not. Naming the cause saves a long hunt for a network fault.
|
|
21
|
-
|
|
21
|
+
const why = cause instanceof Error ? cause.message : String(cause);
|
|
22
|
+
throw new OAuthError(typeof window === 'undefined'
|
|
23
|
+
? // On a server nothing blocks the read: the provider is down, or the address is wrong.
|
|
24
|
+
`Could not reach the provider at ${documentUrl(issuer)}: is it running, and is the issuer right? (${why})`
|
|
25
|
+
: `Could not read the discovery document at ${documentUrl(issuer)}. In a browser this is usually the provider refusing a cross-origin read: give \`endpoints\` directly, or resolve them on a server. (${why})`, 'discovery_unreachable');
|
|
22
26
|
}
|
|
23
27
|
if (!response.ok) {
|
|
24
28
|
throw new OAuthError(`Discovery failed for ${issuer}: ${response.status}`, 'discovery_failed');
|
|
@@ -35,6 +39,7 @@ export async function discover(issuer, fetcher = fetch) {
|
|
|
35
39
|
jwks: doc.jwks_uri,
|
|
36
40
|
introspection: doc.introspection_endpoint,
|
|
37
41
|
revocation: doc.revocation_endpoint,
|
|
42
|
+
checkSession: doc.check_session_iframe,
|
|
38
43
|
issuer: doc.issuer ?? issuer,
|
|
39
44
|
};
|
|
40
45
|
})();
|
package/dist/discovery.mjs
CHANGED
|
@@ -1,62 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
this.name = "OAuthError";
|
|
8
|
-
}
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
// src/discovery.ts
|
|
12
|
-
var cache = /* @__PURE__ */ new Map();
|
|
13
|
-
function documentUrl(issuer) {
|
|
14
|
-
return `${issuer.replace(/\/$/, "")}/.well-known/openid-configuration`;
|
|
15
|
-
}
|
|
16
|
-
async function discover(issuer, fetcher = fetch) {
|
|
17
|
-
const cached = cache.get(issuer);
|
|
18
|
-
if (cached) return cached;
|
|
19
|
-
const pending = (async () => {
|
|
20
|
-
let response;
|
|
21
|
-
try {
|
|
22
|
-
response = await fetcher(documentUrl(issuer));
|
|
23
|
-
} catch (cause) {
|
|
24
|
-
throw new OAuthError(
|
|
25
|
-
`Could not read the discovery document at ${documentUrl(issuer)}. In a browser this is usually the provider refusing a cross-origin read: give \`endpoints\` directly, or resolve them on a server. (${cause instanceof Error ? cause.message : String(cause)})`,
|
|
26
|
-
"discovery_unreachable"
|
|
27
|
-
);
|
|
28
|
-
}
|
|
29
|
-
if (!response.ok) {
|
|
30
|
-
throw new OAuthError(
|
|
31
|
-
`Discovery failed for ${issuer}: ${response.status}`,
|
|
32
|
-
"discovery_failed"
|
|
33
|
-
);
|
|
34
|
-
}
|
|
35
|
-
const doc = await response.json();
|
|
36
|
-
if (!doc.authorization_endpoint || !doc.token_endpoint) {
|
|
37
|
-
throw new OAuthError(
|
|
38
|
-
`${issuer} published a discovery document with no authorization or token endpoint.`,
|
|
39
|
-
"discovery_incomplete"
|
|
40
|
-
);
|
|
41
|
-
}
|
|
42
|
-
return {
|
|
43
|
-
authorization: doc.authorization_endpoint,
|
|
44
|
-
token: doc.token_endpoint,
|
|
45
|
-
userinfo: doc.userinfo_endpoint,
|
|
46
|
-
endSession: doc.end_session_endpoint,
|
|
47
|
-
jwks: doc.jwks_uri,
|
|
48
|
-
introspection: doc.introspection_endpoint,
|
|
49
|
-
revocation: doc.revocation_endpoint,
|
|
50
|
-
issuer: doc.issuer ?? issuer
|
|
51
|
-
};
|
|
52
|
-
})();
|
|
53
|
-
cache.set(issuer, pending);
|
|
54
|
-
pending.catch(() => cache.delete(issuer));
|
|
55
|
-
return pending;
|
|
56
|
-
}
|
|
57
|
-
function clearDiscoveryCache() {
|
|
58
|
-
cache.clear();
|
|
59
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
clearDiscoveryCache,
|
|
3
|
+
discover
|
|
4
|
+
} from "./chunk-AAN3YN4S.mjs";
|
|
5
|
+
import "./chunk-AXPSPTNP.mjs";
|
|
6
|
+
import "./chunk-7P6ASYW6.mjs";
|
|
60
7
|
export {
|
|
61
8
|
clearDiscoveryCache,
|
|
62
9
|
discover
|
package/dist/dpop-store.mjs
CHANGED
|
@@ -1,122 +1,10 @@
|
|
|
1
|
+
import {
|
|
2
|
+
forgetKey,
|
|
3
|
+
keyFor
|
|
4
|
+
} from "./chunk-XQJTQV3C.mjs";
|
|
5
|
+
import "./chunk-27BGFJIY.mjs";
|
|
6
|
+
import "./chunk-AXPSPTNP.mjs";
|
|
1
7
|
import "./chunk-7P6ASYW6.mjs";
|
|
2
|
-
|
|
3
|
-
// src/types.ts
|
|
4
|
-
var OAuthError = class extends Error {
|
|
5
|
-
constructor(message, code, description) {
|
|
6
|
-
super(message);
|
|
7
|
-
this.code = code;
|
|
8
|
-
this.description = description;
|
|
9
|
-
this.name = "OAuthError";
|
|
10
|
-
}
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
// src/dpop.ts
|
|
14
|
-
async function loadJose() {
|
|
15
|
-
try {
|
|
16
|
-
return await import("./browser-FDUOEUM2.mjs");
|
|
17
|
-
} catch {
|
|
18
|
-
throw new OAuthError(
|
|
19
|
-
"DPoP needs `jose`. Install it: npm i jose. It is an optional peer, so a client not using DPoP does not carry it.",
|
|
20
|
-
"jose_missing"
|
|
21
|
-
);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
async function createDpopKey(alg = "ES256", options = {}) {
|
|
25
|
-
const jose = await loadJose();
|
|
26
|
-
const { privateKey, publicKey } = await jose.generateKeyPair(alg, {
|
|
27
|
-
extractable: options.extractable ?? false
|
|
28
|
-
});
|
|
29
|
-
const publicJwk = { ...await jose.exportJWK(publicKey) };
|
|
30
|
-
return {
|
|
31
|
-
privateKey,
|
|
32
|
-
publicJwk,
|
|
33
|
-
thumbprint: await jose.calculateJwkThumbprint(publicJwk, "sha256"),
|
|
34
|
-
alg
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// src/dpop-store.ts
|
|
39
|
-
var DB = "fluixi-oauth2-dpop";
|
|
40
|
-
var STORE = "keys";
|
|
41
|
-
function available() {
|
|
42
|
-
return typeof indexedDB !== "undefined";
|
|
43
|
-
}
|
|
44
|
-
function open() {
|
|
45
|
-
return new Promise((resolve, reject) => {
|
|
46
|
-
const request = indexedDB.open(DB, 1);
|
|
47
|
-
request.onupgradeneeded = () => {
|
|
48
|
-
const db = request.result;
|
|
49
|
-
if (!db.objectStoreNames.contains(STORE)) db.createObjectStore(STORE);
|
|
50
|
-
};
|
|
51
|
-
request.onsuccess = () => resolve(request.result);
|
|
52
|
-
request.onerror = () => reject(request.error ?? new Error("indexedDB.open failed"));
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
function act(mode, run) {
|
|
56
|
-
return open().then(
|
|
57
|
-
(db) => new Promise((resolve, reject) => {
|
|
58
|
-
const tx = db.transaction(STORE, mode);
|
|
59
|
-
const request = run(tx.objectStore(STORE));
|
|
60
|
-
request.onsuccess = () => resolve(request.result);
|
|
61
|
-
request.onerror = () => reject(request.error ?? new Error("DPoP key request failed"));
|
|
62
|
-
tx.oncomplete = () => db.close();
|
|
63
|
-
})
|
|
64
|
-
);
|
|
65
|
-
}
|
|
66
|
-
function usable(value) {
|
|
67
|
-
const stored = value;
|
|
68
|
-
return !!stored && typeof stored.thumbprint === "string" && typeof stored.alg === "string" && !!stored.publicJwk && // A CryptoKey survives the round trip as a CryptoKey. Anything else means the record was
|
|
69
|
-
// written by something other than this, and binding to it would fail at the provider.
|
|
70
|
-
typeof stored.privateKey?.type === "string";
|
|
71
|
-
}
|
|
72
|
-
async function keyFor(clientKey, alg = "ES256", onEphemeral) {
|
|
73
|
-
if (!available()) {
|
|
74
|
-
onEphemeral?.("This environment has no IndexedDB, so the key lasts only as long as this document.");
|
|
75
|
-
return createDpopKey(alg);
|
|
76
|
-
}
|
|
77
|
-
try {
|
|
78
|
-
const found = await act("readonly", (store) => store.get(clientKey));
|
|
79
|
-
if (usable(found) && found.alg === alg) {
|
|
80
|
-
return {
|
|
81
|
-
privateKey: found.privateKey,
|
|
82
|
-
publicJwk: found.publicJwk,
|
|
83
|
-
thumbprint: found.thumbprint,
|
|
84
|
-
alg: found.alg
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
} catch (cause) {
|
|
88
|
-
onEphemeral?.(
|
|
89
|
-
`The stored DPoP key could not be read (${cause instanceof Error ? cause.message : String(cause)}), so a new one was made.`
|
|
90
|
-
);
|
|
91
|
-
}
|
|
92
|
-
const key = await createDpopKey(alg);
|
|
93
|
-
try {
|
|
94
|
-
await act(
|
|
95
|
-
"readwrite",
|
|
96
|
-
(store) => store.put(
|
|
97
|
-
{
|
|
98
|
-
privateKey: key.privateKey,
|
|
99
|
-
publicJwk: key.publicJwk,
|
|
100
|
-
thumbprint: key.thumbprint,
|
|
101
|
-
alg: key.alg
|
|
102
|
-
},
|
|
103
|
-
clientKey
|
|
104
|
-
)
|
|
105
|
-
);
|
|
106
|
-
} catch (cause) {
|
|
107
|
-
onEphemeral?.(
|
|
108
|
-
`The DPoP key could not be stored (${cause instanceof Error ? cause.message : String(cause)}), so a refresh after the next navigation will be refused.`
|
|
109
|
-
);
|
|
110
|
-
}
|
|
111
|
-
return key;
|
|
112
|
-
}
|
|
113
|
-
async function forgetKey(clientKey) {
|
|
114
|
-
if (!available()) return;
|
|
115
|
-
try {
|
|
116
|
-
await act("readwrite", (store) => store.delete(clientKey));
|
|
117
|
-
} catch {
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
8
|
export {
|
|
121
9
|
forgetKey,
|
|
122
10
|
keyFor
|