@lenne.tech/nest-server 12.11.0 → 12.11.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/.claude/rules/better-auth.md +10 -1
- package/.claude/rules/configurable-features.md +1 -1
- package/.claude/rules/testing.md +4 -4
- package/CLAUDE.md +1 -1
- package/FRAMEWORK-API.md +1 -1
- package/dist/core/modules/better-auth/core-better-auth-api.middleware.js +7 -3
- package/dist/core/modules/better-auth/core-better-auth-api.middleware.js.map +1 -1
- package/dist/core/modules/better-auth/core-better-auth-session-token.helper.d.ts +1 -0
- package/dist/core/modules/better-auth/core-better-auth-session-token.helper.js +3 -1
- package/dist/core/modules/better-auth/core-better-auth-session-token.helper.js.map +1 -1
- package/dist/core/modules/better-auth/core-better-auth.controller.js +1 -1
- package/dist/core/modules/better-auth/core-better-auth.controller.js.map +1 -1
- package/dist/core/modules/better-auth/core-better-auth.service.d.ts +6 -2
- package/dist/core/modules/better-auth/core-better-auth.service.js +4 -3
- package/dist/core/modules/better-auth/core-better-auth.service.js.map +1 -1
- package/docs/.navigation.yml +4 -0
- package/docs/REQUEST-LIFECYCLE.md +4 -1
- package/docs/choosing-a-database.md +4 -1
- package/docs/error-codes.md +4 -1
- package/docs/index.md +34 -0
- package/docs/{brevo-manual-test.md → internal/brevo-manual-test.md} +1 -1
- package/docs/native-driver-security.md +4 -1
- package/docs/offline-sync-protocol.md +4 -1
- package/docs/process-performance-optimization.md +158 -126
- package/docs/security-overrides.md +5 -2
- package/migration-guides/12.10.x-to-12.11.x.md +2 -0
- package/migration-guides/12.11.0-to-12.11.1.md +86 -0
- package/package.json +5 -5
- package/src/core/modules/better-auth/INTEGRATION-CHECKLIST.md +1 -0
- package/src/core/modules/better-auth/README.md +6 -6
- package/src/core/modules/better-auth/core-better-auth-api.middleware.ts +7 -3
- package/src/core/modules/better-auth/core-better-auth-session-token.helper.ts +18 -1
- package/src/core/modules/better-auth/core-better-auth.controller.ts +1 -1
- package/src/core/modules/better-auth/core-better-auth.service.ts +17 -4
|
@@ -53,6 +53,7 @@ Since 12.11.0:
|
|
|
53
53
|
| A JWT issued before the update | `401` on its next request | With a session cookie (the default `cookies: true`): nothing for `@lenne.tech/nuxt-extensions` / `@lenne.tech/ng-base`, they fetch a new JWT with the session on the 401; other clients fetch `GET /iam/token` with the session again. **With `cookies: false` the client holds the JWT alone and cannot renew it: every user signs in once more after the update** |
|
|
54
54
|
| Sign-out, revoked session, deleted user | The session's JWTs answer `401` at once | — |
|
|
55
55
|
| Enabling 2FA (the first `verify-totp`) | Better-Auth replaces the session; the old JWT answers `401`. The answer's `token` is the JWT of the new session | A JWT client continues with that `token` (or fetches a new one on the 401) |
|
|
56
|
+
| Disabling 2FA (`POST /iam/two-factor/disable`); enabling it with `method: 'otp'` or `skipVerificationOnEnable` | Better-Auth replaces the session; the old JWT answers `401`. **Since 12.11.1** the answer carries `token`, the JWT of the new session (`{ status: true, token }`, `{ method: 'otp', token }`). 12.11.0 answered without it, so with `cookies: false` turning 2FA off signed the user out | Update to 12.11.1; a JWT client continues with that `token`, like after `verify-totp` |
|
|
56
57
|
| Several devices | Each JWT follows its own session; signing out one leaves the others signed in | — |
|
|
57
58
|
|
|
58
59
|
A JWT is still verified without a call to the identity provider, and the session check costs at most one lookup per
|
|
@@ -275,6 +276,7 @@ which pins 11.0.9 itself.
|
|
|
275
276
|
|---|---|---|
|
|
276
277
|
| Every JWT answers 401 right after the update | JWTs issued before 12.11.0 carry no session reference (§1) | Fetch a new one with the session (`GET /iam/token`); the frontend libraries do it on the 401. With `cookies: false` users sign in once more |
|
|
277
278
|
| 401 right after enabling 2FA in JWT mode | The first `verify-totp` replaced the session (§1) | Use the `token` of the `verify-totp` answer |
|
|
279
|
+
| Signed out right after turning 2FA off in JWT mode (`cookies: false`) | `two-factor/disable` replaced the session, and 12.11.0 answered without the new JWT (§1) | Update to 12.11.1 and use the `token` of the answer |
|
|
278
280
|
| Passkey or 2FA routes answer 404 | Sign-in through a provider alone: `emailAndPassword.enabled: false`, no plugin sign-in route (§2) | `passkey: true` / `twoFactor: { enabled: true }` |
|
|
279
281
|
| `ID_TOKEN_NOT_SUPPORTED` from a native app | Id token sign-in closed by default (§3) | `disableIdTokenSignIn: false` on that provider |
|
|
280
282
|
| `microsoft-sign-in-example.spec.ts` fails: `expected 'ID_TOKEN_NOT_SUPPORTED' not to be 'ID_TOKEN_NOT_SUPPORTED'` | The starter's test removes the option and expected an open path (§3) | Set `disableIdTokenSignIn: false` in that case instead of removing it |
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# Migration Guide: 12.11.0 → 12.11.1
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
| Category | Details |
|
|
6
|
+
|---|---|
|
|
7
|
+
| **Breaking Changes** | None |
|
|
8
|
+
| **New Features** | None |
|
|
9
|
+
| **Bugfixes** | Turning two-factor authentication off no longer signs out a client that holds a JWT alone (`cookies: false`): the answer of `POST /iam/two-factor/disable` carries the JWT of the new session (§1) |
|
|
10
|
+
| **Security** | MongoDB driver 7.9.1 (three medium advisories, §2) |
|
|
11
|
+
| **Migration Effort** | Package update (`pnpm run update` raises the MongoDB driver pins). JWT clients store the `token` of the disable answer, as they do after `verify-totp` |
|
|
12
|
+
|
|
13
|
+
## Quick Migration
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pnpm add @lenne.tech/nest-server@12.11.1
|
|
17
|
+
pnpm run update # nest-server-starter based projects
|
|
18
|
+
pnpm run build
|
|
19
|
+
pnpm test
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
**Vendor mode**: run `/lt-dev:backend:update-nest-server-core` as usual.
|
|
23
|
+
|
|
24
|
+
## 1. Bugfix: two-factor disable answers with the JWT of the new session
|
|
25
|
+
|
|
26
|
+
**No backend action needed.** A JWT client takes the new `token` from the answer.
|
|
27
|
+
|
|
28
|
+
Since 12.11.0 a Better-Auth JWT is bound to the session it was issued from (see the 12.10.x → 12.11.x guide, §1).
|
|
29
|
+
Better-Auth replaces the session on these routes: it creates a new one, sets its cookie and deletes the request's
|
|
30
|
+
session.
|
|
31
|
+
|
|
32
|
+
| Route | Answer in 12.11.0 | Answer since 12.11.1, where the body carries a token |
|
|
33
|
+
|---|---|---|
|
|
34
|
+
| `POST /iam/two-factor/disable` | `{ status: true }` | `{ status: true, token }` |
|
|
35
|
+
| `POST /iam/two-factor/enable` with `method: 'otp'` | `{ method: 'otp' }` | `{ method: 'otp', token }` |
|
|
36
|
+
| `POST /iam/two-factor/enable` with `skipVerificationOnEnable` | `{ method: 'totp', totpURI, backupCodes }` | the same plus `token` |
|
|
37
|
+
| `POST /iam/two-factor/verify-totp` (first confirmation while enabling) | `{ token, user }` with the JWT of the new session | unchanged |
|
|
38
|
+
|
|
39
|
+
`token` is the JWT of the new session. The body carries a token when cookies are disabled (`cookies: false`) or
|
|
40
|
+
`cookies.exposeTokenInBody` is on; in the default cookie mode the answer stays as it is, and the session cookie carries
|
|
41
|
+
the new session.
|
|
42
|
+
|
|
43
|
+
In 12.11.0 a client without a session cookie kept a JWT of the deleted session after turning 2FA off: every request
|
|
44
|
+
answered `401`, and it could not fetch a new JWT from `GET /iam/token` either. The user had to sign in again. The
|
|
45
|
+
framework's default configuration reaches only the disable route; the two enable variants need a project's own
|
|
46
|
+
`twoFactor` setup (`otpOptions.sendOTP`, `skipVerificationOnEnable`).
|
|
47
|
+
|
|
48
|
+
**Clients:** store the `token` of these answers when it is present, the same way as after `verify-totp`.
|
|
49
|
+
`@lenne.tech/nuxt-extensions` 4.0.5 and `@lenne.tech/ng-base` 22.1.3 do this. Against 12.11.0 the field is absent and
|
|
50
|
+
nothing changes.
|
|
51
|
+
|
|
52
|
+
## 2. Dependencies: MongoDB driver security fix
|
|
53
|
+
|
|
54
|
+
| Package | 12.11.0 | 12.11.1 | Why |
|
|
55
|
+
|---|---|---|---|
|
|
56
|
+
| `mongodb` (optional peer) | 7.8.0 | 7.9.1 | GHSA-779g-w5r4-wr2x (credentials from the connection string in parse errors and in logged clients), GHSA-prmg-mvj2-c4f7 (NUL bytes in SASL PLAIN credentials change the authenticated identity), GHSA-c5cf-gf4q-92vj (OIDC bearer token leak); all medium, fixed in 7.9.x only |
|
|
57
|
+
| `mongoose` (optional peer) | 9.12.3 | 9.13.0 | the first mongoose that allows `mongodb ~7.9` |
|
|
58
|
+
| `ioredis` (optional peer) | 6.0.0 | 6.1.0 | maintenance |
|
|
59
|
+
|
|
60
|
+
These packages are optional peers: the project pins them. In a nest-server-starter based project, `pnpm run update`
|
|
61
|
+
raises its `mongoose` and `mongodb` pins (and `ioredis`, where declared) to these versions. Raise `mongoose` and `mongodb`
|
|
62
|
+
together; mongoose 9.12.3 accepts only `mongodb ~7.8`, and two driver copies give two `bson` copies, which breaks
|
|
63
|
+
Better-Auth's MongoDB adapter (`instanceof ObjectId`). The peer ranges stay as they are (`mongodb >=7.8.0 <8`,
|
|
64
|
+
`mongoose >=9.12.0 <10`).
|
|
65
|
+
|
|
66
|
+
## Compatibility Notes
|
|
67
|
+
|
|
68
|
+
| Pattern | Status |
|
|
69
|
+
|---|---|
|
|
70
|
+
| Cookie mode (`cookies: true`, the default) | Unchanged |
|
|
71
|
+
| JWT mode (`cookies: false`) with a frontend library that takes the token | Stays signed in after turning 2FA off |
|
|
72
|
+
| A project's own controller that forwards to Better-Auth through `sendBetterAuthResponse(res, response)` | Works as before; pass `{ requestSessionToken }` as the third argument to get the new rule on its routes |
|
|
73
|
+
| An override of `CoreBetterAuthService.presentSessionTokens(response)` | Still called; the second argument `{ requestSessionToken }` is new and optional |
|
|
74
|
+
|
|
75
|
+
## Troubleshooting
|
|
76
|
+
|
|
77
|
+
| Symptom | Cause | Fix |
|
|
78
|
+
|---|---|---|
|
|
79
|
+
| Signed out right after turning 2FA off in JWT mode | The client keeps the JWT of the replaced session | Update to 12.11.1 and store the `token` of the disable answer |
|
|
80
|
+
| `401` right after enabling 2FA in JWT mode | The first `verify-totp` replaced the session (12.10.x → 12.11.x guide, §1) | Use the `token` of the `verify-totp` answer |
|
|
81
|
+
|
|
82
|
+
## Module Documentation
|
|
83
|
+
|
|
84
|
+
- `src/core/modules/better-auth/README.md`: "The Better-Auth JWT"
|
|
85
|
+
- `src/core/modules/better-auth/core-better-auth-session-token.helper.ts`: `presentSessionTokens()` and its rules
|
|
86
|
+
- `migration-guides/12.10.x-to-12.11.x.md`, §1: the JWT session binding
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lenne.tech/nest-server",
|
|
3
|
-
"version": "12.11.
|
|
3
|
+
"version": "12.11.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Server framework in TypeScript on top of NestJS: GraphQL and REST APIs on MongoDB, PostgreSQL, MySQL or MariaDB, with authentication, field-level permissions, multi-tenancy, offline sync and an AI assistant.",
|
|
6
6
|
"keywords": [
|
|
@@ -100,7 +100,7 @@
|
|
|
100
100
|
"node": ">= 22.15",
|
|
101
101
|
"pnpm": "^11.0.0"
|
|
102
102
|
},
|
|
103
|
-
"packageManager": "pnpm@11.
|
|
103
|
+
"packageManager": "pnpm@11.29.0+sha512.d913f9b37cd5fe6780052e809774edabb7bdcf69a6e6d9b8fd3b38bc052fa8a7d891b6c201a38fb504a07ffce2e5e8c262ef54fb8f57db62f22d353a30bc8a96",
|
|
104
104
|
"dependencies": {
|
|
105
105
|
"@apollo/server": "5.5.1",
|
|
106
106
|
"@as-integrations/express5": "1.1.2",
|
|
@@ -279,10 +279,10 @@
|
|
|
279
279
|
"bullmq": "6.7.8",
|
|
280
280
|
"cross-env": "10.1.0",
|
|
281
281
|
"drizzle-orm": "1.0.0-rc.5",
|
|
282
|
-
"ioredis": "6.
|
|
282
|
+
"ioredis": "6.1.0",
|
|
283
283
|
"kysely": "0.29.9",
|
|
284
|
-
"mongodb": "7.
|
|
285
|
-
"mongoose": "9.
|
|
284
|
+
"mongodb": "7.9.1",
|
|
285
|
+
"mongoose": "9.13.0",
|
|
286
286
|
"mysql2": "3.24.6",
|
|
287
287
|
"nodemon": "3.1.14",
|
|
288
288
|
"otpauth": "9.6.0",
|
|
@@ -505,6 +505,7 @@ assignment (README → "Sign-in through a provider alone"):
|
|
|
505
505
|
| Missing `signUpValidator` in custom Resolver (v11.13.0+) | Sign-up validation not working | Add `@Optional() signUpValidator?: CoreBetterAuthSignUpValidatorService` to constructor and pass to `super()` |
|
|
506
506
|
| Missing `termsAndPrivacyAccepted` parameter in Resolver (v11.13.0+) | GraphQL error "Unknown argument" | Add `@Args('termsAndPrivacyAccepted', { nullable: true })` to `betterAuthSignUp` method |
|
|
507
507
|
| A JWT client keeps the JWT it had before `verify-totp` while enabling 2FA (12.11.0+) | `401` right after 2FA was switched on | Use the `token` of the `verify-totp` answer: the first confirmation replaces the session, and a JWT is bound to its session |
|
|
508
|
+
| A JWT client keeps the JWT it had before `two-factor/disable` (12.11.0) | Signed out right after 2FA was switched off | Update to 12.11.1 and use the `token` of the answer: turning 2FA off replaces the session |
|
|
508
509
|
| An SSO-only project (`emailAndPassword.enabled: false`) expects Passkey or 2FA (12.11.0+) | Passkey and 2FA routes answer `404` | Switch them on deliberately with `passkey: true` / `twoFactor: true` — they keep a user signed in after the provider withdrew the assignment |
|
|
509
510
|
|
|
510
511
|
---
|
|
@@ -712,12 +712,12 @@ A project that signs in through a single sign-on provider alone (`emailAndPasswo
|
|
|
712
712
|
Microsoft Entra) expects the provider to decide who gets in. Since 12.11.0 nothing the framework offers keeps someone
|
|
713
713
|
in after the provider withdrew the assignment or role:
|
|
714
714
|
|
|
715
|
-
| What | Since 12.11.0
|
|
716
|
-
| -------------------------------------------------------------------------------------------- |
|
|
717
|
-
| Passkey and two-factor | Off while sign-in runs through a provider alone (`emailAndPassword` off and no plugin with a sign-in route of its own — `username()` / `phoneNumber()` keep both on); `passkey: true` / `twoFactor: true` (or `{ enabled: true, … }`) switches them on deliberately. Their routes answer 404, also for a passkey registered earlier. The boot names what it switched off, as a warning for a factor with settings of its own
|
|
718
|
-
| A client-submitted id token (`POST /iam/sign-in/social` / `/iam/link-social` with `idToken`) | Refused with `ID_TOKEN_NOT_SUPPORTED` for every provider unless the provider sets `disableIdTokenSignIn: false` (native apps, Google One Tap). The redirect flow is unaffected
|
|
719
|
-
| The Better-Auth JWT | Bound to the session it was issued from (claim `sid`): refused once that session is signed out, revoked, expired or its user deleted — at once on this replica and, with `redis`, on every replica; otherwise within 10 s. A JWT without `sid` (issued before 12.11.0) is refused, and the client fetches a new one with its session (`GET /iam/token`). After a session rotation (the first `verify-totp` while enabling 2FA) the answer carries the JWT of the new session | Valid until its own expiry (15 minutes) after a sign-out, and a JWT client's sign-out ended no session at all |
|
|
720
|
-
| `GET /iam/features` | Reports `emailAndPassword` (also in the GraphQL `betterAuthFeatures`), so a frontend can hide the e-mail form
|
|
715
|
+
| What | Since 12.11.0 | Before |
|
|
716
|
+
| -------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
717
|
+
| Passkey and two-factor | Off while sign-in runs through a provider alone (`emailAndPassword` off and no plugin with a sign-in route of its own — `username()` / `phoneNumber()` keep both on); `passkey: true` / `twoFactor: true` (or `{ enabled: true, … }`) switches them on deliberately. Their routes answer 404, also for a passkey registered earlier. The boot names what it switched off, as a warning for a factor with settings of its own | On: a passkey registered once after a provider sign-in kept signing its owner in |
|
|
718
|
+
| A client-submitted id token (`POST /iam/sign-in/social` / `/iam/link-social` with `idToken`) | Refused with `ID_TOKEN_NOT_SUPPORTED` for every provider unless the provider sets `disableIdTokenSignIn: false` (native apps, Google One Tap). The redirect flow is unaffected | Accepted: an id token kept from an earlier sign-in (`/iam/get-access-token` hands it out) signed its owner in again for about an hour, without asking the provider |
|
|
719
|
+
| The Better-Auth JWT | Bound to the session it was issued from (claim `sid`): refused once that session is signed out, revoked, expired or its user deleted — at once on this replica and, with `redis`, on every replica; otherwise within 10 s. A JWT without `sid` (issued before 12.11.0) is refused, and the client fetches a new one with its session (`GET /iam/token`). After a session rotation (the first `verify-totp` while enabling 2FA; since 12.11.1 also `two-factor/disable` and enabling with OTP) the answer carries the JWT of the new session | Valid until its own expiry (15 minutes) after a sign-out, and a JWT client's sign-out ended no session at all |
|
|
720
|
+
| `GET /iam/features` | Reports `emailAndPassword` (also in the GraphQL `betterAuthFeatures`), so a frontend can hide the e-mail form | — |
|
|
721
721
|
|
|
722
722
|
A session that is not extended while it is used ends a fixed time after the sign-in, so a withdrawal at the provider
|
|
723
723
|
reaches it at the latest then:
|
|
@@ -504,7 +504,9 @@ export class CoreBetterAuthApiMiddleware implements NestMiddleware {
|
|
|
504
504
|
this.logger.debug(`Stored challenge mapping with ID: ${challengeId.substring(0, 8)}...`);
|
|
505
505
|
|
|
506
506
|
// Send the enhanced response
|
|
507
|
-
await this.betterAuthService.sendBetterAuthResponse(res, enhancedResponse
|
|
507
|
+
await this.betterAuthService.sendBetterAuthResponse(res, enhancedResponse, {
|
|
508
|
+
requestSessionToken: sessionToken,
|
|
509
|
+
});
|
|
508
510
|
|
|
509
511
|
return;
|
|
510
512
|
} else {
|
|
@@ -565,7 +567,9 @@ export class CoreBetterAuthApiMiddleware implements NestMiddleware {
|
|
|
565
567
|
});
|
|
566
568
|
|
|
567
569
|
this.logger.debug('Enriched passkey verify response with user data');
|
|
568
|
-
await this.betterAuthService.sendBetterAuthResponse(res, enrichedResponse
|
|
570
|
+
await this.betterAuthService.sendBetterAuthResponse(res, enrichedResponse, {
|
|
571
|
+
requestSessionToken: sessionToken,
|
|
572
|
+
});
|
|
569
573
|
return;
|
|
570
574
|
}
|
|
571
575
|
}
|
|
@@ -585,7 +589,7 @@ export class CoreBetterAuthApiMiddleware implements NestMiddleware {
|
|
|
585
589
|
|
|
586
590
|
// Convert Web Standard Response to Express response, through the token policy (12.6.1): a forwarded route must
|
|
587
591
|
// not hand out what the framework's own sign-in would not
|
|
588
|
-
await this.betterAuthService.sendBetterAuthResponse(res, response);
|
|
592
|
+
await this.betterAuthService.sendBetterAuthResponse(res, response, { requestSessionToken: sessionToken });
|
|
589
593
|
} catch (error) {
|
|
590
594
|
// A refused request (the password length policy) is an answer, not a handler failure. Nest's middleware
|
|
591
595
|
// proxy hands a thrown exception to the exception filters, so it is rendered like the same refusal from a
|
|
@@ -18,6 +18,14 @@ export interface SessionTokenPresentation {
|
|
|
18
18
|
* (`CoreBetterAuthService.resolveJwtToken()`), the token itself when it is off, `undefined` when a conversion failed
|
|
19
19
|
*/
|
|
20
20
|
resolveJwt: (sessionToken: string) => Promise<string | undefined>;
|
|
21
|
+
/**
|
|
22
|
+
* The session token the request was made with: its session cookie, a bearer session token, or the session of its
|
|
23
|
+
* JWT (`getJwtSession()`). It tells a rotation from a refresh in an answer that names no token itself: a session
|
|
24
|
+
* cookie with ANOTHER token replaced the request's session (`/two-factor/disable` answers `{ status: true }`,
|
|
25
|
+
* `/two-factor/enable` with OTP `{ method: 'otp' }`), a refresh sets the cookie of the same session (12.11.1).
|
|
26
|
+
* Unset or empty: such answers stay as they are.
|
|
27
|
+
*/
|
|
28
|
+
requestSessionToken?: null | string;
|
|
21
29
|
/**
|
|
22
30
|
* Whether the body may carry a token at all: cookies disabled (JWT-only mode) or `cookies.exposeTokenInBody`.
|
|
23
31
|
* `false` in the default cookie mode, where the session lives in an httpOnly cookie and nowhere else.
|
|
@@ -76,6 +84,10 @@ export function sessionTokenFromSetCookie(response: globalThis.Response, cookieN
|
|
|
76
84
|
* rotated the session (the first verify-totp while enabling 2FA answers with the token of the session it deleted):
|
|
77
85
|
* that token is treated as the session token — removed in cookie mode, replaced by the JWT of the NEW session where
|
|
78
86
|
* the body carries one (12.7.1). A plugin's own `{ token }` next to a refreshed session cookie is not a rotation.
|
|
87
|
+
* - A response without a top-level token that sets a session cookie for another session than the request's
|
|
88
|
+
* ({@link SessionTokenPresentation.requestSessionToken}) replaced the request's session: two-factor disable, and
|
|
89
|
+
* enable with OTP or `skipVerificationOnEnable`. Where the body carries a token it gains the JWT of the new session;
|
|
90
|
+
* the JWT the client holds names the deleted one, and without a session cookie it cannot fetch another (12.11.1).
|
|
79
91
|
* - Untouched: error responses, non-JSON bodies, a JWT (`/token` hands one out on purpose), and a top-level `token`
|
|
80
92
|
* that is not the session token (another plugin's value).
|
|
81
93
|
*
|
|
@@ -125,6 +137,11 @@ export async function presentSessionTokens(
|
|
|
125
137
|
const answersWithUser = !!record.user && typeof record.user === 'object' && !Array.isArray(record.user);
|
|
126
138
|
const rotatedFrom =
|
|
127
139
|
answersWithUser && !!topToken && !isJwtShaped(topToken) && !!cookieToken && topToken !== cookieToken;
|
|
140
|
+
// An answer naming no token that replaced the REQUEST's session (12.11.1): two-factor disable answers
|
|
141
|
+
// `{ status: true }`, the client's JWT names the session that was just deleted. A refresh sets the cookie of the
|
|
142
|
+
// request's own session, so only another token counts.
|
|
143
|
+
const requestToken = typeof options.requestSessionToken === 'string' ? options.requestSessionToken : '';
|
|
144
|
+
const replacedRequestSession = !topToken && !!requestToken && !!cookieToken && cookieToken !== requestToken;
|
|
128
145
|
const topIsSessionToken =
|
|
129
146
|
!!topToken &&
|
|
130
147
|
!isJwtShaped(topToken) &&
|
|
@@ -133,7 +150,7 @@ export async function presentSessionTokens(
|
|
|
133
150
|
topToken === sessionFieldToken ||
|
|
134
151
|
(await isLiveSessionToken(topToken, options.isSessionToken)));
|
|
135
152
|
|
|
136
|
-
if (!topIsSessionToken && !sessionFieldToken) {
|
|
153
|
+
if (!topIsSessionToken && !sessionFieldToken && !(options.tokenInBody && replacedRequestSession)) {
|
|
137
154
|
return response;
|
|
138
155
|
}
|
|
139
156
|
|
|
@@ -1171,7 +1171,7 @@ export class CoreBetterAuthController {
|
|
|
1171
1171
|
|
|
1172
1172
|
// Send the response back
|
|
1173
1173
|
// Through the token policy: a forwarded route must not hand out what sign-in would not (12.6.1)
|
|
1174
|
-
await this.betterAuthService.sendBetterAuthResponse(res, response);
|
|
1174
|
+
await this.betterAuthService.sendBetterAuthResponse(res, response, { requestSessionToken: sessionToken });
|
|
1175
1175
|
} catch (error) {
|
|
1176
1176
|
this.logger.error(`Better Auth handler error: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
1177
1177
|
|
|
@@ -828,13 +828,21 @@ export class CoreBetterAuthService implements OnApplicationBootstrap, OnModuleDe
|
|
|
828
828
|
* framework's own sign-in keeps it in an httpOnly cookie only, and as the raw token where sign-in hands out a JWT.
|
|
829
829
|
* See `presentSessionTokens()` (`core-better-auth-session-token.helper.ts`) for the rules.
|
|
830
830
|
*
|
|
831
|
+
* `requestSessionToken` is the session token the request was forwarded with (12.11.1): an answer that replaced that
|
|
832
|
+
* session without naming a token (two-factor disable) then carries the JWT of the new session where the body may carry
|
|
833
|
+
* a token.
|
|
834
|
+
*
|
|
831
835
|
* Override to present tokens differently; keep the cookie-mode rule, it is what keeps the session out of scripts.
|
|
832
836
|
*/
|
|
833
|
-
async presentSessionTokens(
|
|
837
|
+
async presentSessionTokens(
|
|
838
|
+
response: globalThis.Response,
|
|
839
|
+
options?: { requestSessionToken?: null | string },
|
|
840
|
+
): Promise<globalThis.Response> {
|
|
834
841
|
const cookiesConfig = ConfigService.configFastButReadOnly?.cookies as boolean | ICookiesConfig | undefined;
|
|
835
842
|
return presentSessionTokens(response, {
|
|
836
843
|
cookieName: await this.sessionCookieNameOfInstance(),
|
|
837
844
|
isSessionToken: async (token) => !!(await this.getSessionByToken(token))?.session,
|
|
845
|
+
requestSessionToken: options?.requestSessionToken,
|
|
838
846
|
resolveJwt: (sessionToken) => this.resolveJwtToken(sessionToken),
|
|
839
847
|
tokenInBody: !isCookiesEnabled(cookiesConfig) || isExposeTokenInBodyEnabled(cookiesConfig),
|
|
840
848
|
});
|
|
@@ -861,10 +869,15 @@ export class CoreBetterAuthService implements OnApplicationBootstrap, OnModuleDe
|
|
|
861
869
|
/**
|
|
862
870
|
* Sends a response of Better-Auth's handler to the client, through {@link presentSessionTokens}. Every place that
|
|
863
871
|
* forwards to the handler (the API middleware, the controller's catch-all) sends through here, so a forwarded route
|
|
864
|
-
* cannot hand a client a token the framework's own sign-in would not.
|
|
872
|
+
* cannot hand a client a token the framework's own sign-in would not. Pass the session token the request was
|
|
873
|
+
* forwarded with (`requestSessionToken`), so an answer that replaced that session carries the new one's JWT (12.11.1).
|
|
865
874
|
*/
|
|
866
|
-
async sendBetterAuthResponse(
|
|
867
|
-
|
|
875
|
+
async sendBetterAuthResponse(
|
|
876
|
+
res: ExpressResponse,
|
|
877
|
+
response: globalThis.Response,
|
|
878
|
+
options?: { requestSessionToken?: null | string },
|
|
879
|
+
): Promise<void> {
|
|
880
|
+
await sendWebResponse(res, await this.presentSessionTokens(response, options));
|
|
868
881
|
}
|
|
869
882
|
|
|
870
883
|
/**
|