@simple-auth-kit/cli 1.4.2 → 1.5.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.
Files changed (133) hide show
  1. package/package.json +1 -1
  2. package/registry/combos/express-drizzle/shared/src/{auth-core-error.middleware.ts → middleware/auth-core-error.middleware.ts} +17 -4
  3. package/registry/combos/express-drizzle/shared/src/{auth.middleware.ts → middleware/auth.middleware.ts} +23 -7
  4. package/registry/combos/{express-prisma/shared/src → express-drizzle/shared/src/middleware}/response-envelope.middleware.ts +15 -3
  5. package/registry/combos/express-drizzle/shared/src/{oauth.repository.ts → repositories/oauth.repository.ts} +32 -9
  6. package/registry/combos/express-drizzle/shared/src/repositories/password-reset.repository.ts +59 -0
  7. package/registry/combos/express-drizzle/shared/src/{session.repository.ts → repositories/session.repository.ts} +52 -14
  8. package/registry/combos/express-drizzle/shared/src/{two-factor.repository.ts → repositories/two-factor.repository.ts} +20 -6
  9. package/registry/combos/express-drizzle/shared/src/request-context.ts +1 -1
  10. package/registry/combos/express-drizzle/shared/src/routers/auth.router.ts +395 -0
  11. package/registry/combos/express-drizzle/variants/base/src/create-auth-app.ts +52 -19
  12. package/registry/combos/express-drizzle/variants/base/src/{authz.middleware.ts → middleware/authz.middleware.ts} +15 -7
  13. package/registry/combos/express-drizzle/variants/base/src/{audit-log.repository.ts → repositories/audit-log.repository.ts} +28 -10
  14. package/registry/combos/express-drizzle/variants/base/src/{rbac.repository.ts → repositories/rbac.repository.ts} +239 -48
  15. package/registry/combos/express-drizzle/variants/base/src/routers/admin.router.ts +497 -0
  16. package/registry/combos/express-drizzle/variants/base/src/{auth.service.ts → services/auth.service.ts} +480 -109
  17. package/registry/combos/express-drizzle/variants/workspaces/src/create-auth-app.ts +69 -23
  18. package/registry/combos/express-drizzle/variants/workspaces/src/{authz.middleware.ts → middleware/authz.middleware.ts} +32 -12
  19. package/registry/combos/express-drizzle/variants/workspaces/src/openapi-workspace.ts +147 -28
  20. package/registry/combos/express-drizzle/variants/workspaces/src/{audit-log.repository.ts → repositories/audit-log.repository.ts} +30 -11
  21. package/registry/combos/express-drizzle/variants/workspaces/src/{rbac.repository.ts → repositories/rbac.repository.ts} +319 -70
  22. package/registry/combos/express-drizzle/variants/workspaces/src/{workspace.repository.ts → repositories/workspace.repository.ts} +127 -30
  23. package/registry/combos/express-drizzle/variants/workspaces/src/routers/admin.router.ts +534 -0
  24. package/registry/combos/express-drizzle/variants/workspaces/src/routers/workspace.router.ts +194 -0
  25. package/registry/combos/express-drizzle/variants/workspaces/src/{auth.service.ts → services/auth.service.ts} +544 -122
  26. package/registry/combos/express-prisma/shared/src/{auth-core-error.middleware.ts → middleware/auth-core-error.middleware.ts} +17 -4
  27. package/registry/combos/express-prisma/shared/src/{auth.middleware.ts → middleware/auth.middleware.ts} +19 -6
  28. package/registry/combos/{express-drizzle/shared/src → express-prisma/shared/src/middleware}/response-envelope.middleware.ts +15 -3
  29. package/registry/combos/express-prisma/shared/src/{oauth.repository.ts → repositories/oauth.repository.ts} +27 -9
  30. package/registry/combos/express-prisma/shared/src/repositories/password-reset.repository.ts +55 -0
  31. package/registry/combos/express-prisma/shared/src/{session.repository.ts → repositories/session.repository.ts} +30 -9
  32. package/registry/combos/express-prisma/shared/src/{two-factor.repository.ts → repositories/two-factor.repository.ts} +11 -4
  33. package/registry/combos/express-prisma/shared/src/request-context.ts +1 -1
  34. package/registry/combos/express-prisma/shared/src/routers/auth.router.ts +398 -0
  35. package/registry/combos/express-prisma/variants/base/src/create-auth-app.ts +57 -22
  36. package/registry/combos/express-prisma/variants/base/src/{authz.middleware.ts → middleware/authz.middleware.ts} +15 -7
  37. package/registry/combos/express-prisma/variants/base/src/rbac.defaults.ts +67 -19
  38. package/registry/combos/express-prisma/variants/base/src/{audit-log.repository.ts → repositories/audit-log.repository.ts} +16 -5
  39. package/registry/combos/express-prisma/variants/base/src/{rbac.repository.ts → repositories/rbac.repository.ts} +199 -48
  40. package/registry/combos/express-prisma/variants/base/src/routers/admin.router.ts +508 -0
  41. package/registry/combos/express-prisma/variants/base/src/seed.ts +78 -20
  42. package/registry/combos/express-prisma/variants/base/src/{auth.service.ts → services/auth.service.ts} +454 -105
  43. package/registry/combos/express-prisma/variants/workspaces/src/create-auth-app.ts +81 -26
  44. package/registry/combos/express-prisma/variants/workspaces/src/{authz.middleware.ts → middleware/authz.middleware.ts} +32 -12
  45. package/registry/combos/express-prisma/variants/workspaces/src/openapi-workspace.ts +147 -28
  46. package/registry/combos/express-prisma/variants/workspaces/src/rbac.defaults.ts +73 -21
  47. package/registry/combos/express-prisma/variants/workspaces/src/{audit-log.repository.ts → repositories/audit-log.repository.ts} +16 -5
  48. package/registry/combos/express-prisma/variants/workspaces/src/{rbac.repository.ts → repositories/rbac.repository.ts} +272 -64
  49. package/registry/combos/express-prisma/variants/workspaces/src/{workspace.repository.ts → repositories/workspace.repository.ts} +86 -23
  50. package/registry/combos/express-prisma/variants/workspaces/src/routers/admin.router.ts +545 -0
  51. package/registry/combos/express-prisma/variants/workspaces/src/routers/workspace.router.ts +194 -0
  52. package/registry/combos/express-prisma/variants/workspaces/src/seed.ts +109 -29
  53. package/registry/combos/express-prisma/variants/workspaces/src/{auth.service.ts → services/auth.service.ts} +518 -119
  54. package/registry/combos/nestjs-drizzle/shared/src/{auth.controller.ts → controllers/auth.controller.ts} +6 -6
  55. package/registry/combos/nestjs-drizzle/shared/src/{auth-core-error.filter.ts → filters/auth-core-error.filter.ts} +29 -5
  56. package/registry/combos/nestjs-drizzle/shared/src/{ability.guard.ts → guards/ability.guard.ts} +20 -7
  57. package/registry/combos/nestjs-drizzle/shared/src/{auth.guard.ts → guards/auth.guard.ts} +19 -7
  58. package/registry/combos/{nestjs-prisma/shared/src → nestjs-drizzle/shared/src/interceptors}/response.interceptor.ts +10 -2
  59. package/registry/combos/nestjs-drizzle/shared/src/repositories/oauth.repository.ts +63 -0
  60. package/registry/combos/nestjs-drizzle/shared/src/repositories/password-reset.repository.ts +59 -0
  61. package/registry/combos/nestjs-drizzle/shared/src/{session.repository.ts → repositories/session.repository.ts} +52 -14
  62. package/registry/combos/nestjs-drizzle/shared/src/{two-factor.repository.ts → repositories/two-factor.repository.ts} +21 -7
  63. package/registry/combos/nestjs-drizzle/shared/src/request-context.ts +1 -1
  64. package/registry/combos/nestjs-drizzle/test/prove-cycle.ts +1097 -307
  65. package/registry/combos/nestjs-drizzle/variants/base/src/auth.module.ts +44 -20
  66. package/registry/combos/nestjs-drizzle/variants/base/src/{admin.controller.ts → controllers/admin.controller.ts} +228 -62
  67. package/registry/combos/nestjs-drizzle/variants/base/src/{authz.guard.ts → guards/authz.guard.ts} +12 -5
  68. package/registry/combos/nestjs-drizzle/variants/base/src/{audit-log.repository.ts → repositories/audit-log.repository.ts} +27 -10
  69. package/registry/combos/nestjs-drizzle/variants/base/src/{rbac.repository.ts → repositories/rbac.repository.ts} +249 -51
  70. package/registry/combos/nestjs-drizzle/variants/base/src/{auth.service.ts → services/auth.service.ts} +499 -116
  71. package/registry/combos/nestjs-drizzle/variants/workspaces/src/auth.module.ts +48 -22
  72. package/registry/combos/nestjs-drizzle/variants/workspaces/src/{admin.controller.ts → controllers/admin.controller.ts} +269 -70
  73. package/registry/combos/nestjs-drizzle/variants/workspaces/src/{workspace.controller.ts → controllers/workspace.controller.ts} +93 -26
  74. package/registry/combos/nestjs-drizzle/variants/workspaces/src/{authz.guard.ts → guards/authz.guard.ts} +26 -8
  75. package/registry/combos/nestjs-drizzle/variants/workspaces/src/{audit-log.repository.ts → repositories/audit-log.repository.ts} +29 -11
  76. package/registry/combos/nestjs-drizzle/variants/workspaces/src/{rbac.repository.ts → repositories/rbac.repository.ts} +323 -71
  77. package/registry/combos/nestjs-drizzle/variants/workspaces/src/{workspace.repository.ts → repositories/workspace.repository.ts} +131 -30
  78. package/registry/combos/nestjs-drizzle/variants/workspaces/src/services/auth.service.ts +988 -0
  79. package/registry/combos/nestjs-prisma/shared/src/{auth.controller.ts → controllers/auth.controller.ts} +6 -6
  80. package/registry/combos/nestjs-prisma/shared/src/{auth-core-error.filter.ts → filters/auth-core-error.filter.ts} +29 -5
  81. package/registry/combos/nestjs-prisma/shared/src/{ability.guard.ts → guards/ability.guard.ts} +20 -7
  82. package/registry/combos/nestjs-prisma/shared/src/{auth.guard.ts → guards/auth.guard.ts} +19 -7
  83. package/registry/combos/{nestjs-drizzle/shared/src → nestjs-prisma/shared/src/interceptors}/response.interceptor.ts +10 -2
  84. package/registry/combos/nestjs-prisma/shared/src/{oauth.repository.ts → repositories/oauth.repository.ts} +25 -8
  85. package/registry/combos/nestjs-prisma/shared/src/repositories/password-reset.repository.ts +55 -0
  86. package/registry/combos/nestjs-prisma/shared/src/{session.repository.ts → repositories/session.repository.ts} +30 -9
  87. package/registry/combos/nestjs-prisma/shared/src/{two-factor.repository.ts → repositories/two-factor.repository.ts} +11 -4
  88. package/registry/combos/nestjs-prisma/shared/src/request-context.ts +1 -1
  89. package/registry/combos/nestjs-prisma/test/prove-cycle.ts +1097 -307
  90. package/registry/combos/nestjs-prisma/variants/base/src/auth.module.ts +59 -28
  91. package/registry/combos/nestjs-prisma/variants/base/src/{admin.controller.ts → controllers/admin.controller.ts} +469 -110
  92. package/registry/combos/nestjs-prisma/variants/base/src/{audit-log.gateway.ts → gateways/audit-log.gateway.ts} +22 -6
  93. package/registry/combos/nestjs-prisma/variants/base/src/{authz.guard.ts → guards/authz.guard.ts} +12 -5
  94. package/registry/combos/nestjs-prisma/variants/base/src/rbac.defaults.ts +98 -27
  95. package/registry/combos/nestjs-prisma/variants/base/src/{audit-log.repository.ts → repositories/audit-log.repository.ts} +16 -5
  96. package/registry/combos/nestjs-prisma/variants/base/src/{country.repository.ts → repositories/country.repository.ts} +98 -25
  97. package/registry/combos/nestjs-prisma/variants/base/src/{customer.repository.ts → repositories/customer.repository.ts} +106 -27
  98. package/registry/combos/nestjs-prisma/variants/base/src/{language.repository.ts → repositories/language.repository.ts} +93 -25
  99. package/registry/combos/nestjs-prisma/variants/base/src/{rbac.repository.ts → repositories/rbac.repository.ts} +229 -55
  100. package/registry/combos/nestjs-prisma/variants/base/src/seed.ts +78 -20
  101. package/registry/combos/nestjs-prisma/variants/base/src/services/auth.service.ts +1064 -0
  102. package/registry/combos/nestjs-prisma/variants/workspaces/src/auth.module.ts +55 -27
  103. package/registry/combos/nestjs-prisma/variants/workspaces/src/{admin.controller.ts → controllers/admin.controller.ts} +544 -128
  104. package/registry/combos/nestjs-prisma/variants/workspaces/src/{workspace.controller.ts → controllers/workspace.controller.ts} +93 -26
  105. package/registry/combos/nestjs-prisma/variants/workspaces/src/{authz.guard.ts → guards/authz.guard.ts} +26 -8
  106. package/registry/combos/nestjs-prisma/variants/workspaces/src/rbac.defaults.ts +104 -29
  107. package/registry/combos/nestjs-prisma/variants/workspaces/src/{audit-log.repository.ts → repositories/audit-log.repository.ts} +16 -5
  108. package/registry/combos/nestjs-prisma/variants/workspaces/src/{country.repository.ts → repositories/country.repository.ts} +127 -26
  109. package/registry/combos/nestjs-prisma/variants/workspaces/src/{customer.repository.ts → repositories/customer.repository.ts} +137 -28
  110. package/registry/combos/nestjs-prisma/variants/workspaces/src/repositories/language.repository.ts +243 -0
  111. package/registry/combos/nestjs-prisma/variants/workspaces/src/{rbac.repository.ts → repositories/rbac.repository.ts} +314 -74
  112. package/registry/combos/nestjs-prisma/variants/workspaces/src/{workspace.repository.ts → repositories/workspace.repository.ts} +94 -24
  113. package/registry/combos/nestjs-prisma/variants/workspaces/src/seed.ts +109 -29
  114. package/registry/combos/nestjs-prisma/variants/workspaces/src/services/auth.service.ts +961 -0
  115. package/registry.json +6 -4
  116. package/simple-auth-kit.ts +582 -124
  117. package/registry/combos/express-drizzle/shared/src/auth.router.ts +0 -266
  118. package/registry/combos/express-drizzle/shared/src/password-reset.repository.ts +0 -35
  119. package/registry/combos/express-drizzle/variants/base/src/admin.router.ts +0 -307
  120. package/registry/combos/express-drizzle/variants/workspaces/src/admin.router.ts +0 -323
  121. package/registry/combos/express-drizzle/variants/workspaces/src/workspace.router.ts +0 -123
  122. package/registry/combos/express-prisma/shared/src/auth.router.ts +0 -269
  123. package/registry/combos/express-prisma/shared/src/password-reset.repository.ts +0 -29
  124. package/registry/combos/express-prisma/variants/base/src/admin.router.ts +0 -314
  125. package/registry/combos/express-prisma/variants/workspaces/src/admin.router.ts +0 -330
  126. package/registry/combos/express-prisma/variants/workspaces/src/workspace.router.ts +0 -123
  127. package/registry/combos/nestjs-drizzle/shared/src/oauth.repository.ts +0 -36
  128. package/registry/combos/nestjs-drizzle/shared/src/password-reset.repository.ts +0 -29
  129. package/registry/combos/nestjs-drizzle/variants/workspaces/src/auth.service.ts +0 -553
  130. package/registry/combos/nestjs-prisma/shared/src/password-reset.repository.ts +0 -29
  131. package/registry/combos/nestjs-prisma/variants/base/src/auth.service.ts +0 -618
  132. package/registry/combos/nestjs-prisma/variants/workspaces/src/auth.service.ts +0 -509
  133. package/registry/combos/nestjs-prisma/variants/workspaces/src/language.repository.ts +0 -143
