@shipfox/api-workspaces 16.0.0 → 17.0.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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@shipfox/api-workspaces",
3
3
  "license": "MIT",
4
- "version": "16.0.0",
4
+ "version": "17.0.0",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/ShipfoxHQ/shipfox.git",
@@ -25,24 +25,24 @@
25
25
  "drizzle-orm": "^0.45.2",
26
26
  "zod": "^4.4.3",
27
27
  "@shipfox/api-common-dto": "15.0.0",
28
- "@shipfox/api-auth-dto": "15.0.0",
29
- "@shipfox/api-auth-context": "15.0.0",
28
+ "@shipfox/api-auth-context": "17.0.0",
29
+ "@shipfox/api-auth-dto": "17.0.0",
30
30
  "@shipfox/api-projects-dto": "15.0.0",
31
- "@shipfox/api-workspaces-dto": "15.0.0",
32
31
  "@shipfox/api-runners-dto": "16.0.0",
33
32
  "@shipfox/config": "1.2.4",
34
33
  "@shipfox/inter-module": "0.2.3",
35
- "@shipfox/node-auth-root-key": "0.2.3",
36
- "@shipfox/node-drizzle": "0.3.5",
37
34
  "@shipfox/node-fastify": "0.4.3",
35
+ "@shipfox/api-workspaces-dto": "15.0.0",
38
36
  "@shipfox/node-email": "0.3.5",
37
+ "@shipfox/node-drizzle": "0.3.5",
38
+ "@shipfox/node-auth-root-key": "0.2.3",
39
+ "@shipfox/node-mailer": "0.2.6",
39
40
  "@shipfox/node-module": "1.0.7",
40
- "@shipfox/node-outbox": "0.2.6",
41
- "@shipfox/node-postgres": "0.5.0",
42
41
  "@shipfox/node-opentelemetry": "0.6.5",
42
+ "@shipfox/node-outbox": "0.2.6",
43
+ "@shipfox/node-postgres": "0.5.1",
43
44
  "@shipfox/node-rate-limit": "0.4.0",
44
- "@shipfox/node-tokens": "0.3.3",
45
- "@shipfox/node-mailer": "0.2.6"
45
+ "@shipfox/node-tokens": "0.3.3"
46
46
  },
47
47
  "imports": {
48
48
  "#*": "./dist/*"
@@ -27,12 +27,19 @@ function adminHeaders(idempotencyKey: string) {
27
27
  };
28
28
  }
29
29
 
