@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
package/dist/dpop.mjs
CHANGED
|
@@ -1,99 +1,15 @@
|
|
|
1
|
+
import {
|
|
2
|
+
boundKeyOf,
|
|
3
|
+
createDpopKey,
|
|
4
|
+
exportDpopKey,
|
|
5
|
+
importDpopKey,
|
|
6
|
+
nonceFrom,
|
|
7
|
+
proofFor,
|
|
8
|
+
thumbprintOf,
|
|
9
|
+
wantsNonce
|
|
10
|
+
} from "./chunk-27BGFJIY.mjs";
|
|
11
|
+
import "./chunk-AXPSPTNP.mjs";
|
|
1
12
|
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
|
-
async function exportDpopKey(key) {
|
|
38
|
-
const jose = await loadJose();
|
|
39
|
-
return { jwk: { ...await jose.exportJWK(key.privateKey) }, alg: key.alg };
|
|
40
|
-
}
|
|
41
|
-
var PRIVATE_MEMBERS = /* @__PURE__ */ new Set(["d", "p", "q", "dp", "dq", "qi", "oth"]);
|
|
42
|
-
async function importDpopKey(stored) {
|
|
43
|
-
const jose = await loadJose();
|
|
44
|
-
const privateKey = await jose.importJWK(stored.jwk, stored.alg);
|
|
45
|
-
const publicJwk = Object.fromEntries(Object.entries(stored.jwk).filter(([k]) => !PRIVATE_MEMBERS.has(k)));
|
|
46
|
-
return {
|
|
47
|
-
privateKey,
|
|
48
|
-
publicJwk,
|
|
49
|
-
thumbprint: await jose.calculateJwkThumbprint(publicJwk, "sha256"),
|
|
50
|
-
alg: stored.alg
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
async function thumbprintOf(jwk) {
|
|
54
|
-
const jose = await loadJose();
|
|
55
|
-
return jose.calculateJwkThumbprint(jwk, "sha256");
|
|
56
|
-
}
|
|
57
|
-
async function proofFor(key, options) {
|
|
58
|
-
const jose = await loadJose();
|
|
59
|
-
const htu = (() => {
|
|
60
|
-
try {
|
|
61
|
-
const url = new URL(options.url);
|
|
62
|
-
url.search = "";
|
|
63
|
-
url.hash = "";
|
|
64
|
-
return url.toString();
|
|
65
|
-
} catch {
|
|
66
|
-
return options.url;
|
|
67
|
-
}
|
|
68
|
-
})();
|
|
69
|
-
const claims = {
|
|
70
|
-
htm: options.method.toUpperCase(),
|
|
71
|
-
htu,
|
|
72
|
-
jti: crypto.randomUUID()
|
|
73
|
-
};
|
|
74
|
-
if (options.accessToken) {
|
|
75
|
-
const digest = await crypto.subtle.digest(
|
|
76
|
-
"SHA-256",
|
|
77
|
-
new TextEncoder().encode(options.accessToken)
|
|
78
|
-
);
|
|
79
|
-
let binary = "";
|
|
80
|
-
for (const byte of new Uint8Array(digest)) binary += String.fromCharCode(byte);
|
|
81
|
-
claims.ath = btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
82
|
-
}
|
|
83
|
-
if (options.nonce) claims.nonce = options.nonce;
|
|
84
|
-
return new jose.SignJWT(claims).setProtectedHeader({ typ: "dpop+jwt", alg: key.alg, jwk: key.publicJwk }).setIssuedAt().sign(key.privateKey);
|
|
85
|
-
}
|
|
86
|
-
function nonceFrom(response) {
|
|
87
|
-
return response.headers.get("dpop-nonce");
|
|
88
|
-
}
|
|
89
|
-
function wantsNonce(error, response) {
|
|
90
|
-
const code = typeof error === "string" ? error : error?.error;
|
|
91
|
-
return code === "use_dpop_nonce" || !!response && !!nonceFrom(response);
|
|
92
|
-
}
|
|
93
|
-
function boundKeyOf(claims) {
|
|
94
|
-
const cnf = claims.cnf;
|
|
95
|
-
return typeof cnf?.jkt === "string" ? cnf.jkt : null;
|
|
96
|
-
}
|
|
97
13
|
export {
|
|
98
14
|
boundKeyOf,
|
|
99
15
|
createDpopKey,
|
package/dist/env-config.cjs
CHANGED
|
@@ -46,7 +46,7 @@ var rolesFrom = {
|
|
|
46
46
|
/**
|
|
47
47
|
* Auth0, which requires a namespaced custom claim added by an action.
|
|
48
48
|
*
|
|
49
|
-
* `rolesFrom.namespaced('https://
|
|
49
|
+
* `rolesFrom.namespaced('https://acme.com/roles')`. Auth0 silently drops a custom claim
|
|
50
50
|
* that is not a URI, so an empty list here usually means the namespace, not the action.
|
|
51
51
|
*/
|
|
52
52
|
namespaced: (claim) => (claims) => {
|
package/dist/env-config.mjs
CHANGED
|
@@ -1,217 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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://adafri.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
|
-
// src/presets.ts
|
|
35
|
-
var trim = (url) => url.replace(/\/+$/, "");
|
|
36
|
-
var presets = {
|
|
37
|
-
/**
|
|
38
|
-
* Any OIDC provider publishing a discovery document.
|
|
39
|
-
*
|
|
40
|
-
* The agnostic path, and the one to reach for first. A provider not listed below almost
|
|
41
|
-
* certainly works through this.
|
|
42
|
-
*/
|
|
43
|
-
oidc(options) {
|
|
44
|
-
return {
|
|
45
|
-
label: options.issuer,
|
|
46
|
-
client: { issuer: trim(options.issuer), scopes: options.scopes },
|
|
47
|
-
roles: options.roles ?? rolesFrom.scope,
|
|
48
|
-
verifier: (clientId) => ({
|
|
49
|
-
issuer: trim(options.issuer),
|
|
50
|
-
audience: options.audience ?? clientId,
|
|
51
|
-
...options.audience === false ? { authorizedParty: clientId } : {}
|
|
52
|
-
}),
|
|
53
|
-
constraints: []
|
|
54
|
-
};
|
|
55
|
-
},
|
|
56
|
-
/**
|
|
57
|
-
* A plain OAuth2 server with no discovery document.
|
|
58
|
-
*
|
|
59
|
-
* Every endpoint is given, because there is nothing to ask. Token verification needs an
|
|
60
|
-
* issuer regardless, since a verifier will not accept a token without one.
|
|
61
|
-
*/
|
|
62
|
-
oauth2(options) {
|
|
63
|
-
return {
|
|
64
|
-
label: options.issuer,
|
|
65
|
-
client: { endpoints: options.endpoints, scopes: options.scopes },
|
|
66
|
-
roles: options.roles ?? rolesFrom.scope,
|
|
67
|
-
verifier: (clientId) => ({
|
|
68
|
-
issuer: trim(options.issuer),
|
|
69
|
-
audience: options.audience ?? clientId,
|
|
70
|
-
...options.audience === false ? { authorizedParty: clientId } : {}
|
|
71
|
-
}),
|
|
72
|
-
constraints: ["Publishes no discovery document, so every endpoint is configured by hand."]
|
|
73
|
-
};
|
|
74
|
-
},
|
|
75
|
-
/** Keycloak. A realm is its own issuer. */
|
|
76
|
-
keycloak(options) {
|
|
77
|
-
const issuer = `${trim(options.baseUrl)}/realms/${options.realm}`;
|
|
78
|
-
return {
|
|
79
|
-
label: "Keycloak",
|
|
80
|
-
client: {
|
|
81
|
-
issuer,
|
|
82
|
-
scopes: options.scopes ?? ["openid", "profile", "email"]
|
|
83
|
-
},
|
|
84
|
-
roles: rolesFrom.keycloakRealm,
|
|
85
|
-
// No `aud` on an access token until an audience mapper is configured, and the client is
|
|
86
|
-
// named in `azp` instead. So the check moves to `authorizedParty` rather than being
|
|
87
|
-
// dropped: `audience: false` alone would accept a token minted for any client of the
|
|
88
|
-
// realm, which is the check being skipped.
|
|
89
|
-
verifier: (clientId) => ({ issuer, audience: false, authorizedParty: clientId }),
|
|
90
|
-
constraints: [
|
|
91
|
-
"Puts no `aud` on an access token until a dedicated audience mapper is configured. Configure one and switch the verifier to `audience: clientId`.",
|
|
92
|
-
"Revokes the whole session when an authorization code is replayed, so a replay test has to run last."
|
|
93
|
-
]
|
|
94
|
-
};
|
|
95
|
-
},
|
|
96
|
-
/**
|
|
97
|
-
* Auth0.
|
|
98
|
-
*
|
|
99
|
-
* `audience` is the field that decides what kind of token comes back. Without it Auth0
|
|
100
|
-
* issues an opaque token for its own userinfo endpoint and the JWT checks have nothing to
|
|
101
|
-
* work with.
|
|
102
|
-
*/
|
|
103
|
-
auth0(options) {
|
|
104
|
-
const issuer = `https://${options.domain.replace(/^https?:\/\//, "").replace(/\/+$/, "")}/`;
|
|
105
|
-
return {
|
|
106
|
-
label: "Auth0",
|
|
107
|
-
client: {
|
|
108
|
-
issuer,
|
|
109
|
-
scopes: options.scopes ?? ["openid", "profile", "email", "offline_access"],
|
|
110
|
-
...options.audience ? { extraParams: { audience: options.audience } } : {}
|
|
111
|
-
},
|
|
112
|
-
roles: options.rolesClaim ? rolesFrom.namespaced(options.rolesClaim) : rolesFrom.scope,
|
|
113
|
-
verifier: (clientId) => ({
|
|
114
|
-
issuer,
|
|
115
|
-
// The API identifier is the audience of an access token. Without one the token is
|
|
116
|
-
// opaque and there is nothing for a verifier to check.
|
|
117
|
-
audience: options.audience ?? false,
|
|
118
|
-
...options.audience ? {} : { authorizedParty: clientId }
|
|
119
|
-
}),
|
|
120
|
-
constraints: [
|
|
121
|
-
"Issues an opaque access token unless `audience` names an API, so token verification needs it.",
|
|
122
|
-
"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.",
|
|
123
|
-
"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.",
|
|
124
|
-
"The issuer carries a trailing slash. A verifier pinned without it rejects every token."
|
|
125
|
-
]
|
|
126
|
-
};
|
|
127
|
-
},
|
|
128
|
-
/**
|
|
129
|
-
* Okta.
|
|
130
|
-
*
|
|
131
|
-
* Two authorization servers that are not interchangeable: a custom one per application,
|
|
132
|
-
* and the org itself for Okta's own management API.
|
|
133
|
-
*/
|
|
134
|
-
okta(options) {
|
|
135
|
-
const host = `https://${options.domain.replace(/^https?:\/\//, "").replace(/\/+$/, "")}`;
|
|
136
|
-
const server = options.authorizationServer === void 0 ? "default" : options.authorizationServer;
|
|
137
|
-
const issuer = server === null ? host : `${host}/oauth2/${server}`;
|
|
138
|
-
return {
|
|
139
|
-
label: "Okta",
|
|
140
|
-
client: {
|
|
141
|
-
issuer,
|
|
142
|
-
scopes: options.scopes ?? ["openid", "profile", "email", "offline_access"],
|
|
143
|
-
dpop: options.dpop ?? true
|
|
144
|
-
},
|
|
145
|
-
roles: rolesFrom.groups,
|
|
146
|
-
verifier: (clientId) => ({
|
|
147
|
-
issuer,
|
|
148
|
-
// An access token from a custom server is minted for that server's audience, named in
|
|
149
|
-
// the console and `api://default` by default. It does not carry the client id.
|
|
150
|
-
audience: false,
|
|
151
|
-
authorizedParty: clientId
|
|
152
|
-
}),
|
|
153
|
-
constraints: [
|
|
154
|
-
"Two authorization servers: /oauth2/<name> for an application, the bare domain for the management API. They are not interchangeable.",
|
|
155
|
-
"Turns DPoP on by default for a new application, and an integrator org will not let you untick it.",
|
|
156
|
-
"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.",
|
|
157
|
-
"A public SPA cannot introspect at all. An API Services client with `private_key_jwt` has to ask instead.",
|
|
158
|
-
"Client credentials against the org server must use `private_key_jwt`, whatever the application is set to."
|
|
159
|
-
]
|
|
160
|
-
};
|
|
161
|
-
},
|
|
162
|
-
/**
|
|
163
|
-
* Google.
|
|
164
|
-
*
|
|
165
|
-
* Refuses a public client outright, so server mode is the only option: the token endpoint
|
|
166
|
-
* checks for a client secret before it looks at PKCE.
|
|
167
|
-
*/
|
|
168
|
-
google(options = {}) {
|
|
169
|
-
return {
|
|
170
|
-
label: "Google",
|
|
171
|
-
client: {
|
|
172
|
-
issuer: "https://accounts.google.com",
|
|
173
|
-
scopes: options.scopes ?? ["openid", "profile", "email"],
|
|
174
|
-
...options.hostedDomain ? { extraParams: { hd: options.hostedDomain } } : {}
|
|
175
|
-
},
|
|
176
|
-
roles: rolesFrom.scope,
|
|
177
|
-
verifier: (clientId) => ({
|
|
178
|
-
// Names itself two ways, and a service pinned to one rejects tokens carrying the
|
|
179
|
-
// other. Discovery uses the first.
|
|
180
|
-
issuer: ["https://accounts.google.com", "accounts.google.com"],
|
|
181
|
-
audience: clientId
|
|
182
|
-
}),
|
|
183
|
-
constraints: [
|
|
184
|
-
"Refuses a public client: the token endpoint reports `client_secret is missing` before checking PKCE, so browser mode cannot work.",
|
|
185
|
-
"Publishes its issuer both with and without a scheme, so a verifier needs both spellings.",
|
|
186
|
-
"Issues a refresh token only on the first consent unless `prompt=consent` and `access_type=offline` are sent."
|
|
187
|
-
]
|
|
188
|
-
};
|
|
189
|
-
},
|
|
190
|
-
/**
|
|
191
|
-
* Dex.
|
|
192
|
-
*
|
|
193
|
-
* Sends no CORS headers, on discovery or the token endpoint, so a browser cannot talk to it
|
|
194
|
-
* at all. Server mode only, and giving endpoints explicitly does not help: the token request
|
|
195
|
-
* is blocked too.
|
|
196
|
-
*/
|
|
197
|
-
dex(options) {
|
|
198
|
-
const issuer = trim(options.baseUrl);
|
|
199
|
-
return {
|
|
200
|
-
label: "Dex",
|
|
201
|
-
client: {
|
|
202
|
-
issuer,
|
|
203
|
-
scopes: options.scopes ?? ["openid", "profile", "email", "offline_access"]
|
|
204
|
-
},
|
|
205
|
-
roles: rolesFrom.groups,
|
|
206
|
-
verifier: (clientId) => ({ issuer, audience: clientId }),
|
|
207
|
-
constraints: [
|
|
208
|
-
"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.",
|
|
209
|
-
"Has no admin interface. Users are static entries in its config file, and a password is a bcrypt hash.",
|
|
210
|
-
"Publishes no revocation endpoint."
|
|
211
|
-
]
|
|
212
|
-
};
|
|
213
|
-
}
|
|
214
|
-
};
|
|
1
|
+
import {
|
|
2
|
+
presets
|
|
3
|
+
} from "./chunk-GN5SERAQ.mjs";
|
|
4
|
+
import "./chunk-KVW2S6VY.mjs";
|
|
5
|
+
import "./chunk-7P6ASYW6.mjs";
|
|
215
6
|
|
|
216
7
|
// src/env-config.ts
|
|
217
8
|
var SECRETISH = /(SECRET|PASSWORD|PRIVATE_KEY|CREDENTIALS?)($|_)/;
|
package/dist/env-credentials.mjs
CHANGED
|
@@ -1,50 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
]);
|
|
7
|
-
const file = path.replace(/^~(?=$|\/)/, homedir());
|
|
8
|
-
let text;
|
|
9
|
-
try {
|
|
10
|
-
text = readFileSync(file, "utf8");
|
|
11
|
-
} catch {
|
|
12
|
-
throw new Error(
|
|
13
|
-
`[fluixi/oauth2] the key file ${file} cannot be read. A signing key is named by path so it can stay outside the repository.`
|
|
14
|
-
);
|
|
15
|
-
}
|
|
16
|
-
try {
|
|
17
|
-
return JSON.parse(text);
|
|
18
|
-
} catch {
|
|
19
|
-
throw new Error(`[fluixi/oauth2] ${file} is not JSON, and a private key is expected as a JWK.`);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
async function credentialsFromEnv(options) {
|
|
23
|
-
const source = options.env ?? (typeof process === "undefined" ? {} : process.env);
|
|
24
|
-
const upper = options.prefix.toUpperCase();
|
|
25
|
-
const prefer = (options.preferPrefix ?? `${upper}_INTROSPECT`).toUpperCase();
|
|
26
|
-
const read = (suffix) => source[`${prefer}_${suffix}`] ?? source[`${upper}_${suffix}`];
|
|
27
|
-
const clientId = read("CLIENT_ID") ?? options.clientId;
|
|
28
|
-
if (!clientId) return null;
|
|
29
|
-
const keyPath = read("PRIVATE_KEY");
|
|
30
|
-
if (keyPath) {
|
|
31
|
-
return {
|
|
32
|
-
clientId,
|
|
33
|
-
privateKey: await jwkAt(keyPath),
|
|
34
|
-
keyId: read("KEY_ID"),
|
|
35
|
-
clientAuthMethod: "private_key_jwt",
|
|
36
|
-
assertionAlgorithm: read("ASSERTION_ALG")
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
const clientSecret = read("SECRET") ?? read("CLIENT_SECRET");
|
|
40
|
-
if (clientSecret) return { clientId, clientSecret };
|
|
41
|
-
return null;
|
|
42
|
-
}
|
|
43
|
-
function explainCredentials(options) {
|
|
44
|
-
const upper = options.prefix.toUpperCase();
|
|
45
|
-
const who = options.label ?? options.prefix;
|
|
46
|
-
return `This call authenticates its caller, so ${who} needs credentials. For a client with a shared secret, set ${upper}_CLIENT_SECRET. For one registered with a public key (Okta calls this "Public key / Private key"), set ${upper}_PRIVATE_KEY to the path of its private JWK and ${upper}_KEY_ID to the kid the provider lists. Prefix either with ${upper}_INTROSPECT_ to have a different client ask, which Okta requires: a public SPA cannot introspect at all.`;
|
|
47
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
credentialsFromEnv,
|
|
3
|
+
explainCredentials
|
|
4
|
+
} from "./chunk-7AMC336S.mjs";
|
|
5
|
+
import "./chunk-7P6ASYW6.mjs";
|
|
48
6
|
export {
|
|
49
7
|
credentialsFromEnv,
|
|
50
8
|
explainCredentials
|
package/dist/index.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*! @fluixi/oauth2 v1.0.0-alpha.
|
|
1
|
+
/*! @fluixi/oauth2 v1.0.0-alpha.110 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
|
|
2
2
|
"use strict";
|
|
3
3
|
var __create = Object.create;
|
|
4
4
|
var __defProp = Object.defineProperty;
|
|
@@ -51,7 +51,8 @@ __export(index_exports, {
|
|
|
51
51
|
paramsFromUrl: () => paramsFromUrl,
|
|
52
52
|
proofFor: () => proofFor,
|
|
53
53
|
safeReturnTo: () => safeReturnTo,
|
|
54
|
-
thumbprintOf: () => thumbprintOf
|
|
54
|
+
thumbprintOf: () => thumbprintOf,
|
|
55
|
+
watchProviderSession: () => watchProviderSession
|
|
55
56
|
});
|
|
56
57
|
module.exports = __toCommonJS(index_exports);
|
|
57
58
|
|
|
@@ -364,8 +365,12 @@ async function discover(issuer, fetcher = fetch) {
|
|
|
364
365
|
try {
|
|
365
366
|
response = await fetcher(documentUrl(issuer));
|
|
366
367
|
} catch (cause) {
|
|
368
|
+
const why = cause instanceof Error ? cause.message : String(cause);
|
|
367
369
|
throw new OAuthError(
|
|
368
|
-
|
|
370
|
+
typeof window === "undefined" ? (
|
|
371
|
+
// On a server nothing blocks the read: the provider is down, or the address is wrong.
|
|
372
|
+
`Could not reach the provider at ${documentUrl(issuer)}: is it running, and is the issuer right? (${why})`
|
|
373
|
+
) : `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})`,
|
|
369
374
|
"discovery_unreachable"
|
|
370
375
|
);
|
|
371
376
|
}
|
|
@@ -390,6 +395,7 @@ async function discover(issuer, fetcher = fetch) {
|
|
|
390
395
|
jwks: doc.jwks_uri,
|
|
391
396
|
introspection: doc.introspection_endpoint,
|
|
392
397
|
revocation: doc.revocation_endpoint,
|
|
398
|
+
checkSession: doc.check_session_iframe,
|
|
393
399
|
issuer: doc.issuer ?? issuer
|
|
394
400
|
};
|
|
395
401
|
})();
|
|
@@ -848,6 +854,81 @@ function createServerClient(config, fetcher = fetch) {
|
|
|
848
854
|
};
|
|
849
855
|
}
|
|
850
856
|
|
|
857
|
+
// src/session-monitor.ts
|
|
858
|
+
var SETTLE_MS = 15e3;
|
|
859
|
+
var GUARD_KEY = "fx-session-check";
|
|
860
|
+
function recentlyActed() {
|
|
861
|
+
try {
|
|
862
|
+
return Date.now() - Number(sessionStorage.getItem(GUARD_KEY) ?? 0) < SETTLE_MS;
|
|
863
|
+
} catch {
|
|
864
|
+
return false;
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
function markActed() {
|
|
868
|
+
try {
|
|
869
|
+
sessionStorage.setItem(GUARD_KEY, String(Date.now()));
|
|
870
|
+
} catch {
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
function watchProviderSession(options = {}) {
|
|
874
|
+
if (typeof window === "undefined" || typeof document === "undefined") return () => {
|
|
875
|
+
};
|
|
876
|
+
const base = (options.basePath ?? "/auth").replace(/\/$/, "");
|
|
877
|
+
const fetcher = options.fetcher ?? fetch;
|
|
878
|
+
const onChange = options.onChange ?? (() => {
|
|
879
|
+
const login = new URLSearchParams({ prompt: "none", returnTo: location.pathname + location.search });
|
|
880
|
+
location.assign(`${base}/login?${login}`);
|
|
881
|
+
});
|
|
882
|
+
let stopped = false;
|
|
883
|
+
const cleanups = [];
|
|
884
|
+
const stop = () => {
|
|
885
|
+
stopped = true;
|
|
886
|
+
for (const cleanup of cleanups.splice(0)) cleanup();
|
|
887
|
+
};
|
|
888
|
+
void (async () => {
|
|
889
|
+
const check = await fetcher(`${base}/session-check`, { credentials: "same-origin" }).then((response) => response.ok ? response.json() : null).catch(() => null);
|
|
890
|
+
if (stopped || !check?.frame || !check.state) return;
|
|
891
|
+
const origin = new URL(check.frame).origin;
|
|
892
|
+
const frame = document.createElement("iframe");
|
|
893
|
+
frame.src = check.frame;
|
|
894
|
+
frame.title = "session check";
|
|
895
|
+
frame.setAttribute("aria-hidden", "true");
|
|
896
|
+
frame.tabIndex = -1;
|
|
897
|
+
frame.style.cssText = "position:absolute;width:0;height:0;border:0;visibility:hidden";
|
|
898
|
+
const message = `${check.clientId} ${check.state}`;
|
|
899
|
+
const ask = () => {
|
|
900
|
+
if (!stopped && document.visibilityState === "visible") frame.contentWindow?.postMessage(message, origin);
|
|
901
|
+
};
|
|
902
|
+
let confirmed = false;
|
|
903
|
+
const answer = (event) => {
|
|
904
|
+
if (event.origin !== origin || event.source !== frame.contentWindow) return;
|
|
905
|
+
if (event.data === "unchanged") confirmed = true;
|
|
906
|
+
else if (event.data === "changed") {
|
|
907
|
+
stop();
|
|
908
|
+
if (!confirmed && recentlyActed()) return;
|
|
909
|
+
markActed();
|
|
910
|
+
onChange();
|
|
911
|
+
} else if (event.data === "error") {
|
|
912
|
+
stop();
|
|
913
|
+
}
|
|
914
|
+
};
|
|
915
|
+
window.addEventListener("message", answer);
|
|
916
|
+
frame.addEventListener("load", ask);
|
|
917
|
+
document.addEventListener("visibilitychange", ask);
|
|
918
|
+
window.addEventListener("focus", ask);
|
|
919
|
+
const timer = setInterval(ask, options.intervalMs ?? 3e3);
|
|
920
|
+
document.body.appendChild(frame);
|
|
921
|
+
cleanups.push(() => {
|
|
922
|
+
window.removeEventListener("message", answer);
|
|
923
|
+
document.removeEventListener("visibilitychange", ask);
|
|
924
|
+
window.removeEventListener("focus", ask);
|
|
925
|
+
clearInterval(timer);
|
|
926
|
+
frame.remove();
|
|
927
|
+
});
|
|
928
|
+
})();
|
|
929
|
+
return stop;
|
|
930
|
+
}
|
|
931
|
+
|
|
851
932
|
// src/service.ts
|
|
852
933
|
function paramsFromUrl(url) {
|
|
853
934
|
const href = url ?? (typeof location === "undefined" ? null : location.href);
|
|
@@ -874,16 +955,19 @@ function createAuthService(options) {
|
|
|
874
955
|
function buildAuthService(options) {
|
|
875
956
|
const client = clientFor(options);
|
|
876
957
|
let latest = null;
|
|
958
|
+
const fetchUserFor = async (token) => {
|
|
959
|
+
if (options.fetchUser) return options.fetchUser(token);
|
|
960
|
+
return client.fetchUser(token === COOKIE_SESSION ? void 0 : token);
|
|
961
|
+
};
|
|
962
|
+
const resumesFromCookie = options.config.tokens === "server" && typeof window !== "undefined";
|
|
963
|
+
const [ready, setReady] = (0, import_signal.createSignal)(!resumesFromCookie);
|
|
877
964
|
const session = (0, import_session.createSession)({
|
|
878
965
|
/** The credential is the callback, so this is the code exchange. */
|
|
879
966
|
async login(params) {
|
|
880
967
|
latest = await client.handleCallback(params);
|
|
881
968
|
return { token: latest.accessToken, expiresAt: latest.expiresAt };
|
|
882
969
|
},
|
|
883
|
-
|
|
884
|
-
if (options.fetchUser) return options.fetchUser(token);
|
|
885
|
-
return client.fetchUser(token === COOKIE_SESSION ? void 0 : token);
|
|
886
|
-
},
|
|
970
|
+
fetchUser: fetchUserFor,
|
|
887
971
|
async refresh() {
|
|
888
972
|
latest = await client.refresh();
|
|
889
973
|
return { token: latest.accessToken, expiresAt: latest.expiresAt };
|
|
@@ -906,11 +990,27 @@ function buildAuthService(options) {
|
|
|
906
990
|
});
|
|
907
991
|
const hooks = (0, import_client.createAuthHooks)({
|
|
908
992
|
session,
|
|
993
|
+
ready,
|
|
909
994
|
navigate: options.navigate,
|
|
910
995
|
loginPath: options.loginPath ?? "/login",
|
|
911
996
|
homePath: options.homePath ?? "/",
|
|
912
997
|
forbiddenPath: options.forbiddenPath ?? "/403"
|
|
913
998
|
});
|
|
999
|
+
if (resumesFromCookie && options.config.tokens === "server" && options.config.watchSession !== false) {
|
|
1000
|
+
watchProviderSession({ basePath: options.config.basePath });
|
|
1001
|
+
}
|
|
1002
|
+
const renderedSignedOut = hooks.store.seeded && hooks.store.value().user == null;
|
|
1003
|
+
if (resumesFromCookie && renderedSignedOut) setReady(true);
|
|
1004
|
+
else if (resumesFromCookie) {
|
|
1005
|
+
void (async () => {
|
|
1006
|
+
try {
|
|
1007
|
+
await session.adopt(COOKIE_SESSION, await fetchUserFor(COOKIE_SESSION));
|
|
1008
|
+
} catch {
|
|
1009
|
+
} finally {
|
|
1010
|
+
setReady(true);
|
|
1011
|
+
}
|
|
1012
|
+
})();
|
|
1013
|
+
}
|
|
914
1014
|
return Object.assign(session, {
|
|
915
1015
|
client,
|
|
916
1016
|
protectedRoute: hooks.protectedRoute,
|
|
@@ -946,7 +1046,7 @@ function buildAuthService(options) {
|
|
|
946
1046
|
}
|
|
947
1047
|
|
|
948
1048
|
// src/tokens.ts
|
|
949
|
-
var import_di = require("@fluixi/
|
|
1049
|
+
var import_di = require("@fluixi/di");
|
|
950
1050
|
var AuthServiceToken = (0, import_di.injectionToken)("AuthService");
|
|
951
1051
|
var AuthClientToken = (0, import_di.injectionToken)("AuthClient");
|
|
952
1052
|
|
package/dist/index.d.ts
CHANGED
|
@@ -93,6 +93,8 @@ export type { AuthService, AuthServiceOptions, AuthCredentials } from './service
|
|
|
93
93
|
export { createBrowserClient } from './browser.js';
|
|
94
94
|
export { createServerClient, COOKIE_SESSION } from './server-client.js';
|
|
95
95
|
export { discover, clearDiscoveryCache } from './discovery.js';
|
|
96
|
+
export { watchProviderSession } from './session-monitor.js';
|
|
97
|
+
export type { SessionCheck, WatchOptions } from './session-monitor.js';
|
|
96
98
|
export { createPkcePair, createVerifier, createChallenge, createStateValue, expiryFromJwt, } from './pkce.js';
|
|
97
99
|
export { createDpopKey, proofFor, thumbprintOf, boundKeyOf } from './dpop.js';
|
|
98
100
|
export type { DpopKey, ProofOptions } from './dpop.js';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyFG;AACH,OAAO,EAAE,iBAAiB,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAChE,YAAY,EAAE,WAAW,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAErF,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AACnD,OAAO,EAAE,kBAAkB,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAExE,OAAO,EAAE,QAAQ,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAC/D,OAAO,EACL,cAAc,EACd,cAAc,EACd,eAAe,EACf,gBAAgB,EAChB,aAAa,GACd,MAAM,WAAW,CAAC;AAEnB,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAC9E,YAAY,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAEvD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,YAAY,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAEtD,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACxC,YAAY,EACV,UAAU,EACV,UAAU,EACV,UAAU,EACV,aAAa,EACb,cAAc,EACd,SAAS,EACT,YAAY,EACZ,YAAY,EACZ,MAAM,GACP,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAChE,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyFG;AACH,OAAO,EAAE,iBAAiB,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAChE,YAAY,EAAE,WAAW,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAErF,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AACnD,OAAO,EAAE,kBAAkB,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAExE,OAAO,EAAE,QAAQ,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAC/D,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAC5D,YAAY,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACvE,OAAO,EACL,cAAc,EACd,cAAc,EACd,eAAe,EACf,gBAAgB,EAChB,aAAa,GACd,MAAM,WAAW,CAAC;AAEnB,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAC9E,YAAY,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAEvD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,YAAY,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAEtD,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACxC,YAAY,EACV,UAAU,EACV,UAAU,EACV,UAAU,EACV,aAAa,EACb,cAAc,EACd,SAAS,EACT,YAAY,EACZ,YAAY,EACZ,MAAM,GACP,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAChE,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -92,6 +92,7 @@ export { createAuthService, paramsFromUrl } from './service.js';
|
|
|
92
92
|
export { createBrowserClient } from './browser.js';
|
|
93
93
|
export { createServerClient, COOKIE_SESSION } from './server-client.js';
|
|
94
94
|
export { discover, clearDiscoveryCache } from './discovery.js';
|
|
95
|
+
export { watchProviderSession } from './session-monitor.js';
|
|
95
96
|
export { createPkcePair, createVerifier, createChallenge, createStateValue, expiryFromJwt, } from './pkce.js';
|
|
96
97
|
export { createDpopKey, proofFor, thumbprintOf, boundKeyOf } from './dpop.js';
|
|
97
98
|
export { safeReturnTo } from './return-to.js';
|