@@ -0,0 +1,545 @@
1
+ import type { NextFunction, Request, RequestHandler, Response } from "express";
2
+ import { AuthService } from "../services/auth.service.js";
3
+ import { HttpError } from "../http-error.js";
4
+ import { ability, createTieredRouter } from "../route-tiers.js";
5
+ import { WorkspaceRepository } from "../repositories/workspace.repository.js";
6
+ import { hashPassword } from "@/lib/auth/core/crypto.js";
7
+ import "../request-context.js";
8
+
9
+ function requireString(value: unknown, field: string): string {
10
+ if (typeof value !== "string" || value.length === 0)
11
+ throw new HttpError(400, `${field} is required`);
12
+ return value;
13
+ }
14
+
15
+ const optionalString = (value: unknown): string | undefined =>
16
+ typeof value === "string" ? value : undefined;
17
+
18
+ export interface AdminRouterDeps {
19
+ auth: AuthService;
20
+ workspaces: WorkspaceRepository;
21
+ authentication: RequestHandler;
22
+ /** The mandatory workspace resolution — every route here acts inside one. See authz.middleware.ts. */
23
+ workspaceScope: RequestHandler;
24
+ }
25
+
26
+ /**
27
+ * Administration *of one workspace*, named by the `X-Workspace-Id` header. Authority is a
28
+ * permission, never a role name: the workspace middleware resolves the caller's membership and the
29
+ * roles and permissions it carries **in that workspace**, builds the CASL ability from them, and
30
+ * each route's `ability(...)` tier checks its slugs against it. There is no role-based bypass —
31
+ * holding a role called "admin" that carries no permissions gets exactly the same 403 as holding no
32
+ * role at all, which is why `npm run seed` (which provisions the catalog and the roles that carry
33
+ * it) is a prerequisite for this router doing anything.
34
+ *
35
+ * Every handler passes `req.authz` down to the service, and every service method reaches the
36
+ * database through a workspace-scoped query. That is the security property this model rests on: an
37
+ * admin of one workspace has no expressible way to name a row in another.
38
+ *
39
+ * Each route says what it demands; the rows say who is granted it. The slugs below are checked at
40
+ * compile time against `PERMISSION_CATALOG` in `rbac.defaults.ts`, while who holds them — and
41
+ * whether the permission is active at all — is edited through this very router and takes effect on
42
+ * the next request. `GET /auth/me` returns the caller's slugs, and a console rebuilds the same
43
+ * ability from them with the same function the middleware used.
44
+ *
45
+ * Replaces the reference combo's `AdminController`. Mounted under `/auth/admin` by
46
+ * create-auth-app.ts; `createTieredRouter` stands in for its
47
+ * `@UseGuards(AuthGuard, AuthzGuard, AbilityGuard)` — see route-tiers.ts for why the tier is a
48
+ * required argument rather than a decorator.
49
+ */
50
+ export function createAdminRouter(deps: AdminRouterDeps): RequestHandler {
51
+ const { auth, workspaces, authentication, workspaceScope } = deps;
52
+ const admin = createTieredRouter({
53
+ authentication,
54
+ authorization: workspaceScope,
55
+ });
56
+
57
+ admin.route(
58
+ "get",
59
+ "/users",
60
+ ability("users:read"),
61
+ async (req: Request, res: Response, next: NextFunction) => {
62
+ try {
63
+ const { search, page, limit } = req.query as Record<
64
+ string,
65
+ string | undefined
66
+ >;
67
+ res
68
+ .status(200)
69
+ .json(
70
+ await auth.listUsers(req.authz!, {
71
+ search,
72
+ page: page ? Number(page) : undefined,
73
+ limit: limit ? Number(limit) : undefined,
74
+ }),
75
+ );
76
+ } catch (err) {
77
+ next(err);
78
+ }
79
+ },
80
+ );
81
+
82
+ // No invitation email — the account is usable immediately with the password given here.
83
+ admin.route(
84
+ "post",
85
+ "/users",
86
+ ability("users:manage"),
87
+ async (req: Request, res: Response, next: NextFunction) => {
88
+ try {
89
+ const body = req.body as Record<string, unknown>;
90
+ const passwordHash = await hashPassword(
91
+ requireString(body.password, "password"),
92
+ );
93
+ const member = await workspaces.createMember(
94
+ req.authz!.workspaceId,
95
+ {
96
+ email: requireString(body.email, "email"),
97
+ passwordHash,
98
+ firstName: optionalString(body.firstName),
99
+ lastName: optionalString(body.lastName),
100
+ displayName: optionalString(body.displayName),
101
+ phone: optionalString(body.phone),
102
+ username: optionalString(body.username),
103
+ roles: Array.isArray(body.roles)
104
+ ? body.roles.filter(
105
+ (role): role is string => typeof role === "string",
106
+ )
107
+ : undefined,
108
+ },
109
+ req.auth!.sub,
110
+ );
111
+ res.status(201).json(await auth.getUser(req.authz!, member.userId));
112
+ } catch (err) {
113
+ next(err);
114
+ }
115
+ },
116
+ );
117
+
118
+ admin.route(
119
+ "get",
120
+ "/users/:userId",
121
+ ability("users:read"),
122
+ async (req: Request, res: Response, next: NextFunction) => {
123
+ try {
124
+ res
125
+ .status(200)
126
+ .json(
127
+ await auth.getUser(
128
+ req.authz!,
129
+ requireString(req.params.userId, "userId"),
130
+ ),
131
+ );
132
+ } catch (err) {
133
+ next(err);
134
+ }
135
+ },
136
+ );
137
+
138
+ // Profile fields only — email is the login identifier and is not editable here.
139
+ admin.route(
140
+ "patch",
141
+ "/users/:userId",
142
+ ability("users:manage"),
143
+ async (req: Request, res: Response, next: NextFunction) => {
144
+ try {
145
+ const userId = requireString(req.params.userId, "userId");
146
+ const body = req.body as Record<string, unknown>;
147
+ res.status(200).json(
148
+ await auth.updateUser(
149
+ req.authz!,
150
+ userId,
151
+ {
152
+ firstName:
153
+ body.firstName === null ? null : optionalString(body.firstName),
154
+ lastName:
155
+ body.lastName === null ? null : optionalString(body.lastName),
156
+ displayName:
157
+ body.displayName === null
158
+ ? null
159
+ : optionalString(body.displayName),
160
+ phone: body.phone === null ? null : optionalString(body.phone),
161
+ username:
162
+ body.username === null ? null : optionalString(body.username),
163
+ photo: body.photo === null ? null : optionalString(body.photo),
164
+ },
165
+ req.auth!.sub,
166
+ ),
167
+ );
168
+ } catch (err) {
169
+ next(err);
170
+ }
171
+ },
172
+ );
173
+
174
+ // Soft-delete: the row survives for audit purposes, stops appearing in listings, and can no
175
+ // longer authenticate. This disables the account across every workspace it belongs to, not
176
+ // just this one — the same reach `block` already has.
177
+ admin.route(
178
+ "delete",
179
+ "/users/:userId",
180
+ ability("users:manage"),
181
+ async (req: Request, res: Response, next: NextFunction) => {
182
+ try {
183
+ const userId = requireString(req.params.userId, "userId");
184
+ if (userId === req.auth!.sub)
185
+ throw new HttpError(403, "cannot delete your own account");
186
+ await auth.deleteUser(
187
+ req.authz!,
188
+ userId,
189
+ req.auth!.sub,
190
+ optionalString(
191
+ (req.body as Record<string, unknown> | undefined)?.reason,
192
+ ),
193
+ );
194
+ res.status(200).json({ ok: true });
195
+ } catch (err) {
196
+ next(err);
197
+ }
198
+ },
199
+ );
200
+
201
+ admin.route(
202
+ "get",
203
+ "/audit-log",
204
+ ability("audit-log:read"),
205
+ async (req: Request, res: Response, next: NextFunction) => {
206
+ try {
207
+ const { userId, action, since, until, page, limit } =
208
+ req.query as Record<string, string | undefined>;
209
+ res
210
+ .status(200)
211
+ .json(
212
+ await auth.listAuditLog(req.authz!, {
213
+ userId,
214
+ action,
215
+ since,
216
+ until,
217
+ page: page ? Number(page) : undefined,
218
+ limit: limit ? Number(limit) : undefined,
219
+ }),
220
+ );
221
+ } catch (err) {
222
+ next(err);
223
+ }
224
+ },
225
+ );
226
+
227
+ admin.route(
228
+ "get",
229
+ "/permissions",
230
+ ability("permissions:read"),
231
+ async (_req: Request, res: Response, next: NextFunction) => {
232
+ try {
233
+ res.status(200).json(await auth.listPermissions());
234
+ } catch (err) {
235
+ next(err);
236
+ }
237
+ },
238
+ );
239
+
240
+ admin.route(
241
+ "post",
242
+ "/permissions",
243
+ ability("permissions:define"),
244
+ async (req: Request, res: Response, next: NextFunction) => {
245
+ try {
246
+ const body = req.body as Record<string, unknown>;
247
+ res.status(201).json(
248
+ await auth.definePermission(
249
+ {
250
+ slug: requireString(body.slug, "slug"),
251
+ name: optionalString(body.name),
252
+ displayName: optionalString(body.displayName),
253
+ description:
254
+ body.description === null
255
+ ? null
256
+ : optionalString(body.description),
257
+ group: optionalString(body.group),
258
+ groupOrder:
259
+ typeof body.groupOrder === "number"
260
+ ? body.groupOrder
261
+ : undefined,
262
+ order: typeof body.order === "number" ? body.order : undefined,
263
+ isActive:
264
+ typeof body.isActive === "boolean" ? body.isActive : undefined,
265
+ },
266
+ req.auth!.sub,
267
+ ),
268
+ );
269
+ } catch (err) {
270
+ next(err);
271
+ }
272
+ },
273
+ );
274
+
275
+ admin.route(
276
+ "get",
277
+ "/roles",
278
+ ability("roles:manage"),
279
+ async (req: Request, res: Response, next: NextFunction) => {
280
+ try {
281
+ res.status(200).json(await auth.listRoles(req.authz!));
282
+ } catch (err) {
283
+ next(err);
284
+ }
285
+ },
286
+ );
287
+
288
+ admin.route(
289
+ "post",
290
+ "/roles",
291
+ ability("roles:manage"),
292
+ async (req: Request, res: Response, next: NextFunction) => {
293
+ try {
294
+ const body = req.body as Record<string, unknown>;
295
+ res.status(201).json(
296
+ await auth.createRole(
297
+ req.authz!,
298
+ {
299
+ slug: requireString(body.slug, "slug"),
300
+ name: optionalString(body.name),
301
+ displayName: optionalString(body.displayName),
302
+ description: optionalString(body.description) ?? null,
303
+ },
304
+ req.auth!.sub,
305
+ ),
306
+ );
307
+ } catch (err) {
308
+ next(err);
309
+ }
310
+ },
311
+ );
312
+
313
+ admin.route(
314
+ "patch",
315
+ "/roles/:roleId",
316
+ ability("roles:manage"),
317
+ async (req: Request, res: Response, next: NextFunction) => {
318
+ try {
319
+ const roleId = requireString(req.params.roleId, "roleId");
320
+ const body = req.body as Record<string, unknown>;
321
+ res.status(200).json(
322
+ await auth.updateRole(
323
+ req.authz!,
324
+ roleId,
325
+ {
326
+ name: optionalString(body.name),
327
+ displayName: optionalString(body.displayName),
328
+ description:
329
+ body.description === null
330
+ ? null
331
+ : optionalString(body.description),
332
+ isActive:
333
+ typeof body.isActive === "boolean" ? body.isActive : undefined,
334
+ },
335
+ req.auth!.sub,
336
+ ),
337
+ );
338
+ } catch (err) {
339
+ next(err);
340
+ }
341
+ },
342
+ );
343
+
344
+ // Soft-delete: existing assignments are left in place rather than cascade-deleted, and the role
345
+ // simply stops being resolved.
346
+ admin.route(
347
+ "delete",
348
+ "/roles/:roleId",
349
+ ability("roles:manage"),
350
+ async (req: Request, res: Response, next: NextFunction) => {
351
+ try {
352
+ const roleId = requireString(req.params.roleId, "roleId");
353
+ await auth.deleteRole(
354
+ req.authz!,
355
+ roleId,
356
+ req.auth!.sub,
357
+ optionalString(
358
+ (req.body as Record<string, unknown> | undefined)?.reason,
359
+ ),
360
+ );
361
+ res.status(200).json({ ok: true });
362
+ } catch (err) {
363
+ next(err);
364
+ }
365
+ },
366
+ );
367
+
368
+ admin.route(
369
+ "post",
370
+ "/roles/:roleId/permissions",
371
+ ability("roles:manage"),
372
+ async (req: Request, res: Response, next: NextFunction) => {
373
+ try {
374
+ const body = req.body as Record<string, unknown>;
375
+ await auth.attachPermissionToRole(
376
+ req.authz!,
377
+ requireString(req.params.roleId, "roleId"),
378
+ requireString(body.permission, "permission"),
379
+ req.auth!.sub,
380
+ );
381
+ res.status(201).json({ ok: true });
382
+ } catch (err) {
383
+ next(err);
384
+ }
385
+ },
386
+ );
387
+
388
+ admin.route(
389
+ "post",
390
+ "/users/:userId/roles",
391
+ ability("roles:assign"),
392
+ async (req: Request, res: Response, next: NextFunction) => {
393
+ try {
394
+ const body = req.body as Record<string, unknown>;
395
+ await auth.assignRole(
396
+ req.authz!,
397
+ requireString(req.params.userId, "userId"),
398
+ requireString(body.role, "role"),
399
+ );
400
+ res.status(201).json({ ok: true });
401
+ } catch (err) {
402
+ next(err);
403
+ }
404
+ },
405
+ );
406
+
407
+ admin.route(
408
+ "post",
409
+ "/users/:userId/roles/:roleSlug/revoke",
410
+ ability("roles:assign"),
411
+ async (req: Request, res: Response, next: NextFunction) => {
412
+ try {
413
+ const userId = requireString(req.params.userId, "userId");
414
+ // Revoking your own `admin` would strip the very permission that let you call this,
415
+ // with no route back in. Assigning to yourself is fine — it can't lock anyone out.
416
+ if (userId === req.auth!.sub)
417
+ throw new HttpError(403, "cannot change your own roles");
418
+ await auth.revokeRole(
419
+ req.authz!,
420
+ userId,
421
+ requireString(req.params.roleSlug, "roleSlug"),
422
+ );
423
+ res.status(201).json({ ok: true });
424
+ } catch (err) {
425
+ next(err);
426
+ }
427
+ },
428
+ );
429
+
430
+ admin.route(
431
+ "post",
432
+ "/users/:userId/permissions",
433
+ ability("permissions:grant"),
434
+ async (req: Request, res: Response, next: NextFunction) => {
435
+ try {
436
+ const body = req.body as Record<string, unknown>;
437
+ await auth.grantPermission(
438
+ req.authz!,
439
+ requireString(req.params.userId, "userId"),
440
+ requireString(body.permission, "permission"),
441
+ req.auth!.sub,
442
+ );
443
+ res.status(201).json({ ok: true });
444
+ } catch (err) {
445
+ next(err);
446
+ }
447
+ },
448
+ );
449
+
450
+ admin.route(
451
+ "post",
452
+ "/users/:userId/permissions/:permissionSlug/revoke",
453
+ ability("permissions:grant"),
454
+ async (req: Request, res: Response, next: NextFunction) => {
455
+ try {
456
+ await auth.revokePermission(
457
+ req.authz!,
458
+ requireString(req.params.userId, "userId"),
459
+ requireString(req.params.permissionSlug, "permissionSlug"),
460
+ );
461
+ res.status(201).json({ ok: true });
462
+ } catch (err) {
463
+ next(err);
464
+ }
465
+ },
466
+ );
467
+
468
+ admin.route(
469
+ "post",
470
+ "/users/:userId/block",
471
+ ability("users:block"),
472
+ async (req: Request, res: Response, next: NextFunction) => {
473
+ try {
474
+ const userId = requireString(req.params.userId, "userId");
475
+ if (userId === req.auth!.sub)
476
+ throw new HttpError(403, "cannot block your own account");
477
+ await auth.block(req.authz!, userId, {
478
+ userId: req.auth!.sub,
479
+ ip: req.ip,
480
+ });
481
+ res.status(201).json({ ok: true });
482
+ } catch (err) {
483
+ next(err);
484
+ }
485
+ },
486
+ );
487
+
488
+ admin.route(
489
+ "post",
490
+ "/users/:userId/unblock",
491
+ ability("users:block"),
492
+ async (req: Request, res: Response, next: NextFunction) => {
493
+ try {
494
+ await auth.unblock(
495
+ req.authz!,
496
+ requireString(req.params.userId, "userId"),
497
+ { userId: req.auth!.sub, ip: req.ip },
498
+ );
499
+ res.status(201).json({ ok: true });
500
+ } catch (err) {
501
+ next(err);
502
+ }
503
+ },
504
+ );
505
+
506
+ admin.route(
507
+ "post",
508
+ "/users/:userId/deactivate",
509
+ ability("users:block"),
510
+ async (req: Request, res: Response, next: NextFunction) => {
511
+ try {
512
+ const userId = requireString(req.params.userId, "userId");
513
+ if (userId === req.auth!.sub)
514
+ throw new HttpError(403, "cannot deactivate your own account");
515
+ await auth.deactivate(req.authz!, userId, {
516
+ userId: req.auth!.sub,
517
+ ip: req.ip,
518
+ });
519
+ res.status(201).json({ ok: true });
520
+ } catch (err) {
521
+ next(err);
522
+ }
523
+ },
524
+ );
525
+
526
+ admin.route(
527
+ "post",
528
+ "/users/:userId/activate",
529
+ ability("users:block"),
530
+ async (req: Request, res: Response, next: NextFunction) => {
531
+ try {
532
+ await auth.activate(
533
+ req.authz!,
534
+ requireString(req.params.userId, "userId"),
535
+ { userId: req.auth!.sub, ip: req.ip },
536
+ );
537
+ res.status(201).json({ ok: true });
538
+ } catch (err) {
539
+ next(err);
540
+ }
541
+ },
542
+ );
543
+
544
+ return admin.handler;
545
+ }