@guren/server 2.10.0 → 2.11.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/dist/DocsViewerServiceProvider-qFCaeiUv.js +77 -0
- package/dist/Hash-CzrwKeDp.js +133 -0
- package/dist/HttpException-BkS3euNG.js +161 -0
- package/dist/Job-BVT813DO.js +441 -0
- package/dist/McpServiceProvider-BoAN-UcD.js +367 -0
- package/dist/MessageSigner-Bd25ZJAV.js +93 -0
- package/dist/Random-DbIJ22KQ.js +191 -0
- package/dist/address-D0Iq-XIH.js +20 -0
- package/dist/auth/index.js +2 -122
- package/dist/auth-C7sVmYgc.js +1954 -0
- package/dist/authorization/index.js +2 -38
- package/dist/authorization-Dbcuc5ZK.js +610 -0
- package/dist/broadcasting/index.js +2 -26
- package/dist/broadcasting-Dw-FhyjN.js +1055 -0
- package/dist/cache/index.js +2 -17
- package/dist/cache/types.d.ts +4 -4
- package/dist/cache-BYnZf2FR.js +812 -0
- package/dist/client-Dd7vLcqp.js +27 -0
- package/dist/console/Output.d.ts +18 -18
- package/dist/encryption/index.js +5 -96
- package/dist/encryption-fOWQaKqX.js +250 -0
- package/dist/endpoint-D-20PiHY.js +46 -0
- package/dist/endpoint-D6GIqOpe.js +31 -0
- package/dist/errors-quJOkkCn.js +81 -0
- package/dist/events/index.js +2 -28
- package/dist/events-aIH_bWRx.js +410 -0
- package/dist/health/index.js +2 -24
- package/dist/health-VWdWXRwh.js +345 -0
- package/dist/hot-disposables-WsTX2ySN.js +281 -0
- package/dist/hot-reload/testing.d.ts +1 -1
- package/dist/http/resources/Resource.d.ts +23 -3
- package/dist/i18n/index.js +2 -30
- package/dist/i18n-mcWi7LCd.js +617 -0
- package/dist/index.js +7515 -7276
- package/dist/lambda/index.js +114 -71
- package/dist/logging/index.js +2 -24
- package/dist/logging-CPaRMuPP.js +518 -0
- package/dist/loopback-guard-D0Kc7-xc.js +224 -0
- package/dist/mail/index.js +2 -27
- package/dist/mail-DfEfSQEr.js +699 -0
- package/dist/mcp/index.js +2 -10
- package/dist/notifications/index.js +2 -33
- package/dist/notifications-BGqmUNZ8.js +759 -0
- package/dist/queue/index.js +3 -42
- package/dist/queue-CXrRK85G.js +948 -0
- package/dist/redis/index.js +672 -642
- package/dist/rolldown-runtime-8H4AJuhK.js +14 -0
- package/dist/runtime/index.js +472 -616
- package/dist/scheduling/index.js +2 -29
- package/dist/scheduling-CPiEdtQd.js +839 -0
- package/dist/storage/index.js +2 -15
- package/dist/storage-tWHMv2H_.js +841 -0
- package/dist/support/expiry.js +69 -12
- package/dist/trim-slashes-ZJ_VsY5s.js +25 -0
- package/dist/vite/index.js +118 -181
- package/dist/vite/plugin.d.ts +1 -1
- package/dist/vite-dev-server-BSJkduhZ.js +131 -0
- package/package.json +6 -5
- package/dist/DocsViewerServiceProvider-RUYDFBMW.js +0 -75
- package/dist/McpServiceProvider-SL7DUZ4G.js +0 -8
- package/dist/chunk-2SC566CD.js +0 -376
- package/dist/chunk-2V3YID4H.js +0 -20
- package/dist/chunk-3KV5X2FL.js +0 -31
- package/dist/chunk-63SKW5PS.js +0 -457
- package/dist/chunk-6J6B5KOB.js +0 -425
- package/dist/chunk-A5JJ7XTH.js +0 -24
- package/dist/chunk-AZOYI43J.js +0 -18
- package/dist/chunk-B5E5VBDI.js +0 -115
- package/dist/chunk-DAQKYKLH.js +0 -182
- package/dist/chunk-DRNPVBAA.js +0 -1002
- package/dist/chunk-EI5GYMAT.js +0 -853
- package/dist/chunk-H3B23O6T.js +0 -271
- package/dist/chunk-HPET6XY5.js +0 -853
- package/dist/chunk-HR2KZ3WL.js +0 -616
- package/dist/chunk-IOTWFHZU.js +0 -558
- package/dist/chunk-K6CUBJX6.js +0 -642
- package/dist/chunk-KTNJG7R7.js +0 -911
- package/dist/chunk-LAJQCSSF.js +0 -355
- package/dist/chunk-LHR6OHI7.js +0 -189
- package/dist/chunk-N7PLD7MB.js +0 -729
- package/dist/chunk-Q2P6YLRR.js +0 -24
- package/dist/chunk-QQKTH5KX.js +0 -114
- package/dist/chunk-SFQP6O52.js +0 -126
- package/dist/chunk-SQR7277M.js +0 -911
- package/dist/chunk-T2FFUU6J.js +0 -484
- package/dist/chunk-TVV5YNEC.js +0 -1842
- package/dist/chunk-UY3AZSYL.js +0 -14
- package/dist/chunk-V4DNC2ER.js +0 -114
- package/dist/chunk-VSA3QY5Q.js +0 -274
- package/dist/chunk-YSOJKAAU.js +0 -427
- package/dist/errors-PUNHAFL2.js +0 -34
|
@@ -1,38 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
Policy,
|
|
4
|
-
Response,
|
|
5
|
-
authorize,
|
|
6
|
-
authorizeAllMiddleware,
|
|
7
|
-
authorizeMiddleware,
|
|
8
|
-
authorizeResourceMiddleware,
|
|
9
|
-
can,
|
|
10
|
-
cannot,
|
|
11
|
-
createGate,
|
|
12
|
-
defineGate,
|
|
13
|
-
definePolicy,
|
|
14
|
-
getGate,
|
|
15
|
-
isAuthorizationResponse,
|
|
16
|
-
setGate,
|
|
17
|
-
withAuthorization
|
|
18
|
-
} from "../chunk-T2FFUU6J.js";
|
|
19
|
-
import "../chunk-63SKW5PS.js";
|
|
20
|
-
import "../chunk-DAQKYKLH.js";
|
|
21
|
-
export {
|
|
22
|
-
Gate,
|
|
23
|
-
Policy,
|
|
24
|
-
Response,
|
|
25
|
-
authorize,
|
|
26
|
-
authorizeAllMiddleware,
|
|
27
|
-
authorizeMiddleware,
|
|
28
|
-
authorizeResourceMiddleware,
|
|
29
|
-
can,
|
|
30
|
-
cannot,
|
|
31
|
-
createGate,
|
|
32
|
-
defineGate,
|
|
33
|
-
definePolicy,
|
|
34
|
-
getGate,
|
|
35
|
-
isAuthorizationResponse,
|
|
36
|
-
setGate,
|
|
37
|
-
withAuthorization
|
|
38
|
-
};
|
|
1
|
+
import { a as Policy, c as Response, d as cannot, f as createGate, g as setGate, h as isAuthorizationResponse, i as withAuthorization, l as authorize, m as getGate, n as authorizeMiddleware, o as definePolicy, p as defineGate, r as authorizeResourceMiddleware, s as Gate, t as authorizeAllMiddleware, u as can } from "../authorization-Dbcuc5ZK.js";
|
|
2
|
+
export { Gate, Policy, Response, authorize, authorizeAllMiddleware, authorizeMiddleware, authorizeResourceMiddleware, can, cannot, createGate, defineGate, definePolicy, getGate, isAuthorizationResponse, setGate, withAuthorization };
|
|
@@ -0,0 +1,610 @@
|
|
|
1
|
+
import { t as HttpException } from "./HttpException-BkS3euNG.js";
|
|
2
|
+
//#region src/errors/exceptions/AuthorizationException.ts
|
|
3
|
+
/**
|
|
4
|
+
* Authorization exception.
|
|
5
|
+
*
|
|
6
|
+
* Thrown when a user is authenticated but not authorized to perform an action.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* throw new AuthorizationException()
|
|
11
|
+
* throw new AuthorizationException('You cannot edit this post')
|
|
12
|
+
* throw new AuthorizationException('Access denied', 'edit', 'Post')
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
var AuthorizationException = class AuthorizationException extends HttpException {
|
|
16
|
+
/**
|
|
17
|
+
* The action that was attempted.
|
|
18
|
+
*/
|
|
19
|
+
action;
|
|
20
|
+
/**
|
|
21
|
+
* The resource that was being accessed.
|
|
22
|
+
*/
|
|
23
|
+
resource;
|
|
24
|
+
constructor(message = "This action is unauthorized.", action, resource) {
|
|
25
|
+
super(403, message);
|
|
26
|
+
this.name = "AuthorizationException";
|
|
27
|
+
this.action = action;
|
|
28
|
+
this.resource = resource;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Create exception for a specific action and resource.
|
|
32
|
+
*/
|
|
33
|
+
static forAction(action, resource) {
|
|
34
|
+
const message = resource ? `You are not authorized to ${action} this ${resource}.` : `You are not authorized to ${action}.`;
|
|
35
|
+
return new AuthorizationException(message, action, resource);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Deny access to a resource.
|
|
39
|
+
*/
|
|
40
|
+
static deny(resource) {
|
|
41
|
+
const message = resource ? `Access to ${resource} denied.` : "Access denied.";
|
|
42
|
+
return new AuthorizationException(message, "access", resource);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
//#endregion
|
|
46
|
+
//#region src/authorization/Gate.ts
|
|
47
|
+
/**
|
|
48
|
+
* Response builder for authorization checks.
|
|
49
|
+
*/
|
|
50
|
+
const Response = {
|
|
51
|
+
allow(message) {
|
|
52
|
+
return {
|
|
53
|
+
allowed: true,
|
|
54
|
+
message
|
|
55
|
+
};
|
|
56
|
+
},
|
|
57
|
+
deny(message, code) {
|
|
58
|
+
return {
|
|
59
|
+
allowed: false,
|
|
60
|
+
message: message ?? "This action is unauthorized.",
|
|
61
|
+
code
|
|
62
|
+
};
|
|
63
|
+
},
|
|
64
|
+
denyWithStatus(status, message) {
|
|
65
|
+
return {
|
|
66
|
+
allowed: false,
|
|
67
|
+
message: message ?? "This action is unauthorized.",
|
|
68
|
+
status
|
|
69
|
+
};
|
|
70
|
+
},
|
|
71
|
+
denyAsNotFound(message) {
|
|
72
|
+
return {
|
|
73
|
+
allowed: false,
|
|
74
|
+
message: message ?? "Not found.",
|
|
75
|
+
status: 404
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* Narrow a policy/gate return value to an `AuthorizationResponse`.
|
|
81
|
+
*
|
|
82
|
+
* Policies may return either a boolean or one of the `Response` objects
|
|
83
|
+
* produced by `allow()`/`deny()`/`denyWithStatus()`/`denyAsNotFound()`.
|
|
84
|
+
*/
|
|
85
|
+
function isAuthorizationResponse(value) {
|
|
86
|
+
return typeof value === "object" && value !== null && typeof value.allowed === "boolean";
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Normalize whatever a policy or gate callback returned into a response.
|
|
90
|
+
*
|
|
91
|
+
* A `{ allowed: false }` object is truthy, so anything that decides access
|
|
92
|
+
* from the raw return value fails open. Every path in this class routes
|
|
93
|
+
* through here, and unknown shapes deny.
|
|
94
|
+
*/
|
|
95
|
+
function toAuthorizationResponse(value) {
|
|
96
|
+
if (isAuthorizationResponse(value)) return value;
|
|
97
|
+
return value === true ? Response.allow() : Response.deny();
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Build the exception for a denial, honouring `denyWithStatus()` /
|
|
101
|
+
* `denyAsNotFound()` so a policy can hide a record as a 404.
|
|
102
|
+
*/
|
|
103
|
+
function denialToException(response) {
|
|
104
|
+
const message = response.message ?? "This action is unauthorized.";
|
|
105
|
+
const status = response.status;
|
|
106
|
+
if (typeof status === "number" && status !== 403) return new HttpException(status, message);
|
|
107
|
+
return new AuthorizationException(message);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Authorization gate for defining and checking abilities.
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```typescript
|
|
114
|
+
* const gate = new Gate()
|
|
115
|
+
*
|
|
116
|
+
* // Define abilities
|
|
117
|
+
* gate.define('edit-post', (user, post) => user.id === post.authorId)
|
|
118
|
+
* gate.define('admin', (user) => user.role === 'admin')
|
|
119
|
+
*
|
|
120
|
+
* // Check abilities
|
|
121
|
+
* if (await gate.allows('edit-post', post)) {
|
|
122
|
+
* // User can edit
|
|
123
|
+
* }
|
|
124
|
+
*
|
|
125
|
+
* // Or throw on denial
|
|
126
|
+
* await gate.authorize('edit-post', post) // throws if not allowed
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
var Gate = class {
|
|
130
|
+
/**
|
|
131
|
+
* Defined gates.
|
|
132
|
+
*/
|
|
133
|
+
gates = /* @__PURE__ */ new Map();
|
|
134
|
+
/**
|
|
135
|
+
* Registered policies.
|
|
136
|
+
*/
|
|
137
|
+
policies = /* @__PURE__ */ new Map();
|
|
138
|
+
/**
|
|
139
|
+
* Policy instances cache.
|
|
140
|
+
*/
|
|
141
|
+
policyInstances = /* @__PURE__ */ new Map();
|
|
142
|
+
/**
|
|
143
|
+
* Before callbacks.
|
|
144
|
+
*/
|
|
145
|
+
beforeCallbacks = [];
|
|
146
|
+
/**
|
|
147
|
+
* After callbacks.
|
|
148
|
+
*/
|
|
149
|
+
afterCallbacks = [];
|
|
150
|
+
/**
|
|
151
|
+
* User resolver function.
|
|
152
|
+
*/
|
|
153
|
+
userResolver;
|
|
154
|
+
/**
|
|
155
|
+
* Current user for checks.
|
|
156
|
+
*/
|
|
157
|
+
currentUser = null;
|
|
158
|
+
constructor(options = {}) {
|
|
159
|
+
this.userResolver = options.userResolver;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Define a new gate.
|
|
163
|
+
*/
|
|
164
|
+
define(ability, callback) {
|
|
165
|
+
this.gates.set(ability, { callback });
|
|
166
|
+
return this;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Register a policy for a model class.
|
|
170
|
+
*/
|
|
171
|
+
policy(modelClass, policyClass) {
|
|
172
|
+
this.policies.set(modelClass, policyClass);
|
|
173
|
+
return this;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Register a before callback.
|
|
177
|
+
* Return true to allow, false to deny, undefined to continue.
|
|
178
|
+
*/
|
|
179
|
+
before(callback) {
|
|
180
|
+
this.beforeCallbacks.push(callback);
|
|
181
|
+
return this;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Register an after callback.
|
|
185
|
+
*/
|
|
186
|
+
after(callback) {
|
|
187
|
+
this.afterCallbacks.push(callback);
|
|
188
|
+
return this;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Set the current user for authorization checks.
|
|
192
|
+
*/
|
|
193
|
+
forUser(user) {
|
|
194
|
+
const clone = Object.assign(Object.create(Object.getPrototypeOf(this)), this);
|
|
195
|
+
clone.currentUser = user;
|
|
196
|
+
return clone;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Resolve the user from context.
|
|
200
|
+
*/
|
|
201
|
+
async resolveUser(ctx) {
|
|
202
|
+
if (this.currentUser !== null) return this.currentUser;
|
|
203
|
+
if (this.userResolver) return this.userResolver(ctx);
|
|
204
|
+
return ctx.get("user") ?? null;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Check if the user has the given ability.
|
|
208
|
+
*/
|
|
209
|
+
async allows(ability, ...args) {
|
|
210
|
+
return this.check(ability, this.currentUser, ...args);
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Check if the user doesn't have the given ability.
|
|
214
|
+
*/
|
|
215
|
+
async denies(ability, ...args) {
|
|
216
|
+
return !await this.allows(ability, ...args);
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Check if the user has any of the given abilities.
|
|
220
|
+
*/
|
|
221
|
+
async any(abilities, ...args) {
|
|
222
|
+
for (const ability of abilities) if (await this.allows(ability, ...args)) return true;
|
|
223
|
+
return false;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Check if the user has all of the given abilities.
|
|
227
|
+
*/
|
|
228
|
+
async all(abilities, ...args) {
|
|
229
|
+
for (const ability of abilities) if (!await this.allows(ability, ...args)) return false;
|
|
230
|
+
return true;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Check if the user has none of the given abilities.
|
|
234
|
+
*/
|
|
235
|
+
async none(abilities, ...args) {
|
|
236
|
+
return !await this.any(abilities, ...args);
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Authorize the ability or throw an exception.
|
|
240
|
+
*/
|
|
241
|
+
async authorize(ability, ...args) {
|
|
242
|
+
const response = await this.checkResponse(ability, this.currentUser, ...args);
|
|
243
|
+
if (!response.allowed) throw denialToException(response);
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Get the authorization response.
|
|
247
|
+
*/
|
|
248
|
+
async inspect(ability, ...args) {
|
|
249
|
+
return this.checkResponse(ability, this.currentUser, ...args);
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Check the ability with a specific user.
|
|
253
|
+
*/
|
|
254
|
+
async check(ability, user, ...args) {
|
|
255
|
+
return (await this.checkResponse(ability, user, ...args)).allowed;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Check the ability with a specific user and keep the full response.
|
|
259
|
+
*
|
|
260
|
+
* Policies may answer with a boolean or with a `Response` object. Both
|
|
261
|
+
* shapes are normalized here so no caller has to truthy-test a
|
|
262
|
+
* `{ allowed: false }` object.
|
|
263
|
+
*/
|
|
264
|
+
async checkResponse(ability, user, ...args) {
|
|
265
|
+
for (const beforeCallback of this.beforeCallbacks) {
|
|
266
|
+
const result = await beforeCallback(user, ability, ...args);
|
|
267
|
+
if (result !== void 0) return this.settle(user, ability, toAuthorizationResponse(result), args);
|
|
268
|
+
}
|
|
269
|
+
const model = args[0];
|
|
270
|
+
if (model !== void 0 && model !== null) {
|
|
271
|
+
const policyResult = await this.checkPolicy(ability, user, model, args.slice(1));
|
|
272
|
+
if (policyResult !== void 0) return this.settle(user, ability, toAuthorizationResponse(policyResult), args);
|
|
273
|
+
}
|
|
274
|
+
const gate = this.gates.get(ability);
|
|
275
|
+
if (gate) {
|
|
276
|
+
const result = await gate.callback(user, ...args);
|
|
277
|
+
return this.settle(user, ability, toAuthorizationResponse(result), args);
|
|
278
|
+
}
|
|
279
|
+
return this.settle(user, ability, Response.deny(), args);
|
|
280
|
+
}
|
|
281
|
+
async settle(user, ability, response, args) {
|
|
282
|
+
await this.runAfterCallbacks(user, ability, response.allowed, args);
|
|
283
|
+
return response;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Check a policy for the given ability.
|
|
287
|
+
*
|
|
288
|
+
* The subject may be a class instance (policy resolved via its constructor)
|
|
289
|
+
* or a `[ModelClass, record]` / `['key', record]` tuple. The tuple form is
|
|
290
|
+
* required for plain records returned by the ORM, which carry no constructor
|
|
291
|
+
* information.
|
|
292
|
+
*/
|
|
293
|
+
async checkPolicy(ability, user, model, additionalArgs) {
|
|
294
|
+
let policyKey;
|
|
295
|
+
let subject = model;
|
|
296
|
+
let hasSubject = true;
|
|
297
|
+
if (Array.isArray(model) && model.length === 2 && (typeof model[0] === "function" || typeof model[0] === "string")) {
|
|
298
|
+
policyKey = model[0];
|
|
299
|
+
subject = model[1];
|
|
300
|
+
} else if (typeof model === "function") {
|
|
301
|
+
policyKey = model;
|
|
302
|
+
hasSubject = false;
|
|
303
|
+
} else policyKey = model?.constructor;
|
|
304
|
+
if (!policyKey) return;
|
|
305
|
+
const policyClass = this.policies.get(policyKey);
|
|
306
|
+
if (!policyClass) return;
|
|
307
|
+
const policy = this.getPolicyInstance(policyClass);
|
|
308
|
+
if (policy.before) {
|
|
309
|
+
const beforeResult = await policy.before(user, ability);
|
|
310
|
+
if (beforeResult !== void 0) return beforeResult;
|
|
311
|
+
}
|
|
312
|
+
const method = policy[ability];
|
|
313
|
+
if (typeof method === "function") return hasSubject ? method.call(policy, user, subject, ...additionalArgs) : method.call(policy, user, ...additionalArgs);
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Get or create a policy instance.
|
|
317
|
+
*/
|
|
318
|
+
getPolicyInstance(policyClass) {
|
|
319
|
+
let instance = this.policyInstances.get(policyClass);
|
|
320
|
+
if (!instance) {
|
|
321
|
+
instance = new policyClass();
|
|
322
|
+
this.policyInstances.set(policyClass, instance);
|
|
323
|
+
}
|
|
324
|
+
return instance;
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* Run after callbacks.
|
|
328
|
+
*/
|
|
329
|
+
async runAfterCallbacks(user, ability, result, args) {
|
|
330
|
+
for (const afterCallback of this.afterCallbacks) await afterCallback(user, ability, result, args);
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* Get all defined abilities.
|
|
334
|
+
*/
|
|
335
|
+
abilities() {
|
|
336
|
+
return Array.from(this.gates.keys());
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Check if an ability is defined.
|
|
340
|
+
*/
|
|
341
|
+
has(ability) {
|
|
342
|
+
return this.gates.has(ability);
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Get the policy for a model.
|
|
346
|
+
*/
|
|
347
|
+
getPolicyFor(model) {
|
|
348
|
+
const modelConstructor = model?.constructor;
|
|
349
|
+
if (!modelConstructor) return;
|
|
350
|
+
const policyClass = this.policies.get(modelConstructor);
|
|
351
|
+
if (!policyClass) return;
|
|
352
|
+
return this.getPolicyInstance(policyClass);
|
|
353
|
+
}
|
|
354
|
+
};
|
|
355
|
+
let globalGate = null;
|
|
356
|
+
/**
|
|
357
|
+
* Create a new gate instance.
|
|
358
|
+
*/
|
|
359
|
+
function createGate(options) {
|
|
360
|
+
return new Gate(options);
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Set the global gate instance.
|
|
364
|
+
*/
|
|
365
|
+
function setGate(gate) {
|
|
366
|
+
globalGate = gate;
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* Get the global gate instance.
|
|
370
|
+
*/
|
|
371
|
+
function getGate() {
|
|
372
|
+
if (!globalGate) throw new Error("Gate not initialized. Call setGate() first.");
|
|
373
|
+
return globalGate;
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* Define a gate on the global instance.
|
|
377
|
+
*/
|
|
378
|
+
function defineGate(ability, callback) {
|
|
379
|
+
getGate().define(ability, callback);
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* Check if the current user can perform an ability.
|
|
383
|
+
*/
|
|
384
|
+
async function can(ability, ...args) {
|
|
385
|
+
return getGate().allows(ability, ...args);
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* Check if the current user cannot perform an ability.
|
|
389
|
+
*/
|
|
390
|
+
async function cannot(ability, ...args) {
|
|
391
|
+
return getGate().denies(ability, ...args);
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* Authorize or throw.
|
|
395
|
+
*/
|
|
396
|
+
async function authorize(ability, ...args) {
|
|
397
|
+
return getGate().authorize(ability, ...args);
|
|
398
|
+
}
|
|
399
|
+
//#endregion
|
|
400
|
+
//#region src/authorization/Policy.ts
|
|
401
|
+
/**
|
|
402
|
+
* Base policy class for model authorization.
|
|
403
|
+
*
|
|
404
|
+
* @example
|
|
405
|
+
* ```typescript
|
|
406
|
+
* class PostPolicy extends Policy {
|
|
407
|
+
* // Runs before all checks - return true/false to short-circuit
|
|
408
|
+
* before(user: AuthUser | null, ability: string) {
|
|
409
|
+
* if (user?.role === 'admin') {
|
|
410
|
+
* return true // Admins can do anything
|
|
411
|
+
* }
|
|
412
|
+
* return undefined // Continue to specific check
|
|
413
|
+
* }
|
|
414
|
+
*
|
|
415
|
+
* viewAny(user: AuthUser | null) {
|
|
416
|
+
* return true // Anyone can view posts list
|
|
417
|
+
* }
|
|
418
|
+
*
|
|
419
|
+
* view(user: AuthUser | null, post: Post) {
|
|
420
|
+
* return post.published || user?.id === post.authorId
|
|
421
|
+
* }
|
|
422
|
+
*
|
|
423
|
+
* create(user: AuthUser | null) {
|
|
424
|
+
* return user !== null
|
|
425
|
+
* }
|
|
426
|
+
*
|
|
427
|
+
* update(user: AuthUser | null, post: Post) {
|
|
428
|
+
* return user?.id === post.authorId
|
|
429
|
+
* }
|
|
430
|
+
*
|
|
431
|
+
* delete(user: AuthUser | null, post: Post) {
|
|
432
|
+
* return user?.id === post.authorId
|
|
433
|
+
* }
|
|
434
|
+
* }
|
|
435
|
+
* ```
|
|
436
|
+
*/
|
|
437
|
+
var Policy = class {
|
|
438
|
+
/**
|
|
439
|
+
* Allow the action.
|
|
440
|
+
*/
|
|
441
|
+
allow(message) {
|
|
442
|
+
return Response.allow(message);
|
|
443
|
+
}
|
|
444
|
+
/**
|
|
445
|
+
* Deny the action.
|
|
446
|
+
*/
|
|
447
|
+
deny(message, code) {
|
|
448
|
+
return Response.deny(message, code);
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* Deny with a specific HTTP status.
|
|
452
|
+
*/
|
|
453
|
+
denyWithStatus(status, message) {
|
|
454
|
+
return Response.denyWithStatus(status, message);
|
|
455
|
+
}
|
|
456
|
+
/**
|
|
457
|
+
* Deny as not found (404).
|
|
458
|
+
*/
|
|
459
|
+
denyAsNotFound(message) {
|
|
460
|
+
return Response.denyAsNotFound(message);
|
|
461
|
+
}
|
|
462
|
+
};
|
|
463
|
+
/**
|
|
464
|
+
* Create a simple policy from an object.
|
|
465
|
+
*/
|
|
466
|
+
function definePolicy(definition) {
|
|
467
|
+
return class extends Policy {
|
|
468
|
+
before = definition.before;
|
|
469
|
+
viewAny(user) {
|
|
470
|
+
return definition.viewAny?.(user) ?? false;
|
|
471
|
+
}
|
|
472
|
+
view(user, model) {
|
|
473
|
+
return definition.view?.(user, model) ?? false;
|
|
474
|
+
}
|
|
475
|
+
create(user) {
|
|
476
|
+
return definition.create?.(user) ?? false;
|
|
477
|
+
}
|
|
478
|
+
update(user, model) {
|
|
479
|
+
return definition.update?.(user, model) ?? false;
|
|
480
|
+
}
|
|
481
|
+
delete(user, model) {
|
|
482
|
+
return definition.delete?.(user, model) ?? false;
|
|
483
|
+
}
|
|
484
|
+
restore(user, model) {
|
|
485
|
+
return definition.restore?.(user, model) ?? false;
|
|
486
|
+
}
|
|
487
|
+
forceDelete(user, model) {
|
|
488
|
+
return definition.forceDelete?.(user, model) ?? false;
|
|
489
|
+
}
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
//#endregion
|
|
493
|
+
//#region src/authorization/middleware.ts
|
|
494
|
+
/**
|
|
495
|
+
* Middleware to authorize an ability.
|
|
496
|
+
*
|
|
497
|
+
* @example
|
|
498
|
+
* ```typescript
|
|
499
|
+
* // Authorize a simple ability
|
|
500
|
+
* app.get('/admin', authorizeMiddleware('admin'), adminHandler)
|
|
501
|
+
*
|
|
502
|
+
* // Authorize with model (from route param)
|
|
503
|
+
* app.put('/posts/:id', authorizeMiddleware('update', getPost), updateHandler)
|
|
504
|
+
*
|
|
505
|
+
* // Multiple abilities (any)
|
|
506
|
+
* app.get('/dashboard', authorizeMiddleware(['admin', 'moderator']), dashboardHandler)
|
|
507
|
+
* ```
|
|
508
|
+
*/
|
|
509
|
+
function authorizeMiddleware(ability, modelResolver, options = {}) {
|
|
510
|
+
return async (ctx, next) => {
|
|
511
|
+
const gate = getGate();
|
|
512
|
+
const user = await gate.resolveUser(ctx);
|
|
513
|
+
const gateForUser = gate.forUser(user);
|
|
514
|
+
const abilities = Array.isArray(ability) ? ability : [ability];
|
|
515
|
+
const model = modelResolver ? await modelResolver(ctx) : void 0;
|
|
516
|
+
if (Array.isArray(ability)) {
|
|
517
|
+
if (!await gateForUser.any(abilities, model)) throw new AuthorizationException(options.message ?? "This action is unauthorized.");
|
|
518
|
+
} else {
|
|
519
|
+
const response = await gateForUser.checkResponse(ability, user, model);
|
|
520
|
+
if (!response.allowed) throw denialToException(options.message ? {
|
|
521
|
+
...response,
|
|
522
|
+
message: options.message
|
|
523
|
+
} : response);
|
|
524
|
+
}
|
|
525
|
+
await next();
|
|
526
|
+
};
|
|
527
|
+
}
|
|
528
|
+
/**
|
|
529
|
+
* Middleware to authorize all given abilities.
|
|
530
|
+
*/
|
|
531
|
+
function authorizeAllMiddleware(abilities, modelResolver, options = {}) {
|
|
532
|
+
return async (ctx, next) => {
|
|
533
|
+
const gate = getGate();
|
|
534
|
+
const user = await gate.resolveUser(ctx);
|
|
535
|
+
const gateForUser = gate.forUser(user);
|
|
536
|
+
const model = modelResolver ? await modelResolver(ctx) : void 0;
|
|
537
|
+
if (!await gateForUser.all(abilities, model)) throw new AuthorizationException(options.message ?? "This action is unauthorized.");
|
|
538
|
+
await next();
|
|
539
|
+
};
|
|
540
|
+
}
|
|
541
|
+
const RESOURCE_ABILITY_BY_METHOD = {
|
|
542
|
+
GET: "view",
|
|
543
|
+
HEAD: "view",
|
|
544
|
+
QUERY: "view",
|
|
545
|
+
POST: "create",
|
|
546
|
+
PUT: "update",
|
|
547
|
+
PATCH: "update",
|
|
548
|
+
DELETE: "delete"
|
|
549
|
+
};
|
|
550
|
+
/**
|
|
551
|
+
* Middleware factory for resource authorization.
|
|
552
|
+
* Automatically maps HTTP methods to policy abilities.
|
|
553
|
+
*
|
|
554
|
+
* Methods outside the built-in mapping (custom verbs registered via
|
|
555
|
+
* `router.on()`) are denied with a 403 rather than downgraded to a `view`
|
|
556
|
+
* check — the middleware cannot know whether an unknown verb mutates, so
|
|
557
|
+
* guessing would let a view-only user reach a mutating handler. Map custom
|
|
558
|
+
* verbs explicitly with `options.abilityFor`.
|
|
559
|
+
*
|
|
560
|
+
* @example
|
|
561
|
+
* ```typescript
|
|
562
|
+
* // Authorize resource actions based on HTTP method
|
|
563
|
+
* app.use('/posts/:id', authorizeResourceMiddleware(getPost))
|
|
564
|
+
* // GET/HEAD/QUERY -> view, POST -> create, PUT/PATCH -> update, DELETE -> delete
|
|
565
|
+
*
|
|
566
|
+
* // Custom verbs must be mapped explicitly, or they are denied
|
|
567
|
+
* authorizeResourceMiddleware(getPost, {
|
|
568
|
+
* abilityFor: (method) => (method === 'PURGE' ? 'delete' : undefined),
|
|
569
|
+
* })
|
|
570
|
+
* ```
|
|
571
|
+
*/
|
|
572
|
+
function authorizeResourceMiddleware(modelResolver, options = {}) {
|
|
573
|
+
return async (ctx, next) => {
|
|
574
|
+
const method = ctx.req.method.toUpperCase();
|
|
575
|
+
const ability = options.abilityFor?.(method) ?? RESOURCE_ABILITY_BY_METHOD[method];
|
|
576
|
+
if (ability === void 0) throw new AuthorizationException(options.message ?? "This action is unauthorized.");
|
|
577
|
+
const gate = getGate();
|
|
578
|
+
const user = await gate.resolveUser(ctx);
|
|
579
|
+
const gateForUser = gate.forUser(user);
|
|
580
|
+
const model = await modelResolver(ctx);
|
|
581
|
+
const response = await gateForUser.checkResponse(ability, user, model);
|
|
582
|
+
if (!response.allowed) throw denialToException(options.message ? {
|
|
583
|
+
...response,
|
|
584
|
+
message: options.message
|
|
585
|
+
} : response);
|
|
586
|
+
await next();
|
|
587
|
+
};
|
|
588
|
+
}
|
|
589
|
+
/**
|
|
590
|
+
* Create a gate-aware context with authorization helpers.
|
|
591
|
+
*/
|
|
592
|
+
function withAuthorization(gate) {
|
|
593
|
+
return async (ctx, next) => {
|
|
594
|
+
const user = await gate.resolveUser(ctx);
|
|
595
|
+
const gateForUser = gate.forUser(user);
|
|
596
|
+
ctx.set("gate", gateForUser);
|
|
597
|
+
ctx.set("can", async (ability, ...args) => {
|
|
598
|
+
return gateForUser.allows(ability, ...args);
|
|
599
|
+
});
|
|
600
|
+
ctx.set("cannot", async (ability, ...args) => {
|
|
601
|
+
return gateForUser.denies(ability, ...args);
|
|
602
|
+
});
|
|
603
|
+
ctx.set("authorize", async (ability, ...args) => {
|
|
604
|
+
return gateForUser.authorize(ability, ...args);
|
|
605
|
+
});
|
|
606
|
+
await next();
|
|
607
|
+
};
|
|
608
|
+
}
|
|
609
|
+
//#endregion
|
|
610
|
+
export { AuthorizationException as _, Policy as a, Response as c, cannot as d, createGate as f, setGate as g, isAuthorizationResponse as h, withAuthorization as i, authorize as l, getGate as m, authorizeMiddleware as n, definePolicy as o, defineGate as p, authorizeResourceMiddleware as r, Gate as s, authorizeAllMiddleware as t, can as u };
|
|
@@ -1,26 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
Channel,
|
|
4
|
-
MemoryDriver,
|
|
5
|
-
PresenceChannel,
|
|
6
|
-
PrivateChannel,
|
|
7
|
-
RedisDriver,
|
|
8
|
-
createBroadcastManager,
|
|
9
|
-
createTypedBroadcaster,
|
|
10
|
-
getBroadcastManager,
|
|
11
|
-
setBroadcastManager
|
|
12
|
-
} from "../chunk-DRNPVBAA.js";
|
|
13
|
-
import "../chunk-B5E5VBDI.js";
|
|
14
|
-
import "../chunk-V4DNC2ER.js";
|
|
15
|
-
export {
|
|
16
|
-
BroadcastManager,
|
|
17
|
-
Channel,
|
|
18
|
-
MemoryDriver,
|
|
19
|
-
PresenceChannel,
|
|
20
|
-
PrivateChannel,
|
|
21
|
-
RedisDriver,
|
|
22
|
-
createBroadcastManager,
|
|
23
|
-
createTypedBroadcaster,
|
|
24
|
-
getBroadcastManager,
|
|
25
|
-
setBroadcastManager
|
|
26
|
-
};
|
|
1
|
+
import { a as setBroadcastManager, c as PresenceChannel, i as getBroadcastManager, l as PrivateChannel, n as BroadcastManager, o as RedisDriver, r as createBroadcastManager, s as MemoryDriver, t as createTypedBroadcaster, u as Channel } from "../broadcasting-Dw-FhyjN.js";
|
|
2
|
+
export { BroadcastManager, Channel, MemoryDriver, PresenceChannel, PrivateChannel, RedisDriver, createBroadcastManager, createTypedBroadcaster, getBroadcastManager, setBroadcastManager };
|