@terreno/api 57.16.2 → 57.17.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/dist/actions.d.ts +26 -3
- package/dist/actions.js +102 -16
- package/dist/api.d.ts +7 -0
- package/dist/api.js +59 -3
- package/dist/auth.d.ts +28 -4
- package/dist/auth.js +140 -15
- package/dist/authRecovery.d.ts +31 -0
- package/dist/authRecovery.js +86 -6
- package/dist/betterAuth.d.ts +86 -0
- package/dist/betterAuthApp.d.ts +17 -0
- package/dist/betterAuthApp.js +12 -1
- package/dist/betterAuthOAuthHandoff.d.ts +24 -0
- package/dist/betterAuthOAuthHandoff.js +305 -0
- package/dist/betterAuthSetup.d.ts +37 -2
- package/dist/betterAuthSetup.js +304 -24
- package/dist/expressServer.d.ts +16 -0
- package/dist/expressServer.js +61 -14
- package/dist/githubAuth.d.ts +9 -0
- package/dist/githubAuth.js +230 -31
- package/dist/index.d.ts +9 -1
- package/dist/index.js +27 -3
- package/dist/mcp/actionTools.d.ts +4 -0
- package/dist/mcp/actionTools.js +158 -0
- package/dist/mcp/auth.d.ts +30 -0
- package/dist/mcp/auth.js +83 -28
- package/dist/mcp/createMCPRequest.d.ts +5 -1
- package/dist/mcp/createMCPRequest.js +3 -3
- package/dist/mcp/handlers.d.ts +3 -0
- package/dist/mcp/handlers.js +43 -27
- package/dist/mcp/oauth.d.ts +14 -0
- package/dist/mcp/oauth.js +618 -0
- package/dist/mcp/oauthMetadata.d.ts +89 -0
- package/dist/mcp/oauthMetadata.js +137 -0
- package/dist/mcp/registry.d.ts +5 -0
- package/dist/mcp/registry.js +9 -1
- package/dist/mcp/server.d.ts +6 -0
- package/dist/mcp/server.js +44 -7
- package/dist/mcp/serviceTokens.js +12 -2
- package/dist/mcp/toolExecution.d.ts +12 -0
- package/dist/mcp/toolExecution.js +60 -0
- package/dist/mcp/toolGenerator.d.ts +4 -1
- package/dist/mcp/toolGenerator.js +26 -60
- package/dist/mcp/types.d.ts +4 -4
- package/dist/models/mcpOAuthAuthorization.d.ts +2 -0
- package/dist/models/mcpOAuthAuthorization.js +86 -0
- package/dist/models/mcpOAuthClient.d.ts +2 -0
- package/dist/models/mcpOAuthClient.js +42 -0
- package/dist/models/mcpOAuthRevokedFamily.d.ts +2 -0
- package/dist/models/mcpOAuthRevokedFamily.js +36 -0
- package/dist/models/mcpOAuthToken.d.ts +12 -0
- package/dist/models/mcpOAuthToken.js +159 -0
- package/dist/models/mcpServiceToken.d.ts +1 -0
- package/dist/models/mcpServiceToken.js +21 -4
- package/dist/permissions.d.ts +7 -0
- package/dist/permissions.js +18 -1
- package/dist/rateLimit/policies.d.ts +4 -0
- package/dist/rateLimit/policies.js +46 -7
- package/dist/rbac/access.js +1 -0
- package/dist/rbac/membershipOrgAdminSource.d.ts +4 -0
- package/dist/rbac/membershipOrgAdminSource.js +5 -0
- package/dist/rbac/modelRouterAccess.d.ts +7 -1
- package/dist/rbac/modelRouterAccess.js +37 -24
- package/dist/rbac/realtimeAccess.js +17 -3
- package/dist/rbac/resolve.js +66 -9
- package/dist/rbac/roleManager.js +60 -5
- package/dist/rbac/roleModel.js +1 -1
- package/dist/rbac/statements.d.ts +1 -1
- package/dist/rbac/statements.js +13 -1
- package/dist/rbac/types.d.ts +12 -0
- package/dist/rbac/userWriteGuards.d.ts +50 -0
- package/dist/rbac/userWriteGuards.js +156 -0
- package/dist/realtime/changeStreamWatcher.d.ts +4 -1
- package/dist/realtime/changeStreamWatcher.js +157 -73
- package/dist/realtime/queryMatcher.d.ts +3 -0
- package/dist/realtime/queryMatcher.js +25 -10
- package/dist/realtime/realtimeApp.d.ts +16 -1
- package/dist/realtime/realtimeApp.js +76 -8
- package/dist/realtime/socketAuth.d.ts +17 -6
- package/dist/realtime/socketAuth.js +77 -16
- package/dist/realtime/types.d.ts +8 -5
- package/dist/requestContext.d.ts +22 -0
- package/dist/requestContext.js +22 -1
- package/dist/seedRunner.js +3 -1
- package/dist/sortFields.d.ts +33 -0
- package/dist/sortFields.js +55 -0
- package/dist/sync/executors.js +140 -2
- package/dist/sync/models.d.ts +2 -0
- package/dist/sync/models.js +5 -0
- package/dist/sync/mutationHandler.js +70 -7
- package/dist/sync/routes.js +4 -2
- package/dist/sync/syncSeqPlugin.js +6 -5
- package/dist/terrenoApp.d.ts +14 -0
- package/dist/terrenoApp.js +21 -6
- package/dist/types/mcpOAuth.d.ts +101 -0
- package/dist/types/mcpOAuth.js +2 -0
- package/dist/types/mcpServiceToken.d.ts +8 -0
- package/package.json +3 -3
- package/src/actions.test.ts +118 -0
- package/src/actions.ts +139 -17
- package/src/api.arrayOperations.test.ts +78 -0
- package/src/api.test.ts +36 -0
- package/src/api.ts +76 -4
- package/src/auth.test.ts +521 -3
- package/src/auth.ts +171 -17
- package/src/authRecovery.test.ts +99 -0
- package/src/authRecovery.ts +117 -5
- package/src/authVerification.test.ts +3 -3
- package/src/betterAuth.ts +104 -1
- package/src/betterAuthApp.test.ts +634 -1
- package/src/betterAuthApp.ts +28 -0
- package/src/betterAuthOAuthHandoff.test.ts +946 -0
- package/src/betterAuthOAuthHandoff.ts +375 -0
- package/src/betterAuthSetup.test.ts +393 -4
- package/src/betterAuthSetup.ts +402 -29
- package/src/expressServer.test.ts +93 -0
- package/src/expressServer.ts +81 -14
- package/src/githubAuth.test.ts +448 -6
- package/src/githubAuth.ts +276 -34
- package/src/index.ts +35 -1
- package/src/mcp/actionTools.test.ts +498 -0
- package/src/mcp/actionTools.ts +220 -0
- package/src/mcp/auth.test.ts +114 -1
- package/src/mcp/auth.ts +122 -35
- package/src/mcp/createMCPRequest.ts +8 -2
- package/src/mcp/handlers.test.ts +27 -0
- package/src/mcp/handlers.ts +21 -6
- package/src/mcp/oauth.test.ts +1026 -0
- package/src/mcp/oauth.ts +798 -0
- package/src/mcp/oauthMetadata.ts +234 -0
- package/src/mcp/registry.ts +11 -0
- package/src/mcp/server.ts +54 -14
- package/src/mcp/serviceTokens.ts +16 -3
- package/src/mcp/toolExecution.ts +67 -0
- package/src/mcp/toolGenerator.ts +27 -83
- package/src/mcp/types.ts +4 -4
- package/src/models/mcpOAuthAuthorization.ts +97 -0
- package/src/models/mcpOAuthClient.ts +49 -0
- package/src/models/mcpOAuthRevokedFamily.ts +47 -0
- package/src/models/mcpOAuthToken.test.ts +82 -0
- package/src/models/mcpOAuthToken.ts +195 -0
- package/src/models/mcpServiceToken.ts +26 -2
- package/src/permissions.ts +17 -0
- package/src/rateLimit/policies.ts +54 -10
- package/src/rateLimit/rateLimit.test.ts +41 -0
- package/src/rbac/access.ts +1 -0
- package/src/rbac/membershipOrgAdminSource.ts +5 -0
- package/src/rbac/modelRouterAccess.ts +40 -23
- package/src/rbac/realtimeAccess.test.ts +43 -0
- package/src/rbac/realtimeAccess.ts +23 -3
- package/src/rbac/resolve.test.ts +179 -1
- package/src/rbac/resolve.ts +78 -9
- package/src/rbac/roleManager.test.ts +132 -0
- package/src/rbac/roleManager.ts +69 -5
- package/src/rbac/roleModel.ts +1 -1
- package/src/rbac/statements.ts +14 -1
- package/src/rbac/types.ts +12 -0
- package/src/rbac/userWriteGuards.test.ts +407 -0
- package/src/rbac/userWriteGuards.ts +185 -0
- package/src/realtime/changeStreamWatcher.ts +203 -75
- package/src/realtime/queryMatcher.ts +30 -13
- package/src/realtime/realtime.test.ts +132 -0
- package/src/realtime/realtimeApp.ts +94 -10
- package/src/realtime/realtimeAppSetup.test.ts +282 -1
- package/src/realtime/socketAuth.test.ts +140 -8
- package/src/realtime/socketAuth.ts +90 -19
- package/src/realtime/types.ts +8 -5
- package/src/requestContext.ts +52 -1
- package/src/requestCredential.test.ts +89 -0
- package/src/seedRunner.test.ts +21 -0
- package/src/seedRunner.ts +3 -1
- package/src/sortFields.ts +69 -0
- package/src/sync/executors.test.ts +382 -3
- package/src/sync/executors.ts +179 -4
- package/src/sync/models.ts +8 -0
- package/src/sync/mutationHandler.test.ts +166 -2
- package/src/sync/mutationHandler.ts +97 -5
- package/src/sync/routes.ts +4 -2
- package/src/sync/syncFrontier.test.ts +11 -0
- package/src/sync/syncPhaseC.test.ts +18 -0
- package/src/sync/syncSeqPlugin.ts +6 -5
- package/src/sync/syncSocket.test.ts +92 -0
- package/src/terrenoApp.ts +36 -21
- package/src/types/mcpOAuth.ts +129 -0
- package/src/types/mcpServiceToken.ts +6 -1
|
@@ -3,13 +3,15 @@ import * as Sentry from "@sentry/bun";
|
|
|
3
3
|
import type express from "express";
|
|
4
4
|
import {Server, type Socket} from "socket.io";
|
|
5
5
|
|
|
6
|
-
import type {User} from "../auth";
|
|
6
|
+
import type {User, UserModel} from "../auth";
|
|
7
|
+
import {findBetterAuthProvider, getProvidedBetterAuth} from "../betterAuthApp";
|
|
7
8
|
import {APIError} from "../errors";
|
|
8
9
|
import {logger} from "../logger";
|
|
9
10
|
import {canSubscribeRealtime} from "../rbac/realtimeAccess";
|
|
10
11
|
import {warnOnSyncScopesWithoutUserModel} from "../sync/registry";
|
|
11
12
|
import {installSyncSocketHandlers} from "../sync/socketHandlers";
|
|
12
13
|
import {getSyncAppOptions} from "../sync/syncApp";
|
|
14
|
+
import type {TerrenoApp} from "../terrenoApp";
|
|
13
15
|
import type {TerrenoPlugin} from "../terrenoPlugin";
|
|
14
16
|
import {startChangeStreamWatcher, stopChangeStreamWatcher} from "./changeStreamWatcher";
|
|
15
17
|
import {
|
|
@@ -24,7 +26,7 @@ import {
|
|
|
24
26
|
type SessionRevalidationHandle,
|
|
25
27
|
startSessionRevalidationSweep,
|
|
26
28
|
} from "./sessionRevalidation";
|
|
27
|
-
import {createSocketAuthMiddleware} from "./socketAuth";
|
|
29
|
+
import {type BetterAuthSocketOptions, createSocketAuthMiddleware} from "./socketAuth";
|
|
28
30
|
import {getSocketUser, type SocketDataBag, type SocketWithDecodedToken} from "./socketUser";
|
|
29
31
|
import type {DocumentSubscription, QuerySubscription, RealtimeAppOptions} from "./types";
|
|
30
32
|
|
|
@@ -72,6 +74,44 @@ export interface RealtimeSocketLike extends SocketWithDecodedToken {
|
|
|
72
74
|
on: (event: string, handler: (...args: never[]) => void | Promise<void>) => void;
|
|
73
75
|
}
|
|
74
76
|
|
|
77
|
+
const LOGICAL_QUERY_KEYS = new Set(["$and", "$or"]);
|
|
78
|
+
|
|
79
|
+
const isPlainQueryObject = (value: unknown): value is Record<string, unknown> =>
|
|
80
|
+
typeof value === "object" && value !== null && !Array.isArray(value);
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Returns the first field a client query names outside `queryFields`, walking `$and` / `$or`
|
|
84
|
+
* clauses, or undefined when every field is allowed. A query room's event method reveals
|
|
85
|
+
* whether a document matches, so subscriptions may only filter on the fields REST list
|
|
86
|
+
* accepts.
|
|
87
|
+
*/
|
|
88
|
+
const findDisallowedQueryField = (
|
|
89
|
+
query: Record<string, unknown>,
|
|
90
|
+
queryFields: string[]
|
|
91
|
+
): string | undefined => {
|
|
92
|
+
for (const [key, condition] of Object.entries(query)) {
|
|
93
|
+
if (LOGICAL_QUERY_KEYS.has(key)) {
|
|
94
|
+
if (!Array.isArray(condition)) {
|
|
95
|
+
return key;
|
|
96
|
+
}
|
|
97
|
+
for (const clause of condition) {
|
|
98
|
+
if (!isPlainQueryObject(clause)) {
|
|
99
|
+
return key;
|
|
100
|
+
}
|
|
101
|
+
const nested = findDisallowedQueryField(clause, queryFields);
|
|
102
|
+
if (nested) {
|
|
103
|
+
return nested;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
if (!queryFields.includes(key)) {
|
|
109
|
+
return key;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return undefined;
|
|
113
|
+
};
|
|
114
|
+
|
|
75
115
|
const canSubscribe = async (
|
|
76
116
|
entry: RealtimeRegistryEntry,
|
|
77
117
|
method: "list" | "read",
|
|
@@ -274,6 +314,18 @@ export const installRealtimeSocketHandlers = (
|
|
|
274
314
|
return;
|
|
275
315
|
}
|
|
276
316
|
|
|
317
|
+
const disallowedField = findDisallowedQueryField(
|
|
318
|
+
payload.query,
|
|
319
|
+
entry.options.queryFields ?? []
|
|
320
|
+
);
|
|
321
|
+
if (disallowedField) {
|
|
322
|
+
logInfo(
|
|
323
|
+
`[realtime] User ${userId} denied query subscription for ${payload.collection}: ` +
|
|
324
|
+
`${disallowedField} is not in queryFields`
|
|
325
|
+
);
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
|
|
277
329
|
let query = await getAuthorizedQuery(entry, {...payload.query}, currentUser());
|
|
278
330
|
if (!query) {
|
|
279
331
|
logInfo(
|
|
@@ -347,6 +399,7 @@ export class RealtimeApp implements TerrenoPlugin {
|
|
|
347
399
|
private io: Server | null = null;
|
|
348
400
|
private config: RealtimeAppOptions;
|
|
349
401
|
private sessionRevalidation: SessionRevalidationHandle | null = null;
|
|
402
|
+
private terrenoApp: TerrenoApp | undefined;
|
|
350
403
|
|
|
351
404
|
constructor(config: RealtimeAppOptions = {}) {
|
|
352
405
|
this.config = config;
|
|
@@ -355,8 +408,9 @@ export class RealtimeApp implements TerrenoPlugin {
|
|
|
355
408
|
/**
|
|
356
409
|
* Register routes and middleware. Adds a /realtime/health endpoint.
|
|
357
410
|
*/
|
|
358
|
-
register(app: express.Application): void {
|
|
411
|
+
register(app: express.Application, _openApi?: unknown, terrenoApp?: TerrenoApp): void {
|
|
359
412
|
this.app = app;
|
|
413
|
+
this.terrenoApp = terrenoApp;
|
|
360
414
|
app.get("/realtime/health", (_req, res) => {
|
|
361
415
|
const connected = this.io?.engine?.clientsCount ?? 0;
|
|
362
416
|
res.json({
|
|
@@ -367,6 +421,33 @@ export class RealtimeApp implements TerrenoPlugin {
|
|
|
367
421
|
});
|
|
368
422
|
}
|
|
369
423
|
|
|
424
|
+
/**
|
|
425
|
+
* This RealtimeApp's `userModel`, else the TerrenoApp's, matching what `TerrenoApp({realtime})`
|
|
426
|
+
* passes to the RealtimeApp it registers.
|
|
427
|
+
*/
|
|
428
|
+
private resolveUserModel(): UserModel | undefined {
|
|
429
|
+
return this.config.userModel ?? (this.terrenoApp?.getUserModel() as UserModel | undefined);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* The Better Auth instance for socket sessions: the explicit `betterAuth` option, else the
|
|
434
|
+
* one from a Better Auth plugin (such as BetterAuthApp) registered on the same TerrenoApp,
|
|
435
|
+
* so apps do not have to build the instance early and pass it to both plugins. The socket
|
|
436
|
+
* validator falls back to the resolved `userModel`, so the handshake and the revalidation
|
|
437
|
+
* sweep resolve the same users.
|
|
438
|
+
*/
|
|
439
|
+
private resolveBetterAuth(): BetterAuthSocketOptions | undefined {
|
|
440
|
+
const userModel = this.resolveUserModel();
|
|
441
|
+
if (this.config.betterAuth) {
|
|
442
|
+
return {...this.config.betterAuth, userModel: this.config.betterAuth.userModel ?? userModel};
|
|
443
|
+
}
|
|
444
|
+
const auth = getProvidedBetterAuth(findBetterAuthProvider(this.terrenoApp?.getPlugins() ?? []));
|
|
445
|
+
if (!auth) {
|
|
446
|
+
return undefined;
|
|
447
|
+
}
|
|
448
|
+
return {auth, userModel};
|
|
449
|
+
}
|
|
450
|
+
|
|
370
451
|
/**
|
|
371
452
|
* Called after the HTTP server is created. Sets up Socket.io, auth, rooms,
|
|
372
453
|
* and starts the change stream watcher.
|
|
@@ -395,7 +476,9 @@ export class RealtimeApp implements TerrenoPlugin {
|
|
|
395
476
|
|
|
396
477
|
// Authentication middleware: legacy JWT first when configured, optionally Better Auth sessions.
|
|
397
478
|
const tokenSecret = this.config.tokenSecret ?? process.env.TOKEN_SECRET;
|
|
398
|
-
|
|
479
|
+
const userModel = this.resolveUserModel();
|
|
480
|
+
const betterAuth = this.resolveBetterAuth();
|
|
481
|
+
if (!tokenSecret && !betterAuth) {
|
|
399
482
|
throw new APIError({
|
|
400
483
|
status: 500,
|
|
401
484
|
title:
|
|
@@ -415,13 +498,14 @@ export class RealtimeApp implements TerrenoPlugin {
|
|
|
415
498
|
|
|
416
499
|
this.io.use(
|
|
417
500
|
createSocketAuthMiddleware({
|
|
418
|
-
betterAuth
|
|
501
|
+
betterAuth,
|
|
419
502
|
issuer: resolveTokenIssuer,
|
|
420
503
|
tokenSecret,
|
|
504
|
+
userModel,
|
|
421
505
|
})
|
|
422
506
|
);
|
|
423
507
|
|
|
424
|
-
const authKinds = [tokenSecret ? "JWT" : null,
|
|
508
|
+
const authKinds = [tokenSecret ? "JWT" : null, betterAuth ? "Better Auth" : null]
|
|
425
509
|
.filter(Boolean)
|
|
426
510
|
.join(" + ");
|
|
427
511
|
logInfo(`[realtime] Socket auth middleware added (${authKinds})`);
|
|
@@ -429,7 +513,7 @@ export class RealtimeApp implements TerrenoPlugin {
|
|
|
429
513
|
// Task 9.21: tenant/custom sync scopes can only be resolved from the full user
|
|
430
514
|
// document, so warn loudly here (once, at startup) rather than silently serving
|
|
431
515
|
// empty tenant streams for the life of the process.
|
|
432
|
-
warnOnSyncScopesWithoutUserModel({userModel
|
|
516
|
+
warnOnSyncScopesWithoutUserModel({userModel});
|
|
433
517
|
|
|
434
518
|
// Configure adapter for multi-instance deployments
|
|
435
519
|
this.setupAdapter(logInfo);
|
|
@@ -444,7 +528,7 @@ export class RealtimeApp implements TerrenoPlugin {
|
|
|
444
528
|
// back to the synthetic shape until this resolves.
|
|
445
529
|
void loadFullUserForSocket(
|
|
446
530
|
socket as unknown as Socket & {data: SocketDataBag},
|
|
447
|
-
|
|
531
|
+
userModel
|
|
448
532
|
);
|
|
449
533
|
// The auth middleware adds `decodedToken` at runtime; cast through
|
|
450
534
|
// RealtimeSocketLike (a structural subset) to keep socket handler logic testable.
|
|
@@ -479,11 +563,11 @@ export class RealtimeApp implements TerrenoPlugin {
|
|
|
479
563
|
// re-resolves sync room membership (D4) for sockets that remain valid.
|
|
480
564
|
// A thunk (not a snapshot) reads this app's SyncApp options fresh on every sweep.
|
|
481
565
|
this.sessionRevalidation = startSessionRevalidationSweep(this.io, () => ({
|
|
482
|
-
betterAuth
|
|
566
|
+
betterAuth,
|
|
483
567
|
intervalMs: this.config.sessionRevalidationIntervalMs,
|
|
484
568
|
logInfo,
|
|
485
569
|
sync: this.config.sync ?? (this.app ? getSyncAppOptions(this.app) : undefined),
|
|
486
|
-
userModel
|
|
570
|
+
userModel,
|
|
487
571
|
}));
|
|
488
572
|
|
|
489
573
|
logInfo("[realtime] Socket.io server setup complete");
|
|
@@ -5,9 +5,16 @@
|
|
|
5
5
|
* are process-wide and would break later files that need the real implementations).
|
|
6
6
|
*/
|
|
7
7
|
import {afterEach, beforeEach, describe, expect, it} from "bun:test";
|
|
8
|
-
import
|
|
8
|
+
import http from "node:http";
|
|
9
9
|
import {assert} from "chai";
|
|
10
|
+
import express from "express";
|
|
11
|
+
import {DateTime} from "luxon";
|
|
12
|
+
import mongoose, {Schema} from "mongoose";
|
|
13
|
+
import passportLocalMongoose from "passport-local-mongoose";
|
|
10
14
|
|
|
15
|
+
import type {UserModel} from "../auth";
|
|
16
|
+
import {BetterAuthApp} from "../betterAuthApp";
|
|
17
|
+
import {TerrenoApp} from "../terrenoApp";
|
|
11
18
|
import {RealtimeApp} from "./realtimeApp";
|
|
12
19
|
|
|
13
20
|
type Handler = (...args: unknown[]) => void;
|
|
@@ -16,6 +23,7 @@ interface FakeIo {
|
|
|
16
23
|
handlers: Map<string, Handler>;
|
|
17
24
|
middleware: unknown[];
|
|
18
25
|
closed: boolean;
|
|
26
|
+
sockets: {sockets: Map<string, unknown>};
|
|
19
27
|
}
|
|
20
28
|
|
|
21
29
|
let ios: FakeIo[] = [];
|
|
@@ -26,6 +34,7 @@ class FakeServer {
|
|
|
26
34
|
middleware: unknown[] = [];
|
|
27
35
|
closed = false;
|
|
28
36
|
engine = {clientsCount: 0};
|
|
37
|
+
sockets = {sockets: new Map<string, unknown>()};
|
|
29
38
|
|
|
30
39
|
constructor(..._args: unknown[]) {
|
|
31
40
|
ios.push(this as unknown as FakeIo);
|
|
@@ -184,3 +193,275 @@ describe("RealtimeApp.onServerCreated", () => {
|
|
|
184
193
|
);
|
|
185
194
|
});
|
|
186
195
|
});
|
|
196
|
+
|
|
197
|
+
interface WiringUserFields {
|
|
198
|
+
_id: mongoose.Types.ObjectId;
|
|
199
|
+
admin?: boolean;
|
|
200
|
+
betterAuthId?: string;
|
|
201
|
+
disabled?: boolean;
|
|
202
|
+
email?: string;
|
|
203
|
+
name?: string;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const wiringUserSchema = new Schema({
|
|
207
|
+
admin: {default: false, description: "Whether the user is an admin", type: Boolean},
|
|
208
|
+
betterAuthId: {description: "Better Auth user id", type: String},
|
|
209
|
+
disabled: {default: false, description: "Whether the account is disabled", type: Boolean},
|
|
210
|
+
email: {description: "User email", type: String},
|
|
211
|
+
name: {description: "Display name", type: String},
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
const getWiringUserModel = (): mongoose.Model<WiringUserFields> => {
|
|
215
|
+
try {
|
|
216
|
+
return mongoose.model<WiringUserFields>("RealtimeWiringUser");
|
|
217
|
+
} catch {
|
|
218
|
+
return mongoose.model<WiringUserFields>("RealtimeWiringUser", wiringUserSchema);
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
type SocketMiddleware = (socket: unknown, next: (error?: Error) => void) => void;
|
|
223
|
+
|
|
224
|
+
describe("RealtimeApp with a registered BetterAuthApp", () => {
|
|
225
|
+
const WiringUser = getWiringUserModel();
|
|
226
|
+
const userModel = WiringUser as unknown as UserModel;
|
|
227
|
+
let realtime: RealtimeApp | undefined;
|
|
228
|
+
|
|
229
|
+
// A TerrenoApp stand-in that only exposes its registered plugins, as RealtimeApp reads them.
|
|
230
|
+
const registerWithBetterAuthApp = (
|
|
231
|
+
app: RealtimeApp,
|
|
232
|
+
terrenoUserModel?: UserModel
|
|
233
|
+
): BetterAuthApp => {
|
|
234
|
+
const betterAuthApp = new BetterAuthApp({
|
|
235
|
+
config: {
|
|
236
|
+
baseURL: "http://localhost:3000",
|
|
237
|
+
enabled: true,
|
|
238
|
+
secret: "test-secret-at-least-32-characters-long",
|
|
239
|
+
},
|
|
240
|
+
userModel,
|
|
241
|
+
});
|
|
242
|
+
const terrenoApp = {
|
|
243
|
+
getPlugins: () => [betterAuthApp, app],
|
|
244
|
+
getUserModel: () => terrenoUserModel,
|
|
245
|
+
} as unknown as TerrenoApp;
|
|
246
|
+
app.register(express(), undefined, terrenoApp);
|
|
247
|
+
return betterAuthApp;
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
const signUp = async (betterAuthApp: BetterAuthApp, email: string): Promise<string> => {
|
|
251
|
+
const created = await betterAuthApp.ensureAuth().api.signUpEmail({
|
|
252
|
+
body: {email, name: "Socket User", password: "password-123456"},
|
|
253
|
+
});
|
|
254
|
+
return created.token as string;
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
const handshake = (token: string): Promise<{error?: Error; socket: Record<string, unknown>}> => {
|
|
258
|
+
const socket: Record<string, unknown> = {handshake: {auth: {token: `Bearer ${token}`}}};
|
|
259
|
+
const middleware = lastIo().middleware[0] as SocketMiddleware;
|
|
260
|
+
return new Promise((resolve) => {
|
|
261
|
+
middleware(socket, (error?: Error) => resolve({error, socket}));
|
|
262
|
+
});
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
beforeEach(async () => {
|
|
266
|
+
ios = [];
|
|
267
|
+
await WiringUser.deleteMany({});
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
afterEach(async () => {
|
|
271
|
+
await realtime?.close();
|
|
272
|
+
realtime = undefined;
|
|
273
|
+
Reflect.deleteProperty(process.env, "TOKEN_SECRET");
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
it("authenticates a Better Auth bearer at handshake without a betterAuth option", async () => {
|
|
277
|
+
process.env.TOKEN_SECRET = "test-secret";
|
|
278
|
+
realtime = new RealtimeApp(testRealtimeOptions({sessionRevalidationIntervalMs: 0, userModel}));
|
|
279
|
+
const betterAuthApp = registerWithBetterAuthApp(realtime);
|
|
280
|
+
realtime.onServerCreated(fakeServer);
|
|
281
|
+
const token = await signUp(betterAuthApp, `wired-${DateTime.now().toMillis()}@example.com`);
|
|
282
|
+
|
|
283
|
+
const {error, socket} = await handshake(token);
|
|
284
|
+
|
|
285
|
+
expect(error).toBeUndefined();
|
|
286
|
+
expect((socket.decodedToken as {authKind?: string}).authKind).toBe("better-auth");
|
|
287
|
+
expect(await WiringUser.countDocuments()).toBe(1);
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it("uses the TerrenoApp's user model when the RealtimeApp has none", async () => {
|
|
291
|
+
process.env.TOKEN_SECRET = "test-secret";
|
|
292
|
+
realtime = new RealtimeApp(testRealtimeOptions({sessionRevalidationIntervalMs: 0}));
|
|
293
|
+
const betterAuthApp = registerWithBetterAuthApp(realtime, userModel);
|
|
294
|
+
realtime.onServerCreated(fakeServer);
|
|
295
|
+
const freshToken = await signUp(
|
|
296
|
+
betterAuthApp,
|
|
297
|
+
`fallback-${DateTime.now().toMillis()}@example.com`
|
|
298
|
+
);
|
|
299
|
+
|
|
300
|
+
const fresh = await handshake(freshToken);
|
|
301
|
+
const [created] = await WiringUser.find({});
|
|
302
|
+
|
|
303
|
+
expect(fresh.error).toBeUndefined();
|
|
304
|
+
expect((fresh.socket.decodedToken as {id?: string}).id).toBe(String(created?._id));
|
|
305
|
+
|
|
306
|
+
await WiringUser.updateMany({}, {disabled: true});
|
|
307
|
+
const refused = await handshake(freshToken);
|
|
308
|
+
expect(refused.error?.message).toContain("disabled");
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
it("fills in the RealtimeApp's user model for an explicit betterAuth option without one", async () => {
|
|
312
|
+
process.env.TOKEN_SECRET = "test-secret";
|
|
313
|
+
const betterAuthApp = new BetterAuthApp({
|
|
314
|
+
config: {
|
|
315
|
+
baseURL: "http://localhost:3000",
|
|
316
|
+
enabled: true,
|
|
317
|
+
secret: "test-secret-at-least-32-characters-long",
|
|
318
|
+
},
|
|
319
|
+
});
|
|
320
|
+
realtime = new RealtimeApp(
|
|
321
|
+
testRealtimeOptions({
|
|
322
|
+
betterAuth: {auth: betterAuthApp.ensureAuth()},
|
|
323
|
+
sessionRevalidationIntervalMs: 0,
|
|
324
|
+
userModel,
|
|
325
|
+
})
|
|
326
|
+
);
|
|
327
|
+
realtime.onServerCreated(fakeServer);
|
|
328
|
+
const token = await signUp(betterAuthApp, `explicit-${DateTime.now().toMillis()}@example.com`);
|
|
329
|
+
|
|
330
|
+
const {error, socket} = await handshake(token);
|
|
331
|
+
const [created] = await WiringUser.find({});
|
|
332
|
+
|
|
333
|
+
expect(error).toBeUndefined();
|
|
334
|
+
expect((socket.decodedToken as {id?: string}).id).toBe(String(created?._id));
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
it("refuses a disabled user's legacy JWT at handshake", async () => {
|
|
338
|
+
const jwtLib = (await import("jsonwebtoken")).default;
|
|
339
|
+
realtime = new RealtimeApp(
|
|
340
|
+
testRealtimeOptions({
|
|
341
|
+
sessionRevalidationIntervalMs: 0,
|
|
342
|
+
tokenIssuer: "wiring-issuer",
|
|
343
|
+
tokenSecret: "test-secret",
|
|
344
|
+
userModel,
|
|
345
|
+
})
|
|
346
|
+
);
|
|
347
|
+
realtime.onServerCreated(fakeServer);
|
|
348
|
+
const user = await WiringUser.create({email: "jwt-disabled@example.com"});
|
|
349
|
+
const token = jwtLib.sign({id: String(user._id)}, "test-secret", {issuer: "wiring-issuer"});
|
|
350
|
+
|
|
351
|
+
const accepted = await handshake(token);
|
|
352
|
+
expect(accepted.error).toBeUndefined();
|
|
353
|
+
|
|
354
|
+
await WiringUser.updateOne({_id: user._id}, {disabled: true});
|
|
355
|
+
const refused = await handshake(token);
|
|
356
|
+
expect(refused.error?.message).toContain("disabled");
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
it("starts without TOKEN_SECRET when a BetterAuthApp is registered", () => {
|
|
360
|
+
Reflect.deleteProperty(process.env, "TOKEN_SECRET");
|
|
361
|
+
realtime = new RealtimeApp(testRealtimeOptions({sessionRevalidationIntervalMs: 0, userModel}));
|
|
362
|
+
registerWithBetterAuthApp(realtime);
|
|
363
|
+
|
|
364
|
+
assert.doesNotThrow(() => realtime?.onServerCreated(fakeServer));
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
it("disconnects a disabled user's socket on the revalidation sweep", async () => {
|
|
368
|
+
process.env.TOKEN_SECRET = "test-secret";
|
|
369
|
+
realtime = new RealtimeApp(testRealtimeOptions({sessionRevalidationIntervalMs: 10, userModel}));
|
|
370
|
+
const betterAuthApp = registerWithBetterAuthApp(realtime);
|
|
371
|
+
realtime.onServerCreated(fakeServer);
|
|
372
|
+
const token = await signUp(betterAuthApp, `swept-${DateTime.now().toMillis()}@example.com`);
|
|
373
|
+
const {socket} = await handshake(token);
|
|
374
|
+
|
|
375
|
+
const expired = new Promise<unknown>((resolve) => {
|
|
376
|
+
Object.assign(socket, {
|
|
377
|
+
data: {},
|
|
378
|
+
disconnect: (): void => {},
|
|
379
|
+
emit: (event: string, payload: unknown): void => {
|
|
380
|
+
if (event === "sync:auth-expired") {
|
|
381
|
+
resolve(payload);
|
|
382
|
+
}
|
|
383
|
+
},
|
|
384
|
+
id: "socket-swept",
|
|
385
|
+
});
|
|
386
|
+
});
|
|
387
|
+
await WiringUser.updateMany({}, {disabled: true});
|
|
388
|
+
lastIo().sockets.sockets.set("socket-swept", socket);
|
|
389
|
+
|
|
390
|
+
expect(await expired).toEqual({reason: "disabled"});
|
|
391
|
+
});
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
describe("RealtimeApp on a real TerrenoApp with BetterAuthApp", () => {
|
|
395
|
+
// TerrenoApp's JWT setup needs passport-local-mongoose's createStrategy on the user model.
|
|
396
|
+
const getTerrenoWiringUserModel = (): mongoose.Model<WiringUserFields> => {
|
|
397
|
+
try {
|
|
398
|
+
return mongoose.model<WiringUserFields>("RealtimeTerrenoWiringUser");
|
|
399
|
+
} catch {
|
|
400
|
+
const schema = wiringUserSchema.clone();
|
|
401
|
+
schema.plugin(
|
|
402
|
+
passportLocalMongoose as unknown as (
|
|
403
|
+
target: Schema,
|
|
404
|
+
options?: Record<string, unknown>
|
|
405
|
+
) => void,
|
|
406
|
+
{usernameField: "email"}
|
|
407
|
+
);
|
|
408
|
+
return mongoose.model<WiringUserFields>("RealtimeTerrenoWiringUser", schema);
|
|
409
|
+
}
|
|
410
|
+
};
|
|
411
|
+
const TerrenoWiringUser = getTerrenoWiringUserModel();
|
|
412
|
+
const userModel = TerrenoWiringUser as unknown as UserModel;
|
|
413
|
+
let realtime: RealtimeApp | undefined;
|
|
414
|
+
|
|
415
|
+
beforeEach(async () => {
|
|
416
|
+
ios = [];
|
|
417
|
+
await TerrenoWiringUser.deleteMany({});
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
afterEach(async () => {
|
|
421
|
+
await realtime?.close();
|
|
422
|
+
realtime = undefined;
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
it("authenticates a Better Auth session at handshake with no manual wiring", async () => {
|
|
426
|
+
const betterAuthApp = new BetterAuthApp({
|
|
427
|
+
config: {
|
|
428
|
+
baseURL: "http://localhost:3000",
|
|
429
|
+
enabled: true,
|
|
430
|
+
secret: "test-secret-at-least-32-characters-long",
|
|
431
|
+
},
|
|
432
|
+
userModel,
|
|
433
|
+
});
|
|
434
|
+
realtime = new RealtimeApp(testRealtimeOptions({sessionRevalidationIntervalMs: 0}));
|
|
435
|
+
const terrenoApp = new TerrenoApp({
|
|
436
|
+
httpServer: http.createServer(),
|
|
437
|
+
userModel: userModel as never,
|
|
438
|
+
})
|
|
439
|
+
.register(betterAuthApp)
|
|
440
|
+
.register(realtime);
|
|
441
|
+
terrenoApp.start();
|
|
442
|
+
await terrenoApp.whenReady();
|
|
443
|
+
|
|
444
|
+
const created = await betterAuthApp.ensureAuth().api.signUpEmail({
|
|
445
|
+
body: {
|
|
446
|
+
email: `terreno-wired-${DateTime.now().toMillis()}@example.com`,
|
|
447
|
+
name: "Wired User",
|
|
448
|
+
password: "password-123456",
|
|
449
|
+
},
|
|
450
|
+
});
|
|
451
|
+
const socket: Record<string, unknown> = {
|
|
452
|
+
handshake: {auth: {token: `Bearer ${created.token as string}`}},
|
|
453
|
+
};
|
|
454
|
+
const middleware = lastIo().middleware[0] as (
|
|
455
|
+
target: unknown,
|
|
456
|
+
next: (error?: Error) => void
|
|
457
|
+
) => void;
|
|
458
|
+
const error = await new Promise<Error | undefined>((resolve) => {
|
|
459
|
+
middleware(socket, resolve);
|
|
460
|
+
});
|
|
461
|
+
const [appUser] = await TerrenoWiringUser.find({});
|
|
462
|
+
|
|
463
|
+
expect(error).toBeUndefined();
|
|
464
|
+
expect((socket.decodedToken as {authKind?: string; id?: string}).authKind).toBe("better-auth");
|
|
465
|
+
expect((socket.decodedToken as {id?: string}).id).toBe(String(appUser?._id));
|
|
466
|
+
});
|
|
467
|
+
});
|
|
@@ -9,9 +9,10 @@
|
|
|
9
9
|
* Moved out of sync/syncSocket.test.ts (which still covers the higher-level
|
|
10
10
|
* subscribe/mutate socket handlers) into its own file per the syncdb hardening plan.
|
|
11
11
|
*/
|
|
12
|
-
import {describe, expect, it} from "bun:test";
|
|
12
|
+
import {beforeEach, describe, expect, it} from "bun:test";
|
|
13
13
|
import {assert} from "chai";
|
|
14
14
|
import jwt from "jsonwebtoken";
|
|
15
|
+
import mongoose, {Schema} from "mongoose";
|
|
15
16
|
import type {Socket} from "socket.io";
|
|
16
17
|
|
|
17
18
|
import type {UserModel} from "../auth";
|
|
@@ -25,6 +26,31 @@ import {
|
|
|
25
26
|
|
|
26
27
|
type StubbedAuth = BetterAuthSocketOptions["auth"];
|
|
27
28
|
|
|
29
|
+
interface SocketAuthUserFields {
|
|
30
|
+
_id: mongoose.Types.ObjectId;
|
|
31
|
+
admin?: boolean;
|
|
32
|
+
betterAuthId?: string;
|
|
33
|
+
disabled?: boolean;
|
|
34
|
+
email?: string;
|
|
35
|
+
name?: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const socketAuthUserSchema = new Schema({
|
|
39
|
+
admin: {default: false, description: "Whether the user is an admin", type: Boolean},
|
|
40
|
+
betterAuthId: {description: "Better Auth user id", type: String},
|
|
41
|
+
disabled: {default: false, description: "Whether the account is disabled", type: Boolean},
|
|
42
|
+
email: {description: "User email", type: String},
|
|
43
|
+
name: {description: "Display name", type: String},
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const getSocketAuthUserModel = (): mongoose.Model<SocketAuthUserFields> => {
|
|
47
|
+
try {
|
|
48
|
+
return mongoose.model<SocketAuthUserFields>("SocketAuthUser");
|
|
49
|
+
} catch {
|
|
50
|
+
return mongoose.model<SocketAuthUserFields>("SocketAuthUser", socketAuthUserSchema);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
|
|
28
54
|
describe("socketAuth", () => {
|
|
29
55
|
const tokenSecret = "socket-auth-test-secret";
|
|
30
56
|
|
|
@@ -145,6 +171,60 @@ describe("socketAuth", () => {
|
|
|
145
171
|
});
|
|
146
172
|
});
|
|
147
173
|
|
|
174
|
+
describe("createLegacyJwtValidator with a user model", () => {
|
|
175
|
+
const SocketAuthUser = getSocketAuthUserModel();
|
|
176
|
+
const userModel = SocketAuthUser as unknown as UserModel;
|
|
177
|
+
|
|
178
|
+
beforeEach(async () => {
|
|
179
|
+
await SocketAuthUser.deleteMany({});
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it("accepts an enabled user's JWT", async () => {
|
|
183
|
+
const user = await SocketAuthUser.create({email: "enabled-jwt@example.com"});
|
|
184
|
+
const validator = createLegacyJwtValidator(tokenSecret, undefined, userModel);
|
|
185
|
+
const token = jwt.sign({id: String(user._id)}, tokenSecret);
|
|
186
|
+
const socket = makeAuthSocket(`Bearer ${token}`);
|
|
187
|
+
|
|
188
|
+
await validator(socket);
|
|
189
|
+
|
|
190
|
+
expect(socket.decodedToken?.id).toBe(String(user._id));
|
|
191
|
+
expect(socket.decodedToken?.authKind).toBe("jwt");
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it("rejects a disabled user's JWT", async () => {
|
|
195
|
+
const user = await SocketAuthUser.create({disabled: true, email: "disabled-jwt@example.com"});
|
|
196
|
+
const validator = createLegacyJwtValidator(tokenSecret, undefined, userModel);
|
|
197
|
+
const token = jwt.sign({id: String(user._id)}, tokenSecret);
|
|
198
|
+
const socket = makeAuthSocket(`Bearer ${token}`);
|
|
199
|
+
|
|
200
|
+
await expect(validator(socket)).rejects.toThrow("disabled");
|
|
201
|
+
expect(socket.decodedToken).toBeUndefined();
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it("rejects a JWT whose user no longer exists", async () => {
|
|
205
|
+
const validator = createLegacyJwtValidator(tokenSecret, undefined, userModel);
|
|
206
|
+
const token = jwt.sign({id: String(new mongoose.Types.ObjectId())}, tokenSecret);
|
|
207
|
+
const socket = makeAuthSocket(`Bearer ${token}`);
|
|
208
|
+
|
|
209
|
+
await expect(validator(socket)).rejects.toThrow("Unauthorized");
|
|
210
|
+
expect(socket.decodedToken).toBeUndefined();
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it("refuses a disabled user through the middleware chain on every handshake", async () => {
|
|
214
|
+
const user = await SocketAuthUser.create({email: "reconnect-jwt@example.com"});
|
|
215
|
+
const middleware = createSocketAuthMiddleware({tokenSecret, userModel});
|
|
216
|
+
const token = jwt.sign({id: String(user._id)}, tokenSecret);
|
|
217
|
+
|
|
218
|
+
expect(await runMiddleware(middleware, makeAuthSocket(`Bearer ${token}`))).toBeUndefined();
|
|
219
|
+
|
|
220
|
+
await SocketAuthUser.updateOne({_id: user._id}, {disabled: true});
|
|
221
|
+
const first = await runMiddleware(middleware, makeAuthSocket(`Bearer ${token}`));
|
|
222
|
+
const second = await runMiddleware(middleware, makeAuthSocket(`Bearer ${token}`));
|
|
223
|
+
expect(first?.message).toContain("disabled");
|
|
224
|
+
expect(second?.message).toContain("disabled");
|
|
225
|
+
});
|
|
226
|
+
});
|
|
227
|
+
|
|
148
228
|
describe("createBetterAuthValidator", () => {
|
|
149
229
|
const stubAuth = (
|
|
150
230
|
session: unknown,
|
|
@@ -231,14 +311,66 @@ describe("socketAuth", () => {
|
|
|
231
311
|
});
|
|
232
312
|
});
|
|
233
313
|
|
|
234
|
-
|
|
235
|
-
const
|
|
236
|
-
|
|
237
|
-
|
|
314
|
+
describe("with a real user model", () => {
|
|
315
|
+
const SocketAuthUser = getSocketAuthUserModel();
|
|
316
|
+
const userModel = SocketAuthUser as unknown as UserModel;
|
|
317
|
+
|
|
318
|
+
beforeEach(async () => {
|
|
319
|
+
await SocketAuthUser.deleteMany({});
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
it("creates the app user when a freshly signed-up user connects first", async () => {
|
|
323
|
+
const validator = createBetterAuthValidator({
|
|
324
|
+
auth: stubAuth({
|
|
325
|
+
session: {id: "s"},
|
|
326
|
+
user: {email: "fresh@example.com", id: "ba-fresh", name: "Fresh User"},
|
|
327
|
+
}),
|
|
328
|
+
userModel,
|
|
329
|
+
});
|
|
330
|
+
const socket = makeAuthSocket("session-token");
|
|
331
|
+
|
|
332
|
+
await validator(socket);
|
|
333
|
+
|
|
334
|
+
const created = await SocketAuthUser.find({betterAuthId: "ba-fresh"});
|
|
335
|
+
expect(created).toHaveLength(1);
|
|
336
|
+
expect(created[0].email).toBe("fresh@example.com");
|
|
337
|
+
expect(socket.decodedToken?.id).toBe(String(created[0]._id));
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
it("rejects with an Unauthorized error, not the database error, when the lookup fails", async () => {
|
|
341
|
+
const validator = createBetterAuthValidator({
|
|
342
|
+
auth: stubAuth({session: {id: "s"}, user: {email: "x@example.com", id: "ba-x"}}),
|
|
343
|
+
userModel: {
|
|
344
|
+
findOneOrNone: async () => {
|
|
345
|
+
throw new Error("E11000 duplicate key error collection: users");
|
|
346
|
+
},
|
|
347
|
+
} as unknown as UserModel,
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
const error = await validator(makeAuthSocket("session-token")).catch((e: Error) => e);
|
|
351
|
+
|
|
352
|
+
expect(error?.message).toContain("could not resolve the application user");
|
|
353
|
+
expect(error?.message).not.toContain("E11000");
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
it("rejects a disabled user's session", async () => {
|
|
357
|
+
await SocketAuthUser.create({
|
|
358
|
+
betterAuthId: "ba-disabled",
|
|
359
|
+
disabled: true,
|
|
360
|
+
email: "disabled@example.com",
|
|
361
|
+
});
|
|
362
|
+
const validator = createBetterAuthValidator({
|
|
363
|
+
auth: stubAuth({
|
|
364
|
+
session: {id: "s"},
|
|
365
|
+
user: {email: "disabled@example.com", id: "ba-disabled", name: "Disabled"},
|
|
366
|
+
}),
|
|
367
|
+
userModel,
|
|
368
|
+
});
|
|
369
|
+
const socket = makeAuthSocket("session-token");
|
|
370
|
+
|
|
371
|
+
await expect(validator(socket)).rejects.toThrow("disabled");
|
|
372
|
+
expect(socket.decodedToken).toBeUndefined();
|
|
238
373
|
});
|
|
239
|
-
await expect(validator(makeAuthSocket("session-token"))).rejects.toThrow(
|
|
240
|
-
"no application user"
|
|
241
|
-
);
|
|
242
374
|
});
|
|
243
375
|
});
|
|
244
376
|
|