@lenne.tech/nest-server 12.10.1 → 12.11.0
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 +19 -0
- package/.claude/rules/configurable-features.md +11 -9
- package/.claude/rules/package-management.md +1 -1
- package/.claude/rules/role-system.md +4 -1
- package/.claude/rules/testing.md +4 -4
- package/CLAUDE.md +5 -0
- package/FRAMEWORK-API.md +2 -1
- package/dist/core/common/decorators/unified-field.decorator.js +5 -2
- package/dist/core/common/decorators/unified-field.decorator.js.map +1 -1
- package/dist/core/common/helpers/config.helper.js +31 -9
- package/dist/core/common/helpers/config.helper.js.map +1 -1
- package/dist/core/common/helpers/graceful-shutdown.helper.js +3 -1
- package/dist/core/common/helpers/graceful-shutdown.helper.js.map +1 -1
- package/dist/core/common/helpers/graphql-ws-connection.helper.d.ts +8 -3
- package/dist/core/common/helpers/graphql-ws-connection.helper.js +44 -0
- package/dist/core/common/helpers/graphql-ws-connection.helper.js.map +1 -1
- package/dist/core/common/helpers/http-stream-auth.helper.d.ts +13 -0
- package/dist/core/common/helpers/http-stream-auth.helper.js +90 -0
- package/dist/core/common/helpers/http-stream-auth.helper.js.map +1 -0
- package/dist/core/common/helpers/shutdown-state.helper.d.ts +3 -0
- package/dist/core/common/helpers/shutdown-state.helper.js +11 -0
- package/dist/core/common/helpers/shutdown-state.helper.js.map +1 -0
- package/dist/core/common/interfaces/cron-job-config-with-utc-offset.interface.d.ts +1 -1
- package/dist/core/common/interfaces/cron-job-config.interface.d.ts +1 -1
- package/dist/core/common/interfaces/server-options.interface.d.ts +14 -0
- package/dist/core/common/services/core-cron-jobs.service.d.ts +13 -0
- package/dist/core/common/services/core-cron-jobs.service.js +129 -4
- package/dist/core/common/services/core-cron-jobs.service.js.map +1 -1
- package/dist/core/modules/better-auth/better-auth.config.js +48 -4
- package/dist/core/modules/better-auth/better-auth.config.js.map +1 -1
- package/dist/core/modules/better-auth/core-better-auth-jwt-session.cache.d.ts +8 -0
- package/dist/core/modules/better-auth/core-better-auth-jwt-session.cache.js +47 -0
- package/dist/core/modules/better-auth/core-better-auth-jwt-session.cache.js.map +1 -0
- package/dist/core/modules/better-auth/core-better-auth-models.d.ts +1 -0
- package/dist/core/modules/better-auth/core-better-auth-models.js +7 -0
- package/dist/core/modules/better-auth/core-better-auth-models.js.map +1 -1
- package/dist/core/modules/better-auth/core-better-auth-sign-in-factors.helper.d.ts +11 -0
- package/dist/core/modules/better-auth/core-better-auth-sign-in-factors.helper.js +62 -0
- package/dist/core/modules/better-auth/core-better-auth-sign-in-factors.helper.js.map +1 -0
- package/dist/core/modules/better-auth/core-better-auth-user.mapper.d.ts +1 -0
- package/dist/core/modules/better-auth/core-better-auth-user.mapper.js +28 -1
- package/dist/core/modules/better-auth/core-better-auth-user.mapper.js.map +1 -1
- package/dist/core/modules/better-auth/core-better-auth.controller.js +2 -1
- package/dist/core/modules/better-auth/core-better-auth.controller.js.map +1 -1
- package/dist/core/modules/better-auth/core-better-auth.middleware.js +2 -2
- package/dist/core/modules/better-auth/core-better-auth.middleware.js.map +1 -1
- package/dist/core/modules/better-auth/core-better-auth.resolver.js +1 -0
- package/dist/core/modules/better-auth/core-better-auth.resolver.js.map +1 -1
- package/dist/core/modules/better-auth/core-better-auth.service.d.ts +10 -0
- package/dist/core/modules/better-auth/core-better-auth.service.js +105 -19
- package/dist/core/modules/better-auth/core-better-auth.service.js.map +1 -1
- package/dist/core/modules/health-check/core-health-check-readiness.controller.d.ts +6 -0
- package/dist/core/modules/health-check/core-health-check-readiness.controller.js +36 -0
- package/dist/core/modules/health-check/core-health-check-readiness.controller.js.map +1 -0
- package/dist/core/modules/health-check/core-health-check.helpers.d.ts +10 -0
- package/dist/core/modules/health-check/core-health-check.helpers.js +31 -0
- package/dist/core/modules/health-check/core-health-check.helpers.js.map +1 -0
- package/dist/core/modules/health-check/core-health-check.module.d.ts +9 -0
- package/dist/core/modules/health-check/core-health-check.module.js +13 -2
- package/dist/core/modules/health-check/core-health-check.module.js.map +1 -1
- package/dist/core/modules/health-check/core-health-check.service.d.ts +8 -1
- package/dist/core/modules/health-check/core-health-check.service.js +66 -29
- package/dist/core/modules/health-check/core-health-check.service.js.map +1 -1
- package/dist/core/modules/sync/core-sync.service.d.ts +2 -0
- package/dist/core/modules/sync/core-sync.service.js +11 -1
- package/dist/core/modules/sync/core-sync.service.js.map +1 -1
- package/dist/core.module.js +5 -1
- package/dist/core.module.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/main.js.map +1 -1
- package/docs/REQUEST-LIFECYCLE.md +4 -4
- package/docs/offline-sync-protocol.md +5 -3
- package/migration-guides/12.10.x-to-12.11.x.md +295 -0
- package/package.json +17 -17
- package/src/core/common/decorators/unified-field.decorator.ts +14 -4
- package/src/core/common/helpers/config.helper.ts +55 -8
- package/src/core/common/helpers/graceful-shutdown.helper.ts +10 -3
- package/src/core/common/helpers/graphql-ws-connection.helper.ts +99 -4
- package/src/core/common/helpers/http-stream-auth.helper.ts +197 -0
- package/src/core/common/helpers/shutdown-state.helper.ts +28 -0
- package/src/core/common/interfaces/cron-job-config-with-utc-offset.interface.ts +1 -1
- package/src/core/common/interfaces/cron-job-config.interface.ts +1 -1
- package/src/core/common/interfaces/server-options.interface.ts +113 -1
- package/src/core/common/services/core-cron-jobs.service.ts +241 -14
- package/src/core/modules/better-auth/INTEGRATION-CHECKLIST.md +26 -10
- package/src/core/modules/better-auth/README.md +37 -8
- package/src/core/modules/better-auth/better-auth.config.ts +83 -12
- package/src/core/modules/better-auth/core-better-auth-api.middleware.ts +1 -1
- package/src/core/modules/better-auth/core-better-auth-jwt-session.cache.ts +104 -0
- package/src/core/modules/better-auth/core-better-auth-models.ts +5 -0
- package/src/core/modules/better-auth/core-better-auth-sign-in-factors.helper.ts +105 -0
- package/src/core/modules/better-auth/core-better-auth-user.mapper.ts +37 -1
- package/src/core/modules/better-auth/core-better-auth.controller.ts +6 -3
- package/src/core/modules/better-auth/core-better-auth.middleware.ts +4 -3
- package/src/core/modules/better-auth/core-better-auth.module.ts +1 -1
- package/src/core/modules/better-auth/core-better-auth.resolver.ts +1 -0
- package/src/core/modules/better-auth/core-better-auth.service.ts +160 -27
- package/src/core/modules/health-check/core-health-check-readiness.controller.ts +27 -0
- package/src/core/modules/health-check/core-health-check.helpers.ts +65 -0
- package/src/core/modules/health-check/core-health-check.module.ts +37 -2
- package/src/core/modules/health-check/core-health-check.service.ts +154 -59
- package/src/core/modules/sync/README.md +18 -6
- package/src/core/modules/sync/core-sync.service.ts +35 -6
- package/src/core.module.ts +9 -1
- package/src/index.ts +4 -0
- package/src/main.ts +2 -1
|
@@ -33,6 +33,12 @@ import { getTenantContextResolver, ResolvedTenantContext } from '../services/cor
|
|
|
33
33
|
* WHAT IT DOES NOT DO: an event is checked AFTER graphql-js executed it and BEFORE it is sent. Reads
|
|
34
34
|
* made while computing a dropped event still happened — under the old context — but nothing of them
|
|
35
35
|
* reaches the client. Subscription resolvers that WRITE are outside what this can take back.
|
|
36
|
+
*
|
|
37
|
+
* HTTP STREAMS (Server-Sent Events) are registered the same way, with `endsOnRecheck`: they end on
|
|
38
|
+
* every re-check and the client's reconnect passes the guards again. Since 12.11.0
|
|
39
|
+
* `recheckHttpStreamInPlace()` gives a stream whose credential can be re-run the WebSocket semantics
|
|
40
|
+
* instead — re-checked in place, closed only when the rights changed (`registerHttpStream()` in
|
|
41
|
+
* `http-stream-auth.helper.ts` registers a stream that way in one call).
|
|
36
42
|
*/
|
|
37
43
|
|
|
38
44
|
/**
|
|
@@ -90,6 +96,10 @@ export interface IWsConnectionRegistration {
|
|
|
90
96
|
* it, and the client's reconnect passes the guards again. The end the re-check interval brings is planned and logged
|
|
91
97
|
* at debug; one an invalidation brings (sign-out, role or tenant change) is logged like every withdrawn
|
|
92
98
|
* authorization. `reauthenticate` is not called.
|
|
99
|
+
*
|
|
100
|
+
* Since 12.11.0 `recheckHttpStreamInPlace()` switches such a stream to the WebSocket semantics once its credential
|
|
101
|
+
* can be re-run (a Better-Auth session cookie or bearer token: `createHttpStreamReauthenticator()`): it then stays
|
|
102
|
+
* open while the rights are unchanged and is closed only when they changed. `registerHttpStream()` does both steps.
|
|
93
103
|
*/
|
|
94
104
|
endsOnRecheck?: boolean;
|
|
95
105
|
|
|
@@ -118,14 +128,19 @@ export interface IWsConnectionIdentity {
|
|
|
118
128
|
export interface WsConnection {
|
|
119
129
|
readonly channel: string;
|
|
120
130
|
readonly email?: string;
|
|
121
|
-
|
|
131
|
+
/** Writable only for `recheckHttpStreamInPlace()`, which switches an HTTP stream to the in-place re-check. */
|
|
132
|
+
endsOnRecheck: boolean;
|
|
122
133
|
readonly iamId?: string;
|
|
123
|
-
|
|
134
|
+
/** Writable only for `recheckHttpStreamInPlace()`. */
|
|
135
|
+
reauthenticate?: () => any;
|
|
124
136
|
readonly socket?: IWsConnectionSocket;
|
|
125
137
|
/** The tenant scope the connection's operations resolved, once one has. */
|
|
126
138
|
tenant?: ResolvedTenantContext;
|
|
127
|
-
/**
|
|
128
|
-
|
|
139
|
+
/**
|
|
140
|
+
* The tenant header the client sent — constant for the life of the connection. Writable only for
|
|
141
|
+
* `recheckHttpStreamInPlace()`, which takes it from the HTTP request rather than from the carrier.
|
|
142
|
+
*/
|
|
143
|
+
tenantHeader?: string;
|
|
129
144
|
terminated: boolean;
|
|
130
145
|
readonly user: any;
|
|
131
146
|
readonly userFingerprint: string;
|
|
@@ -375,6 +390,86 @@ export function noteWsConnectionTenant(connection: WsConnection, tenant: Resolve
|
|
|
375
390
|
return true;
|
|
376
391
|
}
|
|
377
392
|
|
|
393
|
+
/** Options of {@link recheckHttpStreamInPlace}. */
|
|
394
|
+
export interface IHttpStreamRecheckOptions {
|
|
395
|
+
/**
|
|
396
|
+
* Re-run the credential the stream was opened with — the same authentication the request passed. Resolves to the
|
|
397
|
+
* user the credential authenticates as NOW, or to a falsy value when it no longer does; a rejection counts as falsy.
|
|
398
|
+
* `createHttpStreamReauthenticator()` builds one for a Better-Auth session cookie or bearer token.
|
|
399
|
+
*/
|
|
400
|
+
reauthenticate: () => any;
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* The tenant header the HTTP request carried (`readWsTenantHeader(request)`). It is validated against an active
|
|
404
|
+
* membership again on every re-check. Default: what the carrier held at registration.
|
|
405
|
+
*/
|
|
406
|
+
tenantHeader?: string;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* Let an HTTP stream registered with `endsOnRecheck` (Server-Sent Events) be re-checked IN PLACE, the way a GraphQL
|
|
411
|
+
* WebSocket is (12.11.0).
|
|
412
|
+
*
|
|
413
|
+
* Until then such a stream ended on the first event after `graphQl.subscriptionRevalidationMs` and on every
|
|
414
|
+
* invalidation, and the client got that event only after its reconnect (an EventSource waits its `retry`, ~3 s).
|
|
415
|
+
* Switched, the re-check re-runs the credential (`reauthenticate`) and the tenant resolution and compares user id,
|
|
416
|
+
* roles, verification and tenant scope with what the stream was authorized with: unchanged, the stream stays open and
|
|
417
|
+
* the event is delivered; changed — in either direction — the stream is closed and the client reconnects with the
|
|
418
|
+
* rights it holds now.
|
|
419
|
+
*
|
|
420
|
+
* The switch is made only when the credential, run once NOW, answers the user the guards authorized the stream for
|
|
421
|
+
* with the same rights and — when the stream recorded its tenant scope (`noteWsConnectionTenant()`, record it BEFORE
|
|
422
|
+
* calling this) — the same tenant scope. Otherwise nothing can tell a later difference from a different shape of the
|
|
423
|
+
* same user, and the stream keeps ending on every re-check (logged at debug). Answers whether the stream is re-checked
|
|
424
|
+
* in place now.
|
|
425
|
+
*/
|
|
426
|
+
export async function recheckHttpStreamInPlace(
|
|
427
|
+
connection: undefined | WsConnection,
|
|
428
|
+
options: IHttpStreamRecheckOptions,
|
|
429
|
+
): Promise<boolean> {
|
|
430
|
+
if (!connection?.endsOnRecheck || connection.terminated || typeof options?.reauthenticate !== 'function') {
|
|
431
|
+
return false;
|
|
432
|
+
}
|
|
433
|
+
const keepsEnding = (reason: string): false => {
|
|
434
|
+
logger.debug(`${connection.channel} of user ${connection.userId ?? '(unknown)'} ends on every re-check: ${reason}`);
|
|
435
|
+
return false;
|
|
436
|
+
};
|
|
437
|
+
const tenantHeader = options.tenantHeader ?? connection.tenantHeader;
|
|
438
|
+
let user: any;
|
|
439
|
+
try {
|
|
440
|
+
user = await options.reauthenticate();
|
|
441
|
+
} catch {
|
|
442
|
+
user = undefined;
|
|
443
|
+
}
|
|
444
|
+
if (!user) {
|
|
445
|
+
return keepsEnding('its credential cannot be re-run');
|
|
446
|
+
}
|
|
447
|
+
if (fingerprintUser(user) !== connection.userFingerprint) {
|
|
448
|
+
return keepsEnding('the re-run credential does not answer the authorized user with the same rights');
|
|
449
|
+
}
|
|
450
|
+
if (connection.tenantFingerprint !== undefined) {
|
|
451
|
+
let tenant: ResolvedTenantContext;
|
|
452
|
+
try {
|
|
453
|
+
const resolver = getTenantContextResolver();
|
|
454
|
+
tenant = resolver ? await resolver.resolve(user, tenantHeader) : {};
|
|
455
|
+
} catch {
|
|
456
|
+
return keepsEnding('its tenant scope cannot be resolved again');
|
|
457
|
+
}
|
|
458
|
+
if (fingerprintTenant(tenant) !== connection.tenantFingerprint) {
|
|
459
|
+
return keepsEnding('its tenant scope resolves differently');
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
// An invalidation while the credential ran has ended the stream (it still ended on every re-check): nothing to switch
|
|
463
|
+
if (connection.terminated || connection.stale) {
|
|
464
|
+
return false;
|
|
465
|
+
}
|
|
466
|
+
connection.reauthenticate = options.reauthenticate;
|
|
467
|
+
connection.tenantHeader = tenantHeader;
|
|
468
|
+
connection.validatedAt = Date.now();
|
|
469
|
+
connection.endsOnRecheck = false;
|
|
470
|
+
return true;
|
|
471
|
+
}
|
|
472
|
+
|
|
378
473
|
/** Mark a connection for an immediate re-check and start it. */
|
|
379
474
|
function markStale(connection: WsConnection): void {
|
|
380
475
|
connection.stale = true;
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { hasApiTokenCredential } from '../../modules/api-token/core-api-token.helpers.js';
|
|
2
|
+
import { isLegacyJwt } from '../../modules/better-auth/core-better-auth-token.helper.js';
|
|
3
|
+
import { extractSessionToken, parseCookieHeader } from '../../modules/better-auth/core-better-auth-web.helper.js';
|
|
4
|
+
import { getBetterAuthTokenService } from '../../modules/better-auth/core-better-auth.registry.js';
|
|
5
|
+
import { ConfigService } from '../services/config.service.js';
|
|
6
|
+
import {
|
|
7
|
+
getWsConnection,
|
|
8
|
+
type IWsConnectionSocket,
|
|
9
|
+
noteWsConnectionTenant,
|
|
10
|
+
readWsTenantHeader,
|
|
11
|
+
recheckHttpStreamInPlace,
|
|
12
|
+
registerWsConnection,
|
|
13
|
+
type WsConnection,
|
|
14
|
+
} from './graphql-ws-connection.helper.js';
|
|
15
|
+
|
|
16
|
+
import type { ResolvedTenantContext } from '../services/core-tenant-context.registry.js';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Re-checking an OPEN HTTP stream (Server-Sent Events) against the credential it was opened with (12.11.0).
|
|
20
|
+
*
|
|
21
|
+
* An HTTP stream is authorized once, by the guards of the request that opened it. `registerWsConnection()` with
|
|
22
|
+
* `endsOnRecheck` keeps it inside the user's rights by ending it on every re-check, so the client's reconnect passes
|
|
23
|
+
* the guards again — at the price of a reconnect (an EventSource waits its `retry`, ~3 s) before the event that
|
|
24
|
+
* triggered the re-check reaches it. This file re-runs the request's own credential instead, so the stream can be
|
|
25
|
+
* re-checked in place like a GraphQL WebSocket (`recheckHttpStreamInPlace()`).
|
|
26
|
+
*
|
|
27
|
+
* WHICH CREDENTIALS: the ones `CoreBetterAuthMiddleware` resolves, in its order — the bearer token of the
|
|
28
|
+
* `Authorization` header (a Better-Auth JWT or session token), the `lt-jwt-token` cookie when no header was sent, then
|
|
29
|
+
* the Better-Auth session cookie (`<prefix>.session_token`, signed or not) — each verified and loaded through
|
|
30
|
+
* Better-Auth's token service, the one the IAM role guard uses. A legacy JWT is Passport's and an API token has checks
|
|
31
|
+
* of its own: neither can be re-run here, and such a stream keeps ending on every re-check.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
/** The cookie a JWT-mode frontend keeps the Better-Auth JWT in — the one `CoreBetterAuthMiddleware` reads. */
|
|
35
|
+
const JWT_COOKIE_NAME = 'lt-jwt-token';
|
|
36
|
+
|
|
37
|
+
/** What a request carried to authenticate, as it was when the stream was opened. */
|
|
38
|
+
interface HttpStreamCredential {
|
|
39
|
+
authorization?: string;
|
|
40
|
+
cookieHeader?: string;
|
|
41
|
+
cookies?: Record<string, unknown>;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function firstHeaderValue(value: unknown): string | undefined {
|
|
45
|
+
const first = Array.isArray(value) ? value[0] : value;
|
|
46
|
+
return typeof first === 'string' && first ? first : undefined;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The Better-Auth base path the session cookie name derives from */
|
|
50
|
+
function betterAuthBasePath(): string {
|
|
51
|
+
const betterAuth = ConfigService.configFastButReadOnly?.betterAuth;
|
|
52
|
+
const basePath = betterAuth && typeof betterAuth === 'object' ? betterAuth.basePath : undefined;
|
|
53
|
+
return typeof basePath === 'string' && basePath ? basePath : '/iam';
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The tokens to try, in the order `CoreBetterAuthMiddleware` tries them. Only non-empty strings: a cookie parsed from
|
|
58
|
+
* `j:` JSON is an object and is never handed to a lookup (DEV-4138).
|
|
59
|
+
*/
|
|
60
|
+
function credentialTokens(credential: HttpStreamCredential): string[] {
|
|
61
|
+
const tokens: string[] = [];
|
|
62
|
+
const add = (token: unknown) => {
|
|
63
|
+
if (typeof token === 'string' && token && !tokens.includes(token)) {
|
|
64
|
+
tokens.push(token);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
const isLegacy = (token: string) => token.split('.').length === 3 && isLegacyJwt(token);
|
|
68
|
+
const cookies = credential.cookies ?? parseCookieHeader(credential.cookieHeader);
|
|
69
|
+
if (credential.authorization) {
|
|
70
|
+
const token = credential.authorization.replace(/^bearer\s+/i, '');
|
|
71
|
+
// A legacy JWT is Passport's; the middleware leaves it alone as well
|
|
72
|
+
if (!isLegacy(token)) {
|
|
73
|
+
add(token);
|
|
74
|
+
}
|
|
75
|
+
} else {
|
|
76
|
+
const jwt = cookies?.[JWT_COOKIE_NAME];
|
|
77
|
+
if (typeof jwt === 'string' && jwt.split('.').length === 3 && !isLegacy(jwt)) {
|
|
78
|
+
add(jwt);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
add(
|
|
82
|
+
extractSessionToken(
|
|
83
|
+
{ cookies, headers: { authorization: credential.authorization, cookie: credential.cookieHeader } } as any,
|
|
84
|
+
betterAuthBasePath(),
|
|
85
|
+
{ skipAuthHeader: !!credential.authorization },
|
|
86
|
+
),
|
|
87
|
+
);
|
|
88
|
+
return tokens;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Build the re-authentication of an HTTP stream from the request that opened it: a function that re-runs the
|
|
93
|
+
* request's Better-Auth credential (bearer token, `lt-jwt-token` cookie, session cookie — see the file comment) and
|
|
94
|
+
* resolves to the user it authenticates as NOW, or to `null` when it no longer does (a signed-out or expired session,
|
|
95
|
+
* an expired JWT).
|
|
96
|
+
*
|
|
97
|
+
* The credential is read from the request once, here; the function verifies it again on every call. Answers
|
|
98
|
+
* `undefined` when there is nothing it could re-run: Better-Auth is not active, the request carries an API token, or
|
|
99
|
+
* its only credential is a legacy JWT. Pass the result as `reauthenticate` to `recheckHttpStreamInPlace()`.
|
|
100
|
+
*/
|
|
101
|
+
export function createHttpStreamReauthenticator(request: any): (() => Promise<any>) | undefined {
|
|
102
|
+
if (!request || typeof request !== 'object' || !getBetterAuthTokenService() || hasApiTokenCredential(request)) {
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
const credential: HttpStreamCredential = {
|
|
106
|
+
authorization: firstHeaderValue(request.headers?.authorization),
|
|
107
|
+
cookieHeader: firstHeaderValue(request.headers?.cookie),
|
|
108
|
+
cookies: request.cookies && typeof request.cookies === 'object' ? { ...request.cookies } : undefined,
|
|
109
|
+
};
|
|
110
|
+
if (!credentialTokens(credential).length) {
|
|
111
|
+
return undefined;
|
|
112
|
+
}
|
|
113
|
+
return async () => {
|
|
114
|
+
// Read at call time: a test module rebuilt in between registers a token service of its own
|
|
115
|
+
const tokenService = getBetterAuthTokenService();
|
|
116
|
+
if (!tokenService) {
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
for (const token of credentialTokens(credential)) {
|
|
120
|
+
try {
|
|
121
|
+
const user = await tokenService.verifyAndLoadUser(token);
|
|
122
|
+
if (user) {
|
|
123
|
+
return user;
|
|
124
|
+
}
|
|
125
|
+
} catch {
|
|
126
|
+
// An expired JWT: the middleware goes on to the session cookie, and so does this
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return null;
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** What {@link registerHttpStream} needs to know about a stream. */
|
|
134
|
+
export interface IHttpStreamRegistration {
|
|
135
|
+
/** The object the stream is identified by (`getWsConnection(carrier)`). Default: a new object. */
|
|
136
|
+
carrier?: object;
|
|
137
|
+
|
|
138
|
+
/** How the log names the stream when it is closed, e.g. `SSE hint stream`. Default `HTTP stream`. */
|
|
139
|
+
channel?: string;
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Re-run the credential the stream was opened with. Default: {@link createHttpStreamReauthenticator} of `request`.
|
|
143
|
+
* `false` keeps the stream ending on every re-check (the behaviour before 12.11.0).
|
|
144
|
+
*/
|
|
145
|
+
reauthenticate?: (() => any) | false;
|
|
146
|
+
|
|
147
|
+
/** The HTTP request the stream was opened with: its credential and its tenant header are re-checked. */
|
|
148
|
+
request: any;
|
|
149
|
+
|
|
150
|
+
/** Closes the stream (with the close code as first argument, which a stream ignores) and reports its end. */
|
|
151
|
+
socket: IWsConnectionSocket;
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* The tenant scope the stream's reads are scoped by — what the guards resolved for the request (`RequestContext`:
|
|
155
|
+
* `tenantId`, `tenantIds`, `tenantRole`, `isAdminBypass`). A re-check closes the stream when it resolves
|
|
156
|
+
* differently. Without it the tenant scope is not re-checked.
|
|
157
|
+
*/
|
|
158
|
+
tenant?: ResolvedTenantContext;
|
|
159
|
+
|
|
160
|
+
/** The user the guards authenticated for the request (`@CurrentUser()`). */
|
|
161
|
+
user: any;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Register an HTTP stream (Server-Sent Events) for the authorization lifecycle of `graphql-ws-connection.helper.ts`
|
|
166
|
+
* and let it be re-checked in place when its credential can be re-run (12.11.0).
|
|
167
|
+
*
|
|
168
|
+
* Re-checked in place, the stream stays open while the rights are unchanged — an event after
|
|
169
|
+
* `graphQl.subscriptionRevalidationMs` or after an invalidation is delivered without a reconnect — and is closed when
|
|
170
|
+
* user id, roles, verification or tenant scope changed. A stream whose credential cannot be re-run (see
|
|
171
|
+
* {@link createHttpStreamReauthenticator}) ends on every re-check as before, and the client's reconnect passes the
|
|
172
|
+
* guards again.
|
|
173
|
+
*
|
|
174
|
+
* Call `ensureWsConnectionCurrent(connection)` before every event and send the event only when it answers `true`.
|
|
175
|
+
* Register the stream before anything may end it, and end it through `socket.close()`; report its end through the
|
|
176
|
+
* `close` listener of `socket.once()`.
|
|
177
|
+
*/
|
|
178
|
+
export async function registerHttpStream(registration: IHttpStreamRegistration): Promise<undefined | WsConnection> {
|
|
179
|
+
const { channel, request, socket, tenant, user } = registration;
|
|
180
|
+
const carrier = registration.carrier ?? {};
|
|
181
|
+
registerWsConnection({ carrier, channel: channel || 'HTTP stream', endsOnRecheck: true, socket, user });
|
|
182
|
+
const connection = getWsConnection(carrier);
|
|
183
|
+
if (!connection) {
|
|
184
|
+
return undefined;
|
|
185
|
+
}
|
|
186
|
+
if (tenant) {
|
|
187
|
+
noteWsConnectionTenant(connection, tenant);
|
|
188
|
+
}
|
|
189
|
+
const reauthenticate =
|
|
190
|
+
registration.reauthenticate === false
|
|
191
|
+
? undefined
|
|
192
|
+
: (registration.reauthenticate ?? createHttpStreamReauthenticator(request));
|
|
193
|
+
if (reauthenticate) {
|
|
194
|
+
await recheckHttpStreamInPlace(connection, { reauthenticate, tenantHeader: readWsTenantHeader(request) });
|
|
195
|
+
}
|
|
196
|
+
return connection;
|
|
197
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether the application has started to shut down (12.11.0) — an import-free leaf, so `installGracefulShutdown()` and
|
|
3
|
+
* the health check's readiness endpoint reach the same state without importing each other.
|
|
4
|
+
*
|
|
5
|
+
* `installGracefulShutdown()` marks it on the first SIGTERM / SIGINT, BEFORE the `shutdownDelayMs` wait. From then on
|
|
6
|
+
* `GET /health-check/ready` answers 503 (`shutting_down`) while the liveness check stays healthy: a load balancer that
|
|
7
|
+
* polls readiness takes the replica out of rotation during the wait, and the process is not restarted. Terminus' own
|
|
8
|
+
* shutdown state comes only inside `app.close()`, right before the HTTP server closes — too late for any poll.
|
|
9
|
+
*
|
|
10
|
+
* Process-wide, like the signals it follows.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
let draining = false;
|
|
14
|
+
|
|
15
|
+
/** Marks the start of the shutdown. */
|
|
16
|
+
export function markShutdownDraining(): void {
|
|
17
|
+
draining = true;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Whether the shutdown has started. */
|
|
21
|
+
export function isShutdownDraining(): boolean {
|
|
22
|
+
return draining;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Resets the state — for tests. */
|
|
26
|
+
export function resetShutdownDraining(): void {
|
|
27
|
+
draining = false;
|
|
28
|
+
}
|
|
@@ -23,5 +23,5 @@ export interface CronJobConfigWithUtcOffset<
|
|
|
23
23
|
* This allows you to specify the offset of the timezone rather than using the `timeZone` parameter.
|
|
24
24
|
* Probably don't use both `timeZone` and `utcOffset` together or weird things may happen.
|
|
25
25
|
*/
|
|
26
|
-
utcOffset?: CronJobParams<OC, C>['
|
|
26
|
+
utcOffset?: CronJobParams<OC, C>['utcOffset'];
|
|
27
27
|
}
|
|
@@ -87,5 +87,5 @@ export interface CronJobConfig<OC extends CronOnCompleteCommand | null = null, C
|
|
|
87
87
|
* This allows you to specify the offset of the timezone rather than using the `timeZone` parameter.
|
|
88
88
|
* Probably don't use both `timeZone` and `utcOffset` together or weird things may happen.
|
|
89
89
|
*/
|
|
90
|
-
utcOffset?:
|
|
90
|
+
utcOffset?: number | null;
|
|
91
91
|
}
|
|
@@ -1040,6 +1040,20 @@ export interface IBetterAuthSocialProvider {
|
|
|
1040
1040
|
*/
|
|
1041
1041
|
disableDefaultScope?: boolean;
|
|
1042
1042
|
|
|
1043
|
+
/**
|
|
1044
|
+
* Whether a client may NOT sign in or link an account with an id token it submits itself (`POST /iam/sign-in/social`
|
|
1045
|
+
* or `/iam/link-social` with `idToken`), answered with `ID_TOKEN_NOT_SUPPORTED`.
|
|
1046
|
+
*
|
|
1047
|
+
* On by default since 12.11.0: Better-Auth verifies such a token's signature, audience and expiry and asks the
|
|
1048
|
+
* provider nothing, so an id token kept from an earlier sign-in (`/iam/get-access-token` hands it out) signed its
|
|
1049
|
+
* owner in again for its whole lifetime — about an hour at Microsoft — after the provider had withdrawn the
|
|
1050
|
+
* assignment or role. The redirect flow (`signIn.social` with a `callbackURL`) never uses it. Native apps and Google
|
|
1051
|
+
* One Tap send the provider's id token themselves and need `false`. Only `false` opens it; any other value keeps it
|
|
1052
|
+
* closed.
|
|
1053
|
+
* @default true
|
|
1054
|
+
*/
|
|
1055
|
+
disableIdTokenSignIn?: boolean;
|
|
1056
|
+
|
|
1043
1057
|
/**
|
|
1044
1058
|
* Whether a first sign-in through this provider may create an account.
|
|
1045
1059
|
*
|
|
@@ -1086,6 +1100,26 @@ export interface IBetterAuthSocialProvider {
|
|
|
1086
1100
|
tenantId?: string;
|
|
1087
1101
|
}
|
|
1088
1102
|
|
|
1103
|
+
/**
|
|
1104
|
+
* Session lifetime of Better-Auth (`betterAuth.session`, 12.11.0).
|
|
1105
|
+
*/
|
|
1106
|
+
export interface IBetterAuthSessionConfig {
|
|
1107
|
+
/**
|
|
1108
|
+
* Whether a session is extended while it is used. Better-Auth renews its expiry once per day of use (`updateAge`).
|
|
1109
|
+
* `false`: the session ends `expiresInHours` after the sign-in, used or not (Better-Auth's `disableSessionRefresh`).
|
|
1110
|
+
* Only `false` switches the extension off.
|
|
1111
|
+
* @default true
|
|
1112
|
+
*/
|
|
1113
|
+
extendOnUse?: boolean;
|
|
1114
|
+
|
|
1115
|
+
/**
|
|
1116
|
+
* Lifetime of a session in hours, counted from the sign-in (with `extendOnUse`, from the last extension). A value
|
|
1117
|
+
* that is not a positive number is ignored with a warning and the default applies — never "unlimited".
|
|
1118
|
+
* @default 168 (7 days, Better-Auth's default)
|
|
1119
|
+
*/
|
|
1120
|
+
expiresInHours?: number;
|
|
1121
|
+
}
|
|
1122
|
+
|
|
1089
1123
|
/**
|
|
1090
1124
|
* Two-factor authentication plugin configuration for Better-Auth
|
|
1091
1125
|
*
|
|
@@ -3073,6 +3107,11 @@ export interface IServerOptions {
|
|
|
3073
3107
|
|
|
3074
3108
|
/**
|
|
3075
3109
|
* Whether to activate health check endpoints
|
|
3110
|
+
*
|
|
3111
|
+
* Presence implies enabled: the block registers `GET /health-check` (liveness) and the `healthCheck` query. With
|
|
3112
|
+
* `readiness` (12.11.0) a readiness endpoint `GET /health-check/ready` sits beside it. A project adds checks of its
|
|
3113
|
+
* own by extending `CoreHealthCheckService` and overriding `additionalHealthIndicators(kind)` — registered through
|
|
3114
|
+
* `CoreModule.forRoot(env, { healthCheck: { service: HealthCheckService } })`.
|
|
3076
3115
|
*/
|
|
3077
3116
|
healthCheck?: {
|
|
3078
3117
|
/**
|
|
@@ -3186,6 +3225,42 @@ export interface IServerOptions {
|
|
|
3186
3225
|
* @default true (when `healthCheck` is set)
|
|
3187
3226
|
*/
|
|
3188
3227
|
enabled?: boolean;
|
|
3228
|
+
|
|
3229
|
+
/**
|
|
3230
|
+
* Readiness endpoint `GET /health-check/ready` beside the liveness check `GET /health-check` (12.11.0).
|
|
3231
|
+
*
|
|
3232
|
+
* Boolean shorthand, presence implies enabled: absent, `false` or `{ enabled: false }` registers nothing (the
|
|
3233
|
+
* route answers 404) and leaves the health check as it was; `true` or `{}` registers it with the default checks.
|
|
3234
|
+
* Public like the liveness check (`@Roles(S_EVERYONE)`), REST only. It answers 200 while every check is up and
|
|
3235
|
+
* 503 when one is down — and `shutting_down` from the first SIGTERM / SIGINT on when `installGracefulShutdown(app)`
|
|
3236
|
+
* handles the signals with a `shutdownDelayMs`: it marks the shutdown before its `shutdownDelayMs` wait, so a load balancer polling readiness
|
|
3237
|
+
* stops routing to the replica while it still serves (without it, only Terminus' own state applies, which comes
|
|
3238
|
+
* inside `app.close()`, right before the server closes). Add checks of your own through
|
|
3239
|
+
* `CoreHealthCheckService.additionalHealthIndicators('readiness')`.
|
|
3240
|
+
*
|
|
3241
|
+
* @default undefined (no readiness endpoint)
|
|
3242
|
+
* @since 12.11.0
|
|
3243
|
+
*/
|
|
3244
|
+
readiness?:
|
|
3245
|
+
| boolean
|
|
3246
|
+
| {
|
|
3247
|
+
/**
|
|
3248
|
+
* The checks of `configs` the readiness check runs, with the key, thresholds and options configured there:
|
|
3249
|
+
* any of `database` (MongoDB ping and every SQL source), `memoryHeap`, `memoryRss`, `storage`, `build`. A
|
|
3250
|
+
* listed check runs whatever `configs.<check>.enabled` says — that flag decides for `GET /health-check`.
|
|
3251
|
+
* A comma-separated string is read as a list (what `NSC__HEALTH_CHECK__READINESS__CHECKS` gives); unknown
|
|
3252
|
+
* names are ignored with a warning; `[]` runs only the project's own readiness indicators.
|
|
3253
|
+
*
|
|
3254
|
+
* @default ['database', 'build']
|
|
3255
|
+
*/
|
|
3256
|
+
checks?: ('build' | 'database' | 'memoryHeap' | 'memoryRss' | 'storage')[] | string;
|
|
3257
|
+
|
|
3258
|
+
/**
|
|
3259
|
+
* `false` pre-configures the readiness endpoint without registering it.
|
|
3260
|
+
* @default true (when `readiness` is set)
|
|
3261
|
+
*/
|
|
3262
|
+
enabled?: boolean;
|
|
3263
|
+
};
|
|
3189
3264
|
};
|
|
3190
3265
|
|
|
3191
3266
|
/**
|
|
@@ -4788,7 +4863,10 @@ interface IBetterAuthBase {
|
|
|
4788
4863
|
/**
|
|
4789
4864
|
* Email/password authentication configuration.
|
|
4790
4865
|
* Enabled by default.
|
|
4791
|
-
* Set `enabled: false` to explicitly disable email/password auth.
|
|
4866
|
+
* Set `enabled: false` to explicitly disable email/password auth. Since 12.11.0 Passkey and two-factor are then off
|
|
4867
|
+
* too unless switched on explicitly (`passkey: true`, `twoFactor: true`) — when no plugin of `plugins` adds a sign-in
|
|
4868
|
+
* route of its own (`username()`, `phoneNumber()` keep both on): with sign-in through a provider alone, a passkey
|
|
4869
|
+
* registered once would keep signing its owner in after the provider withdrew the assignment.
|
|
4792
4870
|
*/
|
|
4793
4871
|
emailAndPassword?: {
|
|
4794
4872
|
/**
|
|
@@ -5165,6 +5243,21 @@ interface IBetterAuthBase {
|
|
|
5165
5243
|
*/
|
|
5166
5244
|
socialProviders?: Record<string, IBetterAuthSocialProvider>;
|
|
5167
5245
|
|
|
5246
|
+
/**
|
|
5247
|
+
* How long a session lasts, and whether using it extends it (12.11.0).
|
|
5248
|
+
*
|
|
5249
|
+
* Without it Better-Auth's defaults apply: 7 days, extended once a day while the session is used — so a session that
|
|
5250
|
+
* is used every day never ends, and a withdrawal at a sign-in provider (an Entra assignment) never reaches it. With
|
|
5251
|
+
* `extendOnUse: false` a session ends `expiresInHours` after its sign-in, whatever happens in between.
|
|
5252
|
+
* `betterAuth.options.session` still wins key by key.
|
|
5253
|
+
*
|
|
5254
|
+
* @example
|
|
5255
|
+
* ```typescript
|
|
5256
|
+
* session: { expiresInHours: 8, extendOnUse: false }, // sign in again after 8 hours, every time
|
|
5257
|
+
* ```
|
|
5258
|
+
*/
|
|
5259
|
+
session?: IBetterAuthSessionConfig;
|
|
5260
|
+
|
|
5168
5261
|
/**
|
|
5169
5262
|
* Two-factor authentication configuration.
|
|
5170
5263
|
*
|
|
@@ -5520,6 +5613,25 @@ export interface ICoreModuleOverrides {
|
|
|
5520
5613
|
service?: Type<any>;
|
|
5521
5614
|
};
|
|
5522
5615
|
|
|
5616
|
+
/**
|
|
5617
|
+
* Override the health check service and/or the readiness controller (12.11.0).
|
|
5618
|
+
*
|
|
5619
|
+
* - `service` must extend `CoreHealthCheckService` — the extension point for checks of a project's own:
|
|
5620
|
+
* override `additionalHealthIndicators(kind)` and return Terminus indicators for `'liveness'`
|
|
5621
|
+
* (`GET /health-check`, the `healthCheck` query) and/or `'readiness'` (`GET /health-check/ready`)
|
|
5622
|
+
* - `readinessController` must extend `CoreHealthCheckReadinessController` (registered only with
|
|
5623
|
+
* `healthCheck.readiness`)
|
|
5624
|
+
*
|
|
5625
|
+
* @example
|
|
5626
|
+
* ```typescript
|
|
5627
|
+
* { healthCheck: { service: HealthCheckService } }
|
|
5628
|
+
* ```
|
|
5629
|
+
*/
|
|
5630
|
+
healthCheck?: {
|
|
5631
|
+
readinessController?: Type<any>;
|
|
5632
|
+
service?: Type<any>;
|
|
5633
|
+
};
|
|
5634
|
+
|
|
5523
5635
|
/**
|
|
5524
5636
|
* Override Hub controllers and/or services with project-specific subclasses.
|
|
5525
5637
|
*
|