@shipfox/api-auth 9.3.0 → 10.1.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/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +48 -0
- package/README.md +14 -3
- package/dist/core/administration.d.ts +19 -2
- package/dist/core/administration.d.ts.map +1 -1
- package/dist/core/administration.js +37 -4
- package/dist/core/administration.js.map +1 -1
- package/dist/core/entities/administrator-read-model.d.ts +21 -0
- package/dist/core/entities/administrator-read-model.d.ts.map +1 -0
- package/dist/core/entities/administrator-read-model.js +3 -0
- package/dist/core/entities/administrator-read-model.js.map +1 -0
- package/dist/db/admin-grants.d.ts +25 -1
- package/dist/db/admin-grants.d.ts.map +1 -1
- package/dist/db/admin-grants.js +51 -14
- package/dist/db/admin-grants.js.map +1 -1
- package/dist/db/admin-users.d.ts +21 -0
- package/dist/db/admin-users.d.ts.map +1 -0
- package/dist/db/admin-users.js +32 -0
- package/dist/db/admin-users.js.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -3
- package/dist/index.js.map +1 -1
- package/dist/metrics/instance.d.ts +1 -1
- package/dist/metrics/instance.d.ts.map +1 -1
- package/dist/metrics/instance.js.map +1 -1
- package/dist/presentation/routes/administration.d.ts +2 -0
- package/dist/presentation/routes/administration.d.ts.map +1 -1
- package/dist/presentation/routes/administration.js +113 -10
- package/dist/presentation/routes/administration.js.map +1 -1
- package/dist/presentation/routes/rate-limit.d.ts.map +1 -1
- package/dist/presentation/routes/rate-limit.js +12 -0
- package/dist/presentation/routes/rate-limit.js.map +1 -1
- package/dist/tsconfig.test.tsbuildinfo +1 -1
- package/package.json +7 -7
- package/src/core/administration.ts +50 -4
- package/src/core/entities/administrator-read-model.ts +23 -0
- package/src/db/admin-grants.test.ts +12 -2
- package/src/db/admin-grants.ts +85 -39
- package/src/db/admin-users.ts +58 -0
- package/src/index.test.ts +4 -1
- package/src/index.ts +7 -2
- package/src/metrics/instance.ts +6 -1
- package/src/presentation/routes/administration.test.ts +432 -20
- package/src/presentation/routes/administration.ts +109 -10
- package/src/presentation/routes/rate-limit.ts +6 -0
- package/test/routes.ts +11 -2
- package/tsconfig.build.tsbuildinfo +1 -1
|
@@ -1,23 +1,35 @@
|
|
|
1
1
|
import {AUTH_USER} from '@shipfox/api-auth-context';
|
|
2
2
|
import {
|
|
3
|
+
adminBootstrapStateSchema,
|
|
4
|
+
administratorUserLookupQuerySchema,
|
|
5
|
+
administratorUserSummarySchema,
|
|
3
6
|
bootstrapAdminOwnerBodySchema,
|
|
4
7
|
bootstrapAdminOwnerResponseSchema,
|
|
5
8
|
grantAdminRoleBodySchema,
|
|
6
9
|
grantAdminRoleResponseSchema,
|
|
10
|
+
listAdminGrantsQuerySchema,
|
|
7
11
|
listAdminGrantsResponseSchema,
|
|
8
12
|
revokeAdminGrantBodySchema,
|
|
9
13
|
revokeAdminGrantResponseSchema,
|
|
10
14
|
} from '@shipfox/api-auth-dto';
|
|
15
|
+
import {decodeTimestampIdCursor, encodeTimestampIdCursor} from '@shipfox/node-drizzle';
|
|
11
16
|
import {ClientError, defineRoute, type RouteGroup} from '@shipfox/node-fastify';
|
|
12
17
|
import type {FastifyRequest} from 'fastify';
|
|
13
18
|
import {z} from 'zod';
|
|
19
|
+
import {requireAdminRole} from '#core/admin-role.js';
|
|
14
20
|
import {
|
|
15
21
|
bootstrapFirstAdminOwner,
|
|
22
|
+
findAdministratorUserSummary,
|
|
23
|
+
getAdminBootstrapState,
|
|
16
24
|
grantAdministratorRole,
|
|
17
|
-
|
|
25
|
+
listAdministratorGrantSummaries,
|
|
18
26
|
revokeAdministratorGrant,
|
|
19
27
|
} from '#core/administration.js';
|
|
20
28
|
import type {AdminGrant} from '#core/entities/admin-grant.js';
|
|
29
|
+
import type {
|
|
30
|
+
AdministratorGrantSummary,
|
|
31
|
+
AdministratorUserSummary,
|
|
32
|
+
} from '#core/entities/administrator-read-model.js';
|
|
21
33
|
import {
|
|
22
34
|
AdminBootstrapClosedError,
|
|
23
35
|
AdminGrantAlreadyExistsError,
|
|
@@ -52,6 +64,10 @@ function requireIdempotencyKey(request: FastifyRequest): string {
|
|
|
52
64
|
return key;
|
|
53
65
|
}
|
|
54
66
|
|
|
67
|
+
async function requireAdministratorObserver(request: FastifyRequest): Promise<void> {
|
|
68
|
+
await requireAdminRole({userId: requireActorId(request), minimumRole: 'admin-observer'});
|
|
69
|
+
}
|
|
70
|
+
|
|
55
71
|
function toAdminGrantDto(grant: AdminGrant) {
|
|
56
72
|
return {
|
|
57
73
|
id: grant.id,
|
|
@@ -63,9 +79,31 @@ function toAdminGrantDto(grant: AdminGrant) {
|
|
|
63
79
|
};
|
|
64
80
|
}
|
|
65
81
|
|
|
82
|
+
function toAdministratorUserSummaryDto(user: AdministratorUserSummary) {
|
|
83
|
+
return {
|
|
84
|
+
id: user.id,
|
|
85
|
+
email: user.email,
|
|
86
|
+
name: user.name,
|
|
87
|
+
status: user.status,
|
|
88
|
+
email_verified_at: user.emailVerifiedAt?.toISOString() ?? null,
|
|
89
|
+
created_at: user.createdAt.toISOString(),
|
|
90
|
+
admin_role: user.adminRole,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function toAdministratorGrantSummaryDto(grant: AdministratorGrantSummary) {
|
|
95
|
+
return {
|
|
96
|
+
grant_id: grant.grantId,
|
|
97
|
+
role: grant.role,
|
|
98
|
+
created_at: grant.createdAt.toISOString(),
|
|
99
|
+
revoked_at: grant.revokedAt?.toISOString() ?? null,
|
|
100
|
+
user: grant.user,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
66
104
|
function translateAdministrationError(error: unknown): never {
|
|
67
105
|
if (error instanceof AdminRoleRequiredError) {
|
|
68
|
-
throw new ClientError('Administrator
|
|
106
|
+
throw new ClientError('Administrator role required', 'forbidden', {
|
|
69
107
|
status: 403,
|
|
70
108
|
details: {required_role: error.minimumRole},
|
|
71
109
|
});
|
|
@@ -129,17 +167,66 @@ const bootstrapRoute = defineRoute({
|
|
|
129
167
|
},
|
|
130
168
|
});
|
|
131
169
|
|
|
170
|
+
const bootstrapStateRoute = defineRoute({
|
|
171
|
+
method: 'GET',
|
|
172
|
+
path: '/bootstrap-state',
|
|
173
|
+
description: 'Read whether first administrator owner bootstrap is available.',
|
|
174
|
+
schema: {response: {200: adminBootstrapStateSchema}},
|
|
175
|
+
preHandler: createAuthIpRateLimitPreHandler('bootstrap-state'),
|
|
176
|
+
errorHandler: translateAdministrationError,
|
|
177
|
+
handler: async () => ({state: await getAdminBootstrapState()}),
|
|
178
|
+
});
|
|
179
|
+
|
|
132
180
|
const listRoute = defineRoute({
|
|
133
181
|
method: 'GET',
|
|
134
182
|
path: '/',
|
|
135
|
-
description: 'List local administrator
|
|
136
|
-
schema: {
|
|
183
|
+
description: 'List bounded local administrator grant summaries.',
|
|
184
|
+
schema: {
|
|
185
|
+
querystring: listAdminGrantsQuerySchema,
|
|
186
|
+
response: {200: listAdminGrantsResponseSchema},
|
|
187
|
+
},
|
|
188
|
+
errorHandler: translateAdministrationError,
|
|
189
|
+
handler: async (request) => {
|
|
190
|
+
const {limit, cursor} = request.query;
|
|
191
|
+
const decodedCursor = decodeTimestampIdCursor(cursor);
|
|
192
|
+
if (cursor && !decodedCursor) {
|
|
193
|
+
throw new ClientError('Invalid cursor', 'invalid-cursor', {status: 400});
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const result = await listAdministratorGrantSummaries({
|
|
197
|
+
actorId: requireActorId(request),
|
|
198
|
+
limit,
|
|
199
|
+
...(decodedCursor ? {cursor: decodedCursor} : {}),
|
|
200
|
+
});
|
|
201
|
+
return {
|
|
202
|
+
grants: result.grants.map(toAdministratorGrantSummaryDto),
|
|
203
|
+
next_cursor: result.nextCursor ? encodeTimestampIdCursor(result.nextCursor) : null,
|
|
204
|
+
};
|
|
205
|
+
},
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
const userLookupRoute = defineRoute({
|
|
209
|
+
method: 'GET',
|
|
210
|
+
path: '/',
|
|
211
|
+
description: 'Find one administrator-safe user summary by exact ID or email.',
|
|
212
|
+
schema: {
|
|
213
|
+
querystring: administratorUserLookupQuerySchema,
|
|
214
|
+
response: {200: administratorUserSummarySchema},
|
|
215
|
+
},
|
|
216
|
+
preHandler: [requireAdministratorObserver, createAuthIpRateLimitPreHandler('lookup')],
|
|
137
217
|
errorHandler: translateAdministrationError,
|
|
138
|
-
handler: async (request) =>
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
)
|
|
142
|
-
|
|
218
|
+
handler: async (request) => {
|
|
219
|
+
const {id, user_id: userId, email} = request.query;
|
|
220
|
+
const lookupId = id ?? userId;
|
|
221
|
+
const actorId = requireActorId(request);
|
|
222
|
+
const user = lookupId
|
|
223
|
+
? await findAdministratorUserSummary({actorId, id: lookupId})
|
|
224
|
+
: email
|
|
225
|
+
? await findAdministratorUserSummary({actorId, email})
|
|
226
|
+
: undefined;
|
|
227
|
+
if (!user) throw new UserNotFoundError(lookupId ?? email ?? 'unknown');
|
|
228
|
+
return toAdministratorUserSummaryDto(user);
|
|
229
|
+
},
|
|
143
230
|
});
|
|
144
231
|
|
|
145
232
|
const grantRoute = defineRoute({
|
|
@@ -189,7 +276,19 @@ const revokeRoute = defineRoute({
|
|
|
189
276
|
});
|
|
190
277
|
|
|
191
278
|
export const administrationRoutes: RouteGroup = {
|
|
192
|
-
prefix: '/admin/
|
|
279
|
+
prefix: '/admin/auth/admin-grants',
|
|
193
280
|
auth: AUTH_USER,
|
|
194
281
|
routes: [bootstrapRoute, listRoute, grantRoute, revokeRoute],
|
|
195
282
|
};
|
|
283
|
+
|
|
284
|
+
export const administrationBootstrapRoutes: RouteGroup = {
|
|
285
|
+
prefix: '/admin/auth',
|
|
286
|
+
auth: AUTH_USER,
|
|
287
|
+
routes: [bootstrapStateRoute],
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
export const administrationUserRoutes: RouteGroup = {
|
|
291
|
+
prefix: '/admin/auth/users',
|
|
292
|
+
auth: AUTH_USER,
|
|
293
|
+
routes: [userLookupRoute],
|
|
294
|
+
};
|
|
@@ -23,6 +23,12 @@ const policies: Record<
|
|
|
23
23
|
bootstrap: {
|
|
24
24
|
ip: {limit: 5, windowSeconds: 15 * 60},
|
|
25
25
|
},
|
|
26
|
+
'bootstrap-state': {
|
|
27
|
+
ip: {limit: 60, windowSeconds: 5 * 60},
|
|
28
|
+
},
|
|
29
|
+
lookup: {
|
|
30
|
+
ip: {limit: 60, windowSeconds: 5 * 60},
|
|
31
|
+
},
|
|
26
32
|
};
|
|
27
33
|
|
|
28
34
|
interface EmailBody {
|
package/test/routes.ts
CHANGED
|
@@ -8,7 +8,11 @@ import {and, desc, eq, sql} from 'drizzle-orm';
|
|
|
8
8
|
import {db} from '#db/db.js';
|
|
9
9
|
import {authOutbox} from '#db/schema/outbox.js';
|
|
10
10
|
import {createJwtAuthMethod} from '#presentation/auth/jwt-auth.js';
|
|
11
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
administrationBootstrapRoutes,
|
|
13
|
+
administrationRoutes,
|
|
14
|
+
administrationUserRoutes,
|
|
15
|
+
} from '#presentation/routes/administration.js';
|
|
12
16
|
import {buildAuthRoutes} from '#presentation/routes/index.js';
|
|
13
17
|
|
|
14
18
|
const testConfig = vi.hoisted(
|
|
@@ -153,7 +157,12 @@ export async function createAuthTestApp(params?: {
|
|
|
153
157
|
}): Promise<FastifyInstance> {
|
|
154
158
|
const appConfig: AppConfig = {
|
|
155
159
|
auth: [createJwtAuthMethod()],
|
|
156
|
-
routes: [
|
|
160
|
+
routes: [
|
|
161
|
+
buildAuthRoutes(true, workspaces),
|
|
162
|
+
administrationBootstrapRoutes,
|
|
163
|
+
administrationRoutes,
|
|
164
|
+
administrationUserRoutes,
|
|
165
|
+
],
|
|
157
166
|
swagger: false,
|
|
158
167
|
};
|
|
159
168
|
if (params?.fastifyOptions) appConfig.fastifyOptions = params.fastifyOptions;
|