@guren/server 2.3.0 → 2.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.
- package/dist/auth/types.d.ts +1 -1
- package/dist/authorization/index.d.ts +1 -1
- package/dist/authorization/index.js +1 -1
- package/dist/authorization/middleware.d.ts +14 -3
- package/dist/authorization/types.d.ts +12 -0
- package/dist/broadcasting/index.js +2 -1
- package/dist/{chunk-SIP34GBE.js → chunk-2SC566CD.js} +14 -18
- package/dist/{chunk-6IGN233M.js → chunk-62AB24ZU.js} +16 -4
- package/dist/{chunk-4VE3LYOM.js → chunk-DRNPVBAA.js} +20 -5
- package/dist/{chunk-LVJCZCAV.js → chunk-H3B23O6T.js} +10 -118
- package/dist/{chunk-7OB2YXRE.js → chunk-T2FFUU6J.js} +12 -18
- package/dist/chunk-V4DNC2ER.js +114 -0
- package/dist/encryption/index.js +17 -15
- package/dist/health/index.js +1 -1
- package/dist/http/Application.d.ts +163 -5
- package/dist/http/FormRequest.d.ts +10 -7
- package/dist/http/dev-banner.d.ts +11 -0
- package/dist/http/middleware/cors.d.ts +1 -1
- package/dist/http/middleware/validation-errors-cookie.d.ts +31 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +484 -122
- package/dist/mvc/Router.d.ts +16 -1
- package/dist/redis/index.js +6 -4
- package/dist/runtime/index.js +2 -2
- package/dist/support/inertia-defaults.d.ts +13 -0
- package/package.json +2 -2
package/dist/auth/types.d.ts
CHANGED
|
@@ -47,7 +47,7 @@ export type DefaultSanitizedKeys = 'password' | 'passwordHash' | 'password_hash'
|
|
|
47
47
|
* conventional credential keys plus any extra keys passed as `Hidden`.
|
|
48
48
|
*
|
|
49
49
|
* The runtime strips exactly the *configured* password/remember-token
|
|
50
|
-
* columns plus the model's `
|
|
50
|
+
* columns plus the model's `hidden` fields — a static type cannot see
|
|
51
51
|
* that configuration. If your columns use other names, or the model
|
|
52
52
|
* hides additional fields, list them in `Hidden` explicitly. Only
|
|
53
53
|
* meaningful for concrete record types (index-signature records like
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type { AuthUser, GateCallback, GateDefinition, PolicyMethod, PolicyResult, Policy as PolicyInterface, PolicyClass, AuthorizationResponse, GateOptions, AuthorizeOptions, PolicyRegistration, ResourceAction, ResponseBuilder, } from './types.js';
|
|
1
|
+
export type { AuthUser, GateCallback, GateDefinition, PolicyMethod, PolicyResult, Policy as PolicyInterface, PolicyClass, AuthorizationResponse, GateOptions, AuthorizeOptions, AuthorizeResourceOptions, PolicyRegistration, ResourceAction, ResponseBuilder, } from './types.js';
|
|
2
2
|
export { Gate, Response, isAuthorizationResponse, createGate, setGate, getGate, defineGate, can, cannot, authorize, } from './Gate.js';
|
|
3
3
|
export { Policy, definePolicy } from './Policy.js';
|
|
4
4
|
export { authorizeMiddleware, authorizeAllMiddleware, authorizeResourceMiddleware, withAuthorization, } from './middleware.js';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Context } from '../http/Application.js';
|
|
2
2
|
import type { Middleware } from '../http/middleware/index.js';
|
|
3
|
-
import type { AuthorizeOptions } from './types.js';
|
|
3
|
+
import type { AuthorizeOptions, AuthorizeResourceOptions } from './types.js';
|
|
4
4
|
import { Gate } from './Gate.js';
|
|
5
5
|
/**
|
|
6
6
|
* Middleware to authorize an ability.
|
|
@@ -26,14 +26,25 @@ export declare function authorizeAllMiddleware(abilities: string[], modelResolve
|
|
|
26
26
|
* Middleware factory for resource authorization.
|
|
27
27
|
* Automatically maps HTTP methods to policy abilities.
|
|
28
28
|
*
|
|
29
|
+
* Methods outside the built-in mapping (custom verbs registered via
|
|
30
|
+
* `router.on()`) are denied with a 403 rather than downgraded to a `view`
|
|
31
|
+
* check — the middleware cannot know whether an unknown verb mutates, so
|
|
32
|
+
* guessing would let a view-only user reach a mutating handler. Map custom
|
|
33
|
+
* verbs explicitly with `options.abilityFor`.
|
|
34
|
+
*
|
|
29
35
|
* @example
|
|
30
36
|
* ```typescript
|
|
31
37
|
* // Authorize resource actions based on HTTP method
|
|
32
38
|
* app.use('/posts/:id', authorizeResourceMiddleware(getPost))
|
|
33
|
-
* // GET -> view, POST -> create, PUT/PATCH -> update, DELETE -> delete
|
|
39
|
+
* // GET/HEAD/QUERY -> view, POST -> create, PUT/PATCH -> update, DELETE -> delete
|
|
40
|
+
*
|
|
41
|
+
* // Custom verbs must be mapped explicitly, or they are denied
|
|
42
|
+
* authorizeResourceMiddleware(getPost, {
|
|
43
|
+
* abilityFor: (method) => (method === 'PURGE' ? 'delete' : undefined),
|
|
44
|
+
* })
|
|
34
45
|
* ```
|
|
35
46
|
*/
|
|
36
|
-
export declare function authorizeResourceMiddleware(modelResolver: (ctx: Context) => unknown | Promise<unknown>, options?:
|
|
47
|
+
export declare function authorizeResourceMiddleware(modelResolver: (ctx: Context) => unknown | Promise<unknown>, options?: AuthorizeResourceOptions): Middleware;
|
|
37
48
|
/**
|
|
38
49
|
* Create a gate-aware context with authorization helpers.
|
|
39
50
|
*/
|
|
@@ -82,6 +82,18 @@ export interface AuthorizeOptions {
|
|
|
82
82
|
*/
|
|
83
83
|
status?: number;
|
|
84
84
|
}
|
|
85
|
+
/**
|
|
86
|
+
* Options for `authorizeResourceMiddleware`.
|
|
87
|
+
*/
|
|
88
|
+
export interface AuthorizeResourceOptions extends AuthorizeOptions {
|
|
89
|
+
/**
|
|
90
|
+
* Map an HTTP method (always uppercased) to a policy ability, overriding
|
|
91
|
+
* or extending the built-in mapping. Return `undefined` to fall back to
|
|
92
|
+
* the default mapping; a method with no ability from either source is
|
|
93
|
+
* denied with a 403.
|
|
94
|
+
*/
|
|
95
|
+
abilityFor?: (method: string) => string | undefined;
|
|
96
|
+
}
|
|
85
97
|
/**
|
|
86
98
|
* Policy registration.
|
|
87
99
|
*/
|
|
@@ -9,8 +9,9 @@ import {
|
|
|
9
9
|
createTypedBroadcaster,
|
|
10
10
|
getBroadcastManager,
|
|
11
11
|
setBroadcastManager
|
|
12
|
-
} from "../chunk-
|
|
12
|
+
} from "../chunk-DRNPVBAA.js";
|
|
13
13
|
import "../chunk-B5E5VBDI.js";
|
|
14
|
+
import "../chunk-V4DNC2ER.js";
|
|
14
15
|
export {
|
|
15
16
|
BroadcastManager,
|
|
16
17
|
Channel,
|
|
@@ -105,26 +105,22 @@ var HealthManager = class {
|
|
|
105
105
|
return async (ctx, next) => {
|
|
106
106
|
const report = options.checks ? await this.checkOnly(options.checks) : await this.check();
|
|
107
107
|
const statusCode = report.status === "healthy" ? 200 : report.status === "degraded" ? 200 : 503;
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
ctx.json({
|
|
111
|
-
status: report.status,
|
|
112
|
-
timestamp: report.timestamp.toISOString()
|
|
113
|
-
});
|
|
114
|
-
} else {
|
|
115
|
-
ctx.status(statusCode);
|
|
116
|
-
ctx.json({
|
|
108
|
+
ctx.res = ctx.json(
|
|
109
|
+
{
|
|
117
110
|
status: report.status,
|
|
118
111
|
timestamp: report.timestamp.toISOString(),
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
112
|
+
...options.detailed === false ? {} : {
|
|
113
|
+
checks: report.checks.map((check) => ({
|
|
114
|
+
name: check.name,
|
|
115
|
+
status: check.status,
|
|
116
|
+
message: check.message,
|
|
117
|
+
duration: check.duration,
|
|
118
|
+
meta: check.meta
|
|
119
|
+
}))
|
|
120
|
+
}
|
|
121
|
+
},
|
|
122
|
+
statusCode
|
|
123
|
+
);
|
|
128
124
|
};
|
|
129
125
|
}
|
|
130
126
|
/**
|
|
@@ -6,7 +6,7 @@ import standard from "figlet/importable-fonts/Standard.js";
|
|
|
6
6
|
// package.json
|
|
7
7
|
var package_default = {
|
|
8
8
|
name: "@guren/server",
|
|
9
|
-
version: "2.
|
|
9
|
+
version: "2.5.0",
|
|
10
10
|
type: "module",
|
|
11
11
|
license: "MIT",
|
|
12
12
|
repository: {
|
|
@@ -119,7 +119,7 @@ var package_default = {
|
|
|
119
119
|
test: "bun test"
|
|
120
120
|
},
|
|
121
121
|
dependencies: {
|
|
122
|
-
"@guren/orm": "^2.
|
|
122
|
+
"@guren/orm": "^2.3.0",
|
|
123
123
|
"@modelcontextprotocol/sdk": "^1.30.0",
|
|
124
124
|
chalk: "^5.3.0",
|
|
125
125
|
consola: "^3.4.2",
|
|
@@ -176,14 +176,20 @@ function generateAsciiArt(text) {
|
|
|
176
176
|
}
|
|
177
177
|
var GUREN_ASCII_ART = chalk.redBright.bold(generateAsciiArt("GUREN"));
|
|
178
178
|
var GUREN_VERSION = package_default.version;
|
|
179
|
+
function isWildcardHost(hostname) {
|
|
180
|
+
return hostname === "0.0.0.0" || hostname === "::";
|
|
181
|
+
}
|
|
182
|
+
function formatHostPort(hostname, port) {
|
|
183
|
+
return `${hostname.includes(":") ? `[${hostname}]` : hostname}:${port}`;
|
|
184
|
+
}
|
|
179
185
|
function logDevServerBanner({
|
|
180
186
|
hostname,
|
|
181
187
|
port,
|
|
182
188
|
assetsUrl = "http://localhost:5173"
|
|
183
189
|
}) {
|
|
184
190
|
const localUrl = `http://localhost:${port}`;
|
|
185
|
-
const boundUrl = `http://${hostname
|
|
186
|
-
const boundLabel = hostname
|
|
191
|
+
const boundUrl = `http://${formatHostPort(hostname, port)}`;
|
|
192
|
+
const boundLabel = isWildcardHost(hostname) ? " (all interfaces)" : "";
|
|
187
193
|
const header = [
|
|
188
194
|
GUREN_ASCII_ART,
|
|
189
195
|
chalk.magentaBright.bold(
|
|
@@ -254,9 +260,15 @@ function parseImportMap(value, options = {}) {
|
|
|
254
260
|
}
|
|
255
261
|
}
|
|
256
262
|
|
|
263
|
+
// src/support/inertia-defaults.ts
|
|
264
|
+
var DEFAULT_DEV_STYLES_ENTRY = "/resources/css/app.css";
|
|
265
|
+
|
|
257
266
|
export {
|
|
258
267
|
parseImportMap,
|
|
268
|
+
DEFAULT_DEV_STYLES_ENTRY,
|
|
259
269
|
GUREN_ASCII_ART,
|
|
270
|
+
isWildcardHost,
|
|
271
|
+
formatHostPort,
|
|
260
272
|
logDevServerBanner,
|
|
261
273
|
startViteDevServer
|
|
262
274
|
};
|
|
@@ -2,6 +2,9 @@ import {
|
|
|
2
2
|
claimHotDisposable,
|
|
3
3
|
isHotReloadRuntime
|
|
4
4
|
} from "./chunk-B5E5VBDI.js";
|
|
5
|
+
import {
|
|
6
|
+
randomHex
|
|
7
|
+
} from "./chunk-V4DNC2ER.js";
|
|
5
8
|
|
|
6
9
|
// src/broadcasting/channels/Channel.ts
|
|
7
10
|
var Channel = class {
|
|
@@ -520,6 +523,14 @@ function formatValidationErrors(error, fallbackMessage = "The provided data is i
|
|
|
520
523
|
}
|
|
521
524
|
|
|
522
525
|
// src/broadcasting/BroadcastManager.ts
|
|
526
|
+
function resolveClientUserId(user) {
|
|
527
|
+
if (typeof user !== "object" || user === null) return void 0;
|
|
528
|
+
const candidate = user;
|
|
529
|
+
for (const value of [candidate.id, candidate.sub, candidate.userId]) {
|
|
530
|
+
if (typeof value === "string" || typeof value === "number") return value;
|
|
531
|
+
}
|
|
532
|
+
return void 0;
|
|
533
|
+
}
|
|
523
534
|
var BroadcastManager = class {
|
|
524
535
|
/**
|
|
525
536
|
* Default driver name.
|
|
@@ -689,6 +700,7 @@ var BroadcastManager = class {
|
|
|
689
700
|
authorizedChannels.push(channelName);
|
|
690
701
|
}
|
|
691
702
|
}
|
|
703
|
+
const clientUserId = resolveClientUserId(user);
|
|
692
704
|
let controller = null;
|
|
693
705
|
let client = null;
|
|
694
706
|
let pingTimer = null;
|
|
@@ -718,7 +730,9 @@ var BroadcastManager = class {
|
|
|
718
730
|
controller = streamController;
|
|
719
731
|
client = {
|
|
720
732
|
id: clientId,
|
|
721
|
-
|
|
733
|
+
// Recorded so `POST /broadcasting/auth` can refuse to attach
|
|
734
|
+
// channels to a stream belonging to someone else.
|
|
735
|
+
userId: clientUserId,
|
|
722
736
|
channels: /* @__PURE__ */ new Set(),
|
|
723
737
|
send: (event, data) => {
|
|
724
738
|
const message = `event: ${event}
|
|
@@ -774,13 +788,16 @@ data: ${JSON.stringify(data)}
|
|
|
774
788
|
}
|
|
775
789
|
const clientId = typeof payload.clientId === "string" ? payload.clientId : void 0;
|
|
776
790
|
const results = {};
|
|
791
|
+
const requesterId = resolveClientUserId(user);
|
|
792
|
+
const target = clientId ? this.sseClients.get(clientId) : void 0;
|
|
793
|
+
const attachTo = target && (target.userId === void 0 || target.userId === requesterId) ? target : void 0;
|
|
777
794
|
for (const ch of channels) {
|
|
778
795
|
const authResult = await this.authorize(ch, user);
|
|
779
796
|
if (authResult === false || authResult === null) {
|
|
780
797
|
results[ch] = { authorized: false };
|
|
781
798
|
continue;
|
|
782
799
|
}
|
|
783
|
-
const subscribed =
|
|
800
|
+
const subscribed = attachTo ? this.subscribeClient(attachTo.id, ch) : false;
|
|
784
801
|
if (authResult === true) {
|
|
785
802
|
results[ch] = { authorized: true, subscribed };
|
|
786
803
|
} else {
|
|
@@ -918,9 +935,7 @@ data: ${JSON.stringify(data)}
|
|
|
918
935
|
* Generate a unique client ID.
|
|
919
936
|
*/
|
|
920
937
|
generateClientId(prefix = "sse") {
|
|
921
|
-
|
|
922
|
-
const random = Math.random().toString(36).substring(2, 10);
|
|
923
|
-
return `${prefix}_${timestamp}${random}`;
|
|
938
|
+
return `${prefix}_${randomHex(16)}`;
|
|
924
939
|
}
|
|
925
940
|
};
|
|
926
941
|
var globalBroadcastManager = null;
|
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
|
|
7
7
|
// src/encryption/Encrypter.ts
|
|
8
8
|
import { createCipheriv, createDecipheriv, randomBytes, createHmac } from "crypto";
|
|
9
|
+
var GCM_TAG_BYTES = 16;
|
|
9
10
|
var Encrypter = class {
|
|
10
11
|
/**
|
|
11
12
|
* Encryption key.
|
|
@@ -79,7 +80,7 @@ var Encrypter = class {
|
|
|
79
80
|
*/
|
|
80
81
|
encryptGcm(data) {
|
|
81
82
|
const iv = randomBytes(12);
|
|
82
|
-
const cipher = createCipheriv("aes-256-gcm", this.key, iv);
|
|
83
|
+
const cipher = createCipheriv("aes-256-gcm", this.key, iv, { authTagLength: GCM_TAG_BYTES });
|
|
83
84
|
const encrypted = Buffer.concat([
|
|
84
85
|
cipher.update(data, "utf8"),
|
|
85
86
|
cipher.final()
|
|
@@ -96,11 +97,14 @@ var Encrypter = class {
|
|
|
96
97
|
* Decrypt using AES-256-GCM.
|
|
97
98
|
*/
|
|
98
99
|
decryptGcm(payload) {
|
|
100
|
+
const iv = Buffer.from(payload.iv, "base64");
|
|
101
|
+
const encrypted = Buffer.from(payload.value, "base64");
|
|
102
|
+
const tag = Buffer.from(payload.tag, "base64");
|
|
103
|
+
if (tag.length !== GCM_TAG_BYTES) {
|
|
104
|
+
throw new Error("Invalid authentication tag length.");
|
|
105
|
+
}
|
|
99
106
|
return this.tryDecryptWithKeys((key) => {
|
|
100
|
-
const
|
|
101
|
-
const encrypted = Buffer.from(payload.value, "base64");
|
|
102
|
-
const tag = Buffer.from(payload.tag, "base64");
|
|
103
|
-
const decipher = createDecipheriv("aes-256-gcm", key, iv);
|
|
107
|
+
const decipher = createDecipheriv("aes-256-gcm", key, iv, { authTagLength: GCM_TAG_BYTES });
|
|
104
108
|
decipher.setAuthTag(tag);
|
|
105
109
|
const decrypted = Buffer.concat([
|
|
106
110
|
decipher.update(encrypted),
|
|
@@ -254,104 +258,6 @@ function verifySignedUrl(value, keyring, options = {}) {
|
|
|
254
258
|
return payload?.url === canonical;
|
|
255
259
|
}
|
|
256
260
|
|
|
257
|
-
// src/encryption/Random.ts
|
|
258
|
-
import { randomBytes as randomBytes2, randomUUID, randomInt as cryptoRandomInt } from "crypto";
|
|
259
|
-
var CHARSETS = {
|
|
260
|
-
alphanumeric: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
|
|
261
|
-
alphabetic: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
|
262
|
-
numeric: "0123456789",
|
|
263
|
-
hex: "0123456789abcdef",
|
|
264
|
-
"base64": "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
|
|
265
|
-
"url-safe": "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
|
|
266
|
-
};
|
|
267
|
-
function unbiasedRandomChars(length, chars) {
|
|
268
|
-
if (!Number.isInteger(length) || length < 0) {
|
|
269
|
-
throw new RangeError(`Random string length must be a non-negative integer, received ${length}.`);
|
|
270
|
-
}
|
|
271
|
-
const limit = 256 - 256 % chars.length;
|
|
272
|
-
let result = "";
|
|
273
|
-
while (result.length < length) {
|
|
274
|
-
const remaining = length - result.length;
|
|
275
|
-
const bytes = randomBytes2(Math.ceil(remaining * 256 / limit) + 8);
|
|
276
|
-
for (const byte of bytes) {
|
|
277
|
-
if (byte < limit) {
|
|
278
|
-
result += chars[byte % chars.length];
|
|
279
|
-
if (result.length === length) break;
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
return result;
|
|
284
|
-
}
|
|
285
|
-
function randomString(length, options = {}) {
|
|
286
|
-
const { charset = "alphanumeric" } = options;
|
|
287
|
-
return unbiasedRandomChars(length, CHARSETS[charset]);
|
|
288
|
-
}
|
|
289
|
-
function random(length) {
|
|
290
|
-
return randomBytes2(length);
|
|
291
|
-
}
|
|
292
|
-
function randomHex(length) {
|
|
293
|
-
return randomBytes2(length).toString("hex");
|
|
294
|
-
}
|
|
295
|
-
function randomBase64(length) {
|
|
296
|
-
return randomBytes2(length).toString("base64");
|
|
297
|
-
}
|
|
298
|
-
function randomBase64Url(length) {
|
|
299
|
-
return randomBytes2(length).toString("base64").replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
|
300
|
-
}
|
|
301
|
-
function uuid() {
|
|
302
|
-
return randomUUID();
|
|
303
|
-
}
|
|
304
|
-
function randomInt(min, max) {
|
|
305
|
-
return cryptoRandomInt(min, max + 1);
|
|
306
|
-
}
|
|
307
|
-
function urlSafeToken(length = 32) {
|
|
308
|
-
return randomBase64Url(Math.ceil(length * 0.75)).slice(0, length);
|
|
309
|
-
}
|
|
310
|
-
function generatePassword(length = 16, options = {}) {
|
|
311
|
-
const {
|
|
312
|
-
uppercase = true,
|
|
313
|
-
lowercase = true,
|
|
314
|
-
numbers = true,
|
|
315
|
-
symbols = true
|
|
316
|
-
} = options;
|
|
317
|
-
let chars = "";
|
|
318
|
-
if (uppercase) chars += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
319
|
-
if (lowercase) chars += "abcdefghijklmnopqrstuvwxyz";
|
|
320
|
-
if (numbers) chars += "0123456789";
|
|
321
|
-
if (symbols) chars += "!@#$%^&*()-_=+[]{}|;:,.<>?";
|
|
322
|
-
if (chars.length === 0) {
|
|
323
|
-
chars = CHARSETS.alphanumeric;
|
|
324
|
-
}
|
|
325
|
-
return unbiasedRandomChars(length, chars);
|
|
326
|
-
}
|
|
327
|
-
function generateOtp(length = 6) {
|
|
328
|
-
return randomString(length, { charset: "numeric" });
|
|
329
|
-
}
|
|
330
|
-
function generateSlug(length = 10) {
|
|
331
|
-
return randomString(length, { charset: "hex" });
|
|
332
|
-
}
|
|
333
|
-
function shuffle(array) {
|
|
334
|
-
const result = [...array];
|
|
335
|
-
for (let i = result.length - 1; i > 0; i--) {
|
|
336
|
-
const j = randomInt(0, i);
|
|
337
|
-
[result[i], result[j]] = [result[j], result[i]];
|
|
338
|
-
}
|
|
339
|
-
return result;
|
|
340
|
-
}
|
|
341
|
-
function pick(array) {
|
|
342
|
-
if (array.length === 0) {
|
|
343
|
-
throw new Error("Cannot pick from empty array.");
|
|
344
|
-
}
|
|
345
|
-
return array[randomInt(0, array.length - 1)];
|
|
346
|
-
}
|
|
347
|
-
function sample(array, count) {
|
|
348
|
-
if (count > array.length) {
|
|
349
|
-
throw new Error("Sample count exceeds array length.");
|
|
350
|
-
}
|
|
351
|
-
const shuffled = shuffle(array);
|
|
352
|
-
return shuffled.slice(0, count);
|
|
353
|
-
}
|
|
354
|
-
|
|
355
261
|
export {
|
|
356
262
|
Encrypter,
|
|
357
263
|
generateKey,
|
|
@@ -361,19 +267,5 @@ export {
|
|
|
361
267
|
encrypt,
|
|
362
268
|
decrypt,
|
|
363
269
|
signUrl,
|
|
364
|
-
verifySignedUrl
|
|
365
|
-
randomString,
|
|
366
|
-
random,
|
|
367
|
-
randomHex,
|
|
368
|
-
randomBase64,
|
|
369
|
-
randomBase64Url,
|
|
370
|
-
uuid,
|
|
371
|
-
randomInt,
|
|
372
|
-
urlSafeToken,
|
|
373
|
-
generatePassword,
|
|
374
|
-
generateOtp,
|
|
375
|
-
generateSlug,
|
|
376
|
-
shuffle,
|
|
377
|
-
pick,
|
|
378
|
-
sample
|
|
270
|
+
verifySignedUrl
|
|
379
271
|
};
|
|
@@ -419,27 +419,21 @@ function authorizeAllMiddleware(abilities, modelResolver, options = {}) {
|
|
|
419
419
|
await next();
|
|
420
420
|
};
|
|
421
421
|
}
|
|
422
|
+
var RESOURCE_ABILITY_BY_METHOD = {
|
|
423
|
+
GET: "view",
|
|
424
|
+
HEAD: "view",
|
|
425
|
+
QUERY: "view",
|
|
426
|
+
POST: "create",
|
|
427
|
+
PUT: "update",
|
|
428
|
+
PATCH: "update",
|
|
429
|
+
DELETE: "delete"
|
|
430
|
+
};
|
|
422
431
|
function authorizeResourceMiddleware(modelResolver, options = {}) {
|
|
423
432
|
return async (ctx, next) => {
|
|
424
433
|
const method = ctx.req.method.toUpperCase();
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
case "HEAD":
|
|
429
|
-
ability = "view";
|
|
430
|
-
break;
|
|
431
|
-
case "POST":
|
|
432
|
-
ability = "create";
|
|
433
|
-
break;
|
|
434
|
-
case "PUT":
|
|
435
|
-
case "PATCH":
|
|
436
|
-
ability = "update";
|
|
437
|
-
break;
|
|
438
|
-
case "DELETE":
|
|
439
|
-
ability = "delete";
|
|
440
|
-
break;
|
|
441
|
-
default:
|
|
442
|
-
ability = "view";
|
|
434
|
+
const ability = options.abilityFor?.(method) ?? RESOURCE_ABILITY_BY_METHOD[method];
|
|
435
|
+
if (ability === void 0) {
|
|
436
|
+
throw new AuthorizationException(options.message ?? "This action is unauthorized.");
|
|
443
437
|
}
|
|
444
438
|
const gate = getGate();
|
|
445
439
|
const user = await gate.resolveUser(ctx);
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// src/encryption/Random.ts
|
|
2
|
+
import { randomBytes, randomUUID, randomInt as cryptoRandomInt } from "crypto";
|
|
3
|
+
var CHARSETS = {
|
|
4
|
+
alphanumeric: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
|
|
5
|
+
alphabetic: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
|
6
|
+
numeric: "0123456789",
|
|
7
|
+
hex: "0123456789abcdef",
|
|
8
|
+
"base64": "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
|
|
9
|
+
"url-safe": "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
|
|
10
|
+
};
|
|
11
|
+
function unbiasedRandomChars(length, chars) {
|
|
12
|
+
if (!Number.isInteger(length) || length < 0) {
|
|
13
|
+
throw new RangeError(`Random string length must be a non-negative integer, received ${length}.`);
|
|
14
|
+
}
|
|
15
|
+
const limit = 256 - 256 % chars.length;
|
|
16
|
+
let result = "";
|
|
17
|
+
while (result.length < length) {
|
|
18
|
+
const remaining = length - result.length;
|
|
19
|
+
const bytes = randomBytes(Math.ceil(remaining * 256 / limit) + 8);
|
|
20
|
+
for (const byte of bytes) {
|
|
21
|
+
if (byte < limit) {
|
|
22
|
+
result += chars[byte % chars.length];
|
|
23
|
+
if (result.length === length) break;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
function randomString(length, options = {}) {
|
|
30
|
+
const { charset = "alphanumeric" } = options;
|
|
31
|
+
return unbiasedRandomChars(length, CHARSETS[charset]);
|
|
32
|
+
}
|
|
33
|
+
function random(length) {
|
|
34
|
+
return randomBytes(length);
|
|
35
|
+
}
|
|
36
|
+
function randomHex(length) {
|
|
37
|
+
return randomBytes(length).toString("hex");
|
|
38
|
+
}
|
|
39
|
+
function randomBase64(length) {
|
|
40
|
+
return randomBytes(length).toString("base64");
|
|
41
|
+
}
|
|
42
|
+
function randomBase64Url(length) {
|
|
43
|
+
return randomBytes(length).toString("base64").replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
|
44
|
+
}
|
|
45
|
+
function uuid() {
|
|
46
|
+
return randomUUID();
|
|
47
|
+
}
|
|
48
|
+
function randomInt(min, max) {
|
|
49
|
+
return cryptoRandomInt(min, max + 1);
|
|
50
|
+
}
|
|
51
|
+
function urlSafeToken(length = 32) {
|
|
52
|
+
return randomBase64Url(Math.ceil(length * 0.75)).slice(0, length);
|
|
53
|
+
}
|
|
54
|
+
function generatePassword(length = 16, options = {}) {
|
|
55
|
+
const {
|
|
56
|
+
uppercase = true,
|
|
57
|
+
lowercase = true,
|
|
58
|
+
numbers = true,
|
|
59
|
+
symbols = true
|
|
60
|
+
} = options;
|
|
61
|
+
let chars = "";
|
|
62
|
+
if (uppercase) chars += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
63
|
+
if (lowercase) chars += "abcdefghijklmnopqrstuvwxyz";
|
|
64
|
+
if (numbers) chars += "0123456789";
|
|
65
|
+
if (symbols) chars += "!@#$%^&*()-_=+[]{}|;:,.<>?";
|
|
66
|
+
if (chars.length === 0) {
|
|
67
|
+
chars = CHARSETS.alphanumeric;
|
|
68
|
+
}
|
|
69
|
+
return unbiasedRandomChars(length, chars);
|
|
70
|
+
}
|
|
71
|
+
function generateOtp(length = 6) {
|
|
72
|
+
return randomString(length, { charset: "numeric" });
|
|
73
|
+
}
|
|
74
|
+
function generateSlug(length = 10) {
|
|
75
|
+
return randomString(length, { charset: "hex" });
|
|
76
|
+
}
|
|
77
|
+
function shuffle(array) {
|
|
78
|
+
const result = [...array];
|
|
79
|
+
for (let i = result.length - 1; i > 0; i--) {
|
|
80
|
+
const j = randomInt(0, i);
|
|
81
|
+
[result[i], result[j]] = [result[j], result[i]];
|
|
82
|
+
}
|
|
83
|
+
return result;
|
|
84
|
+
}
|
|
85
|
+
function pick(array) {
|
|
86
|
+
if (array.length === 0) {
|
|
87
|
+
throw new Error("Cannot pick from empty array.");
|
|
88
|
+
}
|
|
89
|
+
return array[randomInt(0, array.length - 1)];
|
|
90
|
+
}
|
|
91
|
+
function sample(array, count) {
|
|
92
|
+
if (count > array.length) {
|
|
93
|
+
throw new Error("Sample count exceeds array length.");
|
|
94
|
+
}
|
|
95
|
+
const shuffled = shuffle(array);
|
|
96
|
+
return shuffled.slice(0, count);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export {
|
|
100
|
+
randomString,
|
|
101
|
+
random,
|
|
102
|
+
randomHex,
|
|
103
|
+
randomBase64,
|
|
104
|
+
randomBase64Url,
|
|
105
|
+
uuid,
|
|
106
|
+
randomInt,
|
|
107
|
+
urlSafeToken,
|
|
108
|
+
generatePassword,
|
|
109
|
+
generateOtp,
|
|
110
|
+
generateSlug,
|
|
111
|
+
shuffle,
|
|
112
|
+
pick,
|
|
113
|
+
sample
|
|
114
|
+
};
|
package/dist/encryption/index.js
CHANGED
|
@@ -4,25 +4,11 @@ import {
|
|
|
4
4
|
decrypt,
|
|
5
5
|
encrypt,
|
|
6
6
|
generateKey,
|
|
7
|
-
generateOtp,
|
|
8
|
-
generatePassword,
|
|
9
|
-
generateSlug,
|
|
10
7
|
getEncrypter,
|
|
11
|
-
pick,
|
|
12
|
-
random,
|
|
13
|
-
randomBase64,
|
|
14
|
-
randomBase64Url,
|
|
15
|
-
randomHex,
|
|
16
|
-
randomInt,
|
|
17
|
-
randomString,
|
|
18
|
-
sample,
|
|
19
8
|
setEncrypter,
|
|
20
|
-
shuffle,
|
|
21
9
|
signUrl,
|
|
22
|
-
urlSafeToken,
|
|
23
|
-
uuid,
|
|
24
10
|
verifySignedUrl
|
|
25
|
-
} from "../chunk-
|
|
11
|
+
} from "../chunk-H3B23O6T.js";
|
|
26
12
|
import {
|
|
27
13
|
MessageSigner,
|
|
28
14
|
deriveAppKey,
|
|
@@ -47,6 +33,22 @@ import {
|
|
|
47
33
|
verifyHmac,
|
|
48
34
|
verifyPassword
|
|
49
35
|
} from "../chunk-QQKTH5KX.js";
|
|
36
|
+
import {
|
|
37
|
+
generateOtp,
|
|
38
|
+
generatePassword,
|
|
39
|
+
generateSlug,
|
|
40
|
+
pick,
|
|
41
|
+
random,
|
|
42
|
+
randomBase64,
|
|
43
|
+
randomBase64Url,
|
|
44
|
+
randomHex,
|
|
45
|
+
randomInt,
|
|
46
|
+
randomString,
|
|
47
|
+
sample,
|
|
48
|
+
shuffle,
|
|
49
|
+
urlSafeToken,
|
|
50
|
+
uuid
|
|
51
|
+
} from "../chunk-V4DNC2ER.js";
|
|
50
52
|
export {
|
|
51
53
|
Encrypter,
|
|
52
54
|
MessageSigner,
|