@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,841 @@
|
|
|
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 supertest from "supertest";
|
|
6
|
+
|
|
7
|
+
import {AuditApp} from "../audit/auditApp";
|
|
8
|
+
import {resetAuditRecorderForTests} from "../audit/record";
|
|
9
|
+
import {BetterAuthApp} from "../betterAuthApp";
|
|
10
|
+
import {Membership, Organization} from "../orgs/organizationModel";
|
|
11
|
+
import {getOrgContext} from "../orgs/orgContext";
|
|
12
|
+
import {OrgsApp} from "../orgs/orgsApp";
|
|
13
|
+
import {createAccess} from "../rbac/access";
|
|
14
|
+
import {terrenoStatements} from "../rbac/statements";
|
|
15
|
+
import type {PermissionSource, PermissionSourceGrants} from "../rbac/types";
|
|
16
|
+
import {TerrenoApp} from "../terrenoApp";
|
|
17
|
+
import {
|
|
18
|
+
access,
|
|
19
|
+
appUserId,
|
|
20
|
+
buildApp,
|
|
21
|
+
resetAccountSecurityState,
|
|
22
|
+
SecurityUser,
|
|
23
|
+
securityEventsFor,
|
|
24
|
+
securityUserModel,
|
|
25
|
+
signInWithPassword,
|
|
26
|
+
signUp,
|
|
27
|
+
testBetterAuthConfig,
|
|
28
|
+
} from "../tests/accountSecurityTestApp";
|
|
29
|
+
import {enrollTwoFactor, uniqueEmail} from "../tests/betterAuthStubs";
|
|
30
|
+
import {AccountSecurityApp} from "./accountSecurityApp";
|
|
31
|
+
import {suspendMembershipAction} from "./membershipActions";
|
|
32
|
+
import type {OrgRecoveryOptions} from "./orgSecurityRoutes";
|
|
33
|
+
import {clearSecurityActionListenersForTests} from "./securityEvents";
|
|
34
|
+
|
|
35
|
+
interface TestUser {
|
|
36
|
+
email: string;
|
|
37
|
+
id: string;
|
|
38
|
+
token: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const memberPath = (orgId: string, userId: string): string =>
|
|
42
|
+
`/orgs/${orgId}/members/${userId}/security`;
|
|
43
|
+
|
|
44
|
+
const orgSummary = (
|
|
45
|
+
app: express.Application,
|
|
46
|
+
token: string,
|
|
47
|
+
orgId: string,
|
|
48
|
+
userId: string
|
|
49
|
+
): supertest.Test =>
|
|
50
|
+
supertest(app).get(memberPath(orgId, userId)).set("authorization", `Bearer ${token}`);
|
|
51
|
+
|
|
52
|
+
const orgAction = (
|
|
53
|
+
app: express.Application,
|
|
54
|
+
token: string,
|
|
55
|
+
orgId: string,
|
|
56
|
+
userId: string,
|
|
57
|
+
action: string
|
|
58
|
+
): supertest.Test =>
|
|
59
|
+
supertest(app)
|
|
60
|
+
.post(`${memberPath(orgId, userId)}/${action}`)
|
|
61
|
+
.set("authorization", `Bearer ${token}`)
|
|
62
|
+
.send({});
|
|
63
|
+
|
|
64
|
+
const actionIdsOf = (res: supertest.Response): string[] =>
|
|
65
|
+
(res.body.data.actions as {id: string}[]).map((action) => action.id);
|
|
66
|
+
|
|
67
|
+
const titleOf = (res: supertest.Response): string | undefined => res.body.title;
|
|
68
|
+
|
|
69
|
+
const NOT_ORG_ADMIN = "Only org-admins of this organization can manage member security";
|
|
70
|
+
|
|
71
|
+
const createOrg = async (name: string, ownerId: string): Promise<string> => {
|
|
72
|
+
const organization = await Organization.create({name, ownerId});
|
|
73
|
+
return String(organization._id);
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const addMember = async (
|
|
77
|
+
orgId: string,
|
|
78
|
+
userId: string,
|
|
79
|
+
{
|
|
80
|
+
roleName = "member",
|
|
81
|
+
status = "active",
|
|
82
|
+
}: {roleName?: "member" | "org-admin"; status?: "active" | "suspended"} = {}
|
|
83
|
+
): Promise<string> => {
|
|
84
|
+
const membership = await Membership.create({
|
|
85
|
+
organizationId: orgId,
|
|
86
|
+
roleName,
|
|
87
|
+
status,
|
|
88
|
+
userId,
|
|
89
|
+
});
|
|
90
|
+
return String(membership._id);
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const membershipStatus = async (orgId: string, userId: string): Promise<string | undefined> => {
|
|
94
|
+
const membership = await Membership.findOneOrNone({organizationId: orgId, userId});
|
|
95
|
+
return membership?.status;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
/** A user with 2FA on, so reset-two-factor has something to change. */
|
|
99
|
+
const signUpWithTwoFactor = async (app: express.Application, label: string): Promise<TestUser> => {
|
|
100
|
+
const email = uniqueEmail(label);
|
|
101
|
+
const {token} = await enrollTwoFactor(app, email);
|
|
102
|
+
return {email, id: await appUserId(email), token};
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Asserts that both the summary and reset-two-factor are refused with `status` (and `title`) and
|
|
107
|
+
* that nothing was recorded for the target.
|
|
108
|
+
*/
|
|
109
|
+
const expectRefused = async ({
|
|
110
|
+
app,
|
|
111
|
+
orgId,
|
|
112
|
+
status,
|
|
113
|
+
targetId,
|
|
114
|
+
title,
|
|
115
|
+
token,
|
|
116
|
+
}: {
|
|
117
|
+
app: express.Application;
|
|
118
|
+
orgId: string;
|
|
119
|
+
status: number;
|
|
120
|
+
targetId: string;
|
|
121
|
+
title?: string;
|
|
122
|
+
token: string;
|
|
123
|
+
}): Promise<void> => {
|
|
124
|
+
const summary = await orgSummary(app, token, orgId, targetId);
|
|
125
|
+
const reset = await orgAction(app, token, orgId, targetId, "reset-two-factor");
|
|
126
|
+
expect({reset: reset.status, summary: summary.status}).toEqual({reset: status, summary: status});
|
|
127
|
+
if (title) {
|
|
128
|
+
expect({reset: titleOf(reset), summary: titleOf(summary)}).toEqual({
|
|
129
|
+
reset: title,
|
|
130
|
+
summary: title,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
if (mongoose.isValidObjectId(targetId)) {
|
|
134
|
+
expect(await securityEventsFor(targetId)).toHaveLength(0);
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
interface OrgFixture {
|
|
139
|
+
app: express.Application;
|
|
140
|
+
accountSecurityApp?: AccountSecurityApp;
|
|
141
|
+
orgA: string;
|
|
142
|
+
orgAdmin: TestUser;
|
|
143
|
+
orgB: string;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const buildOrgApp = async ({
|
|
147
|
+
accountSecurity = true,
|
|
148
|
+
orgRecovery,
|
|
149
|
+
}: {
|
|
150
|
+
accountSecurity?: boolean;
|
|
151
|
+
orgRecovery?: OrgRecoveryOptions;
|
|
152
|
+
} = {}): Promise<OrgFixture> => {
|
|
153
|
+
const {accountSecurityApp, app} = buildApp({
|
|
154
|
+
accountSecurity,
|
|
155
|
+
organizations: orgRecovery ? {orgRecovery} : true,
|
|
156
|
+
});
|
|
157
|
+
const orgAdmin = await signUp(app, "org-admin-a");
|
|
158
|
+
const orgA = await createOrg(`Org A ${orgAdmin.id}`, orgAdmin.id);
|
|
159
|
+
const orgB = await createOrg(`Org B ${orgAdmin.id}`, orgAdmin.id);
|
|
160
|
+
await addMember(orgA, orgAdmin.id, {roleName: "org-admin"});
|
|
161
|
+
return {accountSecurityApp, app, orgA, orgAdmin, orgB};
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
describe("org-admin recovery routes", () => {
|
|
165
|
+
beforeAll(async () => {
|
|
166
|
+
await access.roles.seedDefaults();
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
beforeEach(async () => {
|
|
170
|
+
await resetAccountSecurityState();
|
|
171
|
+
await Membership.deleteMany({});
|
|
172
|
+
await Organization.deleteMany({});
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
afterEach(() => {
|
|
176
|
+
resetAuditRecorderForTests();
|
|
177
|
+
clearSecurityActionListenersForTests();
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
describe("authorization matrix (summary and reset-two-factor)", () => {
|
|
181
|
+
it("allows the org-admin of A on an active member of A who also belongs to B", async () => {
|
|
182
|
+
const {app, orgA, orgAdmin, orgB} = await buildOrgApp();
|
|
183
|
+
const target = await signUpWithTwoFactor(app, "member-a-and-b");
|
|
184
|
+
await addMember(orgA, target.id);
|
|
185
|
+
await addMember(orgB, target.id);
|
|
186
|
+
|
|
187
|
+
const summary = await orgSummary(app, orgAdmin.token, orgA, target.id);
|
|
188
|
+
expect(summary.status).toBe(200);
|
|
189
|
+
expect(summary.body.data.twoFactor.enabled).toBe(true);
|
|
190
|
+
expect(actionIdsOf(summary)).toContain("reset-two-factor");
|
|
191
|
+
|
|
192
|
+
const reset = await orgAction(app, orgAdmin.token, orgA, target.id, "reset-two-factor");
|
|
193
|
+
expect(reset.status).toBe(200);
|
|
194
|
+
expect(reset.body.data.changed).toBe(true);
|
|
195
|
+
|
|
196
|
+
const signIn = await signInWithPassword(app, target.email);
|
|
197
|
+
expect(signIn.status).toBe(200);
|
|
198
|
+
expect(signIn.body.twoFactorRedirect).toBeUndefined();
|
|
199
|
+
|
|
200
|
+
const events = await securityEventsFor(target.id);
|
|
201
|
+
expect(events).toHaveLength(1);
|
|
202
|
+
expect(String(events[0].actorId)).toBe(orgAdmin.id);
|
|
203
|
+
expect(events[0].after).toEqual({organizationId: orgA, securityAction: "reset-two-factor"});
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it("answers 404 for a member of another org only", async () => {
|
|
207
|
+
const {app, orgA, orgAdmin, orgB} = await buildOrgApp();
|
|
208
|
+
const target = await signUpWithTwoFactor(app, "member-b-only");
|
|
209
|
+
await addMember(orgB, target.id);
|
|
210
|
+
|
|
211
|
+
await expectRefused({
|
|
212
|
+
app,
|
|
213
|
+
orgId: orgA,
|
|
214
|
+
status: 404,
|
|
215
|
+
targetId: target.id,
|
|
216
|
+
title: "Member not found",
|
|
217
|
+
token: orgAdmin.token,
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it("answers 404 for a suspended member", async () => {
|
|
222
|
+
const {app, orgA, orgAdmin} = await buildOrgApp();
|
|
223
|
+
const target = await signUpWithTwoFactor(app, "suspended-member");
|
|
224
|
+
await addMember(orgA, target.id, {status: "suspended"});
|
|
225
|
+
|
|
226
|
+
await expectRefused({
|
|
227
|
+
app,
|
|
228
|
+
orgId: orgA,
|
|
229
|
+
status: 404,
|
|
230
|
+
targetId: target.id,
|
|
231
|
+
title: "Member not found",
|
|
232
|
+
token: orgAdmin.token,
|
|
233
|
+
});
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it("refuses with 403 a member who holds superadmin, operator, or admin: true", async () => {
|
|
237
|
+
const {app, orgA, orgAdmin} = await buildOrgApp();
|
|
238
|
+
const superadmin = await signUp(app, "member-superadmin", ["superadmin"]);
|
|
239
|
+
const operator = await signUp(app, "member-operator", ["operator"]);
|
|
240
|
+
const admin = await signUp(app, "member-admin");
|
|
241
|
+
await SecurityUser.updateOne({_id: admin.id}, {$set: {admin: true}});
|
|
242
|
+
for (const target of [superadmin, operator, admin]) {
|
|
243
|
+
await addMember(orgA, target.id);
|
|
244
|
+
await expectRefused({
|
|
245
|
+
app,
|
|
246
|
+
orgId: orgA,
|
|
247
|
+
status: 403,
|
|
248
|
+
targetId: target.id,
|
|
249
|
+
title: "Platform staff are managed by platform staff",
|
|
250
|
+
token: orgAdmin.token,
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it("allows the org-admin of A on another org-admin of A", async () => {
|
|
256
|
+
const {app, orgA, orgAdmin} = await buildOrgApp();
|
|
257
|
+
const target = await signUpWithTwoFactor(app, "second-org-admin");
|
|
258
|
+
await addMember(orgA, target.id, {roleName: "org-admin"});
|
|
259
|
+
|
|
260
|
+
expect((await orgSummary(app, orgAdmin.token, orgA, target.id)).status).toBe(200);
|
|
261
|
+
const reset = await orgAction(app, orgAdmin.token, orgA, target.id, "reset-two-factor");
|
|
262
|
+
expect(reset.status).toBe(200);
|
|
263
|
+
expect(reset.body.data.changed).toBe(true);
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
it("refuses with 403 a member holding permissions the org-admin lacks (D9)", async () => {
|
|
267
|
+
const {app, orgA, orgAdmin} = await buildOrgApp();
|
|
268
|
+
const target = await signUp(app, "member-auditor", ["auditor"]);
|
|
269
|
+
await addMember(orgA, target.id);
|
|
270
|
+
|
|
271
|
+
await expectRefused({
|
|
272
|
+
app,
|
|
273
|
+
orgId: orgA,
|
|
274
|
+
status: 403,
|
|
275
|
+
targetId: target.id,
|
|
276
|
+
title: "Target holds permissions you lack",
|
|
277
|
+
token: orgAdmin.token,
|
|
278
|
+
});
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
it("refuses an org-admin acting on themselves", async () => {
|
|
282
|
+
const {app, orgA, orgAdmin} = await buildOrgApp();
|
|
283
|
+
|
|
284
|
+
await expectRefused({
|
|
285
|
+
app,
|
|
286
|
+
orgId: orgA,
|
|
287
|
+
status: 403,
|
|
288
|
+
targetId: orgAdmin.id,
|
|
289
|
+
title: "Use your own security settings to change your account",
|
|
290
|
+
token: orgAdmin.token,
|
|
291
|
+
});
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
it("refuses a plain member with 403", async () => {
|
|
295
|
+
const {app, orgA} = await buildOrgApp();
|
|
296
|
+
const actor = await signUp(app, "plain-member");
|
|
297
|
+
await addMember(orgA, actor.id);
|
|
298
|
+
const target = await signUpWithTwoFactor(app, "plain-target");
|
|
299
|
+
await addMember(orgA, target.id);
|
|
300
|
+
|
|
301
|
+
await expectRefused({
|
|
302
|
+
app,
|
|
303
|
+
orgId: orgA,
|
|
304
|
+
status: 403,
|
|
305
|
+
targetId: target.id,
|
|
306
|
+
title: NOT_ORG_ADMIN,
|
|
307
|
+
token: actor.token,
|
|
308
|
+
});
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
it("refuses a suspended org-admin with 403", async () => {
|
|
312
|
+
const {app, orgA} = await buildOrgApp();
|
|
313
|
+
const actor = await signUp(app, "suspended-org-admin");
|
|
314
|
+
await addMember(orgA, actor.id, {roleName: "org-admin", status: "suspended"});
|
|
315
|
+
const target = await signUpWithTwoFactor(app, "suspended-admin-target");
|
|
316
|
+
await addMember(orgA, target.id);
|
|
317
|
+
|
|
318
|
+
await expectRefused({
|
|
319
|
+
app,
|
|
320
|
+
orgId: orgA,
|
|
321
|
+
status: 403,
|
|
322
|
+
targetId: target.id,
|
|
323
|
+
title: NOT_ORG_ADMIN,
|
|
324
|
+
token: actor.token,
|
|
325
|
+
});
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
it("refuses the org-admin of B acting in A with 403", async () => {
|
|
329
|
+
const {app, orgA, orgB} = await buildOrgApp();
|
|
330
|
+
const adminB = await signUp(app, "org-admin-b");
|
|
331
|
+
await addMember(orgB, adminB.id, {roleName: "org-admin"});
|
|
332
|
+
const target = await signUpWithTwoFactor(app, "member-of-a");
|
|
333
|
+
await addMember(orgA, target.id);
|
|
334
|
+
await addMember(orgB, target.id);
|
|
335
|
+
|
|
336
|
+
await expectRefused({
|
|
337
|
+
app,
|
|
338
|
+
orgId: orgA,
|
|
339
|
+
status: 403,
|
|
340
|
+
targetId: target.id,
|
|
341
|
+
title: NOT_ORG_ADMIN,
|
|
342
|
+
token: adminB.token,
|
|
343
|
+
});
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
it("refuses with 403 when the organization is disabled", async () => {
|
|
347
|
+
const {app, orgA, orgAdmin} = await buildOrgApp();
|
|
348
|
+
const target = await signUpWithTwoFactor(app, "disabled-org-member");
|
|
349
|
+
await addMember(orgA, target.id);
|
|
350
|
+
await Organization.updateOne({_id: orgA}, {$set: {disabled: true}});
|
|
351
|
+
|
|
352
|
+
await expectRefused({
|
|
353
|
+
app,
|
|
354
|
+
orgId: orgA,
|
|
355
|
+
status: 403,
|
|
356
|
+
targetId: target.id,
|
|
357
|
+
title: "Organization is disabled",
|
|
358
|
+
token: orgAdmin.token,
|
|
359
|
+
});
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
it("answers 404 for an unknown organization", async () => {
|
|
363
|
+
const {app, orgAdmin} = await buildOrgApp();
|
|
364
|
+
const target = await signUp(app, "unknown-org-target");
|
|
365
|
+
|
|
366
|
+
for (const orgId of ["64b7f0f0f0f0f0f0f0f0f0f0", "not-an-id"]) {
|
|
367
|
+
await expectRefused({
|
|
368
|
+
app,
|
|
369
|
+
orgId,
|
|
370
|
+
status: 404,
|
|
371
|
+
targetId: target.id,
|
|
372
|
+
title: "Organization not found",
|
|
373
|
+
token: orgAdmin.token,
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
it("answers 404 for a malformed member id or a member whose user is gone", async () => {
|
|
379
|
+
const {app, orgA, orgAdmin} = await buildOrgApp();
|
|
380
|
+
expect((await orgSummary(app, orgAdmin.token, orgA, "not-an-id")).status).toBe(404);
|
|
381
|
+
const ghostId = new mongoose.Types.ObjectId().toString();
|
|
382
|
+
await addMember(orgA, ghostId);
|
|
383
|
+
|
|
384
|
+
const ghost = await orgSummary(app, orgAdmin.token, orgA, ghostId);
|
|
385
|
+
expect(ghost.status).toBe(404);
|
|
386
|
+
expect(titleOf(ghost)).toBe("Member not found");
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
it("lets a platform operator through without a membership", async () => {
|
|
390
|
+
const {app, orgA} = await buildOrgApp();
|
|
391
|
+
const operator = await signUp(app, "platform-operator", ["operator"]);
|
|
392
|
+
const target = await signUpWithTwoFactor(app, "operator-target");
|
|
393
|
+
await addMember(orgA, target.id);
|
|
394
|
+
|
|
395
|
+
expect((await orgSummary(app, operator.token, orgA, target.id)).status).toBe(200);
|
|
396
|
+
const reset = await orgAction(app, operator.token, orgA, target.id, "reset-two-factor");
|
|
397
|
+
expect(reset.status).toBe(200);
|
|
398
|
+
// The target must still be an active member of the org named in the path.
|
|
399
|
+
const outsider = await signUp(app, "operator-outsider");
|
|
400
|
+
await expectRefused({
|
|
401
|
+
app,
|
|
402
|
+
orgId: orgA,
|
|
403
|
+
status: 404,
|
|
404
|
+
targetId: outsider.id,
|
|
405
|
+
title: "Member not found",
|
|
406
|
+
token: operator.token,
|
|
407
|
+
});
|
|
408
|
+
});
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
describe("orgRecovery.minMembershipDays", () => {
|
|
412
|
+
const attachByEmail = (
|
|
413
|
+
app: express.Application,
|
|
414
|
+
token: string,
|
|
415
|
+
orgId: string,
|
|
416
|
+
email: string
|
|
417
|
+
): supertest.Test =>
|
|
418
|
+
supertest(app)
|
|
419
|
+
.post(`/orgs/${orgId}/members`)
|
|
420
|
+
.set("authorization", `Bearer ${token}`)
|
|
421
|
+
.send({email});
|
|
422
|
+
|
|
423
|
+
it("refuses resetting 2FA on a user attached by email the same day", async () => {
|
|
424
|
+
const {app, orgA, orgAdmin} = await buildOrgApp({orgRecovery: {minMembershipDays: 7}});
|
|
425
|
+
const target = await signUpWithTwoFactor(app, "attached-today");
|
|
426
|
+
expect((await attachByEmail(app, orgAdmin.token, orgA, target.email)).status).toBe(201);
|
|
427
|
+
|
|
428
|
+
const reset = await orgAction(app, orgAdmin.token, orgA, target.id, "reset-two-factor");
|
|
429
|
+
expect(reset.status).toBe(403);
|
|
430
|
+
expect(titleOf(reset)).toBe("Member joined too recently");
|
|
431
|
+
expect(await securityEventsFor(target.id)).toHaveLength(0);
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
it("allows a member who joined longer ago than the minimum", async () => {
|
|
435
|
+
const {app, orgA, orgAdmin} = await buildOrgApp({orgRecovery: {minMembershipDays: 7}});
|
|
436
|
+
const target = await signUpWithTwoFactor(app, "joined-long-ago");
|
|
437
|
+
const membershipId = await addMember(orgA, target.id);
|
|
438
|
+
// Write past createdUpdatedPlugin, which stamps `created` on save.
|
|
439
|
+
await Membership.collection.updateOne(
|
|
440
|
+
{_id: new mongoose.Types.ObjectId(membershipId)},
|
|
441
|
+
{$set: {created: DateTime.now().minus({days: 8}).toJSDate()}}
|
|
442
|
+
);
|
|
443
|
+
|
|
444
|
+
const reset = await orgAction(app, orgAdmin.token, orgA, target.id, "reset-two-factor");
|
|
445
|
+
expect(reset.status).toBe(200);
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
it("allows the same-day reset with the default of 0", async () => {
|
|
449
|
+
const {app, orgA, orgAdmin} = await buildOrgApp();
|
|
450
|
+
const target = await signUpWithTwoFactor(app, "attached-default");
|
|
451
|
+
expect((await attachByEmail(app, orgAdmin.token, orgA, target.email)).status).toBe(201);
|
|
452
|
+
|
|
453
|
+
const reset = await orgAction(app, orgAdmin.token, orgA, target.id, "reset-two-factor");
|
|
454
|
+
expect(reset.status).toBe(200);
|
|
455
|
+
expect(reset.body.data.changed).toBe(true);
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
it("does not apply to platform staff", async () => {
|
|
459
|
+
const {app, orgA, orgAdmin} = await buildOrgApp({orgRecovery: {minMembershipDays: 7}});
|
|
460
|
+
const operator = await signUp(app, "operator-recent", ["operator"]);
|
|
461
|
+
const target = await signUpWithTwoFactor(app, "attached-for-operator");
|
|
462
|
+
expect((await attachByEmail(app, orgAdmin.token, orgA, target.email)).status).toBe(201);
|
|
463
|
+
|
|
464
|
+
const reset = await orgAction(app, operator.token, orgA, target.id, "reset-two-factor");
|
|
465
|
+
expect(reset.status).toBe(200);
|
|
466
|
+
});
|
|
467
|
+
});
|
|
468
|
+
|
|
469
|
+
describe("suspend-membership", () => {
|
|
470
|
+
it("suspends only the A membership; the user can still sign in", async () => {
|
|
471
|
+
const {app, orgA, orgAdmin, orgB} = await buildOrgApp();
|
|
472
|
+
const target = await signUp(app, "suspend-target");
|
|
473
|
+
await addMember(orgA, target.id);
|
|
474
|
+
await addMember(orgB, target.id);
|
|
475
|
+
|
|
476
|
+
const summary = await orgSummary(app, orgAdmin.token, orgA, target.id);
|
|
477
|
+
expect(actionIdsOf(summary)).toContain("suspend-membership");
|
|
478
|
+
expect(actionIdsOf(summary)).not.toContain("disable");
|
|
479
|
+
expect(actionIdsOf(summary)).not.toContain("enable");
|
|
480
|
+
expect(actionIdsOf(summary)).not.toContain("issue-sign-in-link");
|
|
481
|
+
|
|
482
|
+
const res = await orgAction(app, orgAdmin.token, orgA, target.id, "suspend-membership");
|
|
483
|
+
expect(res.status).toBe(200);
|
|
484
|
+
expect(res.body.data).toMatchObject({
|
|
485
|
+
action: "suspend-membership",
|
|
486
|
+
changed: true,
|
|
487
|
+
status: "suspended",
|
|
488
|
+
});
|
|
489
|
+
|
|
490
|
+
expect(await membershipStatus(orgA, target.id)).toBe("suspended");
|
|
491
|
+
expect(await membershipStatus(orgB, target.id)).toBe("active");
|
|
492
|
+
expect((await SecurityUser.findById(target.id))?.disabled).toBe(false);
|
|
493
|
+
expect((await signInWithPassword(app, target.email)).status).toBe(200);
|
|
494
|
+
const me = await supertest(app)
|
|
495
|
+
.get("/auth/me")
|
|
496
|
+
.set("authorization", `Bearer ${target.token}`);
|
|
497
|
+
expect(me.status).toBe(200);
|
|
498
|
+
|
|
499
|
+
const events = await securityEventsFor(target.id);
|
|
500
|
+
expect(events).toHaveLength(1);
|
|
501
|
+
expect(events[0].after).toMatchObject({
|
|
502
|
+
organizationId: orgA,
|
|
503
|
+
roleName: "member",
|
|
504
|
+
securityAction: "suspend-membership",
|
|
505
|
+
});
|
|
506
|
+
// A suspended member is out of reach; restore them through PATCH /orgs/:id/members/:memberId.
|
|
507
|
+
expect((await orgSummary(app, orgAdmin.token, orgA, target.id)).status).toBe(404);
|
|
508
|
+
});
|
|
509
|
+
|
|
510
|
+
it("takes a suspended org-admin's grants away at once, not after the permission cache expires", async () => {
|
|
511
|
+
const {app, orgA, orgAdmin} = await buildOrgApp();
|
|
512
|
+
const target = await signUp(app, "suspended-co-admin");
|
|
513
|
+
await addMember(orgA, target.id, {roleName: "org-admin"});
|
|
514
|
+
const targetUser = await SecurityUser.findById(target.id);
|
|
515
|
+
const asAuthUser = targetUser as unknown as Parameters<
|
|
516
|
+
typeof access.getPermissions
|
|
517
|
+
>[0]["user"];
|
|
518
|
+
// Resolve (and cache) the co-admin's grants while the membership is active.
|
|
519
|
+
expect((await access.getPermissions({user: asAuthUser})).admin).toEqual(["access"]);
|
|
520
|
+
|
|
521
|
+
const res = await orgAction(app, orgAdmin.token, orgA, target.id, "suspend-membership");
|
|
522
|
+
expect(res.status).toBe(200);
|
|
523
|
+
expect(await membershipStatus(orgA, target.id)).toBe("suspended");
|
|
524
|
+
expect((await access.getPermissions({user: asAuthUser})).admin).toBeUndefined();
|
|
525
|
+
});
|
|
526
|
+
|
|
527
|
+
it("refuses to suspend the last active org-admin", async () => {
|
|
528
|
+
const {app, orgA, orgAdmin} = await buildOrgApp();
|
|
529
|
+
const operator = await signUp(app, "operator-last-admin", ["operator"]);
|
|
530
|
+
|
|
531
|
+
const res = await orgAction(app, operator.token, orgA, orgAdmin.id, "suspend-membership");
|
|
532
|
+
expect(res.status).toBe(400);
|
|
533
|
+
expect(titleOf(res)).toBe("Cannot remove the last org-admin");
|
|
534
|
+
expect(await membershipStatus(orgA, orgAdmin.id)).toBe("active");
|
|
535
|
+
});
|
|
536
|
+
|
|
537
|
+
it("changes nothing without an active membership, and refuses to run outside an org", async () => {
|
|
538
|
+
const {app, orgA} = await buildOrgApp();
|
|
539
|
+
const target = await signUp(app, "suspend-direct");
|
|
540
|
+
const user = await SecurityUser.findById(target.id);
|
|
541
|
+
const context = {
|
|
542
|
+
target: user,
|
|
543
|
+
} as unknown as Parameters<typeof suspendMembershipAction.run>[0];
|
|
544
|
+
|
|
545
|
+
expect(await suspendMembershipAction.run({...context, org: {id: orgA}})).toEqual({
|
|
546
|
+
changed: false,
|
|
547
|
+
});
|
|
548
|
+
await expect(suspendMembershipAction.run(context)).rejects.toThrow(
|
|
549
|
+
"Security action not available"
|
|
550
|
+
);
|
|
551
|
+
});
|
|
552
|
+
|
|
553
|
+
it("is not mounted under the platform /users prefix", async () => {
|
|
554
|
+
const {app, orgA} = await buildOrgApp();
|
|
555
|
+
const operator = await signUp(app, "operator-users-prefix", ["operator"]);
|
|
556
|
+
const target = await signUp(app, "users-prefix-target");
|
|
557
|
+
await addMember(orgA, target.id);
|
|
558
|
+
|
|
559
|
+
const res = await supertest(app)
|
|
560
|
+
.post(`/users/${target.id}/security/suspend-membership`)
|
|
561
|
+
.set("authorization", `Bearer ${operator.token}`)
|
|
562
|
+
.send({});
|
|
563
|
+
expect(res.status).toBe(404);
|
|
564
|
+
// A mounted route runs the auth guards first (401 without a session); an unmounted one
|
|
565
|
+
// falls through to the 404, so this tells "not mounted" apart from a handler's own 404.
|
|
566
|
+
const unauthenticated = await supertest(app)
|
|
567
|
+
.post(`/users/${target.id}/security/suspend-membership`)
|
|
568
|
+
.send({});
|
|
569
|
+
expect(unauthenticated.status).toBe(404);
|
|
570
|
+
const mounted = await supertest(app)
|
|
571
|
+
.post(`/users/${target.id}/security/revoke-sessions`)
|
|
572
|
+
.send({});
|
|
573
|
+
expect(mounted.status).toBe(401);
|
|
574
|
+
const summary = await supertest(app)
|
|
575
|
+
.get(`/users/${target.id}/security`)
|
|
576
|
+
.set("authorization", `Bearer ${operator.token}`);
|
|
577
|
+
expect(actionIdsOf(summary)).not.toContain("suspend-membership");
|
|
578
|
+
});
|
|
579
|
+
|
|
580
|
+
it("never leaves the org without an active org-admin when two admins suspend each other", async () => {
|
|
581
|
+
const {app, orgA, orgAdmin} = await buildOrgApp();
|
|
582
|
+
const second = await signUp(app, "mutual-org-admin");
|
|
583
|
+
await addMember(orgA, second.id, {roleName: "org-admin"});
|
|
584
|
+
|
|
585
|
+
const results = await Promise.all([
|
|
586
|
+
orgAction(app, orgAdmin.token, orgA, second.id, "suspend-membership"),
|
|
587
|
+
orgAction(app, second.token, orgA, orgAdmin.id, "suspend-membership"),
|
|
588
|
+
]);
|
|
589
|
+
|
|
590
|
+
const activeAdmins = await Membership.countDocuments({
|
|
591
|
+
organizationId: orgA,
|
|
592
|
+
roleName: "org-admin",
|
|
593
|
+
status: "active",
|
|
594
|
+
});
|
|
595
|
+
expect(activeAdmins).toBeGreaterThanOrEqual(1);
|
|
596
|
+
const suspended = results.filter((res) => res.status === 200 && res.body.data.changed);
|
|
597
|
+
expect(suspended.length).toBeLessThanOrEqual(1);
|
|
598
|
+
expect(activeAdmins).toBe(2 - suspended.length);
|
|
599
|
+
});
|
|
600
|
+
});
|
|
601
|
+
|
|
602
|
+
describe("org-scoped permissions in the reach check (D9)", () => {
|
|
603
|
+
let extraGrant: {orgId: string; userId: string} | undefined;
|
|
604
|
+
// The user whose lookups the source fails, as during an outage.
|
|
605
|
+
let failingUserId: string | undefined;
|
|
606
|
+
/** Grants `user:delete` to one user, but only under their own membership in one org. */
|
|
607
|
+
const orgScopedSource: PermissionSource = {
|
|
608
|
+
getGrants: async ({user}): Promise<PermissionSourceGrants | null> => {
|
|
609
|
+
if (failingUserId && String(user.id) === failingUserId) {
|
|
610
|
+
throw new Error("permission source down");
|
|
611
|
+
}
|
|
612
|
+
const membership = getOrgContext()?.membership;
|
|
613
|
+
if (!membership || !extraGrant) {
|
|
614
|
+
return null;
|
|
615
|
+
}
|
|
616
|
+
const isGranted =
|
|
617
|
+
String(membership.userId) === extraGrant.userId &&
|
|
618
|
+
String(membership.organizationId) === extraGrant.orgId;
|
|
619
|
+
return isGranted ? {permissions: {user: ["delete"]}} : null;
|
|
620
|
+
},
|
|
621
|
+
name: "test-org-scoped-extra",
|
|
622
|
+
ttlMs: 0,
|
|
623
|
+
};
|
|
624
|
+
const scopedAccess = createAccess({
|
|
625
|
+
connection: mongoose.connection,
|
|
626
|
+
organizations: true,
|
|
627
|
+
sources: [orgScopedSource],
|
|
628
|
+
statements: terrenoStatements,
|
|
629
|
+
userModel: securityUserModel,
|
|
630
|
+
});
|
|
631
|
+
|
|
632
|
+
const buildScopedApp = async (): Promise<OrgFixture> => {
|
|
633
|
+
const {accountSecurityApp, app} = buildApp({
|
|
634
|
+
accessControl: scopedAccess,
|
|
635
|
+
organizations: true,
|
|
636
|
+
});
|
|
637
|
+
const orgAdmin = await signUp(app, "scoped-org-admin");
|
|
638
|
+
const orgA = await createOrg(`Scoped A ${orgAdmin.id}`, orgAdmin.id);
|
|
639
|
+
const orgB = await createOrg(`Scoped B ${orgAdmin.id}`, orgAdmin.id);
|
|
640
|
+
await addMember(orgA, orgAdmin.id, {roleName: "org-admin"});
|
|
641
|
+
return {accountSecurityApp, app, orgA, orgAdmin, orgB};
|
|
642
|
+
};
|
|
643
|
+
|
|
644
|
+
afterEach(() => {
|
|
645
|
+
extraGrant = undefined;
|
|
646
|
+
failingUserId = undefined;
|
|
647
|
+
});
|
|
648
|
+
|
|
649
|
+
it("refuses with 503 when the target's grants cannot be resolved, not with their grants dropped", async () => {
|
|
650
|
+
const {app, orgA, orgAdmin} = await buildScopedApp();
|
|
651
|
+
const target = await signUpWithTwoFactor(app, "scoped-source-down");
|
|
652
|
+
await addMember(orgA, target.id);
|
|
653
|
+
extraGrant = {orgId: orgA, userId: target.id};
|
|
654
|
+
// One good fetch first, so the outage is not a cold failure the source would fail closed on.
|
|
655
|
+
expect((await orgSummary(app, orgAdmin.token, orgA, target.id)).status).toBe(403);
|
|
656
|
+
|
|
657
|
+
failingUserId = target.id;
|
|
658
|
+
scopedAccess.invalidateCache({userId: target.id});
|
|
659
|
+
|
|
660
|
+
await expectRefused({
|
|
661
|
+
app,
|
|
662
|
+
orgId: orgA,
|
|
663
|
+
status: 503,
|
|
664
|
+
targetId: target.id,
|
|
665
|
+
title: "Permission source unavailable",
|
|
666
|
+
token: orgAdmin.token,
|
|
667
|
+
});
|
|
668
|
+
});
|
|
669
|
+
|
|
670
|
+
it("refuses a target whose own membership in the org grants more than the actor holds", async () => {
|
|
671
|
+
const {app, orgA, orgAdmin} = await buildScopedApp();
|
|
672
|
+
const target = await signUpWithTwoFactor(app, "scoped-extra-target");
|
|
673
|
+
await addMember(orgA, target.id);
|
|
674
|
+
extraGrant = {orgId: orgA, userId: target.id};
|
|
675
|
+
|
|
676
|
+
await expectRefused({
|
|
677
|
+
app,
|
|
678
|
+
orgId: orgA,
|
|
679
|
+
status: 403,
|
|
680
|
+
targetId: target.id,
|
|
681
|
+
title: "Target holds permissions you lack",
|
|
682
|
+
token: orgAdmin.token,
|
|
683
|
+
});
|
|
684
|
+
});
|
|
685
|
+
|
|
686
|
+
it("lets a platform operator recover a member who holds an org-only grant", async () => {
|
|
687
|
+
const {app, orgA} = await buildScopedApp();
|
|
688
|
+
const operator = await signUp(app, "scoped-platform-operator", ["operator"]);
|
|
689
|
+
const target = await signUpWithTwoFactor(app, "scoped-operator-target");
|
|
690
|
+
await addMember(orgA, target.id);
|
|
691
|
+
extraGrant = {orgId: orgA, userId: target.id};
|
|
692
|
+
|
|
693
|
+
// The same grant blocks an org-admin (their membership is the reach context) and
|
|
694
|
+
// does not block platform staff, who are judged outside the organization.
|
|
695
|
+
expect((await orgSummary(app, operator.token, orgA, target.id)).status).toBe(200);
|
|
696
|
+
const reset = await orgAction(app, operator.token, orgA, target.id, "reset-two-factor");
|
|
697
|
+
expect(reset.status).toBe(200);
|
|
698
|
+
});
|
|
699
|
+
|
|
700
|
+
it("still allows an org-admin acting on another org-admin of the org", async () => {
|
|
701
|
+
const {app, orgA, orgAdmin} = await buildScopedApp();
|
|
702
|
+
const target = await signUpWithTwoFactor(app, "scoped-peer-admin");
|
|
703
|
+
await addMember(orgA, target.id, {roleName: "org-admin"});
|
|
704
|
+
|
|
705
|
+
expect((await orgSummary(app, orgAdmin.token, orgA, target.id)).status).toBe(200);
|
|
706
|
+
const reset = await orgAction(app, orgAdmin.token, orgA, target.id, "reset-two-factor");
|
|
707
|
+
expect(reset.status).toBe(200);
|
|
708
|
+
});
|
|
709
|
+
|
|
710
|
+
it("ignores a grant the target holds only in another org", async () => {
|
|
711
|
+
const {app, orgA, orgAdmin, orgB} = await buildScopedApp();
|
|
712
|
+
const target = await signUpWithTwoFactor(app, "scoped-other-org");
|
|
713
|
+
await addMember(orgA, target.id);
|
|
714
|
+
await addMember(orgB, target.id);
|
|
715
|
+
extraGrant = {orgId: orgB, userId: target.id};
|
|
716
|
+
|
|
717
|
+
expect((await orgSummary(app, orgAdmin.token, orgA, target.id)).status).toBe(200);
|
|
718
|
+
});
|
|
719
|
+
});
|
|
720
|
+
|
|
721
|
+
describe("registration", () => {
|
|
722
|
+
it("refuses an AccountSecurityApp that is not registered on the app", () => {
|
|
723
|
+
expect(() =>
|
|
724
|
+
buildApp({
|
|
725
|
+
accountSecurity: false,
|
|
726
|
+
organizations: {
|
|
727
|
+
accountSecurity: new AccountSecurityApp({audit: false, userModel: securityUserModel}),
|
|
728
|
+
},
|
|
729
|
+
})
|
|
730
|
+
).toThrow("OrgsApp accountSecurity is not registered");
|
|
731
|
+
|
|
732
|
+
const orgsApp = new OrgsApp({
|
|
733
|
+
access,
|
|
734
|
+
accountSecurity: new AccountSecurityApp({audit: false, userModel: securityUserModel}),
|
|
735
|
+
userModel: securityUserModel,
|
|
736
|
+
});
|
|
737
|
+
expect(() => orgsApp.register(express())).toThrow(
|
|
738
|
+
"OrgsApp accountSecurity is not registered"
|
|
739
|
+
);
|
|
740
|
+
});
|
|
741
|
+
|
|
742
|
+
it("accepts an AccountSecurityApp registered on the same app after OrgsApp", async () => {
|
|
743
|
+
const accountSecurity = new AccountSecurityApp();
|
|
744
|
+
const terrenoApp = new TerrenoApp({
|
|
745
|
+
accessControl: access,
|
|
746
|
+
skipListen: true,
|
|
747
|
+
userModel: securityUserModel,
|
|
748
|
+
})
|
|
749
|
+
.register(new BetterAuthApp({config: testBetterAuthConfig(), userModel: securityUserModel}))
|
|
750
|
+
.register(new AuditApp())
|
|
751
|
+
.register(new OrgsApp({access, accountSecurity, userModel: securityUserModel}))
|
|
752
|
+
.register(accountSecurity);
|
|
753
|
+
const app = terrenoApp.build();
|
|
754
|
+
const orgAdmin = await signUp(app, "late-registration-admin");
|
|
755
|
+
const orgA = await createOrg(`Late ${orgAdmin.id}`, orgAdmin.id);
|
|
756
|
+
await addMember(orgA, orgAdmin.id, {roleName: "org-admin"});
|
|
757
|
+
const target = await signUpWithTwoFactor(app, "late-registration-target");
|
|
758
|
+
await addMember(orgA, target.id);
|
|
759
|
+
|
|
760
|
+
expect((await orgSummary(app, orgAdmin.token, orgA, target.id)).status).toBe(200);
|
|
761
|
+
});
|
|
762
|
+
});
|
|
763
|
+
|
|
764
|
+
it("documents the org routes in OpenAPI with orgId and userId path parameters", async () => {
|
|
765
|
+
const {accountSecurityApp, app} = await buildOrgApp();
|
|
766
|
+
const spec = (await supertest(app).get("/openapi.json").expect(200)).body as {
|
|
767
|
+
paths: Record<string, Record<string, {parameters?: {in: string; name: string}[]}>>;
|
|
768
|
+
};
|
|
769
|
+
const orgPaths = Object.keys(spec.paths).filter((path) => path.startsWith("/orgs/"));
|
|
770
|
+
expect(orgPaths.some((path) => path.includes("orgId") && !path.includes("{orgId}"))).toBe(
|
|
771
|
+
false
|
|
772
|
+
);
|
|
773
|
+
|
|
774
|
+
const expected = [
|
|
775
|
+
{method: "get", path: "/orgs/{orgId}/members/{userId}/security"},
|
|
776
|
+
...(accountSecurityApp?.getActions() ?? [])
|
|
777
|
+
.filter((action) => !action.platformOnly)
|
|
778
|
+
.map((action) => ({
|
|
779
|
+
method: action.method,
|
|
780
|
+
path: `/orgs/{orgId}/members/{userId}/security${action.path.replace(/:([A-Za-z]+)/g, "{$1}")}`,
|
|
781
|
+
})),
|
|
782
|
+
];
|
|
783
|
+
for (const {method, path} of expected) {
|
|
784
|
+
const operation = spec.paths[path]?.[method];
|
|
785
|
+
expect({method, operation: Boolean(operation), path}).toEqual({
|
|
786
|
+
method,
|
|
787
|
+
operation: true,
|
|
788
|
+
path,
|
|
789
|
+
});
|
|
790
|
+
const pathParams = (operation?.parameters ?? [])
|
|
791
|
+
.filter((parameter) => parameter.in === "path")
|
|
792
|
+
.map((parameter) => parameter.name);
|
|
793
|
+
expect(pathParams).toEqual(expect.arrayContaining(["orgId", "userId"]));
|
|
794
|
+
}
|
|
795
|
+
expect(spec.paths["/orgs/{orgId}/members/{userId}/security/disable"]).toBeUndefined();
|
|
796
|
+
});
|
|
797
|
+
|
|
798
|
+
it("exposes no /orgs/*/security routes without AccountSecurityApp", async () => {
|
|
799
|
+
const {app, orgA, orgAdmin} = await buildOrgApp({accountSecurity: false});
|
|
800
|
+
const target = await signUp(app, "no-security-target");
|
|
801
|
+
await addMember(orgA, target.id);
|
|
802
|
+
|
|
803
|
+
expect((await orgSummary(app, orgAdmin.token, orgA, target.id)).status).toBe(404);
|
|
804
|
+
const reset = await orgAction(app, orgAdmin.token, orgA, target.id, "reset-two-factor");
|
|
805
|
+
expect(reset.status).toBe(404);
|
|
806
|
+
// OrgsApp itself is still mounted.
|
|
807
|
+
const organization = await supertest(app)
|
|
808
|
+
.get(`/orgs/${orgA}`)
|
|
809
|
+
.set("authorization", `Bearer ${orgAdmin.token}`);
|
|
810
|
+
expect(organization.status).toBe(200);
|
|
811
|
+
});
|
|
812
|
+
|
|
813
|
+
it("reaches every non-platform-only action under the org prefix, and no platform-only one", async () => {
|
|
814
|
+
const {accountSecurityApp, app, orgA} = await buildOrgApp();
|
|
815
|
+
const actor = await signUp(app, "reachability-member");
|
|
816
|
+
await addMember(orgA, actor.id);
|
|
817
|
+
const target = await signUp(app, "reachability-target");
|
|
818
|
+
await addMember(orgA, target.id);
|
|
819
|
+
const actions = accountSecurityApp?.getActions() ?? [];
|
|
820
|
+
expect(actions.length).toBeGreaterThan(0);
|
|
821
|
+
|
|
822
|
+
for (const action of actions) {
|
|
823
|
+
const path = `${memberPath(orgA, target.id)}${action.path.replace(/:[A-Za-z]+/g, "x")}`;
|
|
824
|
+
const res = await supertest(app)
|
|
825
|
+
[action.method](path)
|
|
826
|
+
.set("authorization", `Bearer ${actor.token}`)
|
|
827
|
+
.send({});
|
|
828
|
+
if (action.platformOnly) {
|
|
829
|
+
// Not mounted: the request falls through to a 404 instead of reaching the authorizer.
|
|
830
|
+
expect({id: action.id, status: res.status}).toEqual({id: action.id, status: 404});
|
|
831
|
+
} else {
|
|
832
|
+
// Mounted: the org authorizer answers (a plain member is refused).
|
|
833
|
+
expect({id: action.id, status: res.status, title: titleOf(res)}).toEqual({
|
|
834
|
+
id: action.id,
|
|
835
|
+
status: 403,
|
|
836
|
+
title: NOT_ORG_ADMIN,
|
|
837
|
+
});
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
});
|
|
841
|
+
});
|