@terreno/api 57.18.0 → 57.20.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/CHANGELOG.md +2071 -2
- package/dist/accountSecurity/accessActions.d.ts +38 -0
- package/dist/accountSecurity/accessActions.js +157 -0
- package/dist/accountSecurity/accountSecurityApp.d.ts +219 -0
- package/dist/accountSecurity/accountSecurityApp.js +438 -0
- package/dist/accountSecurity/accountSecurityService.d.ts +200 -0
- package/dist/accountSecurity/accountSecurityService.js +481 -0
- package/dist/accountSecurity/credentialActions.d.ts +16 -0
- package/dist/accountSecurity/credentialActions.js +189 -0
- package/dist/accountSecurity/membershipActions.d.ts +15 -0
- package/dist/accountSecurity/membershipActions.js +81 -0
- package/dist/accountSecurity/orgSecurityRoutes.d.ts +50 -0
- package/dist/accountSecurity/orgSecurityRoutes.js +149 -0
- package/dist/accountSecurity/securityEventHooks.d.ts +44 -0
- package/dist/accountSecurity/securityEventHooks.js +223 -0
- package/dist/accountSecurity/securityEvents.d.ts +162 -0
- package/dist/accountSecurity/securityEvents.js +114 -0
- package/dist/accountSecurity/securityNotifications.d.ts +125 -0
- package/dist/accountSecurity/securityNotifications.js +333 -0
- package/dist/api.js +38 -6
- package/dist/audit/auditEventModel.d.ts +5 -3
- package/dist/audit/auditEventModel.js +8 -6
- package/dist/audit/processQueue.js +4 -2
- package/dist/audit/record.d.ts +2 -2
- package/dist/audit/record.js +3 -0
- package/dist/authRecovery.js +29 -1
- package/dist/betterAuth.d.ts +179 -10
- package/dist/betterAuthApp.d.ts +15 -0
- package/dist/betterAuthApp.js +23 -0
- package/dist/betterAuthChallengeHeaders.d.ts +37 -0
- package/dist/betterAuthChallengeHeaders.js +180 -0
- package/dist/betterAuthFeatures.d.ts +54 -0
- package/dist/betterAuthFeatures.js +239 -0
- package/dist/betterAuthMethods.d.ts +73 -0
- package/dist/betterAuthMethods.js +55 -0
- package/dist/betterAuthOAuthHandoff.d.ts +10 -2
- package/dist/betterAuthOAuthHandoff.js +222 -24
- package/dist/betterAuthSetPassword.d.ts +33 -0
- package/dist/betterAuthSetPassword.js +166 -0
- package/dist/betterAuthSetup.d.ts +57 -8
- package/dist/betterAuthSetup.js +327 -36
- package/dist/consentApp.d.ts +9 -4
- package/dist/consentApp.js +344 -28
- package/dist/consentStatus.d.ts +60 -0
- package/dist/consentStatus.js +111 -0
- package/dist/errors.d.ts +6 -0
- package/dist/errors.js +41 -2
- package/dist/expressServer.js +3 -2
- package/dist/index.d.ts +14 -2
- package/dist/index.js +23 -3
- package/dist/mcp/oauth.js +151 -11
- package/dist/mcp/oauthMetadata.d.ts +2 -1
- package/dist/mcp/oauthMetadata.js +7 -4
- package/dist/models/consentResponse.js +51 -0
- package/dist/models/mcpOAuthClient.js +78 -2
- package/dist/models/mcpOAuthToken.js +28 -4
- package/dist/orgs/membershipGuards.d.ts +7 -0
- package/dist/orgs/membershipGuards.js +32 -0
- package/dist/orgs/orgsApp.d.ts +19 -1
- package/dist/orgs/orgsApp.js +55 -24
- package/dist/rateLimit/policies.js +8 -0
- package/dist/rbac/access.js +2 -1
- package/dist/rbac/resolve.d.ts +9 -1
- package/dist/rbac/resolve.js +21 -16
- package/dist/rbac/roleManager.d.ts +6 -0
- package/dist/rbac/roleManager.js +8 -7
- package/dist/rbac/roleModel.d.ts +1 -0
- package/dist/rbac/roleModel.js +45 -3
- package/dist/rbac/statements.d.ts +2 -2
- package/dist/rbac/statements.js +10 -1
- package/dist/rbac/types.d.ts +6 -0
- package/dist/rbac/userWriteGuards.js +2 -1
- package/dist/schemaMetadata.js +8 -1
- package/dist/seedRunner.js +7 -2
- package/dist/terrenoApp.d.ts +4 -0
- package/dist/terrenoApp.js +15 -0
- package/dist/tests/models.d.ts +5 -0
- package/dist/tests/models.js +8 -0
- package/dist/types/consentResponse.d.ts +8 -0
- package/dist/types/mcpOAuth.d.ts +35 -2
- package/package.json +6 -3
- package/src/__snapshots__/openApi.test.ts.snap +288 -0
- package/src/__snapshots__/openApiBuilder.test.ts.snap +96 -0
- package/src/accountSecurity/accessActions.test.ts +723 -0
- package/src/accountSecurity/accessActions.ts +173 -0
- package/src/accountSecurity/accountSecurity.test.ts +947 -0
- package/src/accountSecurity/accountSecurityApp.ts +720 -0
- package/src/accountSecurity/accountSecurityService.ts +826 -0
- package/src/accountSecurity/credentialActions.test.ts +867 -0
- package/src/accountSecurity/credentialActions.ts +260 -0
- package/src/accountSecurity/membershipActions.ts +89 -0
- package/src/accountSecurity/orgSecurityRoutes.test.ts +841 -0
- package/src/accountSecurity/orgSecurityRoutes.ts +232 -0
- package/src/accountSecurity/securityEventHooks.ts +294 -0
- package/src/accountSecurity/securityEvents.ts +165 -0
- package/src/accountSecurity/securityNotifications.test.ts +1323 -0
- package/src/accountSecurity/securityNotifications.ts +516 -0
- package/src/api.arrayOperations.test.ts +71 -0
- package/src/api.ts +38 -7
- package/src/audit/auditEventModel.ts +10 -7
- package/src/audit/processQueue.ts +4 -2
- package/src/audit/record.test.ts +15 -0
- package/src/audit/record.ts +10 -2
- package/src/authRecovery.test.ts +23 -0
- package/src/authRecovery.ts +34 -1
- package/src/authRecoveryBetterAuth.test.ts +169 -0
- package/src/betterAuth.ts +194 -8
- package/src/betterAuthApp.ts +32 -0
- package/src/betterAuthChallengeHeaders.test.ts +476 -0
- package/src/betterAuthChallengeHeaders.ts +227 -0
- package/src/betterAuthFeatures.test.ts +489 -0
- package/src/betterAuthFeatures.ts +283 -0
- package/src/betterAuthLinking.test.ts +844 -0
- package/src/betterAuthMethods.test.ts +138 -0
- package/src/betterAuthMethods.ts +98 -0
- package/src/betterAuthOAuthHandoff.test.ts +482 -69
- package/src/betterAuthOAuthHandoff.ts +293 -33
- package/src/betterAuthOAuthProxy.test.ts +421 -0
- package/src/betterAuthPasswordReset.test.ts +190 -0
- package/src/betterAuthSetPassword.test.ts +378 -0
- package/src/betterAuthSetPassword.ts +213 -0
- package/src/betterAuthSetup.test.ts +39 -7
- package/src/betterAuthSetup.ts +457 -38
- package/src/consentApp.test.ts +869 -2
- package/src/consentApp.ts +438 -36
- package/src/consentStatus.test.ts +226 -0
- package/src/consentStatus.ts +171 -0
- package/src/errors.test.ts +81 -0
- package/src/errors.ts +50 -1
- package/src/expressServer.test.ts +29 -0
- package/src/expressServer.ts +3 -2
- package/src/index.ts +30 -0
- package/src/mcp/oauth.test.ts +463 -2
- package/src/mcp/oauth.ts +184 -13
- package/src/mcp/oauthMetadata.ts +5 -2
- package/src/models/consentResponse.ts +53 -0
- package/src/models/mcpOAuthClient.ts +100 -3
- package/src/models/mcpOAuthToken.ts +30 -5
- package/src/orgs/membershipGuards.ts +39 -0
- package/src/orgs/orgRbac.test.ts +1 -1
- package/src/orgs/orgsApp.test.ts +101 -2
- package/src/orgs/orgsApp.ts +69 -28
- package/src/rateLimit/policies.test.ts +53 -0
- package/src/rateLimit/policies.ts +8 -0
- package/src/rbac/access.ts +4 -1
- package/src/rbac/resolve.test.ts +65 -0
- package/src/rbac/resolve.ts +37 -17
- package/src/rbac/roleManager.test.ts +64 -0
- package/src/rbac/roleManager.ts +15 -6
- package/src/rbac/roleModel.test.ts +174 -1
- package/src/rbac/roleModel.ts +66 -3
- package/src/rbac/routes.test.ts +58 -0
- package/src/rbac/statements.test.ts +4 -1
- package/src/rbac/statements.ts +11 -1
- package/src/rbac/types.ts +6 -1
- package/src/rbac/userWriteGuards.test.ts +48 -0
- package/src/rbac/userWriteGuards.ts +5 -2
- package/src/schemaMetadata.test.ts +39 -0
- package/src/schemaMetadata.ts +11 -1
- package/src/seedRunner.test.ts +56 -0
- package/src/seedRunner.ts +7 -2
- package/src/terrenoApp.ts +20 -0
- package/src/tests/accountSecurityTestApp.ts +290 -0
- package/src/tests/betterAuthStubs.ts +188 -0
- package/src/tests/mockOidcProvider.ts +105 -0
- package/src/tests/models.ts +13 -0
- package/src/tests/softwareAuthenticator.ts +183 -0
- package/src/types/consentResponse.ts +8 -0
- package/src/types/mcpOAuth.ts +39 -3
|
@@ -0,0 +1,723 @@
|
|
|
1
|
+
import {afterEach, beforeAll, beforeEach, describe, expect, it} from "bun:test";
|
|
2
|
+
import express from "express";
|
|
3
|
+
import {DateTime} from "luxon";
|
|
4
|
+
import mongoose from "mongoose";
|
|
5
|
+
import passportLocalMongoose from "passport-local-mongoose";
|
|
6
|
+
import supertest from "supertest";
|
|
7
|
+
|
|
8
|
+
import {resetAuditRecorderForTests} from "../audit/record";
|
|
9
|
+
import type {User, UserModel} from "../auth";
|
|
10
|
+
import type {BetterAuthApp} from "../betterAuthApp";
|
|
11
|
+
import {apiErrorMiddleware} from "../errors";
|
|
12
|
+
import {type AuthenticatableSocket, createBetterAuthValidator} from "../realtime/socketAuth";
|
|
13
|
+
import {type RequestCredential, runWithRequestContext} from "../requestContext";
|
|
14
|
+
import {
|
|
15
|
+
access,
|
|
16
|
+
betterAuthCollection,
|
|
17
|
+
betterAuthIdFor,
|
|
18
|
+
buildApp,
|
|
19
|
+
captureMail,
|
|
20
|
+
getSummary,
|
|
21
|
+
resetAccountSecurityState,
|
|
22
|
+
resetTokenFrom,
|
|
23
|
+
SecurityUser,
|
|
24
|
+
securityEventsFor,
|
|
25
|
+
securityUserModel,
|
|
26
|
+
signInWithPassword,
|
|
27
|
+
signUp,
|
|
28
|
+
} from "../tests/accountSecurityTestApp";
|
|
29
|
+
import {PASSWORD, uniqueEmail} from "../tests/betterAuthStubs";
|
|
30
|
+
import {AccountSecurityApp} from "./accountSecurityApp";
|
|
31
|
+
import {createAccountSecurityService} from "./accountSecurityService";
|
|
32
|
+
import {clearSecurityActionListenersForTests} from "./securityEvents";
|
|
33
|
+
|
|
34
|
+
const NEW_PASSWORD = "new-password-654321";
|
|
35
|
+
|
|
36
|
+
const PUBLIC_APP_URL = "https://app.example.com";
|
|
37
|
+
|
|
38
|
+
const runAction = (
|
|
39
|
+
app: express.Application,
|
|
40
|
+
token: string,
|
|
41
|
+
userId: string,
|
|
42
|
+
action: string
|
|
43
|
+
): supertest.Test =>
|
|
44
|
+
supertest(app)
|
|
45
|
+
.post(`/users/${userId}/security/${action}`)
|
|
46
|
+
.set("authorization", `Bearer ${token}`)
|
|
47
|
+
.send({});
|
|
48
|
+
|
|
49
|
+
const getMe = (app: express.Application, token: string): supertest.Test =>
|
|
50
|
+
supertest(app).get("/auth/me").set("authorization", `Bearer ${token}`);
|
|
51
|
+
|
|
52
|
+
const actionIdsOf = (res: supertest.Response): string[] =>
|
|
53
|
+
(res.body.data.actions as {id: string}[]).map((action) => action.id);
|
|
54
|
+
|
|
55
|
+
/** Runs RealtimeApp's Better Auth handshake validator on a stub socket. */
|
|
56
|
+
const validateSocket = async (
|
|
57
|
+
betterAuthApp: BetterAuthApp,
|
|
58
|
+
token: string
|
|
59
|
+
): Promise<AuthenticatableSocket> => {
|
|
60
|
+
const socket = {handshake: {auth: {token}}} as unknown as AuthenticatableSocket;
|
|
61
|
+
await createBetterAuthValidator({auth: betterAuthApp.ensureAuth(), userModel: securityUserModel})(
|
|
62
|
+
socket
|
|
63
|
+
);
|
|
64
|
+
return socket;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
interface PlainUserFields {
|
|
68
|
+
admin?: boolean;
|
|
69
|
+
betterAuthId?: string;
|
|
70
|
+
email: string;
|
|
71
|
+
name?: string;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** An app User model with neither `disabled` nor `tokenEpoch`. */
|
|
75
|
+
const PlainUser = ((): mongoose.Model<PlainUserFields> => {
|
|
76
|
+
if (mongoose.models.AccountSecurityPlainUser) {
|
|
77
|
+
return mongoose.models.AccountSecurityPlainUser as mongoose.Model<PlainUserFields>;
|
|
78
|
+
}
|
|
79
|
+
const schema = new mongoose.Schema<PlainUserFields>({
|
|
80
|
+
admin: {default: false, description: "Whether the user is an admin", type: Boolean},
|
|
81
|
+
betterAuthId: {description: "Linked Better Auth user id", index: true, type: String},
|
|
82
|
+
email: {description: "Email address", required: true, type: String},
|
|
83
|
+
name: {description: "Display name", type: String},
|
|
84
|
+
});
|
|
85
|
+
schema.plugin(
|
|
86
|
+
passportLocalMongoose as unknown as (
|
|
87
|
+
target: mongoose.Schema,
|
|
88
|
+
options?: Record<string, unknown>
|
|
89
|
+
) => void,
|
|
90
|
+
{usernameField: "email"}
|
|
91
|
+
);
|
|
92
|
+
return mongoose.model<PlainUserFields>("AccountSecurityPlainUser", schema);
|
|
93
|
+
})();
|
|
94
|
+
|
|
95
|
+
/** A legacy app User with a JWT (passport) password. */
|
|
96
|
+
const registerWithPassword = async (email: string): Promise<string> => {
|
|
97
|
+
const user = await (
|
|
98
|
+
SecurityUser as unknown as {
|
|
99
|
+
register: (user: unknown, password: string) => Promise<{_id: unknown}>;
|
|
100
|
+
}
|
|
101
|
+
).register(new SecurityUser({email, name: "Has Password"}), PASSWORD);
|
|
102
|
+
return String(user._id);
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/** A legacy app User with no Better Auth link and no password, like a GitHub-JWT user. */
|
|
106
|
+
const createLegacyUser = async (label: string): Promise<{email: string; id: string}> => {
|
|
107
|
+
const email = uniqueEmail(label);
|
|
108
|
+
const user = await SecurityUser.create({email, name: label});
|
|
109
|
+
return {email, id: String(user._id)};
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
const signInWithNewPassword = (app: express.Application, email: string): supertest.Test =>
|
|
113
|
+
supertest(app)
|
|
114
|
+
.post("/api/auth/sign-in/email")
|
|
115
|
+
.set("content-type", "application/json")
|
|
116
|
+
.send({email, password: NEW_PASSWORD});
|
|
117
|
+
|
|
118
|
+
const resetPassword = (app: express.Application, token: string): supertest.Test =>
|
|
119
|
+
supertest(app)
|
|
120
|
+
.post("/api/auth/reset-password")
|
|
121
|
+
.set("content-type", "application/json")
|
|
122
|
+
.send({newPassword: NEW_PASSWORD, token});
|
|
123
|
+
|
|
124
|
+
const tokenFromUrl = (url: string): string => new URL(url).searchParams.get("token") ?? "";
|
|
125
|
+
|
|
126
|
+
describe("access recovery actions", () => {
|
|
127
|
+
beforeAll(async () => {
|
|
128
|
+
await access.roles.seedDefaults();
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
beforeEach(async () => {
|
|
132
|
+
await resetAccountSecurityState();
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
afterEach(() => {
|
|
136
|
+
resetAuditRecorderForTests();
|
|
137
|
+
clearSecurityActionListenersForTests();
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
describe("revoke-sessions", () => {
|
|
141
|
+
it("deletes every session, so the old bearer gets 401, and bumps tokenEpoch", async () => {
|
|
142
|
+
const {app} = buildApp();
|
|
143
|
+
const operator = await signUp(app, "operator-revoke", ["operator"]);
|
|
144
|
+
const target = await signUp(app, "revoke-target");
|
|
145
|
+
const second = await signInWithPassword(app, target.email);
|
|
146
|
+
expect(second.status).toBe(200);
|
|
147
|
+
expect((await getMe(app, target.token)).status).toBe(200);
|
|
148
|
+
const summary = await getSummary(app, operator.token, target.id);
|
|
149
|
+
expect(summary.body.data.sessions.active).toBe(2);
|
|
150
|
+
expect(actionIdsOf(summary)).toContain("revoke-sessions");
|
|
151
|
+
|
|
152
|
+
const res = await runAction(app, operator.token, target.id, "revoke-sessions");
|
|
153
|
+
|
|
154
|
+
expect(res.status).toBe(200);
|
|
155
|
+
expect(res.body.data).toEqual({
|
|
156
|
+
action: "revoke-sessions",
|
|
157
|
+
changed: true,
|
|
158
|
+
jwtRefreshRevoked: true,
|
|
159
|
+
revokedCount: 2,
|
|
160
|
+
});
|
|
161
|
+
expect((await getMe(app, target.token)).status).toBe(401);
|
|
162
|
+
expect((await getMe(app, second.body.token)).status).toBe(401);
|
|
163
|
+
expect(
|
|
164
|
+
await betterAuthCollection("session").countDocuments({
|
|
165
|
+
userId: await betterAuthIdFor(target.id),
|
|
166
|
+
})
|
|
167
|
+
).toBe(0);
|
|
168
|
+
expect((await SecurityUser.findById(target.id))?.tokenEpoch).toBe(1);
|
|
169
|
+
const events = await securityEventsFor(target.id);
|
|
170
|
+
expect(events).toHaveLength(1);
|
|
171
|
+
expect(events[0].after).toEqual({
|
|
172
|
+
jwtRefreshRevoked: true,
|
|
173
|
+
revokedCount: 2,
|
|
174
|
+
securityAction: "revoke-sessions",
|
|
175
|
+
});
|
|
176
|
+
const after = await getSummary(app, operator.token, target.id);
|
|
177
|
+
expect(after.body.data.sessions.active).toBe(0);
|
|
178
|
+
expect(actionIdsOf(after)).not.toContain("revoke-sessions");
|
|
179
|
+
// The user can sign in again.
|
|
180
|
+
expect((await signInWithPassword(app, target.email)).status).toBe(200);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it("makes a legacy JWT refresh token fail", async () => {
|
|
184
|
+
const {app} = buildApp();
|
|
185
|
+
const operator = await signUp(app, "operator-revoke-jwt", ["operator"]);
|
|
186
|
+
const email = uniqueEmail("revoke-jwt");
|
|
187
|
+
const userId = await registerWithPassword(email);
|
|
188
|
+
const login = await supertest(app).post("/auth/login").send({email, password: PASSWORD});
|
|
189
|
+
expect(login.status).toBe(200);
|
|
190
|
+
const refreshToken = login.body.data.refreshToken as string;
|
|
191
|
+
|
|
192
|
+
const res = await runAction(app, operator.token, userId, "revoke-sessions");
|
|
193
|
+
|
|
194
|
+
expect(res.status).toBe(200);
|
|
195
|
+
expect(res.body.data.jwtRefreshRevoked).toBe(true);
|
|
196
|
+
const refresh = await supertest(app).post("/auth/refresh_token").send({refreshToken});
|
|
197
|
+
expect(refresh.status).toBe(401);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it("bumps tokenEpoch for a user with no Better Auth sessions", async () => {
|
|
201
|
+
const {app} = buildApp();
|
|
202
|
+
const operator = await signUp(app, "operator-revoke-legacy", ["operator"]);
|
|
203
|
+
const legacy = await createLegacyUser("revoke-legacy");
|
|
204
|
+
|
|
205
|
+
const res = await runAction(app, operator.token, legacy.id, "revoke-sessions");
|
|
206
|
+
|
|
207
|
+
expect(res.status).toBe(200);
|
|
208
|
+
expect(res.body.data.revokedCount).toBe(0);
|
|
209
|
+
expect(res.body.data.changed).toBe(true);
|
|
210
|
+
expect((await SecurityUser.findById(legacy.id))?.tokenEpoch).toBe(1);
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
describe("send-password-reset", () => {
|
|
215
|
+
it("mails a reset link that sets a new password", async () => {
|
|
216
|
+
const mail = captureMail();
|
|
217
|
+
const {app} = buildApp({config: mail.config});
|
|
218
|
+
const operator = await signUp(app, "operator-send-reset", ["operator"]);
|
|
219
|
+
const target = await signUp(app, "send-reset-target");
|
|
220
|
+
mail.sent.length = 0;
|
|
221
|
+
|
|
222
|
+
const res = await runAction(app, operator.token, target.id, "send-password-reset");
|
|
223
|
+
|
|
224
|
+
expect(res.status).toBe(200);
|
|
225
|
+
expect(res.body.data).toEqual({action: "send-password-reset", changed: true, sent: true});
|
|
226
|
+
expect(mail.sent.map((sent) => [sent.to, sent.subject])).toEqual([
|
|
227
|
+
[target.email, "Reset your password"],
|
|
228
|
+
]);
|
|
229
|
+
expect(mail.sent[0].text).toContain(`${PUBLIC_APP_URL}/resetPassword?token=`);
|
|
230
|
+
const events = await securityEventsFor(target.id);
|
|
231
|
+
expect(events.map((event) => event.after)).toEqual([{securityAction: "send-password-reset"}]);
|
|
232
|
+
|
|
233
|
+
expect((await resetPassword(app, resetTokenFrom(mail.sent[0]))).status).toBe(200);
|
|
234
|
+
expect((await signInWithNewPassword(app, target.email)).status).toBe(200);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it("answers 501 when publicAppUrl is unset", async () => {
|
|
238
|
+
const mail = captureMail();
|
|
239
|
+
const {app} = buildApp({config: {sendMail: mail.config.sendMail}});
|
|
240
|
+
const operator = await signUp(app, "operator-no-url", ["operator"]);
|
|
241
|
+
const target = await signUp(app, "no-url-target");
|
|
242
|
+
mail.sent.length = 0;
|
|
243
|
+
|
|
244
|
+
const res = await runAction(app, operator.token, target.id, "send-password-reset");
|
|
245
|
+
|
|
246
|
+
expect(res.status).toBe(501);
|
|
247
|
+
expect(res.body.title).toBe("publicAppUrl is required to send recovery mail");
|
|
248
|
+
expect(mail.sent).toHaveLength(0);
|
|
249
|
+
expect(await securityEventsFor(target.id)).toHaveLength(0);
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it("answers 501 when sendMail is unset", async () => {
|
|
253
|
+
const {app} = buildApp();
|
|
254
|
+
const operator = await signUp(app, "operator-no-mail", ["operator"]);
|
|
255
|
+
const target = await signUp(app, "no-mail-target");
|
|
256
|
+
|
|
257
|
+
const res = await runAction(app, operator.token, target.id, "send-password-reset");
|
|
258
|
+
|
|
259
|
+
expect(res.status).toBe(501);
|
|
260
|
+
expect(res.body.title).toBe("Password reset email is not configured");
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
it("refuses a user with no Better Auth account with 409", async () => {
|
|
264
|
+
const mail = captureMail();
|
|
265
|
+
const {app} = buildApp({config: mail.config});
|
|
266
|
+
const operator = await signUp(app, "operator-reset-legacy", ["operator"]);
|
|
267
|
+
const legacy = await createLegacyUser("reset-legacy");
|
|
268
|
+
|
|
269
|
+
const res = await runAction(app, operator.token, legacy.id, "send-password-reset");
|
|
270
|
+
|
|
271
|
+
expect(res.status).toBe(409);
|
|
272
|
+
expect(res.body.title).toBe("User has no Better Auth account");
|
|
273
|
+
expect(await securityEventsFor(legacy.id)).toHaveLength(0);
|
|
274
|
+
});
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
describe("disable and enable", () => {
|
|
278
|
+
it("disable refuses REST and the socket handshake; enable lets the user back in", async () => {
|
|
279
|
+
const {app, betterAuthApp} = buildApp();
|
|
280
|
+
const operator = await signUp(app, "operator-disable", ["operator"]);
|
|
281
|
+
const target = await signUp(app, "disable-target");
|
|
282
|
+
expect((await validateSocket(betterAuthApp, target.token)).decodedToken?.id).toBe(target.id);
|
|
283
|
+
expect(actionIdsOf(await getSummary(app, operator.token, target.id))).toContain("disable");
|
|
284
|
+
|
|
285
|
+
const disabled = await runAction(app, operator.token, target.id, "disable");
|
|
286
|
+
|
|
287
|
+
expect(disabled.status).toBe(200);
|
|
288
|
+
expect(disabled.body.data).toEqual({
|
|
289
|
+
action: "disable",
|
|
290
|
+
changed: true,
|
|
291
|
+
jwtRefreshRevoked: true,
|
|
292
|
+
revokedCount: 1,
|
|
293
|
+
});
|
|
294
|
+
expect((await SecurityUser.findById(target.id))?.disabled).toBe(true);
|
|
295
|
+
expect((await getMe(app, target.token)).status).toBe(401);
|
|
296
|
+
// A fresh Better Auth session for the disabled user authenticates nothing.
|
|
297
|
+
const signIn = await signInWithPassword(app, target.email);
|
|
298
|
+
expect(signIn.status).toBe(200);
|
|
299
|
+
const freshToken = signIn.body.token as string;
|
|
300
|
+
expect((await getMe(app, freshToken)).status).toBe(401);
|
|
301
|
+
await expect(validateSocket(betterAuthApp, freshToken)).rejects.toThrow(
|
|
302
|
+
"Unauthorized: user is disabled"
|
|
303
|
+
);
|
|
304
|
+
const summary = await getSummary(app, operator.token, target.id);
|
|
305
|
+
expect(summary.body.data.disabled).toBe(true);
|
|
306
|
+
expect(actionIdsOf(summary)).toContain("enable");
|
|
307
|
+
expect(actionIdsOf(summary)).not.toContain("disable");
|
|
308
|
+
|
|
309
|
+
const again = await runAction(app, operator.token, target.id, "disable");
|
|
310
|
+
expect(again.body.data.changed).toBe(false);
|
|
311
|
+
|
|
312
|
+
const enabled = await runAction(app, operator.token, target.id, "enable");
|
|
313
|
+
|
|
314
|
+
expect(enabled.status).toBe(200);
|
|
315
|
+
expect(enabled.body.data).toEqual({action: "enable", changed: true});
|
|
316
|
+
const back = await signInWithPassword(app, target.email);
|
|
317
|
+
expect(back.status).toBe(200);
|
|
318
|
+
expect((await getMe(app, back.body.token)).status).toBe(200);
|
|
319
|
+
expect((await validateSocket(betterAuthApp, back.body.token)).decodedToken?.id).toBe(
|
|
320
|
+
target.id
|
|
321
|
+
);
|
|
322
|
+
const events = await securityEventsFor(target.id);
|
|
323
|
+
expect(events.map((event) => event.after)).toEqual([
|
|
324
|
+
{jwtRefreshRevoked: true, revokedCount: 1, securityAction: "disable"},
|
|
325
|
+
{securityAction: "enable"},
|
|
326
|
+
]);
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
it("enable on an enabled user changes nothing and records nothing", async () => {
|
|
330
|
+
const {app} = buildApp();
|
|
331
|
+
const operator = await signUp(app, "operator-enable-noop", ["operator"]);
|
|
332
|
+
const target = await signUp(app, "enable-noop-target");
|
|
333
|
+
|
|
334
|
+
const res = await runAction(app, operator.token, target.id, "enable");
|
|
335
|
+
|
|
336
|
+
expect(res.status).toBe(200);
|
|
337
|
+
expect(res.body.data.changed).toBe(false);
|
|
338
|
+
expect(await securityEventsFor(target.id)).toHaveLength(0);
|
|
339
|
+
});
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
describe("issue-sign-in-link", () => {
|
|
343
|
+
it("links a legacy user, and the link sets a password once for the same app User", async () => {
|
|
344
|
+
const {app, betterAuthApp} = buildApp({config: {publicAppUrl: PUBLIC_APP_URL}});
|
|
345
|
+
const operator = await signUp(app, "superadmin-link", ["superadmin"]);
|
|
346
|
+
const legacy = await createLegacyUser("link-legacy");
|
|
347
|
+
const usersBefore = await SecurityUser.countDocuments({});
|
|
348
|
+
const summary = await getSummary(app, operator.token, legacy.id);
|
|
349
|
+
expect(summary.body.data.legacyPassword).toBe(false);
|
|
350
|
+
expect(actionIdsOf(summary)).toContain("issue-sign-in-link");
|
|
351
|
+
|
|
352
|
+
const res = await runAction(app, operator.token, legacy.id, "issue-sign-in-link");
|
|
353
|
+
|
|
354
|
+
expect(res.status).toBe(200);
|
|
355
|
+
expect(res.body.data.action).toBe("issue-sign-in-link");
|
|
356
|
+
const url = res.body.data.url as string;
|
|
357
|
+
expect(url.startsWith(`${PUBLIC_APP_URL}/resetPassword?token=`)).toBe(true);
|
|
358
|
+
expect(DateTime.fromISO(res.body.data.expiresAt) > DateTime.now()).toBe(true);
|
|
359
|
+
const betterAuthId = await betterAuthIdFor(legacy.id);
|
|
360
|
+
expect(betterAuthId).not.toBe("");
|
|
361
|
+
const context = await betterAuthApp.ensureAuth().$context;
|
|
362
|
+
const betterAuthUser = await context.internalAdapter.findUserById(betterAuthId);
|
|
363
|
+
expect(betterAuthUser?.email).toBe(legacy.email.toLowerCase());
|
|
364
|
+
expect(betterAuthUser?.emailVerified).toBe(false);
|
|
365
|
+
const events = await securityEventsFor(legacy.id);
|
|
366
|
+
expect(events).toHaveLength(1);
|
|
367
|
+
expect(JSON.stringify(events[0].after)).not.toContain(tokenFromUrl(url));
|
|
368
|
+
expect(events[0].after).toEqual({
|
|
369
|
+
betterAuthId,
|
|
370
|
+
expiresAt: res.body.data.expiresAt,
|
|
371
|
+
securityAction: "issue-sign-in-link",
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
expect((await resetPassword(app, tokenFromUrl(url))).status).toBe(200);
|
|
375
|
+
const signIn = await signInWithNewPassword(app, legacy.email);
|
|
376
|
+
expect(signIn.status).toBe(200);
|
|
377
|
+
const me = await getMe(app, signIn.body.token);
|
|
378
|
+
expect(me.status).toBe(200);
|
|
379
|
+
expect(String(me.body.data._id ?? me.body.data.id)).toBe(legacy.id);
|
|
380
|
+
expect(await SecurityUser.countDocuments({})).toBe(usersBefore);
|
|
381
|
+
|
|
382
|
+
const reuse = await resetPassword(app, tokenFromUrl(url));
|
|
383
|
+
expect(reuse.status).toBe(400);
|
|
384
|
+
const linkedSummary = await getSummary(app, operator.token, legacy.id);
|
|
385
|
+
expect(actionIdsOf(linkedSummary)).not.toContain("issue-sign-in-link");
|
|
386
|
+
const second = await runAction(app, operator.token, legacy.id, "issue-sign-in-link");
|
|
387
|
+
expect(second.status).toBe(409);
|
|
388
|
+
expect(second.body.title).toBe("User already has a Better Auth account");
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
it("refuses a user who already has a Better Auth account", async () => {
|
|
392
|
+
const {app} = buildApp({config: {publicAppUrl: PUBLIC_APP_URL}});
|
|
393
|
+
const operator = await signUp(app, "superadmin-link-linked", ["superadmin"]);
|
|
394
|
+
const target = await signUp(app, "link-linked");
|
|
395
|
+
|
|
396
|
+
const res = await runAction(app, operator.token, target.id, "issue-sign-in-link");
|
|
397
|
+
|
|
398
|
+
expect(res.status).toBe(409);
|
|
399
|
+
expect(res.body.title).toBe("User already has a Better Auth account");
|
|
400
|
+
expect(await securityEventsFor(target.id)).toHaveLength(0);
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
it("refuses a legacy user who has a password", async () => {
|
|
404
|
+
const {app} = buildApp({config: {publicAppUrl: PUBLIC_APP_URL}});
|
|
405
|
+
const operator = await signUp(app, "superadmin-link-password", ["superadmin"]);
|
|
406
|
+
const id = await registerWithPassword(uniqueEmail("link-password"));
|
|
407
|
+
const summary = await getSummary(app, operator.token, id);
|
|
408
|
+
expect(summary.body.data.legacyPassword).toBe(true);
|
|
409
|
+
expect(actionIdsOf(summary)).not.toContain("issue-sign-in-link");
|
|
410
|
+
|
|
411
|
+
const res = await runAction(app, operator.token, id, "issue-sign-in-link");
|
|
412
|
+
|
|
413
|
+
expect(res.status).toBe(409);
|
|
414
|
+
expect(res.body.title).toBe("User already has a password");
|
|
415
|
+
expect(await betterAuthIdFor(id)).toBe("");
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
it("refuses when another Better Auth user already has the email", async () => {
|
|
419
|
+
const {app, betterAuthApp} = buildApp({config: {publicAppUrl: PUBLIC_APP_URL}});
|
|
420
|
+
const operator = await signUp(app, "superadmin-link-taken", ["superadmin"]);
|
|
421
|
+
const legacy = await createLegacyUser("link-taken");
|
|
422
|
+
const context = await betterAuthApp.ensureAuth().$context;
|
|
423
|
+
await context.adapter.create({
|
|
424
|
+
data: {email: legacy.email.toLowerCase(), emailVerified: false, name: "Other"},
|
|
425
|
+
model: "user",
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
const res = await runAction(app, operator.token, legacy.id, "issue-sign-in-link");
|
|
429
|
+
|
|
430
|
+
expect(res.status).toBe(409);
|
|
431
|
+
expect(res.body.title).toBe("Email already belongs to another sign-in account");
|
|
432
|
+
expect(await betterAuthIdFor(legacy.id)).toBe("");
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
it("refuses a user with no email", async () => {
|
|
436
|
+
const {app} = buildApp({config: {publicAppUrl: PUBLIC_APP_URL}});
|
|
437
|
+
const operator = await signUp(app, "superadmin-link-no-email", ["superadmin"]);
|
|
438
|
+
const legacy = await createLegacyUser("link-no-email");
|
|
439
|
+
await SecurityUser.collection.updateOne(
|
|
440
|
+
{_id: (await SecurityUser.findById(legacy.id))?._id},
|
|
441
|
+
{$set: {email: ""}}
|
|
442
|
+
);
|
|
443
|
+
|
|
444
|
+
const res = await runAction(app, operator.token, legacy.id, "issue-sign-in-link");
|
|
445
|
+
|
|
446
|
+
expect(res.status).toBe(409);
|
|
447
|
+
expect(res.body.title).toBe("User has no email");
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
it("relinks a user whose Better Auth user is gone", async () => {
|
|
451
|
+
const {app} = buildApp({config: {publicAppUrl: PUBLIC_APP_URL}});
|
|
452
|
+
const operator = await signUp(app, "superadmin-link-dangling", ["superadmin"]);
|
|
453
|
+
const legacy = await createLegacyUser("link-dangling");
|
|
454
|
+
await SecurityUser.updateOne({_id: legacy.id}, {$set: {betterAuthId: "gone"}});
|
|
455
|
+
|
|
456
|
+
const res = await runAction(app, operator.token, legacy.id, "issue-sign-in-link");
|
|
457
|
+
|
|
458
|
+
expect(res.status).toBe(200);
|
|
459
|
+
const betterAuthId = await betterAuthIdFor(legacy.id);
|
|
460
|
+
expect(betterAuthId).not.toBe("gone");
|
|
461
|
+
expect(betterAuthId).not.toBe("");
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
it("answers 501 when publicAppUrl is unset and creates nothing", async () => {
|
|
465
|
+
const {app} = buildApp();
|
|
466
|
+
const operator = await signUp(app, "superadmin-link-no-url", ["superadmin"]);
|
|
467
|
+
const legacy = await createLegacyUser("link-no-url");
|
|
468
|
+
|
|
469
|
+
const res = await runAction(app, operator.token, legacy.id, "issue-sign-in-link");
|
|
470
|
+
|
|
471
|
+
expect(res.status).toBe(501);
|
|
472
|
+
expect(res.body.title).toBe("Sign-in links are not configured");
|
|
473
|
+
expect(await betterAuthCollection("user").countDocuments({email: legacy.email})).toBe(0);
|
|
474
|
+
});
|
|
475
|
+
|
|
476
|
+
it("uses AccountSecurityApp's own publicAppUrl over the Better Auth config", async () => {
|
|
477
|
+
const {app} = buildApp({security: {publicAppUrl: "https://admin.example.com/"}});
|
|
478
|
+
const operator = await signUp(app, "superadmin-link-own-url", ["superadmin"]);
|
|
479
|
+
const legacy = await createLegacyUser("link-own-url");
|
|
480
|
+
|
|
481
|
+
const res = await runAction(app, operator.token, legacy.id, "issue-sign-in-link");
|
|
482
|
+
|
|
483
|
+
expect(res.status).toBe(200);
|
|
484
|
+
expect(res.body.data.url.startsWith("https://admin.example.com/resetPassword?token=")).toBe(
|
|
485
|
+
true
|
|
486
|
+
);
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
it("needs user:issueSignInLink: an operator gets 403 and does not see it", async () => {
|
|
490
|
+
const {app} = buildApp({config: {publicAppUrl: PUBLIC_APP_URL}});
|
|
491
|
+
const operator = await signUp(app, "operator-link-denied", ["operator"]);
|
|
492
|
+
const legacy = await createLegacyUser("link-denied");
|
|
493
|
+
|
|
494
|
+
const summary = await getSummary(app, operator.token, legacy.id);
|
|
495
|
+
expect(actionIdsOf(summary)).toEqual(["disable"]);
|
|
496
|
+
const res = await runAction(app, operator.token, legacy.id, "issue-sign-in-link");
|
|
497
|
+
|
|
498
|
+
expect(res.status).toBe(403);
|
|
499
|
+
expect(res.body.title).toBe("Access denied");
|
|
500
|
+
expect(await betterAuthIdFor(legacy.id)).toBe("");
|
|
501
|
+
expect(await securityEventsFor(legacy.id)).toHaveLength(0);
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
it("without RBAC, an admin may issue a link", async () => {
|
|
505
|
+
const {app} = buildApp({accessControl: false, config: {publicAppUrl: PUBLIC_APP_URL}});
|
|
506
|
+
const admin = await signUp(app, "admin-link");
|
|
507
|
+
await SecurityUser.updateOne({_id: admin.id}, {$set: {admin: true}});
|
|
508
|
+
const legacy = await createLegacyUser("admin-link-target");
|
|
509
|
+
|
|
510
|
+
const res = await runAction(app, admin.token, legacy.id, "issue-sign-in-link");
|
|
511
|
+
|
|
512
|
+
expect(res.status).toBe(200);
|
|
513
|
+
expect(await betterAuthIdFor(legacy.id)).not.toBe("");
|
|
514
|
+
});
|
|
515
|
+
|
|
516
|
+
it("reissues an expired link that was never used, for the same Better Auth user", async () => {
|
|
517
|
+
const {app} = buildApp({config: {publicAppUrl: PUBLIC_APP_URL}});
|
|
518
|
+
const superadmin = await signUp(app, "superadmin-reissue", ["superadmin"]);
|
|
519
|
+
const legacy = await createLegacyUser("reissue-target");
|
|
520
|
+
const first = await runAction(app, superadmin.token, legacy.id, "issue-sign-in-link");
|
|
521
|
+
expect(first.status).toBe(200);
|
|
522
|
+
const betterAuthId = await betterAuthIdFor(legacy.id);
|
|
523
|
+
await betterAuthCollection("verification").updateMany(
|
|
524
|
+
{value: betterAuthId},
|
|
525
|
+
{$set: {expiresAt: DateTime.now().minus({minutes: 1}).toJSDate()}}
|
|
526
|
+
);
|
|
527
|
+
expect((await resetPassword(app, tokenFromUrl(first.body.data.url))).status).toBe(400);
|
|
528
|
+
const summary = await getSummary(app, superadmin.token, legacy.id);
|
|
529
|
+
expect(actionIdsOf(summary)).toContain("issue-sign-in-link");
|
|
530
|
+
|
|
531
|
+
const second = await runAction(app, superadmin.token, legacy.id, "issue-sign-in-link");
|
|
532
|
+
|
|
533
|
+
expect(second.status).toBe(200);
|
|
534
|
+
expect(tokenFromUrl(second.body.data.url)).not.toBe(tokenFromUrl(first.body.data.url));
|
|
535
|
+
expect(await betterAuthIdFor(legacy.id)).toBe(betterAuthId);
|
|
536
|
+
expect(await betterAuthCollection("user").countDocuments({email: legacy.email})).toBe(1);
|
|
537
|
+
expect(await betterAuthCollection("verification").countDocuments({value: betterAuthId})).toBe(
|
|
538
|
+
1
|
|
539
|
+
);
|
|
540
|
+
expect((await resetPassword(app, tokenFromUrl(second.body.data.url))).status).toBe(200);
|
|
541
|
+
const signIn = await signInWithNewPassword(app, legacy.email);
|
|
542
|
+
expect(signIn.status).toBe(200);
|
|
543
|
+
const me = await getMe(app, signIn.body.token);
|
|
544
|
+
expect(String(me.body.data._id ?? me.body.data.id)).toBe(legacy.id);
|
|
545
|
+
const events = await securityEventsFor(legacy.id);
|
|
546
|
+
expect(events).toHaveLength(2);
|
|
547
|
+
|
|
548
|
+
// Once the link is used the account is live: no further links.
|
|
549
|
+
const third = await runAction(app, superadmin.token, legacy.id, "issue-sign-in-link");
|
|
550
|
+
expect(third.status).toBe(409);
|
|
551
|
+
expect(third.body.title).toBe("User already has a Better Auth account");
|
|
552
|
+
});
|
|
553
|
+
|
|
554
|
+
it("does not reissue for a linked user whose email is verified", async () => {
|
|
555
|
+
const {app} = buildApp({config: {publicAppUrl: PUBLIC_APP_URL}});
|
|
556
|
+
const superadmin = await signUp(app, "superadmin-reissue-verified", ["superadmin"]);
|
|
557
|
+
const legacy = await createLegacyUser("reissue-verified");
|
|
558
|
+
await runAction(app, superadmin.token, legacy.id, "issue-sign-in-link").expect(200);
|
|
559
|
+
await betterAuthCollection("user").updateOne(
|
|
560
|
+
{email: legacy.email},
|
|
561
|
+
{$set: {emailVerified: true}}
|
|
562
|
+
);
|
|
563
|
+
|
|
564
|
+
const res = await runAction(app, superadmin.token, legacy.id, "issue-sign-in-link");
|
|
565
|
+
|
|
566
|
+
expect(res.status).toBe(409);
|
|
567
|
+
expect(res.body.title).toBe("User already has a Better Auth account");
|
|
568
|
+
});
|
|
569
|
+
|
|
570
|
+
it("rolls back the Better Auth user when the User was linked concurrently", async () => {
|
|
571
|
+
const {betterAuthApp} = buildApp({config: {publicAppUrl: PUBLIC_APP_URL}});
|
|
572
|
+
const legacy = await createLegacyUser("concurrent-link");
|
|
573
|
+
const staleTarget = await SecurityUser.findById(legacy.id);
|
|
574
|
+
// Another request links the User after this one loaded it.
|
|
575
|
+
await SecurityUser.updateOne({_id: legacy.id}, {$set: {betterAuthId: "linked-elsewhere"}});
|
|
576
|
+
const service = createAccountSecurityService({
|
|
577
|
+
getAuth: () => betterAuthApp.ensureAuth(),
|
|
578
|
+
getPublicAppUrl: () => PUBLIC_APP_URL,
|
|
579
|
+
getUserModel: () => securityUserModel,
|
|
580
|
+
});
|
|
581
|
+
|
|
582
|
+
const result = await service.issueSignInLink(staleTarget as unknown as User);
|
|
583
|
+
|
|
584
|
+
expect(result).toEqual({issued: false, reason: "linked"});
|
|
585
|
+
expect(await betterAuthCollection("user").countDocuments({email: legacy.email})).toBe(0);
|
|
586
|
+
expect(await betterAuthIdFor(legacy.id)).toBe("linked-elsewhere");
|
|
587
|
+
});
|
|
588
|
+
|
|
589
|
+
it("is left out of mounts that do not include platform-only actions", async () => {
|
|
590
|
+
const {app, betterAuthApp} = buildApp({config: {publicAppUrl: PUBLIC_APP_URL}});
|
|
591
|
+
const actor = await signUp(app, "custom-link-actor");
|
|
592
|
+
const legacy = await createLegacyUser("custom-link-target");
|
|
593
|
+
const security = new AccountSecurityApp({
|
|
594
|
+
audit: false,
|
|
595
|
+
getBetterAuth: () => betterAuthApp.ensureAuth(),
|
|
596
|
+
publicAppUrl: PUBLIC_APP_URL,
|
|
597
|
+
userModel: securityUserModel,
|
|
598
|
+
});
|
|
599
|
+
const router = express.Router();
|
|
600
|
+
security.mountSecurityRoutes(router, async ({req}) => ({
|
|
601
|
+
actor: req.user as never,
|
|
602
|
+
org: {id: "org-1"},
|
|
603
|
+
}));
|
|
604
|
+
const host = express();
|
|
605
|
+
host.use(express.json());
|
|
606
|
+
host.use((req, _res, next) => {
|
|
607
|
+
(req as {user?: unknown}).user = {_id: actor.id, id: actor.id};
|
|
608
|
+
runWithRequestContext({credential: "jwt" as RequestCredential}, () => next());
|
|
609
|
+
});
|
|
610
|
+
host.use("/orgs/org-1/members", router);
|
|
611
|
+
host.use(apiErrorMiddleware);
|
|
612
|
+
|
|
613
|
+
const summary = await supertest(host).get(`/orgs/org-1/members/${legacy.id}/security`);
|
|
614
|
+
expect(summary.status).toBe(200);
|
|
615
|
+
expect(actionIdsOf(summary)).toEqual([]);
|
|
616
|
+
for (const action of ["issue-sign-in-link", "disable", "enable"]) {
|
|
617
|
+
const res = await supertest(host)
|
|
618
|
+
.post(`/orgs/org-1/members/${legacy.id}/security/${action}`)
|
|
619
|
+
.send({});
|
|
620
|
+
expect([action, res.status]).toEqual([action, 404]);
|
|
621
|
+
}
|
|
622
|
+
expect(await betterAuthIdFor(legacy.id)).toBe("");
|
|
623
|
+
expect((await SecurityUser.findById(legacy.id))?.disabled).toBe(false);
|
|
624
|
+
});
|
|
625
|
+
});
|
|
626
|
+
|
|
627
|
+
describe("a User model without disabled or tokenEpoch", () => {
|
|
628
|
+
beforeEach(async () => {
|
|
629
|
+
await PlainUser.deleteMany({});
|
|
630
|
+
});
|
|
631
|
+
|
|
632
|
+
it("offers no disable/enable and revokes nothing for a user with no sessions", async () => {
|
|
633
|
+
const {app} = buildApp({
|
|
634
|
+
accessControl: false,
|
|
635
|
+
userModel: PlainUser as unknown as UserModel,
|
|
636
|
+
});
|
|
637
|
+
const adminEmail = uniqueEmail("plain-admin");
|
|
638
|
+
const signUpRes = await supertest(app)
|
|
639
|
+
.post("/api/auth/sign-up/email")
|
|
640
|
+
.set("content-type", "application/json")
|
|
641
|
+
.send({email: adminEmail, name: "Plain Admin", password: PASSWORD});
|
|
642
|
+
expect(signUpRes.status).toBe(200);
|
|
643
|
+
await PlainUser.updateOne({email: adminEmail}, {$set: {admin: true}});
|
|
644
|
+
const target = await PlainUser.create({email: uniqueEmail("plain-target"), name: "Plain"});
|
|
645
|
+
const targetId = String(target._id);
|
|
646
|
+
|
|
647
|
+
const summary = await getSummary(app, signUpRes.body.token, targetId);
|
|
648
|
+
expect(summary.status).toBe(200);
|
|
649
|
+
expect(actionIdsOf(summary)).toEqual(["issue-sign-in-link"]);
|
|
650
|
+
for (const action of ["disable", "enable"]) {
|
|
651
|
+
const res = await runAction(app, signUpRes.body.token, targetId, action);
|
|
652
|
+
expect([action, res.status, res.body.title]).toEqual([
|
|
653
|
+
action,
|
|
654
|
+
404,
|
|
655
|
+
"Security action not available",
|
|
656
|
+
]);
|
|
657
|
+
}
|
|
658
|
+
const revoke = await runAction(app, signUpRes.body.token, targetId, "revoke-sessions");
|
|
659
|
+
expect(revoke.status).toBe(200);
|
|
660
|
+
expect(revoke.body.data).toEqual({
|
|
661
|
+
action: "revoke-sessions",
|
|
662
|
+
changed: false,
|
|
663
|
+
jwtRefreshRevoked: false,
|
|
664
|
+
revokedCount: 0,
|
|
665
|
+
});
|
|
666
|
+
});
|
|
667
|
+
});
|
|
668
|
+
|
|
669
|
+
describe("authorization", () => {
|
|
670
|
+
const ACTIONS = [
|
|
671
|
+
"revoke-sessions",
|
|
672
|
+
"send-password-reset",
|
|
673
|
+
"disable",
|
|
674
|
+
"enable",
|
|
675
|
+
"issue-sign-in-link",
|
|
676
|
+
];
|
|
677
|
+
|
|
678
|
+
it("refuses a member with 403 on every access action", async () => {
|
|
679
|
+
const mail = captureMail();
|
|
680
|
+
const {app} = buildApp({config: mail.config});
|
|
681
|
+
const member = await signUp(app, "member-access");
|
|
682
|
+
const target = await signUp(app, "member-access-target");
|
|
683
|
+
|
|
684
|
+
for (const action of ACTIONS) {
|
|
685
|
+
const res = await runAction(app, member.token, target.id, action);
|
|
686
|
+
expect([action, res.status]).toEqual([action, 403]);
|
|
687
|
+
}
|
|
688
|
+
expect((await getMe(app, target.token)).status).toBe(200);
|
|
689
|
+
expect((await SecurityUser.findById(target.id))?.disabled).toBe(false);
|
|
690
|
+
expect(await securityEventsFor(target.id)).toHaveLength(0);
|
|
691
|
+
});
|
|
692
|
+
|
|
693
|
+
it("refuses an operator acting on a superadmin or on themselves", async () => {
|
|
694
|
+
const mail = captureMail();
|
|
695
|
+
const {app} = buildApp({config: mail.config});
|
|
696
|
+
const operator = await signUp(app, "operator-access-reach", ["operator"]);
|
|
697
|
+
const superadmin = await signUp(app, "superadmin-access", ["superadmin"]);
|
|
698
|
+
|
|
699
|
+
for (const action of ACTIONS) {
|
|
700
|
+
const onSuperadmin = await runAction(app, operator.token, superadmin.id, action);
|
|
701
|
+
expect([action, onSuperadmin.status, onSuperadmin.body.title]).toEqual([
|
|
702
|
+
action,
|
|
703
|
+
403,
|
|
704
|
+
"Target holds permissions you lack",
|
|
705
|
+
]);
|
|
706
|
+
const onSelf = await runAction(app, operator.token, operator.id, action);
|
|
707
|
+
expect([action, onSelf.status]).toEqual([action, 403]);
|
|
708
|
+
}
|
|
709
|
+
expect((await getMe(app, superadmin.token)).status).toBe(200);
|
|
710
|
+
expect(await securityEventsFor(superadmin.id)).toHaveLength(0);
|
|
711
|
+
});
|
|
712
|
+
|
|
713
|
+
it("refuses an unauthenticated caller with 401", async () => {
|
|
714
|
+
const {app} = buildApp();
|
|
715
|
+
const target = await signUp(app, "anon-access-target");
|
|
716
|
+
|
|
717
|
+
for (const action of ACTIONS) {
|
|
718
|
+
const res = await supertest(app).post(`/users/${target.id}/security/${action}`).send({});
|
|
719
|
+
expect([action, res.status]).toEqual([action, 401]);
|
|
720
|
+
}
|
|
721
|
+
});
|
|
722
|
+
});
|
|
723
|
+
});
|