30
+ let authenticatedImpersonatorId: string | undefined;
31
+
30
32
  const fakeUserAuth: AuthMethod = {
31
33
  name: AUTH_USER,
32
34
  authenticate: (request: FastifyRequest) => {
33
35
  setUserContext(
34
36
  request,
35
- buildUserContext({userId: USER_ID, email: 'admin@example.com', memberships: []}),
37
+ buildUserContext({
38
+ userId: USER_ID,
39
+ email: 'admin@example.com',
40
+ memberships: [],
41
+ impersonatorId: authenticatedImpersonatorId,
42
+ }),
36
43
  );
37
44
  return Promise.resolve();
38
45
  },
@@ -46,6 +53,7 @@ describe('GET /admin/workspaces', () => {
46
53
 
47
54
  beforeEach(async () => {
48
55
  await closeApp();
56
+ authenticatedImpersonatorId = undefined;
49
57
  await db().execute(
50
58
  sql`TRUNCATE workspaces_admin_command_results, workspaces_outbox, workspaces_workspaces CASCADE`,
51
59
  );
@@ -414,6 +422,20 @@ describe('GET /admin/workspaces', () => {
414
422
  ]);
415
423
  });
416
424
 
425
+ test('rejects an impersonated session before consulting the administrator role', async () => {
426
+ authenticatedImpersonatorId = crypto.randomUUID();
427
+
428
+ const response = await app.inject({
429
+ method: 'GET',
430
+ url: '/admin/workspaces',
431
+ headers: {authorization: 'Bearer user'},
432
+ });
433
+
434
+ expect(response.statusCode).toBe(403);
435
+ expect(response.json()).toEqual({code: 'admin-role-required'});
436
+ expect(auth.requireAdminRole).not.toHaveBeenCalled();
437
+ });
438
+
417
439
  test('maps an insufficient administrator role to forbidden', async () => {
418
440
  vi.mocked(auth.requireAdminRole).mockRejectedValue(
419
441
  createInterModuleKnownError(
@@ -1,4 +1,4 @@
1
- import {AUTH_USER, getUserContext} from '@shipfox/api-auth-context';
1
+ import {AUTH_USER, adoptAdministrationActorGuard, getUserContext} from '@shipfox/api-auth-context';
2
2
  import {
3
3
  type AuthInterModuleClient,
4
4
  authInterModuleContract,
@@ -219,5 +219,8 @@ export function createAdminWorkspacesRoutes(params: {
219
219
  execute: reactivateWorkspace,
220
220
  });
221
221
 
222
- return {prefix: '/admin/workspaces', routes: [listRoute, suspendRoute, reactivateRoute]};
222
+ return adoptAdministrationActorGuard({
223
+ prefix: '/admin/workspaces',
224
+ routes: [listRoute, suspendRoute, reactivateRoute],
225
+ });
223
226
  }
@@ -60,6 +60,37 @@ describe('POST /invitations/accept', () => {
60
60
  );
61
61
  });
62
62
 
63
+ test('rejects an impersonated session with impersonation-not-permitted', async () => {
64
+ const owner = await signupVerifyLogin(app, 'accept-impersonated-owner');
65
+ const guest = await signupVerifyLogin(app, 'accept-impersonated-guest');
66
+ const workspaceId = await createWorkspace(app, owner.token);
67
+ const invite = await createInvite(app, {
68
+ token: owner.token,
69
+ workspaceId,
70
+ email: guest.email,
71
+ });
72
+ const impersonatorId = crypto.randomUUID();
73
+ const token = `claim:${guest.userId}:${guest.email}:${workspaceId}:active:${impersonatorId}`;
74
+
75
+ const res = await app.inject({
76
+ method: 'POST',
77
+ url: '/invitations/accept',
78
+ headers: {authorization: `Bearer ${token}`},
79
+ payload: {token: invite.rawToken},
80
+ });
81
+ const members = await app.inject({
82
+ method: 'GET',
83
+ url: `/workspaces/${workspaceId}/members`,
84
+ headers: {authorization: `Bearer ${owner.token}`},
85
+ });
86
+
87
+ expect(res.statusCode).toBe(403);
88
+ expect(res.json().code).toBe('impersonation-not-permitted');
89
+ expect(
90
+ members.json().members.map((member: {user_email: string}) => member.user_email),
91
+ ).not.toContain(guest.email);
92
+ });
93
+
63
94
  test('returns 200 when the invitee is already a member', async () => {
64
95
  const owner = await signupVerifyLogin(app, 'accept-existing-owner');
65
96
  const workspaceId = await createWorkspace(app, owner.token);
@@ -1,4 +1,4 @@
1
- import {AUTH_USER, getUserContext} from '@shipfox/api-auth-context';
1
+ import {AUTH_USER, getUserContext, rejectImpersonatedSession} from '@shipfox/api-auth-context';
2
2
  import {
3
3
  acceptInvitationBodySchema,
4
4
  acceptInvitationResponseSchema,
@@ -48,6 +48,7 @@ export const acceptInvitationRoute = defineRoute({
48
48
  if (!client) {
49
49
  throw new ClientError('Authentication required', 'unauthorized', {status: 401});
50
50
  }
51
+ rejectImpersonatedSession(request);
51
52
 
52
53
  const {token} = request.body;
53
54
  const result = await acceptWorkspaceInvitation({
@@ -81,6 +81,23 @@ describe('POST /workspaces/:workspaceId/invitations', () => {
81
81
  expect(res.json().code).toBe('workspace-suspended');
82
82
  });
83
83
 
84
+ test('rejects an impersonated session with impersonation-not-permitted', async () => {
85
+ const workspaceId = crypto.randomUUID();
86
+ const inviteeEmail = uniqueEmail('impersonated-invite');
87
+ const token = `claim:${crypto.randomUUID()}:impersonated-invite-create@example.com:${workspaceId}:active:${crypto.randomUUID()}`;
88
+
89
+ const res = await app.inject({
90
+ method: 'POST',
91
+ url: `/workspaces/${workspaceId}/invitations`,
92
+ headers: {authorization: `Bearer ${token}`},
93
+ payload: {email: inviteeEmail},
94
+ });
95
+
96
+ expect(res.statusCode).toBe(403);
97
+ expect(res.json().code).toBe('impersonation-not-permitted');
98
+ expect(await invitationOutboxEventsTo(inviteeEmail)).toHaveLength(0);
99
+ });
100
+
84
101
  test('transforms a whitespace/case-equivalent duplicate open invitation into 409', async () => {
85
102
  const owner = await signupVerifyLogin(app, 'invite-create-duplicate-equivalent');
86
103
  const workspaceId = await createWorkspace(app, owner.token);
@@ -1,4 +1,9 @@
1
- import {AUTH_USER, getUserContext, requireWorkspaceAccess} from '@shipfox/api-auth-context';
1
+ import {
2
+ AUTH_USER,
3
+ getUserContext,
4
+ rejectImpersonatedSession,
5
+ requireWorkspaceAccess,
6
+ } from '@shipfox/api-auth-context';
2
7
  import {createInvitationBodySchema, invitationDtoSchema} from '@shipfox/api-workspaces-dto';
3
8
  import {ClientError, defineRoute} from '@shipfox/node-fastify';
4
9
  import {z} from 'zod';
@@ -45,6 +50,7 @@ export const createInvitationRoute = defineRoute({
45
50
  }
46
51
 
47
52
  const {workspaceId} = request.params;
53
+ rejectImpersonatedSession(request);
48
54
  requireWorkspaceAccess({request, workspaceId});
49
55
  const {email} = request.body;
50
56
  const invitation = await createWorkspaceInvitation({
package/test/routes.ts CHANGED
@@ -57,7 +57,7 @@ const fakeUserAuth: AuthMethod = {
57
57
  throw new ClientError('Authentication required', 'unauthorized', {status: 401});
58
58
  }
59
59
  if (raw?.startsWith('claim:')) {
60
- const [, userId, email, workspaceId, status] = raw.split(':');
60
+ const [, userId, email, workspaceId, status, impersonatorId] = raw.split(':');
61
61
  if (!userId || !email || !workspaceId) throw new Error('Invalid test user claim token');
62
62
  const workspaceStatus = status === 'suspended' || status === 'deleted' ? status : 'active';
63
63
  setUserContext(
@@ -66,6 +66,7 @@ const fakeUserAuth: AuthMethod = {
66
66
  userId,
67
67
  email,
68
68
  memberships: [{workspaceId, role: 'admin', workspaceStatus}],
69
+ impersonatorId: impersonatorId || undefined,
69
70
  }),
70
71
  );
71
72
  return;