@molecule/api-resource-user 1.5.1 → 1.5.2
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/README.md +77 -11
- package/dist/authorization.d.ts +48 -5
- package/dist/authorization.d.ts.map +1 -1
- package/dist/authorization.js +87 -4
- package/dist/authorization.js.map +1 -1
- package/dist/index.d.ts +14 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -5
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@ AUTO-GENERATED — DO NOT EDIT THIS FILE.
|
|
|
3
3
|
Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
|
|
4
4
|
Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
|
|
5
5
|
To change this document, edit the module-level JSDoc in src/index.ts.
|
|
6
|
-
Generated: 2026-10-
|
|
6
|
+
Generated: 2026-10-16T09:34:14.675Z
|
|
7
7
|
-->
|
|
8
8
|
|
|
9
9
|
# @molecule/api-resource-user
|
|
@@ -604,6 +604,47 @@ subsequent requests; only these rare revocation events pay a brief re-check.
|
|
|
604
604
|
function invalidateAllDeviceExistsCache(): void
|
|
605
605
|
```
|
|
606
606
|
|
|
607
|
+
##### `authorization.ALLOW_COOKIE_SESSION_LOCALS_KEY`
|
|
608
|
+
|
|
609
|
+
`res.locals` key carrying THIS request's cookie-session policy. {@link verifyMiddleware}
|
|
610
|
+
writes it from its `allowCookieSession` option; {@link set} reads it. When the value is
|
|
611
|
+
`false` the request arrived through a mount that authenticates by `Authorization` header
|
|
612
|
+
only, so `set()` must not hand the client the `token` / `sessionId` / `mol_auth` cookies
|
|
613
|
+
either: a cookie written here authenticates that same client on its NEXT request (an HTTP
|
|
614
|
+
test runner's cookie jar, `curl -b`, a browser) through any mount that does accept one,
|
|
615
|
+
which is how a suite step called "no auth" comes back 200.
|
|
616
|
+
|
|
617
|
+
Exported so a custom auth middleware can declare the same policy for the requests it
|
|
618
|
+
handles, and so tests can assert the key rather than a string literal.
|
|
619
|
+
|
|
620
|
+
```typescript
|
|
621
|
+
const ALLOW_COOKIE_SESSION_LOCALS_KEY: 'allowCookieSession'
|
|
622
|
+
```
|
|
623
|
+
|
|
624
|
+
##### `authorization.AUTH_SESSION_SOURCE_LOCALS_KEY`
|
|
625
|
+
|
|
626
|
+
`res.locals` key carrying HOW this request's session was resolved — `'header'` when
|
|
627
|
+
the `Authorization` header named the token, `'cookie'` when only the `token` cookie
|
|
628
|
+
did. {@link verifyMiddleware} writes it alongside `res.locals.session`.
|
|
629
|
+
|
|
630
|
+
A mount with `allowCookieSession: false` reads it to drop a session an EARLIER, laxer
|
|
631
|
+
mount already resolved out of the cookie (the scaffold mounts `verifyMiddleware` twice,
|
|
632
|
+
and the first one runs for every `/api` request). A session resolved from the header is
|
|
633
|
+
never dropped: that client sent its credentials, which is exactly what a token-only API
|
|
634
|
+
accepts.
|
|
635
|
+
|
|
636
|
+
```typescript
|
|
637
|
+
const AUTH_SESSION_SOURCE_LOCALS_KEY: 'authSessionSource'
|
|
638
|
+
```
|
|
639
|
+
|
|
640
|
+
##### `authorization.AuthSessionSource`
|
|
641
|
+
|
|
642
|
+
How {@link verifyMiddleware} resolved a request's session. See {@link AUTH_SESSION_SOURCE_LOCALS_KEY}.
|
|
643
|
+
|
|
644
|
+
```typescript
|
|
645
|
+
type AuthSessionSource = 'header' | 'cookie'
|
|
646
|
+
```
|
|
647
|
+
|
|
607
648
|
##### `authorization.set(_req, res, session)`
|
|
608
649
|
|
|
609
650
|
Set authorization headers and cookie for a session.
|
|
@@ -614,6 +655,17 @@ device row does not exist (that is how signing out revokes a session). Mint the
|
|
|
614
655
|
(register, login, OAuth, password reset), as `handlers/logIn.ts` does: a placeholder
|
|
615
656
|
(`'unknown'`, `''`, the user's id) yields a token that every later request answers 401.
|
|
616
657
|
|
|
658
|
+
Always sets the `Authorization` response header. Writes the session cookies (the httpOnly
|
|
659
|
+
`token` and `sessionId`, plus the readable `mol_auth` presence hint) UNLESS the request's
|
|
660
|
+
cookie-session policy is off — see {@link ALLOW_COOKIE_SESSION_LOCALS_KEY}, written by
|
|
661
|
+
{@link verifyMiddleware}. A token-only API (`allowCookieSession: false`) gets the header
|
|
662
|
+
and no cookies, so its clients cannot drift into a cookie session.
|
|
663
|
+
|
|
664
|
+
The policy is the MOUNT's, and the mount records it on every request it passes — including
|
|
665
|
+
the sign-in request itself, which carries no token and therefore resolves no session. So a
|
|
666
|
+
token-only API writes no cookie from the very first response: mount
|
|
667
|
+
`verifyMiddleware({ allowCookieSession: false })` in front of the routes that sign users in.
|
|
668
|
+
|
|
617
669
|
```typescript
|
|
618
670
|
function set(
|
|
619
671
|
_req: MoleculeRequest,
|
|
@@ -656,11 +708,16 @@ interface VerifyMiddlewareOptions {
|
|
|
656
708
|
*/
|
|
657
709
|
shouldRefreshSession?: (session: Session, req: MoleculeRequest) => boolean | Promise<boolean>
|
|
658
710
|
/**
|
|
659
|
-
*
|
|
711
|
+
* Whether the `token` cookie is a session on this mount, in BOTH directions.
|
|
660
712
|
* Defaults to `true` (browser clients sign in once and every page request carries
|
|
661
|
-
* the cookie).
|
|
662
|
-
*
|
|
663
|
-
*
|
|
713
|
+
* the cookie).
|
|
714
|
+
*
|
|
715
|
+
* Pass `false` for a token-only JSON API. Then the cookie is not a credential here:
|
|
716
|
+
* the middleware does not resolve a session from it (and drops one an earlier, laxer
|
|
717
|
+
* mount already resolved from it), and {@link set} does not write it either — so a
|
|
718
|
+
* client that keeps a cookie jar (an HTTP test runner, `curl -b`) never picks up a
|
|
719
|
+
* session it should not have, and stops answering 200 on the step its spec calls
|
|
720
|
+
* "no auth".
|
|
664
721
|
*/
|
|
665
722
|
allowCookieSession?: boolean
|
|
666
723
|
}
|
|
@@ -669,7 +726,7 @@ interface VerifyMiddlewareOptions {
|
|
|
669
726
|
##### `authorization.verifyMiddleware(options)`
|
|
670
727
|
|
|
671
728
|
Middleware that verifies the JWT token from the `Authorization` header and sets `res.locals.session`.
|
|
672
|
-
Falls back to cookie-based session for browser clients unless `allowCookieSession` is `false
|
|
729
|
+
Falls back to cookie-based session for browser clients unless `allowCookieSession` is `false` — which also stops `set()` writing the session cookie for this request. Auto-refreshes tokens older than `JWT_REFRESH_TIME`.
|
|
673
730
|
|
|
674
731
|
```typescript
|
|
675
732
|
function verifyMiddleware(options?: VerifyMiddlewareOptions): MoleculeRequestHandler
|
|
@@ -1927,11 +1984,20 @@ before calling a handler, or write the route yourself. A username keeps exactly
|
|
|
1927
1984
|
sent (letters, digits, `.`, `-`, `_`); anything else is answered 400
|
|
1928
1985
|
`user.error.usernameInvalid`, never rewritten.
|
|
1929
1986
|
|
|
1930
|
-
**A token-only JSON API:**
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
whose clients send the
|
|
1987
|
+
**A token-only JSON API:** by default the `token` cookie that login sets IS a session,
|
|
1988
|
+
and `authorization.set()` writes it on every sign-in — so a client that keeps a cookie
|
|
1989
|
+
jar (an HTTP test runner, `curl -b`) stays signed in on a request that sends no
|
|
1990
|
+
`Authorization` header, and gets 200 on the step the suite calls "no auth". Mount
|
|
1991
|
+
`verifyMiddleware({ allowCookieSession: false })` for an API whose clients send the
|
|
1992
|
+
header: that ONE option turns the cookie off in BOTH directions — the middleware no
|
|
1993
|
+
longer resolves a session from it (nor keeps one an earlier, laxer mount resolved), and
|
|
1994
|
+
`set()` stops writing `token` / `sessionId` / `mol_auth`, so even the sign-in response
|
|
1995
|
+
carries the `Authorization` header alone and the jar never fills. Set it on the mount in
|
|
1996
|
+
front of the routes that sign users in — the scaffold's `api/src/App/router.ts` — and
|
|
1997
|
+
check for a SECOND one: a request can pass several mounts (`api/src/server.ts`'s
|
|
1998
|
+
`preApiRouter` runs before `App/router.ts` and covers all of `/api`), the last one it
|
|
1999
|
+
passes sets the policy both halves read, and a laxer mount later in the chain cannot
|
|
2000
|
+
re-admit the cookie. If the app mounts two, mount it once instead.
|
|
1935
2001
|
|
|
1936
2002
|
**Revoking a session yourself:** the middleware caches "this device exists" for 10 s per
|
|
1937
2003
|
process, so a hand-written logout that deletes the `devices` row keeps authorizing that
|
package/dist/authorization.d.ts
CHANGED
|
@@ -89,6 +89,33 @@ export declare const invalidateDeviceExistsCache: (deviceId: string) => void;
|
|
|
89
89
|
* subsequent requests; only these rare revocation events pay a brief re-check.
|
|
90
90
|
*/
|
|
91
91
|
export declare const invalidateAllDeviceExistsCache: () => void;
|
|
92
|
+
/**
|
|
93
|
+
* `res.locals` key carrying THIS request's cookie-session policy. {@link verifyMiddleware}
|
|
94
|
+
* writes it from its `allowCookieSession` option; {@link set} reads it. When the value is
|
|
95
|
+
* `false` the request arrived through a mount that authenticates by `Authorization` header
|
|
96
|
+
* only, so `set()` must not hand the client the `token` / `sessionId` / `mol_auth` cookies
|
|
97
|
+
* either: a cookie written here authenticates that same client on its NEXT request (an HTTP
|
|
98
|
+
* test runner's cookie jar, `curl -b`, a browser) through any mount that does accept one,
|
|
99
|
+
* which is how a suite step called "no auth" comes back 200.
|
|
100
|
+
*
|
|
101
|
+
* Exported so a custom auth middleware can declare the same policy for the requests it
|
|
102
|
+
* handles, and so tests can assert the key rather than a string literal.
|
|
103
|
+
*/
|
|
104
|
+
export declare const ALLOW_COOKIE_SESSION_LOCALS_KEY = "allowCookieSession";
|
|
105
|
+
/**
|
|
106
|
+
* `res.locals` key carrying HOW this request's session was resolved — `'header'` when
|
|
107
|
+
* the `Authorization` header named the token, `'cookie'` when only the `token` cookie
|
|
108
|
+
* did. {@link verifyMiddleware} writes it alongside `res.locals.session`.
|
|
109
|
+
*
|
|
110
|
+
* A mount with `allowCookieSession: false` reads it to drop a session an EARLIER, laxer
|
|
111
|
+
* mount already resolved out of the cookie (the scaffold mounts `verifyMiddleware` twice,
|
|
112
|
+
* and the first one runs for every `/api` request). A session resolved from the header is
|
|
113
|
+
* never dropped: that client sent its credentials, which is exactly what a token-only API
|
|
114
|
+
* accepts.
|
|
115
|
+
*/
|
|
116
|
+
export declare const AUTH_SESSION_SOURCE_LOCALS_KEY = "authSessionSource";
|
|
117
|
+
/** How {@link verifyMiddleware} resolved a request's session. See {@link AUTH_SESSION_SOURCE_LOCALS_KEY}. */
|
|
118
|
+
export type AuthSessionSource = 'header' | 'cookie';
|
|
92
119
|
/**
|
|
93
120
|
* Set authorization headers and cookie for a session.
|
|
94
121
|
*
|
|
@@ -98,6 +125,17 @@ export declare const invalidateAllDeviceExistsCache: () => void;
|
|
|
98
125
|
* (register, login, OAuth, password reset), as `handlers/logIn.ts` does: a placeholder
|
|
99
126
|
* (`'unknown'`, `''`, the user's id) yields a token that every later request answers 401.
|
|
100
127
|
*
|
|
128
|
+
* Always sets the `Authorization` response header. Writes the session cookies (the httpOnly
|
|
129
|
+
* `token` and `sessionId`, plus the readable `mol_auth` presence hint) UNLESS the request's
|
|
130
|
+
* cookie-session policy is off — see {@link ALLOW_COOKIE_SESSION_LOCALS_KEY}, written by
|
|
131
|
+
* {@link verifyMiddleware}. A token-only API (`allowCookieSession: false`) gets the header
|
|
132
|
+
* and no cookies, so its clients cannot drift into a cookie session.
|
|
133
|
+
*
|
|
134
|
+
* The policy is the MOUNT's, and the mount records it on every request it passes — including
|
|
135
|
+
* the sign-in request itself, which carries no token and therefore resolves no session. So a
|
|
136
|
+
* token-only API writes no cookie from the very first response: mount
|
|
137
|
+
* `verifyMiddleware({ allowCookieSession: false })` in front of the routes that sign users in.
|
|
138
|
+
*
|
|
101
139
|
* @param _req - The request object (unused; reserved for handler signature parity).
|
|
102
140
|
* @param res - The response object.
|
|
103
141
|
* @param session - The session.
|
|
@@ -122,17 +160,22 @@ export interface VerifyMiddlewareOptions {
|
|
|
122
160
|
*/
|
|
123
161
|
shouldRefreshSession?: (session: Session, req: MoleculeRequest) => boolean | Promise<boolean>;
|
|
124
162
|
/**
|
|
125
|
-
*
|
|
163
|
+
* Whether the `token` cookie is a session on this mount, in BOTH directions.
|
|
126
164
|
* Defaults to `true` (browser clients sign in once and every page request carries
|
|
127
|
-
* the cookie).
|
|
128
|
-
*
|
|
129
|
-
*
|
|
165
|
+
* the cookie).
|
|
166
|
+
*
|
|
167
|
+
* Pass `false` for a token-only JSON API. Then the cookie is not a credential here:
|
|
168
|
+
* the middleware does not resolve a session from it (and drops one an earlier, laxer
|
|
169
|
+
* mount already resolved from it), and {@link set} does not write it either — so a
|
|
170
|
+
* client that keeps a cookie jar (an HTTP test runner, `curl -b`) never picks up a
|
|
171
|
+
* session it should not have, and stops answering 200 on the step its spec calls
|
|
172
|
+
* "no auth".
|
|
130
173
|
*/
|
|
131
174
|
allowCookieSession?: boolean;
|
|
132
175
|
}
|
|
133
176
|
/**
|
|
134
177
|
* Middleware that verifies the JWT token from the `Authorization` header and sets `res.locals.session`.
|
|
135
|
-
* Falls back to cookie-based session for browser clients unless `allowCookieSession` is `false
|
|
178
|
+
* Falls back to cookie-based session for browser clients unless `allowCookieSession` is `false` — which also stops `set()` writing the session cookie for this request. Auto-refreshes tokens older than `JWT_REFRESH_TIME`.
|
|
136
179
|
* @param options - {@link VerifyMiddlewareOptions} controlling the auto-refresh.
|
|
137
180
|
* @returns An Express-compatible middleware function.
|
|
138
181
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"authorization.d.ts","sourceRoot":"","sources":["../src/authorization.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAKH,OAAO,cAAc,CAAA;AAMrB,OAAO,KAAK,EACV,eAAe,EACf,sBAAsB,EACtB,gBAAgB,EACjB,MAAM,wBAAwB,CAAA;AAE/B,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,aAAa,CAAA;AAwB1C;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,iBAAiB,GAAI,MAAM,MAAM,KAAG,MACP,CAAA;AA4B1C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,eAAO,MAAM,oBAAoB,QAAO;IACtC,MAAM,EAAE,OAAO,CAAA;IAC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;;;;;;;;;;;;;;;;;;;GAwBG;AACH,eAAO,MAAM,GAAG,GACd,MAAM,eAAe,EACrB,KAAK,gBAAgB,EACrB,SAAS,OAAO,KACf,MAAM,GAAG,IA0EX,CAAA;AAED,4CAA4C;AAC5C,MAAM,WAAW,uBAAuB;IACtC;;;;;;;;;;;;;OAaG;IACH,oBAAoB,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,eAAe,KAAK,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAA;IAC7F;;;;;;;;;;;OAWG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAA;CAC7B;AAED;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB,GAC1B,UAAU,uBAAuB,KAAG,sBA0GpC,CAAA"}
|
package/dist/authorization.js
CHANGED
|
@@ -242,6 +242,43 @@ const isDeviceActive = async (deviceId) => {
|
|
|
242
242
|
return true;
|
|
243
243
|
}
|
|
244
244
|
};
|
|
245
|
+
/**
|
|
246
|
+
* `res.locals` key carrying THIS request's cookie-session policy. {@link verifyMiddleware}
|
|
247
|
+
* writes it from its `allowCookieSession` option; {@link set} reads it. When the value is
|
|
248
|
+
* `false` the request arrived through a mount that authenticates by `Authorization` header
|
|
249
|
+
* only, so `set()` must not hand the client the `token` / `sessionId` / `mol_auth` cookies
|
|
250
|
+
* either: a cookie written here authenticates that same client on its NEXT request (an HTTP
|
|
251
|
+
* test runner's cookie jar, `curl -b`, a browser) through any mount that does accept one,
|
|
252
|
+
* which is how a suite step called "no auth" comes back 200.
|
|
253
|
+
*
|
|
254
|
+
* Exported so a custom auth middleware can declare the same policy for the requests it
|
|
255
|
+
* handles, and so tests can assert the key rather than a string literal.
|
|
256
|
+
*/
|
|
257
|
+
export const ALLOW_COOKIE_SESSION_LOCALS_KEY = 'allowCookieSession';
|
|
258
|
+
/**
|
|
259
|
+
* `res.locals` key carrying HOW this request's session was resolved — `'header'` when
|
|
260
|
+
* the `Authorization` header named the token, `'cookie'` when only the `token` cookie
|
|
261
|
+
* did. {@link verifyMiddleware} writes it alongside `res.locals.session`.
|
|
262
|
+
*
|
|
263
|
+
* A mount with `allowCookieSession: false` reads it to drop a session an EARLIER, laxer
|
|
264
|
+
* mount already resolved out of the cookie (the scaffold mounts `verifyMiddleware` twice,
|
|
265
|
+
* and the first one runs for every `/api` request). A session resolved from the header is
|
|
266
|
+
* never dropped: that client sent its credentials, which is exactly what a token-only API
|
|
267
|
+
* accepts.
|
|
268
|
+
*/
|
|
269
|
+
export const AUTH_SESSION_SOURCE_LOCALS_KEY = 'authSessionSource';
|
|
270
|
+
/**
|
|
271
|
+
* Whether THIS request may be handed a session cookie by {@link set}.
|
|
272
|
+
*
|
|
273
|
+
* {@link verifyMiddleware} records its `allowCookieSession` option in `res.locals`
|
|
274
|
+
* ({@link ALLOW_COOKIE_SESSION_LOCALS_KEY}); an ABSENT record means the request never
|
|
275
|
+
* passed a mount that declares a policy, so the historical default applies (cookies on)
|
|
276
|
+
* and a direct `set()` caller outside the scaffold is unchanged.
|
|
277
|
+
*
|
|
278
|
+
* @param res - The response object.
|
|
279
|
+
* @returns `true` when the session cookies may be written.
|
|
280
|
+
*/
|
|
281
|
+
const maySetSessionCookie = (res) => res.locals?.[ALLOW_COOKIE_SESSION_LOCALS_KEY] !== false;
|
|
245
282
|
/**
|
|
246
283
|
* Set authorization headers and cookie for a session.
|
|
247
284
|
*
|
|
@@ -251,6 +288,17 @@ const isDeviceActive = async (deviceId) => {
|
|
|
251
288
|
* (register, login, OAuth, password reset), as `handlers/logIn.ts` does: a placeholder
|
|
252
289
|
* (`'unknown'`, `''`, the user's id) yields a token that every later request answers 401.
|
|
253
290
|
*
|
|
291
|
+
* Always sets the `Authorization` response header. Writes the session cookies (the httpOnly
|
|
292
|
+
* `token` and `sessionId`, plus the readable `mol_auth` presence hint) UNLESS the request's
|
|
293
|
+
* cookie-session policy is off — see {@link ALLOW_COOKIE_SESSION_LOCALS_KEY}, written by
|
|
294
|
+
* {@link verifyMiddleware}. A token-only API (`allowCookieSession: false`) gets the header
|
|
295
|
+
* and no cookies, so its clients cannot drift into a cookie session.
|
|
296
|
+
*
|
|
297
|
+
* The policy is the MOUNT's, and the mount records it on every request it passes — including
|
|
298
|
+
* the sign-in request itself, which carries no token and therefore resolves no session. So a
|
|
299
|
+
* token-only API writes no cookie from the very first response: mount
|
|
300
|
+
* `verifyMiddleware({ allowCookieSession: false })` in front of the routes that sign users in.
|
|
301
|
+
*
|
|
254
302
|
* @param _req - The request object (unused; reserved for handler signature parity).
|
|
255
303
|
* @param res - The response object.
|
|
256
304
|
* @param session - The session.
|
|
@@ -268,7 +316,19 @@ export const set = (_req, res, session) => {
|
|
|
268
316
|
const { exp, iat, nbf, ...sessionPayload } = session;
|
|
269
317
|
const token = sign(sessionPayload);
|
|
270
318
|
if (token) {
|
|
319
|
+
// The header is the credential a token-only API's clients read; it is set on
|
|
320
|
+
// every request that mints a token, cookies or not.
|
|
271
321
|
res.setHeader('Authorization', `Bearer ${token}`);
|
|
322
|
+
// A request whose cookie policy is OFF gets the header and NOTHING else. The
|
|
323
|
+
// cookie is not a convenience here — it is a second credential the client did
|
|
324
|
+
// not ask for: once written, an HTTP test runner's cookie jar (Hurl, `curl -b`)
|
|
325
|
+
// or a browser sends it on the NEXT request, including one that sends no
|
|
326
|
+
// `Authorization` header, and a suite step called "no auth" answers 200 where
|
|
327
|
+
// its spec says 401. Gated on the mount's own option (see
|
|
328
|
+
// `allowCookieSession`), so a token-only API cannot hand one out at sign-in.
|
|
329
|
+
if (!maySetSessionCookie(res)) {
|
|
330
|
+
return token;
|
|
331
|
+
}
|
|
272
332
|
// Set as cookies for web browser clients.
|
|
273
333
|
// In production the names are `__Host-`-prefixed (see getAuthCookieName),
|
|
274
334
|
// which REQUIRES Secure + Path=/ + NO Domain — all satisfied by
|
|
@@ -312,12 +372,20 @@ export const set = (_req, res, session) => {
|
|
|
312
372
|
};
|
|
313
373
|
/**
|
|
314
374
|
* Middleware that verifies the JWT token from the `Authorization` header and sets `res.locals.session`.
|
|
315
|
-
* Falls back to cookie-based session for browser clients unless `allowCookieSession` is `false
|
|
375
|
+
* Falls back to cookie-based session for browser clients unless `allowCookieSession` is `false` — which also stops `set()` writing the session cookie for this request. Auto-refreshes tokens older than `JWT_REFRESH_TIME`.
|
|
316
376
|
* @param options - {@link VerifyMiddlewareOptions} controlling the auto-refresh.
|
|
317
377
|
* @returns An Express-compatible middleware function.
|
|
318
378
|
*/
|
|
319
379
|
export const verifyMiddleware = (options) => async (req, res, next) => {
|
|
320
380
|
try {
|
|
381
|
+
// Record THIS mount's cookie policy for the rest of the request, before any
|
|
382
|
+
// early return: `set()` reads it (so a token-only API writes no cookie from the
|
|
383
|
+
// sign-in response on) and a later, stricter mount reads it to know it may
|
|
384
|
+
// strip a session an earlier one resolved out of the cookie. The sign-in
|
|
385
|
+
// request itself carries no token, so the write cannot sit behind the
|
|
386
|
+
// token lookup below.
|
|
387
|
+
const allowCookieSession = options?.allowCookieSession !== false;
|
|
388
|
+
res.locals[ALLOW_COOKIE_SESSION_LOCALS_KEY] = allowCookieSession;
|
|
321
389
|
// Get token from Authorization header, falling back to the `token`
|
|
322
390
|
// cookie that `set()` writes alongside the header. The JSDoc above
|
|
323
391
|
// promises this fallback for browser clients; without it the cookie
|
|
@@ -333,10 +401,20 @@ export const verifyMiddleware = (options) => async (req, res, next) => {
|
|
|
333
401
|
const cookies = req.cookies;
|
|
334
402
|
const cookieToken = readAuthCookie(cookies, 'token');
|
|
335
403
|
const token = headerToken ??
|
|
336
|
-
(
|
|
337
|
-
? cookieToken
|
|
338
|
-
: null);
|
|
404
|
+
(allowCookieSession && typeof cookieToken === 'string' && cookieToken ? cookieToken : null);
|
|
339
405
|
if (!token) {
|
|
406
|
+
// A token-only mount accepts NOTHING from the cookie — not even a session an
|
|
407
|
+
// earlier, laxer mount already resolved out of it. The scaffold mounts
|
|
408
|
+
// `verifyMiddleware` twice (server.ts's `preApiRouter` runs first, then
|
|
409
|
+
// App/router.ts), and the first one runs for every `/api` request: without
|
|
410
|
+
// this the app's own routes would still read `res.locals.session` off the
|
|
411
|
+
// cookie and answer 200 where the spec says 401. A header-resolved session is
|
|
412
|
+
// left alone — that client sent its credentials, which is what a token-only
|
|
413
|
+
// API accepts.
|
|
414
|
+
if (!allowCookieSession && res.locals[AUTH_SESSION_SOURCE_LOCALS_KEY] === 'cookie') {
|
|
415
|
+
delete res.locals.session;
|
|
416
|
+
delete res.locals[AUTH_SESSION_SOURCE_LOCALS_KEY];
|
|
417
|
+
}
|
|
340
418
|
return next();
|
|
341
419
|
}
|
|
342
420
|
// Try to verify the token.
|
|
@@ -361,6 +439,11 @@ export const verifyMiddleware = (options) => async (req, res, next) => {
|
|
|
361
439
|
return next();
|
|
362
440
|
}
|
|
363
441
|
res.locals.session = session;
|
|
442
|
+
// Record HOW the session was resolved, so a later mount with
|
|
443
|
+
// `allowCookieSession: false` can drop a cookie-resolved session (and only
|
|
444
|
+
// that one) — see {@link AUTH_SESSION_SOURCE_LOCALS_KEY}.
|
|
445
|
+
const sessionSource = headerToken ? 'header' : 'cookie';
|
|
446
|
+
res.locals[AUTH_SESSION_SOURCE_LOCALS_KEY] = sessionSource;
|
|
364
447
|
// Auto-refresh the token if it's old enough.
|
|
365
448
|
try {
|
|
366
449
|
const decoded = decode(token);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"authorization.js","sourceRoot":"","sources":["../src/authorization.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,0EAA0E;AAC1E,gFAAgF;AAChF,oCAAoC;AACpC,OAAO,cAAc,CAAA;AACrB,OAAO,EAAE,EAAE,IAAI,IAAI,EAAE,MAAM,MAAM,CAAA;AAEjC,OAAO,EAAE,GAAG,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA;AACnD,OAAO,EAAE,GAAG,IAAI,SAAS,EAAE,MAAM,sBAAsB,CAAA;AACvD,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,I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ritten.
|
|
37
37
|
*
|
|
38
|
-
* **A token-only JSON API:**
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
* whose clients send the
|
|
38
|
+
* **A token-only JSON API:** by default the `token` cookie that login sets IS a session,
|
|
39
|
+
* and `authorization.set()` writes it on every sign-in — so a client that keeps a cookie
|
|
40
|
+
* jar (an HTTP test runner, `curl -b`) stays signed in on a request that sends no
|
|
41
|
+
* `Authorization` header, and gets 200 on the step the suite calls "no auth". Mount
|
|
42
|
+
* `verifyMiddleware({ allowCookieSession: false })` for an API whose clients send the
|
|
43
|
+
* header: that ONE option turns the cookie off in BOTH directions — the middleware no
|
|
44
|
+
* longer resolves a session from it (nor keeps one an earlier, laxer mount resolved), and
|
|
45
|
+
* `set()` stops writing `token` / `sessionId` / `mol_auth`, so even the sign-in response
|
|
46
|
+
* carries the `Authorization` header alone and the jar never fills. Set it on the mount in
|
|
47
|
+
* front of the routes that sign users in — the scaffold's `api/src/App/router.ts` — and
|
|
48
|
+
* check for a SECOND one: a request can pass several mounts (`api/src/server.ts`'s
|
|
49
|
+
* `preApiRouter` runs before `App/router.ts` and covers all of `/api`), the last one it
|
|
50
|
+
* passes sets the policy both halves read, and a laxer mount later in the chain cannot
|
|
51
|
+
* re-admit the cookie. If the app mounts two, mount it once instead.
|
|
43
52
|
*
|
|
44
53
|
* **Revoking a session yourself:** the middleware caches "this device exists" for 10 s per
|
|
45
54
|
* process, so a hand-written logout that deletes the `devices` row keeps authorizing that
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyHG;AAEH,OAAO,KAAK,aAAa,MAAM,oBAAoB,CAAA;AACnD,OAAO,KAAK,WAAW,MAAM,wBAAwB,CAAA;AACrD,cAAc,oBAAoB,CAAA;AAClC,OAAO,KAAK,QAAQ,MAAM,qBAAqB,CAAA;AAC/C,cAAc,WAAW,CAAA;AACzB,cAAc,wBAAwB,CAAA;AACtC,cAAc,eAAe,CAAA;AAC7B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,OAAO,KAAK,KAAK,MAAM,YAAY,CAAA;AACnC,OAAO,KAAK,SAAS,MAAM,sBAAsB,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -35,11 +35,20 @@
|
|
|
35
35
|
* sent (letters, digits, `.`, `-`, `_`); anything else is answered 400
|
|
36
36
|
* `user.error.usernameInvalid`, never rewritten.
|
|
37
37
|
*
|
|
38
|
-
* **A token-only JSON API:**
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
* whose clients send the
|
|
38
|
+
* **A token-only JSON API:** by default the `token` cookie that login sets IS a session,
|
|
39
|
+
* and `authorization.set()` writes it on every sign-in — so a client that keeps a cookie
|
|
40
|
+
* jar (an HTTP test runner, `curl -b`) stays signed in on a request that sends no
|
|
41
|
+
* `Authorization` header, and gets 200 on the step the suite calls "no auth". Mount
|
|
42
|
+
* `verifyMiddleware({ allowCookieSession: false })` for an API whose clients send the
|
|
43
|
+
* header: that ONE option turns the cookie off in BOTH directions — the middleware no
|
|
44
|
+
* longer resolves a session from it (nor keeps one an earlier, laxer mount resolved), and
|
|
45
|
+
* `set()` stops writing `token` / `sessionId` / `mol_auth`, so even the sign-in response
|
|
46
|
+
* carries the `Authorization` header alone and the jar never fills. Set it on the mount in
|
|
47
|
+
* front of the routes that sign users in — the scaffold's `api/src/App/router.ts` — and
|
|
48
|
+
* check for a SECOND one: a request can pass several mounts (`api/src/server.ts`'s
|
|
49
|
+
* `preApiRouter` runs before `App/router.ts` and covers all of `/api`), the last one it
|
|
50
|
+
* passes sets the policy both halves read, and a laxer mount later in the chain cannot
|
|
51
|
+
* re-admit the cookie. If the app mounts two, mount it once instead.
|
|
43
52
|
*
|
|
44
53
|
* **Revoking a session yourself:** the middleware caches "this device exists" for 10 s per
|
|
45
54
|
* process, so a hand-written logout that deletes the `devices` row keeps authorizing that
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyHG;AAEH,OAAO,KAAK,aAAa,MAAM,oBAAoB,CAAA;AACnD,OAAO,KAAK,WAAW,MAAM,wBAAwB,CAAA;AACrD,cAAc,oBAAoB,CAAA;AAClC,OAAO,KAAK,QAAQ,MAAM,qBAAqB,CAAA;AAC/C,cAAc,WAAW,CAAA;AACzB,cAAc,wBAAwB,CAAA;AACtC,cAAc,eAAe,CAAA;AAC7B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,OAAO,KAAK,KAAK,MAAM,YAAY,CAAA;AACnC,OAAO,KAAK,SAAS,MAAM,sBAAsB,CAAA"}
|