@bymax-one/nest-auth 1.0.11 → 1.1.1
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/CHANGELOG.md +443 -1
- package/README.md +275 -108
- package/dist/client/index.cjs +49 -5
- package/dist/client/index.mjs +49 -5
- package/dist/nextjs/index.cjs +152 -51
- package/dist/nextjs/index.d.cts +164 -58
- package/dist/nextjs/index.d.ts +164 -58
- package/dist/nextjs/index.mjs +152 -51
- package/dist/react/index.cjs +85 -89
- package/dist/react/index.mjs +85 -89
- package/dist/server/index.cjs +6083 -1574
- package/dist/server/index.d.cts +2880 -180
- package/dist/server/index.d.ts +2880 -180
- package/dist/server/index.mjs +6062 -1560
- package/dist/shared/index.cjs +60 -9
- package/dist/shared/index.d.cts +126 -18
- package/dist/shared/index.d.ts +126 -18
- package/dist/shared/index.mjs +60 -9
- package/package.json +112 -43
package/dist/nextjs/index.d.cts
CHANGED
|
@@ -156,11 +156,11 @@ declare function buildSilentRefreshUrl(request: RequestWithUrl, redirectTo?: str
|
|
|
156
156
|
* its structured attributes so dedup logic (and any future cookie
|
|
157
157
|
* inspection) can work against a typed object instead of raw strings.
|
|
158
158
|
*
|
|
159
|
-
* {@link getSetCookieHeaders}
|
|
160
|
-
*
|
|
161
|
-
*
|
|
162
|
-
*
|
|
163
|
-
*
|
|
159
|
+
* {@link getSetCookieHeaders} extracts the list of raw Set-Cookie strings
|
|
160
|
+
* from either `Headers.getSetCookie()` or, for a header-like object that
|
|
161
|
+
* does not implement it, `get('set-cookie')` — which concatenates cookies
|
|
162
|
+
* with commas, and is only safe when the values do not themselves contain
|
|
163
|
+
* unquoted commas.
|
|
164
164
|
*
|
|
165
165
|
* Edge-Runtime-safe: pure string manipulation, no Node-only APIs.
|
|
166
166
|
*
|
|
@@ -177,7 +177,7 @@ declare function buildSilentRefreshUrl(request: RequestWithUrl, redirectTo?: str
|
|
|
177
177
|
* *most-recent-writer-wins* invariant users rely on. If that
|
|
178
178
|
* upstream behaviour ever changes, widen the key.
|
|
179
179
|
* - `parseSetCookieHeader` intentionally does NOT trim the cookie
|
|
180
|
-
* *value* — RFC 6265 §5.2
|
|
180
|
+
* *value* — RFC 6265 §5.2, item 2 says the value is everything
|
|
181
181
|
* after the first `=` up to the first `;`. Attribute *values*
|
|
182
182
|
* (e.g., `Path=/x `) are trimmed because the spec explicitly
|
|
183
183
|
* permits surrounding OWS there.
|
|
@@ -240,11 +240,11 @@ interface HeadersLike {
|
|
|
240
240
|
* with attribute semantics (`SameSite`, `Domain`) are lowercased to
|
|
241
241
|
* keep the dedup key stable. The `name` and `value` of the cookie
|
|
242
242
|
* itself are preserved as-is — including any internal whitespace, per
|
|
243
|
-
* RFC 6265 §5.2
|
|
243
|
+
* RFC 6265 §5.2, item 2.
|
|
244
244
|
*
|
|
245
245
|
* Rejects inputs containing CR or LF characters (header smuggling
|
|
246
246
|
* defence): the returned record has an empty `name`, which the dedup
|
|
247
|
-
*
|
|
247
|
+
* filter discards.
|
|
248
248
|
*
|
|
249
249
|
* Malformed input (no `=` in the first segment) similarly returns an
|
|
250
250
|
* empty parsed record.
|
|
@@ -274,13 +274,14 @@ declare function dedupeSetCookieHeaders(cookies: readonly string[]): string[];
|
|
|
274
274
|
* Return the array of raw `Set-Cookie` header strings from a
|
|
275
275
|
* {@link HeadersLike}.
|
|
276
276
|
*
|
|
277
|
-
* Prefers
|
|
278
|
-
*
|
|
279
|
-
*
|
|
280
|
-
*
|
|
281
|
-
*
|
|
282
|
-
*
|
|
283
|
-
*
|
|
277
|
+
* Prefers `Headers.getSetCookie()`, which correctly separates multiple
|
|
278
|
+
* Set-Cookie headers. Every runtime this library supports has it — the
|
|
279
|
+
* fallback is not for old Node but for the header-like objects consumers
|
|
280
|
+
* legitimately pass instead of a real `Headers`: test doubles, framework
|
|
281
|
+
* wrappers, and hand-rolled shims, all of which `HeadersLike` admits by
|
|
282
|
+
* typing the method optional. For those, the single comma-joined value from
|
|
283
|
+
* `get('set-cookie')` is split with an awareness that commas inside `Expires`
|
|
284
|
+
* values (`Expires=Wed, 09 Jun 2021 10:18:14 GMT`) are NOT separators.
|
|
284
285
|
*
|
|
285
286
|
* @param headers - Any object with `get()` and optionally `getSetCookie()`.
|
|
286
287
|
* @returns An array of raw `Set-Cookie` header strings (possibly empty).
|
|
@@ -324,6 +325,7 @@ declare function getSetCookieHeaders(headers: HeadersLike): string[];
|
|
|
324
325
|
*
|
|
325
326
|
* Pinning the algorithm at verification time eliminates both vectors.
|
|
326
327
|
*/
|
|
328
|
+
|
|
327
329
|
/**
|
|
328
330
|
* Header of a JWT as carried in the first base64url segment.
|
|
329
331
|
*
|
|
@@ -353,14 +355,50 @@ interface DecodedToken {
|
|
|
353
355
|
/**
|
|
354
356
|
* `true` when the payload parses AND `exp` is in the future.
|
|
355
357
|
*
|
|
356
|
-
*
|
|
357
|
-
*
|
|
358
|
-
*
|
|
359
|
-
*
|
|
360
|
-
*
|
|
361
|
-
* only when `verifyJwtToken` was invoked with a non-null secret.
|
|
358
|
+
* Expiry ONLY. It says nothing about the signature:
|
|
359
|
+
* {@link decodeJwtToken} sets it without checking one at all. It is
|
|
360
|
+
* one half of the question an authorisation decision is asking —
|
|
361
|
+
* see {@link DecodedToken.signatureVerified} for the other half and
|
|
362
|
+
* for the check that uses both.
|
|
362
363
|
*/
|
|
363
364
|
readonly isValid: boolean;
|
|
365
|
+
/**
|
|
366
|
+
* `true` only when the signature was actually checked against a
|
|
367
|
+
* non-empty secret — i.e. only past {@link verifyJwtToken}'s HMAC
|
|
368
|
+
* check. `false` on every {@link decodeJwtToken} result, including
|
|
369
|
+
* the ones that carry `isValid: true` and a fully populated payload,
|
|
370
|
+
* and on every `verifyJwtToken` failure (which returns an empty
|
|
371
|
+
* token, so `isValid` is `false` there as well — `verifyJwtToken`
|
|
372
|
+
* fails closed on a missing or empty secret rather than falling back
|
|
373
|
+
* to a decode).
|
|
374
|
+
*
|
|
375
|
+
* The two flags are INDEPENDENT, and neither is sufficient alone:
|
|
376
|
+
*
|
|
377
|
+
* - `isValid` without `signatureVerified` is a token that has not
|
|
378
|
+
* expired and that nobody checked the signature of — including one
|
|
379
|
+
* an attacker minted with `alg: none` and an arbitrary `sub`,
|
|
380
|
+
* `role` and `tenantId`. This is the trap the flag exists for: the
|
|
381
|
+
* two branches are otherwise indistinguishable at runtime, and
|
|
382
|
+
* `if (decoded.isValid && decoded.role === 'ADMIN')` is the natural
|
|
383
|
+
* reading of a function called `verifyJwtToken`.
|
|
384
|
+
* - `signatureVerified` without `isValid` is a **genuinely signed
|
|
385
|
+
* token that has expired**, or one carrying no `exp` at all. The
|
|
386
|
+
* signature is checked before the expiry is read, so this
|
|
387
|
+
* combination is reachable on the ordinary path — a session that
|
|
388
|
+
* simply ran out.
|
|
389
|
+
*
|
|
390
|
+
* So an authorisation decision reads BOTH:
|
|
391
|
+
*
|
|
392
|
+
* ```ts
|
|
393
|
+
* const decoded = await verifyJwtToken(token, secret)
|
|
394
|
+
* if (decoded.isValid && decoded.signatureVerified && decoded.role === 'ADMIN') {
|
|
395
|
+
* ```
|
|
396
|
+
*
|
|
397
|
+
* The proxy's own `TokenState.signatureVerified` is this conjunction
|
|
398
|
+
* already (`decoded.signatureVerified && decoded.isValid && …`); a
|
|
399
|
+
* caller using these helpers directly has to form it here.
|
|
400
|
+
*/
|
|
401
|
+
readonly signatureVerified: boolean;
|
|
364
402
|
/** Raw header object, or `undefined` if the header failed to parse. */
|
|
365
403
|
readonly header: JwtHeader | undefined;
|
|
366
404
|
/**
|
|
@@ -410,24 +448,33 @@ declare function decodeJwtToken(token: string): DecodedToken;
|
|
|
410
448
|
* 6. Verify the signature over `<header>.<payload>` (ASCII bytes).
|
|
411
449
|
* 7. Validate `exp` is in the future.
|
|
412
450
|
*
|
|
413
|
-
*
|
|
414
|
-
*
|
|
415
|
-
*
|
|
416
|
-
*
|
|
417
|
-
*
|
|
418
|
-
*
|
|
419
|
-
*
|
|
420
|
-
*
|
|
421
|
-
*
|
|
422
|
-
*
|
|
423
|
-
*
|
|
424
|
-
*
|
|
451
|
+
* **A missing secret fails closed.** `undefined`, `null` and the
|
|
452
|
+
* empty string all return `emptyDecoded()` rather than falling back
|
|
453
|
+
* to a decode. This function used to fall back to
|
|
454
|
+
* {@link decodeJwtToken}, and the two branches returned the same
|
|
455
|
+
* shape with the same `isValid: true` — so a caller writing
|
|
456
|
+
* `const d = await verifyJwtToken(t, secret)` and then
|
|
457
|
+
* `if (d.isValid && d.role === 'ADMIN')`, the natural
|
|
458
|
+
* reading of the name, admitted a token an attacker minted with
|
|
459
|
+
* `alg: none` the moment the secret went missing. An unset
|
|
460
|
+
* environment variable was enough to arrange that. A function that
|
|
461
|
+
* cannot verify must refuse, not quietly answer a weaker question;
|
|
462
|
+
* {@link decodeJwtToken} remains the explicit, correctly-named entry
|
|
463
|
+
* point for the non-authoritative read.
|
|
464
|
+
*
|
|
465
|
+
* The empty string is refused for a second reason of its own: an
|
|
466
|
+
* empty HMAC key is technically valid to the Web Crypto API and
|
|
467
|
+
* would verify a token signed with the same empty key — a common
|
|
468
|
+
* misconfiguration (`JWT_SECRET=""` in an environment file) that
|
|
469
|
+
* must not silently downgrade to a no-op.
|
|
425
470
|
*
|
|
426
471
|
* @param token - JWS compact serialisation.
|
|
427
|
-
* @param secret - HS256 shared secret
|
|
428
|
-
*
|
|
472
|
+
* @param secret - HS256 shared secret. A missing or empty value
|
|
473
|
+
* makes every token invalid.
|
|
429
474
|
* @returns A {@link DecodedToken} where `isValid` reflects BOTH the
|
|
430
|
-
* signature verification AND the expiry check
|
|
475
|
+
* signature verification AND the expiry check, and
|
|
476
|
+
* `signatureVerified` is `true` only when a signature was
|
|
477
|
+
* actually checked.
|
|
431
478
|
*/
|
|
432
479
|
declare function verifyJwtToken(token: string, secret?: string | null): Promise<DecodedToken>;
|
|
433
480
|
/**
|
|
@@ -492,7 +539,7 @@ declare function getTenantId(token: DecodedToken): string | undefined;
|
|
|
492
539
|
* // proxy.ts
|
|
493
540
|
* import { createAuthProxy } from '@bymax-one/nest-auth/nextjs'
|
|
494
541
|
*
|
|
495
|
-
*
|
|
542
|
+
* const authProxy = createAuthProxy({
|
|
496
543
|
* publicRoutes: ['/', '/auth/login'],
|
|
497
544
|
* publicRoutesRedirectIfAuthenticated: ['/auth/login'],
|
|
498
545
|
* protectedRoutes: [
|
|
@@ -507,6 +554,10 @@ declare function getTenantId(token: DecodedToken): string | undefined;
|
|
|
507
554
|
* blockedUserStatuses: ['BANNED', 'INACTIVE', 'EXPIRED']
|
|
508
555
|
* })
|
|
509
556
|
*
|
|
557
|
+
* // Bound first, then exported: Next 16 scans this file for a function exported as `proxy`
|
|
558
|
+
* // and does not recognise `export const { proxy } = ...`, which fails the build.
|
|
559
|
+
* export const proxy = authProxy.proxy
|
|
560
|
+
*
|
|
510
561
|
* export const config = { matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'] }
|
|
511
562
|
* ```
|
|
512
563
|
*
|
|
@@ -635,11 +686,19 @@ interface AuthProxyConfig {
|
|
|
635
686
|
*/
|
|
636
687
|
interface AuthProxyInstance {
|
|
637
688
|
/**
|
|
638
|
-
* The Next.js 16 proxy function.
|
|
689
|
+
* The Next.js 16 proxy function. Bind the instance, then re-export this property:
|
|
639
690
|
*
|
|
640
691
|
* ```ts
|
|
641
|
-
*
|
|
692
|
+
* const authProxy = createAuthProxy({ ... })
|
|
693
|
+
* export const proxy = authProxy.proxy
|
|
642
694
|
* ```
|
|
695
|
+
*
|
|
696
|
+
* NOT `export const { proxy } = createAuthProxy({ ... })`. Next 16 statically scans
|
|
697
|
+
* `proxy.ts` for a function exported under that name and does not recognise a destructuring
|
|
698
|
+
* pattern, so the destructured form fails the production build outright: "The file
|
|
699
|
+
* ./proxy.ts must export a function, either as a default export or as a named 'proxy'
|
|
700
|
+
* export." It was the documented form here and in the README until a real Next build was
|
|
701
|
+
* run against the published artifact.
|
|
643
702
|
*/
|
|
644
703
|
readonly proxy: (request: NextRequest) => Promise<NextResponse>;
|
|
645
704
|
/**
|
|
@@ -711,9 +770,14 @@ declare function createAuthProxy(config: AuthProxyConfig): AuthProxyInstance;
|
|
|
711
770
|
* - **Rate limiting.** The handler makes one upstream `POST` per
|
|
712
771
|
* inbound `GET`. Apply route-level rate limiting in the consumer
|
|
713
772
|
* app to prevent DoS amplification.
|
|
714
|
-
* - **Host-header trust.**
|
|
715
|
-
* `
|
|
716
|
-
*
|
|
773
|
+
* - **Host-header trust.** No longer an obligation for this
|
|
774
|
+
* handler's redirects: the `Location` it emits is relative, so
|
|
775
|
+
* the browser resolves it against the URL it actually requested
|
|
776
|
+
* and a forged `Host` has no destination to choose. The request
|
|
777
|
+
* origin is still derived from `Host`, but only to REJECT a
|
|
778
|
+
* cross-origin `?redirect=` — a comparison, not a destination,
|
|
779
|
+
* where the worst a forged value buys is admitting a path that
|
|
780
|
+
* lands on the real app anyway.
|
|
717
781
|
* - **`apiBase`.** Always points to your OWN NestJS backend — all
|
|
718
782
|
* incoming cookies are forwarded there.
|
|
719
783
|
*
|
|
@@ -751,7 +815,8 @@ interface SilentRefreshHandlerConfig {
|
|
|
751
815
|
* match the upstream NestJS auth module's configuration. On
|
|
752
816
|
* failure the handler clears:
|
|
753
817
|
* - `access` at path `/`
|
|
754
|
-
* - `refresh` at `refreshCookiePath` (default `/api/auth
|
|
818
|
+
* - `refresh` at `refreshCookiePath` (default `/api/auth`; see the field's note — it
|
|
819
|
+
* must match the scope the upstream module planted the cookie with)
|
|
755
820
|
* - `hasSession` at path `/`
|
|
756
821
|
*/
|
|
757
822
|
readonly cookieNames: {
|
|
@@ -760,11 +825,28 @@ interface SilentRefreshHandlerConfig {
|
|
|
760
825
|
readonly hasSession: string;
|
|
761
826
|
};
|
|
762
827
|
/**
|
|
763
|
-
* Path attribute for the refresh
|
|
764
|
-
*
|
|
765
|
-
*
|
|
828
|
+
* Path attribute for the refresh-cookie clear. Defaults to `/api/auth`.
|
|
829
|
+
*
|
|
830
|
+
* **This is not the server's default**, which is `/auth`. It is the value the *proxy*
|
|
831
|
+
* topology needs: the browser addresses the Next.js route, so the cookie must be scoped to
|
|
832
|
+
* the Next.js path, which means the upstream module has to be configured with
|
|
833
|
+
* `cookies.refreshCookiePath: '/api/auth'` to plant it there in the first place — the proxy
|
|
834
|
+
* forwards `Set-Cookie` verbatim and never rewrites `Path`. A deployment that leaves the
|
|
835
|
+
* server on `/auth` must set this to `/auth` too: a browser matches a deletion on name,
|
|
836
|
+
* domain and path, so a mismatch means the clear silently does nothing and the refresh
|
|
837
|
+
* cookie outlives the logout (the session itself is revoked server-side either way).
|
|
766
838
|
*/
|
|
767
839
|
readonly refreshCookiePath?: string;
|
|
840
|
+
/**
|
|
841
|
+
* The cookie `Domain` the NestJS server planted the session with, when
|
|
842
|
+
* `cookies.resolveDomains` is configured there. Leave unset for the
|
|
843
|
+
* host-only default.
|
|
844
|
+
*
|
|
845
|
+
* A browser matches a deletion on **name, domain AND path** (RFC 6265 §5.3),
|
|
846
|
+
* so a clear that omits `Domain` cannot remove a cookie that carries one — it
|
|
847
|
+
* plants a new host-only cookie and the originals survive.
|
|
848
|
+
*/
|
|
849
|
+
readonly cookieDomain?: string;
|
|
768
850
|
}
|
|
769
851
|
/**
|
|
770
852
|
* Type of the Next.js App Router GET handler returned by the
|
|
@@ -791,11 +873,13 @@ type SilentRefreshHandler = (request: NextRequest) => Promise<NextResponse>;
|
|
|
791
873
|
* DoS amplification against the upstream service.
|
|
792
874
|
*
|
|
793
875
|
* @remarks
|
|
794
|
-
* HOST-HEADER TRUST —
|
|
795
|
-
* `Host`
|
|
796
|
-
*
|
|
797
|
-
*
|
|
798
|
-
*
|
|
876
|
+
* HOST-HEADER TRUST — the handler emits a RELATIVE `Location`, so a
|
|
877
|
+
* forged `Host` cannot choose where the browser goes: the browser
|
|
878
|
+
* resolves the header against the URL it actually requested. The
|
|
879
|
+
* request origin is still read, but only to REJECT a cross-origin
|
|
880
|
+
* `?redirect=`, which is a comparison rather than a destination —
|
|
881
|
+
* the worst a forged `Host` buys there is admitting a path that then
|
|
882
|
+
* lands on the real app anyway. See `redirectToPath`.
|
|
799
883
|
*
|
|
800
884
|
* @throws {Error} When `loginPath` is not a same-origin pathname.
|
|
801
885
|
*/
|
|
@@ -957,12 +1041,13 @@ declare const CLIENT_REFRESH_ROUTE: "/api/auth/client-refresh";
|
|
|
957
1041
|
* manages its own navigation.
|
|
958
1042
|
*
|
|
959
1043
|
* @remarks
|
|
960
|
-
* HOST-HEADER TRUST — in `'redirect'` mode the destination
|
|
961
|
-
*
|
|
962
|
-
*
|
|
963
|
-
*
|
|
964
|
-
*
|
|
965
|
-
*
|
|
1044
|
+
* HOST-HEADER TRUST — in `'redirect'` mode the destination is emitted
|
|
1045
|
+
* as a RELATIVE `Location` and the response never reads
|
|
1046
|
+
* `request.nextUrl.origin`, so the browser resolves it against the
|
|
1047
|
+
* URL it actually requested. A forged `Host` has nothing to influence
|
|
1048
|
+
* here, and this handler needs no vetting of that header to be safe.
|
|
1049
|
+
* That is a property of this response only — it says nothing about
|
|
1050
|
+
* the rest of an application, where `Host` may still reach a URL.
|
|
966
1051
|
*
|
|
967
1052
|
* Edge-Runtime-safe.
|
|
968
1053
|
*/
|
|
@@ -978,8 +1063,29 @@ interface LogoutCookieConfig {
|
|
|
978
1063
|
readonly refresh: string;
|
|
979
1064
|
readonly hasSession: string;
|
|
980
1065
|
};
|
|
981
|
-
/**
|
|
1066
|
+
/**
|
|
1067
|
+
* Path attribute for the refresh-cookie clear. Defaults to `/api/auth`.
|
|
1068
|
+
*
|
|
1069
|
+
* **This is not the server's default**, which is `/auth`. It is the value the *proxy*
|
|
1070
|
+
* topology needs: the browser addresses the Next.js route, so the cookie must be scoped to
|
|
1071
|
+
* the Next.js path, which means the upstream module has to be configured with
|
|
1072
|
+
* `cookies.refreshCookiePath: '/api/auth'` to plant it there in the first place — the proxy
|
|
1073
|
+
* forwards `Set-Cookie` verbatim and never rewrites `Path`. A deployment that leaves the
|
|
1074
|
+
* server on `/auth` must set this to `/auth` too: a browser matches a deletion on name,
|
|
1075
|
+
* domain and path, so a mismatch means the clear silently does nothing and the refresh
|
|
1076
|
+
* cookie outlives the logout (the session itself is revoked server-side either way).
|
|
1077
|
+
*/
|
|
982
1078
|
readonly refreshCookiePath?: string;
|
|
1079
|
+
/**
|
|
1080
|
+
* The cookie `Domain` the NestJS server planted the session with, when
|
|
1081
|
+
* `cookies.resolveDomains` is configured there. Leave unset for the
|
|
1082
|
+
* host-only default.
|
|
1083
|
+
*
|
|
1084
|
+
* A browser matches a deletion on **name, domain AND path** (RFC 6265 §5.3),
|
|
1085
|
+
* so a clear that omits `Domain` cannot remove a cookie that carries one — it
|
|
1086
|
+
* plants a new host-only cookie and the originals survive.
|
|
1087
|
+
*/
|
|
1088
|
+
readonly cookieDomain?: string;
|
|
983
1089
|
}
|
|
984
1090
|
/**
|
|
985
1091
|
* Logout response mode = `'redirect'`. `loginPath` is required and
|