@fluixi/oauth2 1.0.0-alpha.107 → 1.0.0-alpha.109
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 +7 -2
- package/dist/api-guard.mjs +9 -135
- package/dist/backchannel.cjs +7 -2
- package/dist/backchannel.mjs +11 -299
- package/dist/browser.mjs +10 -707
- package/dist/callback.mjs +16 -23
- 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.mjs +6 -64
- package/dist/dpop-store.mjs +6 -118
- package/dist/dpop.mjs +11 -95
- package/dist/env-config.mjs +5 -214
- package/dist/env-credentials.mjs +5 -47
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +48 -1010
- package/dist/module.mjs +5 -20
- package/dist/pkce.mjs +11 -56
- package/dist/presets.mjs +5 -214
- package/dist/return-to.mjs +4 -22
- package/dist/roles.mjs +4 -32
- package/dist/server-client.mjs +7 -107
- package/dist/server.cjs +61 -9
- package/dist/server.d.ts +8 -0
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +80 -5
- package/dist/server.mjs +94 -433
- package/dist/service-client.cjs +1 -1
- package/dist/service-client.mjs +16 -138
- package/dist/service.mjs +14 -978
- package/dist/session-monitor.mjs +4 -74
- package/dist/tokens.mjs +5 -4
- package/dist/tsconfig.lib.tsbuildinfo +1 -1
- package/dist/types.mjs +4 -9
- package/dist/verify.cjs +8 -3
- package/dist/verify.mjs +17 -585
- package/package.json +21 -21
- package/dist/browser-RHLXMWQ2.mjs +0 -3545
- package/dist/chunk-NRB7JOZV.mjs +0 -15
- package/dist/chunk-WE2RTKJY.mjs +0 -10
- package/dist/chunk-ZIX4ITGP.mjs +0 -108
- package/dist/dpop-UCBLIUQC.mjs +0 -100
- package/dist/dpop-W2ACBDEM.mjs +0 -22
- /package/dist/{chunk-OGSFNTKA.mjs → chunk-AXPSPTNP.mjs} +0 -0
package/dist/server.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAgCA,OAAO,EAGL,KAAK,qBAAqB,EAC3B,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAqD,KAAK,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAEvG,OAAO,EAAc,KAAK,SAAS,EAAE,MAAM,YAAY,CAAC;AAExD,OAAO,EAAgB,KAAK,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAEjE,MAAM,WAAW,iBAAiB;IAChC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,SAAS,CAAC;IACtB,QAAQ,EAAE,MAAM,CAAC;IACjB,8EAA8E;IAC9E,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,WAAW,EAAE,MAAM,CAAC;IACpB,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;IAClB,yFAAyF;IACzF,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,6EAA6E;IAC7E,YAAY,EAAE,MAAM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;IACrC,IAAI,EAAE,CAAC,GAAG,IAAI,CAAC;IACf,OAAO,EAAE;QAAE,SAAS,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI,CAAC;IACvC,uCAAuC;IACvC,KAAK,CAAC,EAAE,eAAe,CAAC;IACxB;;;OAGG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB;AAED;;;;;;;;GAQG;AACH,wBAAsB,WAAW,CAAC,CAAC,GAAG,OAAO,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAIxF;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,iBAAiB,GAAG,SAAS,CAErE;AAED,OAAO,EAAE,aAAa,EAAE,KAAK,WAAW,EAAE,KAAK,WAAW,EAAE,MAAM,kBAAkB,CAAC"}
|
package/dist/server.js
CHANGED
|
@@ -41,6 +41,7 @@ import { defineModule } from '@fluixi/start/app';
|
|
|
41
41
|
const RETRY_MARK = '.retry';
|
|
42
42
|
// OpenID Connect Core's prompts, and `create` from its user registration specification.
|
|
43
43
|
const PROMPTS = new Set(['none', 'login', 'consent', 'select_account', 'create']);
|
|
44
|
+
const DEFAULT_SCOPES = ['openid', 'profile', 'email'];
|
|
44
45
|
/** Set by the login route itself: forwarding one from the query string would let a link change it. */
|
|
45
46
|
const CLIENT_PARAMS = new Set(['response_type', 'client_id', 'redirect_uri', 'scope', 'state', 'nonce', 'code_challenge', 'code_challenge_method', 'prompt', 'request', 'request_uri']);
|
|
46
47
|
/** What a `prompt=none` request ends with when the provider would have to ask the visitor. */
|
|
@@ -115,6 +116,66 @@ function signInRedirect(request, config) {
|
|
|
115
116
|
login.set('prompt', 'none');
|
|
116
117
|
return new Response(null, { status: 302, headers: { location: `${base}/login?${login}` } });
|
|
117
118
|
}
|
|
119
|
+
/** A JWT's payload, or nothing: the token is the session's own, so it is read, not verified. */
|
|
120
|
+
function jwtClaims(token) {
|
|
121
|
+
const part = token.split('.')[1];
|
|
122
|
+
if (!part)
|
|
123
|
+
return {};
|
|
124
|
+
try {
|
|
125
|
+
const claims = JSON.parse(Buffer.from(part, 'base64url').toString('utf8'));
|
|
126
|
+
return claims && typeof claims === 'object' ? claims : {};
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
return {};
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* The scopes the app asks for that this session was not granted. `offline_access` is left out:
|
|
134
|
+
* a provider grants it to the refresh token, not the access token. A session that recorded no
|
|
135
|
+
* scope is taken to have what was asked (RFC 6749 §5.1: the answer omits it when equal).
|
|
136
|
+
*/
|
|
137
|
+
function missingScopes(config, session) {
|
|
138
|
+
if (session.scope === undefined)
|
|
139
|
+
return [];
|
|
140
|
+
const granted = new Set(session.scope.split(' ').filter(Boolean));
|
|
141
|
+
return (config.scopes ?? DEFAULT_SCOPES).filter((scope) => scope !== 'offline_access' && !granted.has(scope));
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* The cookie saying a silent sign-in was tried for these missing scopes. Its value is them, so
|
|
145
|
+
* a provider that still withholds one is not asked again on every page load, while a scope
|
|
146
|
+
* added later is.
|
|
147
|
+
*/
|
|
148
|
+
function rescopeCookie(config, missing) {
|
|
149
|
+
const name = `${config.cookieName ?? 'fx_session'}_rescope`;
|
|
150
|
+
const secure = config.secureCookie ?? process.env.NODE_ENV === 'production';
|
|
151
|
+
const value = encodeURIComponent((missing ?? []).join(' '));
|
|
152
|
+
return { name, header: `${name}=${value}; Path=/; Max-Age=${60 * 60 * 24}; HttpOnly; SameSite=Lax${secure ? '; Secure' : ''}` };
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Where a signed-in page request goes when its session lacks a scope the app now asks for: a
|
|
156
|
+
* silent sign-in, once per set of missing scopes, which keeps the session if the provider has
|
|
157
|
+
* to ask the person something. Null to render the page as is.
|
|
158
|
+
*/
|
|
159
|
+
function rescopeRedirect(request, config, session) {
|
|
160
|
+
if (!isPageRequest(request))
|
|
161
|
+
return null;
|
|
162
|
+
const missing = missingScopes(config, session);
|
|
163
|
+
if (!missing.length)
|
|
164
|
+
return null;
|
|
165
|
+
const url = new URL(request.url);
|
|
166
|
+
const base = (config.basePath ?? '/auth').replace(/\/$/, '');
|
|
167
|
+
if ([base, ...(config.publicPaths ?? [])].some((path) => under(url.pathname, path)))
|
|
168
|
+
return null;
|
|
169
|
+
const marker = rescopeCookie(config, missing);
|
|
170
|
+
if (cookieValue(request, marker.name) === encodeURIComponent(missing.join(' ')))
|
|
171
|
+
return null;
|
|
172
|
+
const login = new URLSearchParams({ returnTo: url.pathname + url.search, prompt: 'none' });
|
|
173
|
+
for (const [name, value] of Object.entries(config.rescopeParams?.(jwtClaims(session.accessToken)) ?? {})) {
|
|
174
|
+
if (value)
|
|
175
|
+
login.set(name, value);
|
|
176
|
+
}
|
|
177
|
+
return new Response(null, { status: 302, headers: { location: `${base}/login?${login}`, 'set-cookie': marker.header } });
|
|
178
|
+
}
|
|
118
179
|
/**
|
|
119
180
|
* Nonces providers asked for, per origin, shared by the routes and `sessionLocals`. A nonce
|
|
120
181
|
* is the server's to set (RFC 9449 §8, §9), so the latest one it sent is the one to use.
|
|
@@ -299,6 +360,7 @@ function renew(current, tokenRequest) {
|
|
|
299
360
|
tokenType: typeof payload.token_type === 'string' ? payload.token_type : current.tokenType,
|
|
300
361
|
dpop: current.dpop,
|
|
301
362
|
sessionState: current.sessionState,
|
|
363
|
+
scope: typeof payload.scope === 'string' ? payload.scope : current.scope,
|
|
302
364
|
};
|
|
303
365
|
})().catch((error) => {
|
|
304
366
|
// Not processed (the provider down or busy): nothing was spent. Ask again after the pause.
|
|
@@ -356,7 +418,7 @@ export function oauthRoutes(config) {
|
|
|
356
418
|
headers: { 'content-type': 'application/json', ...headers },
|
|
357
419
|
});
|
|
358
420
|
const base = (config.basePath ?? '/auth').replace(/\/$/, '');
|
|
359
|
-
const scopes = config.scopes ??
|
|
421
|
+
const scopes = config.scopes ?? DEFAULT_SCOPES;
|
|
360
422
|
const cookie = createSessionCookie({
|
|
361
423
|
secret: config.cookieSecret,
|
|
362
424
|
name: config.cookieName,
|
|
@@ -439,9 +501,11 @@ export function oauthRoutes(config) {
|
|
|
439
501
|
target.searchParams.set(name, value);
|
|
440
502
|
}
|
|
441
503
|
// The verifier goes in the cookie, which is the only thing that survives the trip
|
|
442
|
-
// and is not readable by the page.
|
|
504
|
+
// and is not readable by the page. A silent sign-in from a signed-in page (a scope the
|
|
505
|
+
// session lacks) keeps the session beside it, so a provider that has to ask leaves it.
|
|
506
|
+
const kept = prompt === 'none' ? await session() : null;
|
|
443
507
|
const pendingCookie = await cookie.write({
|
|
444
|
-
accessToken: '',
|
|
508
|
+
...(kept?.accessToken ? { ...kept, pending: undefined } : { accessToken: '' }),
|
|
445
509
|
pending: {
|
|
446
510
|
verifier,
|
|
447
511
|
state,
|
|
@@ -456,8 +520,8 @@ export function oauthRoutes(config) {
|
|
|
456
520
|
},
|
|
457
521
|
},
|
|
458
522
|
// Ten minutes: long enough to sign in, short enough that an abandoned attempt
|
|
459
|
-
// does not sit in the browser.
|
|
460
|
-
{ maxAge: 600 });
|
|
523
|
+
// does not sit in the browser. A kept session keeps its own lifetime.
|
|
524
|
+
kept?.accessToken ? undefined : { maxAge: 600 });
|
|
461
525
|
return new Response(null, {
|
|
462
526
|
status: 302,
|
|
463
527
|
headers: {
|
|
@@ -479,6 +543,12 @@ export function oauthRoutes(config) {
|
|
|
479
543
|
allowedOrigins: config.allowedReturnOrigins,
|
|
480
544
|
});
|
|
481
545
|
const headers = new Headers();
|
|
546
|
+
if (ours && pending.silent && SILENT_REFUSALS.has(error) && current?.accessToken) {
|
|
547
|
+
// Signed in already and only after a scope: the session stays as it was.
|
|
548
|
+
headers.set('set-cookie', await cookie.write({ ...current, pending: undefined }));
|
|
549
|
+
headers.set('location', location);
|
|
550
|
+
return new Response(null, { status: 302, headers });
|
|
551
|
+
}
|
|
482
552
|
headers.append('set-cookie', cookie.clear());
|
|
483
553
|
if (ours && pending.silent && SILENT_REFUSALS.has(error)) {
|
|
484
554
|
// Nobody signed in at the provider: remember it was asked, and show the page as is.
|
|
@@ -559,6 +629,7 @@ export function oauthRoutes(config) {
|
|
|
559
629
|
tokenType: typeof payload.token_type === 'string' ? payload.token_type : undefined,
|
|
560
630
|
dpop: key ? await exportDpopKey(key) : undefined,
|
|
561
631
|
sessionState: sessionStateFrom(url),
|
|
632
|
+
scope: typeof payload.scope === 'string' ? payload.scope : undefined,
|
|
562
633
|
});
|
|
563
634
|
return new Response(null, {
|
|
564
635
|
status: 302,
|
|
@@ -769,6 +840,10 @@ export function sessionLocals(config) {
|
|
|
769
840
|
session = null;
|
|
770
841
|
return signedOut();
|
|
771
842
|
}
|
|
843
|
+
// Granted fewer scopes than the app asks for now: a silent sign-in brings them, once.
|
|
844
|
+
const rescope = rescopeRedirect(request, config, session);
|
|
845
|
+
if (rescope)
|
|
846
|
+
return respond(rescope);
|
|
772
847
|
// Server code calls the app's APIs as this user with it; the token itself stays here.
|
|
773
848
|
const live = session;
|
|
774
849
|
const fetchAs = (input, init) => sendAs(live, input, init);
|