@lenne.tech/nest-server 12.6.0 → 12.6.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/.claude/rules/better-auth.md +36 -0
- package/.claude/rules/testing.md +4 -4
- package/FRAMEWORK-API.md +1 -1
- package/dist/core/modules/better-auth/core-better-auth-api.middleware.d.ts +1 -0
- package/dist/core/modules/better-auth/core-better-auth-api.middleware.js +22 -4
- 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 +8 -0
- package/dist/core/modules/better-auth/core-better-auth-session-token.helper.js +115 -0
- package/dist/core/modules/better-auth/core-better-auth-session-token.helper.js.map +1 -0
- package/dist/core/modules/better-auth/core-better-auth.controller.js +2 -2
- 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 +5 -0
- package/dist/core/modules/better-auth/core-better-auth.service.js +42 -2
- package/dist/core/modules/better-auth/core-better-auth.service.js.map +1 -1
- package/docs/REQUEST-LIFECYCLE.md +14 -0
- package/migration-guides/12.6.0-to-12.6.1.md +66 -0
- package/migration-guides/12.6.1-to-12.6.2.md +79 -0
- package/package.json +1 -1
- package/src/core/modules/better-auth/core-better-auth-api.middleware.ts +39 -5
- package/src/core/modules/better-auth/core-better-auth-session-token.helper.ts +194 -0
- package/src/core/modules/better-auth/core-better-auth.controller.ts +3 -2
- package/src/core/modules/better-auth/core-better-auth.service.ts +89 -2
|
@@ -16,10 +16,15 @@ import {
|
|
|
16
16
|
sessionMiddleware,
|
|
17
17
|
} from 'better-auth/api';
|
|
18
18
|
import { Request } from 'express';
|
|
19
|
+
import type { Response as ExpressResponse } from 'express';
|
|
19
20
|
import { importJWK, jwtVerify } from 'jose';
|
|
20
21
|
import type { Connection } from 'mongoose';
|
|
21
22
|
|
|
22
|
-
import {
|
|
23
|
+
import {
|
|
24
|
+
isCookiesEnabled,
|
|
25
|
+
isExposeTokenInBodyEnabled,
|
|
26
|
+
shouldConvertSessionTokenToJwt,
|
|
27
|
+
} from '../../common/helpers/cookies.helper.js';
|
|
23
28
|
import { type ExpirySweep, scheduleExpirySweep } from '../../common/persistence/expiry-sweep.js';
|
|
24
29
|
import { maskEmail, maskToken } from '../../common/helpers/logging.helper.js';
|
|
25
30
|
import { type IBetterAuth, ICookiesConfig } from '../../common/interfaces/server-options.interface.js';
|
|
@@ -38,7 +43,13 @@ import {
|
|
|
38
43
|
import type { IBetterAuthOpenApiDocument, IBetterAuthRuntime } from './core-better-auth-openapi.helper.js';
|
|
39
44
|
import { isJwtShaped } from './core-better-auth-token.helper.js';
|
|
40
45
|
import { BetterAuthSessionUser } from './core-better-auth-user.mapper.js';
|
|
41
|
-
import {
|
|
46
|
+
import { presentSessionTokens } from './core-better-auth-session-token.helper.js';
|
|
47
|
+
import {
|
|
48
|
+
convertExpressHeaders,
|
|
49
|
+
parseCookieHeader,
|
|
50
|
+
sendWebResponse,
|
|
51
|
+
signCookieValueIfNeeded,
|
|
52
|
+
} from './core-better-auth-web.helper.js';
|
|
42
53
|
import {
|
|
43
54
|
ACCOUNT_ISSUER_BACKFILL_ID,
|
|
44
55
|
BACKFILL_MARKER_COLLECTION,
|
|
@@ -804,6 +815,53 @@ export class CoreBetterAuthService implements OnApplicationBootstrap, OnModuleDe
|
|
|
804
815
|
return `${this.getCookiePrefix()}.session_token`;
|
|
805
816
|
}
|
|
806
817
|
|
|
818
|
+
/**
|
|
819
|
+
* Applies the sign-in token policy to a response Better-Auth's own handler produced (12.6.1).
|
|
820
|
+
*
|
|
821
|
+
* Routes the framework forwards to Better-Auth — two-factor verify, passkey verify, OTP and magic-link sign-in,
|
|
822
|
+
* get-session — answered with the opaque session token in the body, also in the default cookie mode, where the
|
|
823
|
+
* framework's own sign-in keeps it in an httpOnly cookie only, and as the raw token where sign-in hands out a JWT.
|
|
824
|
+
* See `presentSessionTokens()` (`core-better-auth-session-token.helper.ts`) for the rules.
|
|
825
|
+
*
|
|
826
|
+
* Override to present tokens differently; keep the cookie-mode rule, it is what keeps the session out of scripts.
|
|
827
|
+
*/
|
|
828
|
+
async presentSessionTokens(response: globalThis.Response): Promise<globalThis.Response> {
|
|
829
|
+
const cookiesConfig = ConfigService.configFastButReadOnly?.cookies as boolean | ICookiesConfig | undefined;
|
|
830
|
+
return presentSessionTokens(response, {
|
|
831
|
+
cookieName: await this.sessionCookieNameOfInstance(),
|
|
832
|
+
isSessionToken: async (token) => !!(await this.getSessionByToken(token))?.session,
|
|
833
|
+
resolveJwt: (sessionToken) => this.resolveJwtToken(sessionToken),
|
|
834
|
+
tokenInBody: !isCookiesEnabled(cookiesConfig) || isExposeTokenInBodyEnabled(cookiesConfig),
|
|
835
|
+
});
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
/**
|
|
839
|
+
* The session cookie's name as the running Better-Auth instance sets it — `__Secure-` prefixed under
|
|
840
|
+
* `advanced.useSecureCookies`, renamed under `advanced.cookies.session_token.name` — falling back to
|
|
841
|
+
* {@link getSessionCookieName} when the instance cannot say.
|
|
842
|
+
*/
|
|
843
|
+
protected async sessionCookieNameOfInstance(): Promise<string> {
|
|
844
|
+
try {
|
|
845
|
+
const context = await (this.getInstance() as { $context?: Promise<any> } | null)?.$context;
|
|
846
|
+
const name = context?.authCookies?.sessionToken?.name;
|
|
847
|
+
if (typeof name === 'string' && name) {
|
|
848
|
+
return name;
|
|
849
|
+
}
|
|
850
|
+
} catch {
|
|
851
|
+
// The configured name below is what the framework writes itself
|
|
852
|
+
}
|
|
853
|
+
return this.getSessionCookieName();
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
/**
|
|
857
|
+
* Sends a response of Better-Auth's handler to the client, through {@link presentSessionTokens}. Every place that
|
|
858
|
+
* forwards to the handler (the API middleware, the controller's catch-all) sends through here, so a forwarded route
|
|
859
|
+
* cannot hand a client a token the framework's own sign-in would not.
|
|
860
|
+
*/
|
|
861
|
+
async sendBetterAuthResponse(res: ExpressResponse, response: globalThis.Response): Promise<void> {
|
|
862
|
+
await sendWebResponse(res, await this.presentSessionTokens(response));
|
|
863
|
+
}
|
|
864
|
+
|
|
807
865
|
/**
|
|
808
866
|
* Gets the cookie prefix (the `iam` in `iam.session_token`).
|
|
809
867
|
*
|
|
@@ -1013,6 +1071,35 @@ export class CoreBetterAuthService implements OnApplicationBootstrap, OnModuleDe
|
|
|
1013
1071
|
}
|
|
1014
1072
|
}
|
|
1015
1073
|
|
|
1074
|
+
/**
|
|
1075
|
+
* The token of the session `sessionId` — only when that session belongs to `userId` (12.6.2).
|
|
1076
|
+
*
|
|
1077
|
+
* `list-sessions` hands out session ids, no tokens, so a client revokes a session by its id; the API middleware asks
|
|
1078
|
+
* this to turn `POST /revoke-session { sessionId }` into the `{ token }` Better-Auth expects. Asked through
|
|
1079
|
+
* Better-Auth's own adapter (`internalAdapter.listSessions()`), so it reads wherever Better-Auth keeps its sessions.
|
|
1080
|
+
*
|
|
1081
|
+
* @returns the token, or `undefined` for an unknown id, a session of another user, or a failed lookup
|
|
1082
|
+
*/
|
|
1083
|
+
async sessionTokenOfUser(userId: string, sessionId: string): Promise<string | undefined> {
|
|
1084
|
+
if (!userId || !sessionId) {
|
|
1085
|
+
return undefined;
|
|
1086
|
+
}
|
|
1087
|
+
try {
|
|
1088
|
+
const context = await (this.getInstance() as { $context?: Promise<any> } | null)?.$context;
|
|
1089
|
+
// Active sessions only — the query Better-Auth's own list-sessions runs. Without the filter the adapter's default
|
|
1090
|
+
// cap (100 rows, oldest first) could hide every current session behind expired rows MongoDB never removes.
|
|
1091
|
+
const sessions: unknown = await context?.internalAdapter?.listSessions?.(userId, { onlyActiveSessions: true });
|
|
1092
|
+
const match = (Array.isArray(sessions) ? sessions : []).find(
|
|
1093
|
+
(session: { id?: unknown; userId?: unknown }) =>
|
|
1094
|
+
String(session?.id) === sessionId && String(session?.userId) === String(userId),
|
|
1095
|
+
);
|
|
1096
|
+
return typeof match?.token === 'string' && match.token ? match.token : undefined;
|
|
1097
|
+
} catch (error) {
|
|
1098
|
+
this.logger.debug(`sessionTokenOfUser error: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
1099
|
+
return undefined;
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
|
|
1016
1103
|
/**
|
|
1017
1104
|
* Revokes a specific session (logout for that session)
|
|
1018
1105
|
*
|