alepha 0.13.0 → 0.13.1
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/api-jobs/index.d.ts +26 -26
- package/dist/api-users/index.d.ts +1 -1
- package/dist/cli/{dist-Sz2EXvQX.cjs → dist-Dl9Vl7Ur.js} +17 -13
- package/dist/cli/{dist-BBPjuQ56.js.map → dist-Dl9Vl7Ur.js.map} +1 -1
- package/dist/cli/index.d.ts +3 -11
- package/dist/cli/index.js +106 -74
- package/dist/cli/index.js.map +1 -1
- package/dist/email/index.js +71 -73
- package/dist/email/index.js.map +1 -1
- package/dist/orm/index.d.ts +1 -1
- package/dist/orm/index.js.map +1 -1
- package/dist/queue/index.d.ts +4 -4
- package/dist/retry/index.d.ts +1 -1
- package/dist/retry/index.js +2 -2
- package/dist/retry/index.js.map +1 -1
- package/dist/scheduler/index.d.ts +6 -6
- package/dist/security/index.d.ts +28 -28
- package/dist/server/index.js +1 -1
- package/dist/server/index.js.map +1 -1
- package/dist/server-health/index.d.ts +17 -17
- package/dist/server-metrics/index.js +170 -174
- package/dist/server-metrics/index.js.map +1 -1
- package/dist/server-security/index.d.ts +9 -9
- package/dist/vite/index.js +4 -5
- package/dist/vite/index.js.map +1 -1
- package/dist/websocket/index.d.ts +7 -7
- package/package.json +52 -103
- package/src/cli/apps/AlephaPackageBuilderCli.ts +7 -2
- package/src/cli/assets/appRouterTs.ts +9 -0
- package/src/cli/assets/indexHtml.ts +2 -1
- package/src/cli/assets/mainBrowserTs.ts +10 -0
- package/src/cli/commands/CoreCommands.ts +6 -5
- package/src/cli/commands/DrizzleCommands.ts +65 -57
- package/src/cli/commands/VerifyCommands.ts +1 -1
- package/src/cli/services/ProjectUtils.ts +44 -38
- package/src/orm/providers/DrizzleKitProvider.ts +1 -1
- package/src/retry/descriptors/$retry.ts +5 -3
- package/src/server/providers/NodeHttpServerProvider.ts +1 -1
- package/src/vite/helpers/boot.ts +3 -3
- package/dist/api-files/index.cjs +0 -1293
- package/dist/api-files/index.cjs.map +0 -1
- package/dist/api-files/index.d.cts +0 -829
- package/dist/api-jobs/index.cjs +0 -274
- package/dist/api-jobs/index.cjs.map +0 -1
- package/dist/api-jobs/index.d.cts +0 -654
- package/dist/api-notifications/index.cjs +0 -380
- package/dist/api-notifications/index.cjs.map +0 -1
- package/dist/api-notifications/index.d.cts +0 -289
- package/dist/api-parameters/index.cjs +0 -66
- package/dist/api-parameters/index.cjs.map +0 -1
- package/dist/api-parameters/index.d.cts +0 -84
- package/dist/api-users/index.cjs +0 -6009
- package/dist/api-users/index.cjs.map +0 -1
- package/dist/api-users/index.d.cts +0 -4740
- package/dist/api-verifications/index.cjs +0 -407
- package/dist/api-verifications/index.cjs.map +0 -1
- package/dist/api-verifications/index.d.cts +0 -207
- package/dist/batch/index.cjs +0 -408
- package/dist/batch/index.cjs.map +0 -1
- package/dist/batch/index.d.cts +0 -330
- package/dist/bin/index.cjs +0 -17
- package/dist/bin/index.cjs.map +0 -1
- package/dist/bin/index.d.cts +0 -1
- package/dist/bucket/index.cjs +0 -303
- package/dist/bucket/index.cjs.map +0 -1
- package/dist/bucket/index.d.cts +0 -355
- package/dist/cache/index.cjs +0 -241
- package/dist/cache/index.cjs.map +0 -1
- package/dist/cache/index.d.cts +0 -202
- package/dist/cache-redis/index.cjs +0 -84
- package/dist/cache-redis/index.cjs.map +0 -1
- package/dist/cache-redis/index.d.cts +0 -40
- package/dist/cli/chunk-DSlc6foC.cjs +0 -43
- package/dist/cli/dist-BBPjuQ56.js +0 -2778
- package/dist/cli/dist-Sz2EXvQX.cjs.map +0 -1
- package/dist/cli/index.cjs +0 -1241
- package/dist/cli/index.cjs.map +0 -1
- package/dist/cli/index.d.cts +0 -422
- package/dist/command/index.cjs +0 -693
- package/dist/command/index.cjs.map +0 -1
- package/dist/command/index.d.cts +0 -340
- package/dist/core/index.cjs +0 -2264
- package/dist/core/index.cjs.map +0 -1
- package/dist/core/index.d.cts +0 -1927
- package/dist/datetime/index.cjs +0 -318
- package/dist/datetime/index.cjs.map +0 -1
- package/dist/datetime/index.d.cts +0 -145
- package/dist/email/index.cjs +0 -10874
- package/dist/email/index.cjs.map +0 -1
- package/dist/email/index.d.cts +0 -186
- package/dist/fake/index.cjs +0 -34641
- package/dist/fake/index.cjs.map +0 -1
- package/dist/fake/index.d.cts +0 -74
- package/dist/file/index.cjs +0 -1212
- package/dist/file/index.cjs.map +0 -1
- package/dist/file/index.d.cts +0 -698
- package/dist/lock/index.cjs +0 -226
- package/dist/lock/index.cjs.map +0 -1
- package/dist/lock/index.d.cts +0 -361
- package/dist/lock-redis/index.cjs +0 -113
- package/dist/lock-redis/index.cjs.map +0 -1
- package/dist/lock-redis/index.d.cts +0 -24
- package/dist/logger/index.cjs +0 -521
- package/dist/logger/index.cjs.map +0 -1
- package/dist/logger/index.d.cts +0 -281
- package/dist/orm/index.cjs +0 -2986
- package/dist/orm/index.cjs.map +0 -1
- package/dist/orm/index.d.cts +0 -2213
- package/dist/queue/index.cjs +0 -1044
- package/dist/queue/index.cjs.map +0 -1
- package/dist/queue/index.d.cts +0 -1265
- package/dist/queue-redis/index.cjs +0 -873
- package/dist/queue-redis/index.cjs.map +0 -1
- package/dist/queue-redis/index.d.cts +0 -82
- package/dist/redis/index.cjs +0 -153
- package/dist/redis/index.cjs.map +0 -1
- package/dist/redis/index.d.cts +0 -82
- package/dist/retry/index.cjs +0 -146
- package/dist/retry/index.cjs.map +0 -1
- package/dist/retry/index.d.cts +0 -172
- package/dist/router/index.cjs +0 -111
- package/dist/router/index.cjs.map +0 -1
- package/dist/router/index.d.cts +0 -46
- package/dist/scheduler/index.cjs +0 -576
- package/dist/scheduler/index.cjs.map +0 -1
- package/dist/scheduler/index.d.cts +0 -145
- package/dist/security/index.cjs +0 -2402
- package/dist/security/index.cjs.map +0 -1
- package/dist/security/index.d.cts +0 -598
- package/dist/server/index.cjs +0 -1680
- package/dist/server/index.cjs.map +0 -1
- package/dist/server/index.d.cts +0 -810
- package/dist/server-auth/index.cjs +0 -3146
- package/dist/server-auth/index.cjs.map +0 -1
- package/dist/server-auth/index.d.cts +0 -1164
- package/dist/server-cache/index.cjs +0 -252
- package/dist/server-cache/index.cjs.map +0 -1
- package/dist/server-cache/index.d.cts +0 -164
- package/dist/server-compress/index.cjs +0 -141
- package/dist/server-compress/index.cjs.map +0 -1
- package/dist/server-compress/index.d.cts +0 -38
- package/dist/server-cookies/index.cjs +0 -234
- package/dist/server-cookies/index.cjs.map +0 -1
- package/dist/server-cookies/index.d.cts +0 -144
- package/dist/server-cors/index.cjs +0 -201
- package/dist/server-cors/index.cjs.map +0 -1
- package/dist/server-cors/index.d.cts +0 -140
- package/dist/server-health/index.cjs +0 -62
- package/dist/server-health/index.cjs.map +0 -1
- package/dist/server-health/index.d.cts +0 -58
- package/dist/server-helmet/index.cjs +0 -131
- package/dist/server-helmet/index.cjs.map +0 -1
- package/dist/server-helmet/index.d.cts +0 -97
- package/dist/server-links/index.cjs +0 -992
- package/dist/server-links/index.cjs.map +0 -1
- package/dist/server-links/index.d.cts +0 -513
- package/dist/server-metrics/index.cjs +0 -4535
- package/dist/server-metrics/index.cjs.map +0 -1
- package/dist/server-metrics/index.d.cts +0 -35
- package/dist/server-multipart/index.cjs +0 -237
- package/dist/server-multipart/index.cjs.map +0 -1
- package/dist/server-multipart/index.d.cts +0 -50
- package/dist/server-proxy/index.cjs +0 -186
- package/dist/server-proxy/index.cjs.map +0 -1
- package/dist/server-proxy/index.d.cts +0 -234
- package/dist/server-rate-limit/index.cjs +0 -241
- package/dist/server-rate-limit/index.cjs.map +0 -1
- package/dist/server-rate-limit/index.d.cts +0 -183
- package/dist/server-security/index.cjs +0 -316
- package/dist/server-security/index.cjs.map +0 -1
- package/dist/server-security/index.d.cts +0 -173
- package/dist/server-static/index.cjs +0 -170
- package/dist/server-static/index.cjs.map +0 -1
- package/dist/server-static/index.d.cts +0 -121
- package/dist/server-swagger/index.cjs +0 -1021
- package/dist/server-swagger/index.cjs.map +0 -1
- package/dist/server-swagger/index.d.cts +0 -382
- package/dist/sms/index.cjs +0 -221
- package/dist/sms/index.cjs.map +0 -1
- package/dist/sms/index.d.cts +0 -130
- package/dist/thread/index.cjs +0 -350
- package/dist/thread/index.cjs.map +0 -1
- package/dist/thread/index.d.cts +0 -260
- package/dist/topic/index.cjs +0 -282
- package/dist/topic/index.cjs.map +0 -1
- package/dist/topic/index.d.cts +0 -523
- package/dist/topic-redis/index.cjs +0 -71
- package/dist/topic-redis/index.cjs.map +0 -1
- package/dist/topic-redis/index.d.cts +0 -42
- package/dist/vite/index.cjs +0 -1077
- package/dist/vite/index.cjs.map +0 -1
- package/dist/vite/index.d.cts +0 -542
- package/dist/websocket/index.cjs +0 -1117
- package/dist/websocket/index.cjs.map +0 -1
- package/dist/websocket/index.d.cts +0 -861
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
import * as alepha1 from "alepha";
|
|
2
|
-
import { Descriptor, KIND, Static } from "alepha";
|
|
3
|
-
import { ServerRequest, ServerRouterProvider } from "alepha/server";
|
|
4
|
-
import * as alepha_logger0 from "alepha/logger";
|
|
5
|
-
import * as alepha_cache0 from "alepha/cache";
|
|
6
|
-
|
|
7
|
-
//#region src/server-rate-limit/providers/ServerRateLimitProvider.d.ts
|
|
8
|
-
interface RateLimitResult {
|
|
9
|
-
allowed: boolean;
|
|
10
|
-
limit: number;
|
|
11
|
-
remaining: number;
|
|
12
|
-
resetTime: number;
|
|
13
|
-
retryAfter?: number;
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Rate limit configuration atom (global defaults)
|
|
17
|
-
*/
|
|
18
|
-
declare const rateLimitOptions: alepha1.Atom<alepha1.TObject<{
|
|
19
|
-
windowMs: alepha1.TOptional<alepha1.TNumber>;
|
|
20
|
-
max: alepha1.TOptional<alepha1.TNumber>;
|
|
21
|
-
skipFailedRequests: alepha1.TOptional<alepha1.TBoolean>;
|
|
22
|
-
skipSuccessfulRequests: alepha1.TOptional<alepha1.TBoolean>;
|
|
23
|
-
}>, "alepha.server.rate-limit.options">;
|
|
24
|
-
type RateLimitAtomOptions = Static<typeof rateLimitOptions.schema>;
|
|
25
|
-
declare module "alepha" {
|
|
26
|
-
interface State {
|
|
27
|
-
[rateLimitOptions.key]: RateLimitAtomOptions;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
declare class ServerRateLimitProvider {
|
|
31
|
-
protected readonly log: alepha_logger0.Logger;
|
|
32
|
-
protected readonly serverRouterProvider: ServerRouterProvider;
|
|
33
|
-
protected readonly env: {
|
|
34
|
-
RATE_LIMIT_WINDOW_MS: number;
|
|
35
|
-
RATE_LIMIT_MAX_REQUESTS: number;
|
|
36
|
-
};
|
|
37
|
-
protected readonly cache: alepha_cache0.CacheDescriptorFn<RateLimitData, any[]>;
|
|
38
|
-
protected readonly globalOptions: Readonly<{
|
|
39
|
-
windowMs?: number | undefined;
|
|
40
|
-
max?: number | undefined;
|
|
41
|
-
skipFailedRequests?: boolean | undefined;
|
|
42
|
-
skipSuccessfulRequests?: boolean | undefined;
|
|
43
|
-
}>;
|
|
44
|
-
/**
|
|
45
|
-
* Registered rate limit configurations with their path patterns
|
|
46
|
-
*/
|
|
47
|
-
readonly registeredConfigs: RateLimitDescriptorOptions[];
|
|
48
|
-
/**
|
|
49
|
-
* Register a rate limit configuration (called by descriptors)
|
|
50
|
-
*/
|
|
51
|
-
registerRateLimit(config: RateLimitDescriptorOptions): void;
|
|
52
|
-
protected readonly onStart: alepha1.HookDescriptor<"start">;
|
|
53
|
-
readonly onRequest: alepha1.HookDescriptor<"server:onRequest">;
|
|
54
|
-
readonly onActionRequest: alepha1.HookDescriptor<"action:onRequest">;
|
|
55
|
-
/**
|
|
56
|
-
* Build complete rate limit options by merging with global defaults
|
|
57
|
-
*/
|
|
58
|
-
protected buildRateLimitOptions(config: RateLimitDescriptorOptions): RateLimitOptions;
|
|
59
|
-
/**
|
|
60
|
-
* Set rate limit headers on the response
|
|
61
|
-
*/
|
|
62
|
-
protected setRateLimitHeaders(request: ServerRequest, result: RateLimitResult): void;
|
|
63
|
-
checkLimit(req: ServerRequest, options?: RateLimitOptions): Promise<RateLimitResult>;
|
|
64
|
-
protected generateKey(req: ServerRequest): string;
|
|
65
|
-
protected getClientIP(req: ServerRequest): string;
|
|
66
|
-
}
|
|
67
|
-
interface RateLimitData {
|
|
68
|
-
count: number;
|
|
69
|
-
windowStart: number;
|
|
70
|
-
hits: number[];
|
|
71
|
-
}
|
|
72
|
-
//#endregion
|
|
73
|
-
//#region src/server-rate-limit/descriptors/$rateLimit.d.ts
|
|
74
|
-
/**
|
|
75
|
-
* Declares rate limiting for server routes or custom usage.
|
|
76
|
-
* This descriptor provides methods to check rate limits and configure behavior
|
|
77
|
-
* within the server request/response cycle.
|
|
78
|
-
*
|
|
79
|
-
* @example
|
|
80
|
-
* ```ts
|
|
81
|
-
* class ApiService {
|
|
82
|
-
* // Apply rate limiting to specific paths
|
|
83
|
-
* apiRateLimit = $rateLimit({
|
|
84
|
-
* paths: ["/api/*"],
|
|
85
|
-
* max: 100,
|
|
86
|
-
* windowMs: 15 * 60 * 1000, // 15 minutes
|
|
87
|
-
* });
|
|
88
|
-
*
|
|
89
|
-
* // Or use check() method for manual rate limiting
|
|
90
|
-
* customAction = $action({
|
|
91
|
-
* handler: async (req) => {
|
|
92
|
-
* const result = await this.apiRateLimit.check(req);
|
|
93
|
-
* if (!result.allowed) throw new Error("Rate limited");
|
|
94
|
-
* return "ok";
|
|
95
|
-
* },
|
|
96
|
-
* });
|
|
97
|
-
* }
|
|
98
|
-
* ```
|
|
99
|
-
*/
|
|
100
|
-
declare const $rateLimit: {
|
|
101
|
-
(options?: RateLimitDescriptorOptions): AbstractRateLimitDescriptor;
|
|
102
|
-
[KIND]: typeof RateLimitDescriptor;
|
|
103
|
-
};
|
|
104
|
-
interface RateLimitDescriptorOptions extends RateLimitOptions {
|
|
105
|
-
/** Name identifier for this rate limit (default: property key) */
|
|
106
|
-
name?: string;
|
|
107
|
-
/** Path patterns to match (supports wildcards like /api/*) */
|
|
108
|
-
paths?: string[];
|
|
109
|
-
}
|
|
110
|
-
interface AbstractRateLimitDescriptor {
|
|
111
|
-
readonly name: string;
|
|
112
|
-
readonly options: RateLimitDescriptorOptions;
|
|
113
|
-
check(request: ServerRequest, options?: RateLimitOptions): Promise<RateLimitResult>;
|
|
114
|
-
}
|
|
115
|
-
declare class RateLimitDescriptor extends Descriptor<RateLimitDescriptorOptions> implements AbstractRateLimitDescriptor {
|
|
116
|
-
protected readonly serverRateLimitProvider: ServerRateLimitProvider;
|
|
117
|
-
get name(): string;
|
|
118
|
-
protected onInit(): void;
|
|
119
|
-
/**
|
|
120
|
-
* Checks rate limit for the given request using this descriptor's configuration.
|
|
121
|
-
*/
|
|
122
|
-
check(request: ServerRequest, options?: RateLimitOptions): Promise<RateLimitResult>;
|
|
123
|
-
}
|
|
124
|
-
//#endregion
|
|
125
|
-
//#region src/server-rate-limit/index.d.ts
|
|
126
|
-
declare module "alepha/server" {
|
|
127
|
-
interface ActionDescriptorOptions<TConfig> {
|
|
128
|
-
/**
|
|
129
|
-
* Rate limiting configuration for this action.
|
|
130
|
-
* When specified, the action will be rate limited according to these settings.
|
|
131
|
-
*/
|
|
132
|
-
rateLimit?: RateLimitOptions;
|
|
133
|
-
}
|
|
134
|
-
interface ServerRoute {
|
|
135
|
-
/**
|
|
136
|
-
* Route-specific rate limit configuration.
|
|
137
|
-
* If set, overrides the global rate limit options for this route.
|
|
138
|
-
*/
|
|
139
|
-
rateLimit?: RateLimitOptions;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
interface RateLimitOptions {
|
|
143
|
-
/** Maximum number of requests per window (default: 100) */
|
|
144
|
-
max?: number;
|
|
145
|
-
/** Window duration in milliseconds (default: 15 minutes) */
|
|
146
|
-
windowMs?: number;
|
|
147
|
-
/** Custom key generator function */
|
|
148
|
-
keyGenerator?: (req: any) => string;
|
|
149
|
-
/** Skip rate limiting for failed requests */
|
|
150
|
-
skipFailedRequests?: boolean;
|
|
151
|
-
/** Skip rate limiting for successful requests */
|
|
152
|
-
skipSuccessfulRequests?: boolean;
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* Provides rate limiting capabilities for server routes and actions with configurable limits and windows.
|
|
156
|
-
*
|
|
157
|
-
* The server-rate-limit module enables per-route and per-action rate limiting using either:
|
|
158
|
-
* - The `$rateLimit` descriptor with `paths` option for path-based rate limiting
|
|
159
|
-
* - The `rateLimit` option in action descriptors for action-specific limiting
|
|
160
|
-
*
|
|
161
|
-
* It offers sliding window rate limiting, custom key generation, and seamless integration with server routes.
|
|
162
|
-
*
|
|
163
|
-
* @example
|
|
164
|
-
* ```ts
|
|
165
|
-
* import { $rateLimit, AlephaServerRateLimit } from "alepha/server-rate-limit";
|
|
166
|
-
*
|
|
167
|
-
* class ApiService {
|
|
168
|
-
* // Path-specific rate limiting
|
|
169
|
-
* apiRateLimit = $rateLimit({
|
|
170
|
-
* paths: ["/api/*"],
|
|
171
|
-
* max: 100,
|
|
172
|
-
* windowMs: 15 * 60 * 1000, // 15 minutes
|
|
173
|
-
* });
|
|
174
|
-
* }
|
|
175
|
-
* ```
|
|
176
|
-
*
|
|
177
|
-
* @see {@link $rateLimit}
|
|
178
|
-
* @module alepha.server.rate-limit
|
|
179
|
-
*/
|
|
180
|
-
declare const AlephaServerRateLimit: alepha1.Service<alepha1.Module>;
|
|
181
|
-
//#endregion
|
|
182
|
-
export { $rateLimit, AbstractRateLimitDescriptor, AlephaServerRateLimit, RateLimitAtomOptions, RateLimitDescriptor, RateLimitDescriptorOptions, RateLimitOptions, RateLimitResult, ServerRateLimitProvider, rateLimitOptions };
|
|
183
|
-
//# sourceMappingURL=index.d.cts.map
|
|
@@ -1,316 +0,0 @@
|
|
|
1
|
-
let alepha = require("alepha");
|
|
2
|
-
let alepha_security = require("alepha/security");
|
|
3
|
-
let alepha_server = require("alepha/server");
|
|
4
|
-
let node_crypto = require("node:crypto");
|
|
5
|
-
let alepha_logger = require("alepha/logger");
|
|
6
|
-
|
|
7
|
-
//#region src/server-security/providers/ServerBasicAuthProvider.ts
|
|
8
|
-
var ServerBasicAuthProvider = class {
|
|
9
|
-
alepha = (0, alepha.$inject)(alepha.Alepha);
|
|
10
|
-
log = (0, alepha_logger.$logger)();
|
|
11
|
-
routerProvider = (0, alepha.$inject)(alepha_server.ServerRouterProvider);
|
|
12
|
-
realm = "Secure Area";
|
|
13
|
-
/**
|
|
14
|
-
* Registered basic auth descriptors with their configurations
|
|
15
|
-
*/
|
|
16
|
-
registeredAuths = [];
|
|
17
|
-
/**
|
|
18
|
-
* Register a basic auth configuration (called by descriptors)
|
|
19
|
-
*/
|
|
20
|
-
registerAuth(config) {
|
|
21
|
-
this.registeredAuths.push(config);
|
|
22
|
-
}
|
|
23
|
-
onStart = (0, alepha.$hook)({
|
|
24
|
-
on: "start",
|
|
25
|
-
handler: async () => {
|
|
26
|
-
for (const auth of this.registeredAuths) if (auth.paths) for (const pattern of auth.paths) {
|
|
27
|
-
const matchedRoutes = this.routerProvider.getRoutes(pattern);
|
|
28
|
-
for (const route of matchedRoutes) route.secure = { basic: {
|
|
29
|
-
username: auth.username,
|
|
30
|
-
password: auth.password
|
|
31
|
-
} };
|
|
32
|
-
}
|
|
33
|
-
if (this.registeredAuths.length > 0) this.log.info(`Initialized with ${this.registeredAuths.length} registered basic-auth configurations.`);
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
|
-
/**
|
|
37
|
-
* Hook into server:onRequest to check basic auth
|
|
38
|
-
*/
|
|
39
|
-
onRequest = (0, alepha.$hook)({
|
|
40
|
-
on: "server:onRequest",
|
|
41
|
-
handler: async ({ route, request }) => {
|
|
42
|
-
const routeAuth = route.secure;
|
|
43
|
-
if (typeof routeAuth === "object" && "basic" in routeAuth && routeAuth.basic) this.checkAuth(request, routeAuth.basic);
|
|
44
|
-
}
|
|
45
|
-
});
|
|
46
|
-
/**
|
|
47
|
-
* Hook into action:onRequest to check basic auth for actions
|
|
48
|
-
*/
|
|
49
|
-
onActionRequest = (0, alepha.$hook)({
|
|
50
|
-
on: "action:onRequest",
|
|
51
|
-
handler: async ({ action, request }) => {
|
|
52
|
-
const routeAuth = action.route.secure;
|
|
53
|
-
if (isBasicAuth(routeAuth)) this.checkAuth(request, routeAuth.basic);
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
/**
|
|
57
|
-
* Check basic authentication
|
|
58
|
-
*/
|
|
59
|
-
checkAuth(request, options) {
|
|
60
|
-
const authHeader = request.headers?.authorization;
|
|
61
|
-
if (!authHeader || !authHeader.startsWith("Basic ")) {
|
|
62
|
-
this.sendAuthRequired(request);
|
|
63
|
-
throw new alepha_server.HttpError({
|
|
64
|
-
status: 401,
|
|
65
|
-
message: "Authentication required"
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
const base64Credentials = authHeader.slice(6);
|
|
69
|
-
const credentials = Buffer.from(base64Credentials, "base64").toString("utf-8");
|
|
70
|
-
const colonIndex = credentials.indexOf(":");
|
|
71
|
-
const username = colonIndex !== -1 ? credentials.slice(0, colonIndex) : credentials;
|
|
72
|
-
const password = colonIndex !== -1 ? credentials.slice(colonIndex + 1) : "";
|
|
73
|
-
if (!this.timingSafeCredentialCheck(username, password, options.username, options.password)) {
|
|
74
|
-
this.sendAuthRequired(request);
|
|
75
|
-
this.log.warn(`Failed basic auth attempt for user`, { username });
|
|
76
|
-
throw new alepha_server.HttpError({
|
|
77
|
-
status: 401,
|
|
78
|
-
message: "Invalid credentials"
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Performs a timing-safe comparison of credentials to prevent timing attacks.
|
|
84
|
-
* Always compares both username and password to avoid leaking which one is wrong.
|
|
85
|
-
*/
|
|
86
|
-
timingSafeCredentialCheck(inputUsername, inputPassword, expectedUsername, expectedPassword) {
|
|
87
|
-
const inputUserBuf = Buffer.from(inputUsername, "utf-8");
|
|
88
|
-
const expectedUserBuf = Buffer.from(expectedUsername, "utf-8");
|
|
89
|
-
const inputPassBuf = Buffer.from(inputPassword, "utf-8");
|
|
90
|
-
const expectedPassBuf = Buffer.from(expectedPassword, "utf-8");
|
|
91
|
-
return (this.safeCompare(inputUserBuf, expectedUserBuf) & this.safeCompare(inputPassBuf, expectedPassBuf)) === 1;
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* Compares two buffers in constant time, handling different lengths safely.
|
|
95
|
-
* Returns 1 if equal, 0 if not equal.
|
|
96
|
-
*/
|
|
97
|
-
safeCompare(input, expected) {
|
|
98
|
-
if (input.length !== expected.length) {
|
|
99
|
-
(0, node_crypto.timingSafeEqual)(input, input);
|
|
100
|
-
return 0;
|
|
101
|
-
}
|
|
102
|
-
return (0, node_crypto.timingSafeEqual)(input, expected) ? 1 : 0;
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* Send WWW-Authenticate header
|
|
106
|
-
*/
|
|
107
|
-
sendAuthRequired(request) {
|
|
108
|
-
request.reply.setHeader("WWW-Authenticate", `Basic realm="${this.realm}"`);
|
|
109
|
-
}
|
|
110
|
-
};
|
|
111
|
-
const isBasicAuth = (value) => {
|
|
112
|
-
return typeof value === "object" && !!value && "basic" in value && !!value.basic;
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
//#endregion
|
|
116
|
-
//#region src/server-security/descriptors/$basicAuth.ts
|
|
117
|
-
/**
|
|
118
|
-
* Declares HTTP Basic Authentication for server routes.
|
|
119
|
-
* This descriptor provides methods to protect routes with username/password authentication.
|
|
120
|
-
*/
|
|
121
|
-
const $basicAuth = (options) => {
|
|
122
|
-
return (0, alepha.createDescriptor)(BasicAuthDescriptor, options);
|
|
123
|
-
};
|
|
124
|
-
var BasicAuthDescriptor = class extends alepha.Descriptor {
|
|
125
|
-
serverBasicAuthProvider = (0, alepha.$inject)(ServerBasicAuthProvider);
|
|
126
|
-
get name() {
|
|
127
|
-
return this.options.name ?? `${this.config.propertyKey}`;
|
|
128
|
-
}
|
|
129
|
-
onInit() {
|
|
130
|
-
this.serverBasicAuthProvider.registerAuth(this.options);
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Checks basic auth for the given request using this descriptor's configuration.
|
|
134
|
-
*/
|
|
135
|
-
check(request, options) {
|
|
136
|
-
const mergedOptions = {
|
|
137
|
-
...this.options,
|
|
138
|
-
...options
|
|
139
|
-
};
|
|
140
|
-
this.serverBasicAuthProvider.checkAuth(request, mergedOptions);
|
|
141
|
-
}
|
|
142
|
-
};
|
|
143
|
-
$basicAuth[alepha.KIND] = BasicAuthDescriptor;
|
|
144
|
-
|
|
145
|
-
//#endregion
|
|
146
|
-
//#region src/server-security/providers/ServerSecurityProvider.ts
|
|
147
|
-
var ServerSecurityProvider = class {
|
|
148
|
-
log = (0, alepha_logger.$logger)();
|
|
149
|
-
securityProvider = (0, alepha.$inject)(alepha_security.SecurityProvider);
|
|
150
|
-
jwtProvider = (0, alepha.$inject)(alepha_security.JwtProvider);
|
|
151
|
-
alepha = (0, alepha.$inject)(alepha.Alepha);
|
|
152
|
-
onConfigure = (0, alepha.$hook)({
|
|
153
|
-
on: "configure",
|
|
154
|
-
handler: async () => {
|
|
155
|
-
for (const action of this.alepha.descriptors(alepha_server.$action)) {
|
|
156
|
-
if (action.options.disabled || action.options.secure === false || this.securityProvider.getRealms().length === 0) continue;
|
|
157
|
-
if (typeof action.options.secure !== "object") this.securityProvider.createPermission({
|
|
158
|
-
name: action.name,
|
|
159
|
-
group: action.group,
|
|
160
|
-
method: action.route.method,
|
|
161
|
-
path: action.route.path
|
|
162
|
-
});
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
});
|
|
166
|
-
onActionRequest = (0, alepha.$hook)({
|
|
167
|
-
on: "action:onRequest",
|
|
168
|
-
handler: async ({ action, request, options }) => {
|
|
169
|
-
if (action.options.secure === false && !options.user) {
|
|
170
|
-
this.log.trace("Skipping security check for route");
|
|
171
|
-
return;
|
|
172
|
-
}
|
|
173
|
-
if (isBasicAuth(action.route.secure)) return;
|
|
174
|
-
const permission = this.securityProvider.getPermissions().find((it) => it.path === action.route.path && it.method === action.route.method);
|
|
175
|
-
try {
|
|
176
|
-
request.user = this.createUserFromLocalFunctionContext(options, permission);
|
|
177
|
-
const route = action.route;
|
|
178
|
-
if (typeof route.secure === "object") this.check(request.user, route.secure);
|
|
179
|
-
this.alepha.state.set("alepha.server.request.user", this.alepha.codec.decode(alepha_security.userAccountInfoSchema, request.user));
|
|
180
|
-
} catch (error) {
|
|
181
|
-
if (action.options.secure || permission) throw error;
|
|
182
|
-
this.log.trace("Skipping security check for action");
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
});
|
|
186
|
-
onRequest = (0, alepha.$hook)({
|
|
187
|
-
on: "server:onRequest",
|
|
188
|
-
priority: "last",
|
|
189
|
-
handler: async ({ request, route }) => {
|
|
190
|
-
if (route.secure === false) {
|
|
191
|
-
this.log.trace("Skipping security check for route - explicitly disabled");
|
|
192
|
-
return;
|
|
193
|
-
}
|
|
194
|
-
if (isBasicAuth(route.secure)) return;
|
|
195
|
-
const permission = this.securityProvider.getPermissions().find((it) => it.path === route.path && it.method === route.method);
|
|
196
|
-
if (!request.headers.authorization && !route.secure && !permission) {
|
|
197
|
-
this.log.trace("Skipping security check for route - no authorization header and not secure");
|
|
198
|
-
return;
|
|
199
|
-
}
|
|
200
|
-
try {
|
|
201
|
-
request.user = await this.securityProvider.createUserFromToken(request.headers.authorization, { permission });
|
|
202
|
-
if (typeof route.secure === "object") this.check(request.user, route.secure);
|
|
203
|
-
this.alepha.state.set("alepha.server.request.user", this.alepha.codec.decode(alepha_security.userAccountInfoSchema, request.user));
|
|
204
|
-
this.log.trace("User set from request token", {
|
|
205
|
-
user: request.user,
|
|
206
|
-
permission
|
|
207
|
-
});
|
|
208
|
-
} catch (error) {
|
|
209
|
-
if (route.secure || permission) throw error;
|
|
210
|
-
this.log.trace("Skipping security check for route - error occurred", error);
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
});
|
|
214
|
-
check(user, secure) {
|
|
215
|
-
if (secure.realm) {
|
|
216
|
-
if (user.realm !== secure.realm) throw new alepha_server.ForbiddenError(`User must belong to realm '${secure.realm}' to access this route`);
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
/**
|
|
220
|
-
* Get the user account token for a local action call.
|
|
221
|
-
* There are three possible sources for the user:
|
|
222
|
-
* - `options.user`: the user passed in the options
|
|
223
|
-
* - `"system"`: the system user from the state (you MUST set state `server.security.system.user`)
|
|
224
|
-
* - `"context"`: the user from the request context (you MUST be in an HTTP request context)
|
|
225
|
-
*
|
|
226
|
-
* Priority order: `options.user` > `"system"` > `"context"`.
|
|
227
|
-
*
|
|
228
|
-
* In testing environment, if no user is provided, a test user is created based on the SecurityProvider's roles.
|
|
229
|
-
*/
|
|
230
|
-
createUserFromLocalFunctionContext(options, permission) {
|
|
231
|
-
const fromOptions = typeof options.user === "object" ? options.user : void 0;
|
|
232
|
-
const type = typeof options.user === "string" ? options.user : void 0;
|
|
233
|
-
let user;
|
|
234
|
-
const fromContext = this.alepha.context.get("request")?.user;
|
|
235
|
-
const fromSystem = this.alepha.state.get("alepha.server.security.system.user");
|
|
236
|
-
if (type === "system") user = fromSystem;
|
|
237
|
-
else if (type === "context") user = fromContext;
|
|
238
|
-
else user = fromOptions ?? fromContext ?? fromSystem;
|
|
239
|
-
if (!user) {
|
|
240
|
-
if (this.alepha.isTest() && !("user" in options)) return this.createTestUser();
|
|
241
|
-
throw new alepha_server.UnauthorizedError("User is required for calling this action");
|
|
242
|
-
}
|
|
243
|
-
const roles = user.roles ?? (this.alepha.isTest() ? this.securityProvider.getRoles().map((role) => role.name) : []);
|
|
244
|
-
let ownership;
|
|
245
|
-
if (permission) {
|
|
246
|
-
const result = this.securityProvider.checkPermission(permission, ...roles);
|
|
247
|
-
if (!result.isAuthorized) throw new alepha_server.ForbiddenError(`Permission '${this.securityProvider.permissionToString(permission)}' is required for this route`);
|
|
248
|
-
ownership = result.ownership;
|
|
249
|
-
}
|
|
250
|
-
return {
|
|
251
|
-
...user,
|
|
252
|
-
ownership
|
|
253
|
-
};
|
|
254
|
-
}
|
|
255
|
-
createTestUser() {
|
|
256
|
-
return {
|
|
257
|
-
id: (0, node_crypto.randomUUID)(),
|
|
258
|
-
name: "Test",
|
|
259
|
-
roles: this.securityProvider.getRoles().map((role) => role.name)
|
|
260
|
-
};
|
|
261
|
-
}
|
|
262
|
-
onClientRequest = (0, alepha.$hook)({
|
|
263
|
-
on: "client:onRequest",
|
|
264
|
-
handler: async ({ request, options }) => {
|
|
265
|
-
if (!this.alepha.isTest()) return;
|
|
266
|
-
if ("user" in options && options.user === void 0) return;
|
|
267
|
-
request.headers = new Headers(request.headers);
|
|
268
|
-
if (!request.headers.has("authorization")) {
|
|
269
|
-
const test = this.createTestUser();
|
|
270
|
-
const user = typeof options?.user === "object" ? options.user : void 0;
|
|
271
|
-
const sub = user?.id ?? test.id;
|
|
272
|
-
const roles = user?.roles ?? test.roles;
|
|
273
|
-
const token = await this.jwtProvider.create({
|
|
274
|
-
sub,
|
|
275
|
-
roles
|
|
276
|
-
}, user?.realm ?? this.securityProvider.getRealms()[0]?.name);
|
|
277
|
-
request.headers.set("authorization", `Bearer ${token}`);
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
});
|
|
281
|
-
};
|
|
282
|
-
|
|
283
|
-
//#endregion
|
|
284
|
-
//#region src/server-security/index.ts
|
|
285
|
-
/**
|
|
286
|
-
* Plugin for Alepha Server that provides security features. Based on the Alepha Security module.
|
|
287
|
-
*
|
|
288
|
-
* By default, all $action will be guarded by a permission check.
|
|
289
|
-
*
|
|
290
|
-
* @see {@link ServerSecurityProvider}
|
|
291
|
-
* @module alepha.server.security
|
|
292
|
-
*/
|
|
293
|
-
const AlephaServerSecurity = (0, alepha.$module)({
|
|
294
|
-
name: "alepha.server.security",
|
|
295
|
-
descriptors: [
|
|
296
|
-
alepha_security.$realm,
|
|
297
|
-
alepha_security.$role,
|
|
298
|
-
alepha_security.$permission,
|
|
299
|
-
$basicAuth
|
|
300
|
-
],
|
|
301
|
-
services: [
|
|
302
|
-
alepha_server.AlephaServer,
|
|
303
|
-
alepha_security.AlephaSecurity,
|
|
304
|
-
ServerSecurityProvider,
|
|
305
|
-
ServerBasicAuthProvider
|
|
306
|
-
]
|
|
307
|
-
});
|
|
308
|
-
|
|
309
|
-
//#endregion
|
|
310
|
-
exports.$basicAuth = $basicAuth;
|
|
311
|
-
exports.AlephaServerSecurity = AlephaServerSecurity;
|
|
312
|
-
exports.BasicAuthDescriptor = BasicAuthDescriptor;
|
|
313
|
-
exports.ServerBasicAuthProvider = ServerBasicAuthProvider;
|
|
314
|
-
exports.ServerSecurityProvider = ServerSecurityProvider;
|
|
315
|
-
exports.isBasicAuth = isBasicAuth;
|
|
316
|
-
//# sourceMappingURL=index.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["Alepha","ServerRouterProvider","HttpError","Descriptor","KIND","SecurityProvider","JwtProvider","Alepha","$action","userAccountInfoSchema","ForbiddenError","user: UserAccountToken | undefined","UnauthorizedError","ownership: boolean | string | undefined","$realm","$role","$permission","AlephaServer","AlephaSecurity"],"sources":["../../src/server-security/providers/ServerBasicAuthProvider.ts","../../src/server-security/descriptors/$basicAuth.ts","../../src/server-security/providers/ServerSecurityProvider.ts","../../src/server-security/index.ts"],"sourcesContent":["import { timingSafeEqual } from \"node:crypto\";\nimport { $hook, $inject, Alepha } from \"alepha\";\nimport { $logger } from \"alepha/logger\";\nimport {\n HttpError,\n type ServerRequest,\n ServerRouterProvider,\n} from \"alepha/server\";\n\n// ---------------------------------------------------------------------------------------------------------------------\n\nexport interface BasicAuthOptions {\n username: string;\n password: string;\n}\n\nexport interface BasicAuthDescriptorConfig extends BasicAuthOptions {\n /** Name identifier for this basic auth (default: property key) */\n name?: string;\n /** Path patterns to match (supports wildcards like /devtools/*) */\n paths?: string[];\n}\n\n// ---------------------------------------------------------------------------------------------------------------------\n\nexport class ServerBasicAuthProvider {\n protected readonly alepha = $inject(Alepha);\n protected readonly log = $logger();\n protected readonly routerProvider = $inject(ServerRouterProvider);\n protected readonly realm = \"Secure Area\";\n\n /**\n * Registered basic auth descriptors with their configurations\n */\n public readonly registeredAuths: BasicAuthDescriptorConfig[] = [];\n\n /**\n * Register a basic auth configuration (called by descriptors)\n */\n public registerAuth(config: BasicAuthDescriptorConfig): void {\n this.registeredAuths.push(config);\n }\n\n public readonly onStart = $hook({\n on: \"start\",\n handler: async () => {\n for (const auth of this.registeredAuths) {\n if (auth.paths) {\n for (const pattern of auth.paths) {\n const matchedRoutes = this.routerProvider.getRoutes(pattern);\n for (const route of matchedRoutes) {\n route.secure = {\n basic: {\n username: auth.username,\n password: auth.password,\n },\n };\n }\n }\n }\n }\n\n if (this.registeredAuths.length > 0) {\n this.log.info(\n `Initialized with ${this.registeredAuths.length} registered basic-auth configurations.`,\n );\n }\n },\n });\n\n /**\n * Hook into server:onRequest to check basic auth\n */\n public readonly onRequest = $hook({\n on: \"server:onRequest\",\n handler: async ({ route, request }) => {\n const routeAuth = route.secure;\n if (\n typeof routeAuth === \"object\" &&\n \"basic\" in routeAuth &&\n routeAuth.basic\n ) {\n this.checkAuth(request, routeAuth.basic);\n }\n },\n });\n\n /**\n * Hook into action:onRequest to check basic auth for actions\n */\n public readonly onActionRequest = $hook({\n on: \"action:onRequest\",\n handler: async ({ action, request }) => {\n const routeAuth = action.route.secure;\n if (isBasicAuth(routeAuth)) {\n this.checkAuth(request, routeAuth.basic);\n }\n },\n });\n\n /**\n * Check basic authentication\n */\n public checkAuth(request: ServerRequest, options: BasicAuthOptions): void {\n const authHeader = request.headers?.authorization;\n\n if (!authHeader || !authHeader.startsWith(\"Basic \")) {\n this.sendAuthRequired(request);\n throw new HttpError({\n status: 401,\n message: \"Authentication required\",\n });\n }\n\n // decode base64 credentials\n const base64Credentials = authHeader.slice(6); // Remove \"Basic \"\n const credentials = Buffer.from(base64Credentials, \"base64\").toString(\n \"utf-8\",\n );\n\n // split only on the first colon to handle passwords with colons\n const colonIndex = credentials.indexOf(\":\");\n const username =\n colonIndex !== -1 ? credentials.slice(0, colonIndex) : credentials;\n const password = colonIndex !== -1 ? credentials.slice(colonIndex + 1) : \"\";\n\n // verify credentials using timing-safe comparison to prevent timing attacks\n const isValid = this.timingSafeCredentialCheck(\n username,\n password,\n options.username,\n options.password,\n );\n\n if (!isValid) {\n this.sendAuthRequired(request);\n this.log.warn(`Failed basic auth attempt for user`, {\n username,\n });\n throw new HttpError({\n status: 401,\n message: \"Invalid credentials\",\n });\n }\n }\n\n /**\n * Performs a timing-safe comparison of credentials to prevent timing attacks.\n * Always compares both username and password to avoid leaking which one is wrong.\n */\n protected timingSafeCredentialCheck(\n inputUsername: string,\n inputPassword: string,\n expectedUsername: string,\n expectedPassword: string,\n ): boolean {\n // Convert to buffers for timing-safe comparison\n const inputUserBuf = Buffer.from(inputUsername, \"utf-8\");\n const expectedUserBuf = Buffer.from(expectedUsername, \"utf-8\");\n const inputPassBuf = Buffer.from(inputPassword, \"utf-8\");\n const expectedPassBuf = Buffer.from(expectedPassword, \"utf-8\");\n\n // timingSafeEqual requires same-length buffers\n // When lengths differ, we compare against a dummy buffer to maintain constant time\n const userMatch = this.safeCompare(inputUserBuf, expectedUserBuf);\n const passMatch = this.safeCompare(inputPassBuf, expectedPassBuf);\n\n // Both must match - bitwise AND avoids short-circuit evaluation\n // eslint-disable-next-line no-bitwise\n return (userMatch & passMatch) === 1;\n }\n\n /**\n * Compares two buffers in constant time, handling different lengths safely.\n * Returns 1 if equal, 0 if not equal.\n */\n protected safeCompare(input: Buffer, expected: Buffer): number {\n // If lengths differ, compare input against itself to maintain timing\n // but return 0 (not equal)\n if (input.length !== expected.length) {\n // Still perform a comparison to keep timing consistent\n timingSafeEqual(input, input);\n return 0;\n }\n\n return timingSafeEqual(input, expected) ? 1 : 0;\n }\n\n /**\n * Send WWW-Authenticate header\n */\n protected sendAuthRequired(request: ServerRequest): void {\n request.reply.setHeader(\"WWW-Authenticate\", `Basic realm=\"${this.realm}\"`);\n }\n}\n\nexport const isBasicAuth = (\n value: unknown,\n): value is { basic: BasicAuthOptions } => {\n return (\n typeof value === \"object\" && !!value && \"basic\" in value && !!value.basic\n );\n};\n","import { $inject, createDescriptor, Descriptor, KIND } from \"alepha\";\nimport type { ServerRequest } from \"alepha/server\";\nimport type {\n BasicAuthDescriptorConfig,\n BasicAuthOptions,\n} from \"../providers/ServerBasicAuthProvider.ts\";\nimport { ServerBasicAuthProvider } from \"../providers/ServerBasicAuthProvider.ts\";\n\n/**\n * Declares HTTP Basic Authentication for server routes.\n * This descriptor provides methods to protect routes with username/password authentication.\n */\nexport const $basicAuth = (\n options: BasicAuthDescriptorConfig,\n): AbstractBasicAuthDescriptor => {\n return createDescriptor(BasicAuthDescriptor, options);\n};\n\n// ---------------------------------------------------------------------------------------------------------------------\n\nexport interface AbstractBasicAuthDescriptor {\n readonly name: string;\n readonly options: BasicAuthDescriptorConfig;\n check(request: ServerRequest, options?: BasicAuthOptions): void;\n}\n\nexport class BasicAuthDescriptor\n extends Descriptor<BasicAuthDescriptorConfig>\n implements AbstractBasicAuthDescriptor\n{\n protected readonly serverBasicAuthProvider = $inject(ServerBasicAuthProvider);\n\n public get name(): string {\n return this.options.name ?? `${this.config.propertyKey}`;\n }\n\n protected onInit() {\n // Register this auth configuration with the provider\n this.serverBasicAuthProvider.registerAuth(this.options);\n }\n\n /**\n * Checks basic auth for the given request using this descriptor's configuration.\n */\n public check(request: ServerRequest, options?: BasicAuthOptions): void {\n const mergedOptions = { ...this.options, ...options };\n this.serverBasicAuthProvider.checkAuth(request, mergedOptions);\n }\n}\n\n$basicAuth[KIND] = BasicAuthDescriptor;\n","import { randomUUID } from \"node:crypto\";\nimport { $hook, $inject, Alepha } from \"alepha\";\nimport { $logger } from \"alepha/logger\";\nimport {\n JwtProvider,\n type Permission,\n SecurityProvider,\n type UserAccountToken,\n userAccountInfoSchema,\n} from \"alepha/security\";\nimport {\n $action,\n ForbiddenError,\n type ServerRequest,\n UnauthorizedError,\n} from \"alepha/server\";\nimport {\n type BasicAuthOptions,\n isBasicAuth,\n} from \"./ServerBasicAuthProvider.ts\";\n\nexport class ServerSecurityProvider {\n protected readonly log = $logger();\n protected readonly securityProvider = $inject(SecurityProvider);\n protected readonly jwtProvider = $inject(JwtProvider);\n protected readonly alepha = $inject(Alepha);\n\n protected readonly onConfigure = $hook({\n on: \"configure\",\n handler: async () => {\n for (const action of this.alepha.descriptors($action)) {\n // -------------------------------------------------------------------------------------------------------------\n // if the action is disabled or not secure, we do NOT create a permission for it\n // -------------------------------------------------------------------------------------------------------------\n if (\n action.options.disabled ||\n action.options.secure === false ||\n this.securityProvider.getRealms().length === 0\n ) {\n continue;\n }\n\n const secure = action.options.secure;\n if (typeof secure !== \"object\") {\n this.securityProvider.createPermission({\n name: action.name,\n group: action.group,\n method: action.route.method,\n path: action.route.path,\n });\n }\n }\n },\n });\n\n // -------------------------------------------------------------------------------------------------------------------\n\n protected readonly onActionRequest = $hook({\n on: \"action:onRequest\",\n handler: async ({ action, request, options }) => {\n // if you set explicitly secure: false, we assume you don't want any security check\n // but only if no user is provided in options\n if (action.options.secure === false && !options.user) {\n this.log.trace(\"Skipping security check for route\");\n return;\n }\n\n if (isBasicAuth(action.route.secure)) {\n return;\n }\n\n const permission = this.securityProvider\n .getPermissions()\n .find(\n (it) =>\n it.path === action.route.path && it.method === action.route.method,\n );\n\n try {\n request.user = this.createUserFromLocalFunctionContext(\n options,\n permission,\n );\n\n const route = action.route;\n if (typeof route.secure === \"object\") {\n this.check(request.user, route.secure);\n }\n\n this.alepha.state.set(\n \"alepha.server.request.user\",\n this.alepha.codec.decode(userAccountInfoSchema, request.user),\n );\n } catch (error) {\n if (action.options.secure || permission) {\n throw error;\n }\n // else, we skip the security check\n this.log.trace(\"Skipping security check for action\");\n }\n },\n });\n\n protected readonly onRequest = $hook({\n on: \"server:onRequest\",\n priority: \"last\",\n handler: async ({ request, route }) => {\n // if you set explicitly secure: false, we assume you don't want any security check\n if (route.secure === false) {\n this.log.trace(\n \"Skipping security check for route - explicitly disabled\",\n );\n return;\n }\n\n if (isBasicAuth(route.secure)) {\n return;\n }\n\n const permission = this.securityProvider\n .getPermissions()\n .find((it) => it.path === route.path && it.method === route.method);\n\n if (!request.headers.authorization && !route.secure && !permission) {\n this.log.trace(\n \"Skipping security check for route - no authorization header and not secure\",\n );\n return;\n }\n\n try {\n // set user to request\n request.user = await this.securityProvider.createUserFromToken(\n request.headers.authorization,\n { permission },\n );\n\n if (typeof route.secure === \"object\") {\n this.check(request.user, route.secure);\n }\n\n this.alepha.state.set(\n \"alepha.server.request.user\",\n // remove sensitive info\n this.alepha.codec.decode(userAccountInfoSchema, request.user),\n );\n\n this.log.trace(\"User set from request token\", {\n user: request.user,\n permission,\n });\n } catch (error) {\n if (route.secure || permission) {\n throw error;\n }\n\n // else, we skip the security check\n this.log.trace(\n \"Skipping security check for route - error occurred\",\n error,\n );\n }\n },\n });\n\n // -------------------------------------------------------------------------------------------------------------------\n\n protected check(user: UserAccountToken, secure: ServerRouteSecure) {\n if (secure.realm) {\n if (user.realm !== secure.realm) {\n throw new ForbiddenError(\n `User must belong to realm '${secure.realm}' to access this route`,\n );\n }\n }\n }\n\n /**\n * Get the user account token for a local action call.\n * There are three possible sources for the user:\n * - `options.user`: the user passed in the options\n * - `\"system\"`: the system user from the state (you MUST set state `server.security.system.user`)\n * - `\"context\"`: the user from the request context (you MUST be in an HTTP request context)\n *\n * Priority order: `options.user` > `\"system\"` > `\"context\"`.\n *\n * In testing environment, if no user is provided, a test user is created based on the SecurityProvider's roles.\n */\n protected createUserFromLocalFunctionContext(\n options: { user?: UserAccountToken | \"system\" | \"context\" },\n permission?: Permission,\n ): UserAccountToken {\n const fromOptions =\n typeof options.user === \"object\" ? options.user : undefined;\n\n const type = typeof options.user === \"string\" ? options.user : undefined;\n\n let user: UserAccountToken | undefined;\n\n const fromContext = this.alepha.context.get<ServerRequest>(\"request\")?.user;\n const fromSystem = this.alepha.state.get(\n \"alepha.server.security.system.user\",\n );\n\n if (type === \"system\") {\n user = fromSystem;\n } else if (type === \"context\") {\n user = fromContext;\n } else {\n user = fromOptions ?? fromContext ?? fromSystem;\n }\n\n if (!user) {\n // in testing mode, we create a test user\n if (this.alepha.isTest() && !(\"user\" in options)) {\n return this.createTestUser();\n }\n\n throw new UnauthorizedError(\"User is required for calling this action\");\n }\n\n const roles =\n user.roles ??\n (this.alepha.isTest()\n ? this.securityProvider.getRoles().map((role) => role.name)\n : []);\n let ownership: boolean | string | undefined;\n\n if (permission) {\n const result = this.securityProvider.checkPermission(\n permission,\n ...roles,\n );\n if (!result.isAuthorized) {\n throw new ForbiddenError(\n `Permission '${this.securityProvider.permissionToString(permission)}' is required for this route`,\n );\n }\n ownership = result.ownership;\n }\n\n // create a new user object with ownership if needed\n return {\n ...user,\n ownership,\n };\n }\n\n // ---------------------------------------------------------------------------------------------------------------\n // TESTING ONLY\n // ---------------------------------------------------------------------------------------------------------------\n\n protected createTestUser(): UserAccountToken {\n return {\n id: randomUUID(),\n name: \"Test\",\n roles: this.securityProvider.getRoles().map((role) => role.name),\n };\n }\n\n protected readonly onClientRequest = $hook({\n on: \"client:onRequest\",\n handler: async ({ request, options }) => {\n if (!this.alepha.isTest()) {\n return;\n }\n\n // skip helper if user is explicitly set to undefined\n if (\"user\" in options && options.user === undefined) {\n return;\n }\n\n request.headers = new Headers(request.headers);\n\n if (!request.headers.has(\"authorization\")) {\n const test = this.createTestUser();\n const user =\n typeof options?.user === \"object\" ? options.user : undefined;\n const sub = user?.id ?? test.id;\n const roles = user?.roles ?? test.roles;\n\n const token = await this.jwtProvider.create(\n {\n sub,\n roles,\n },\n user?.realm ?? this.securityProvider.getRealms()[0]?.name,\n );\n\n request.headers.set(\"authorization\", `Bearer ${token}`);\n }\n },\n });\n}\n\nexport type ServerRouteSecure = {\n realm?: string;\n basic?: BasicAuthOptions;\n};\n","import { $module } from \"alepha\";\nimport {\n $permission,\n $realm,\n $role,\n AlephaSecurity,\n type UserAccount,\n type UserAccountToken,\n} from \"alepha/security\";\nimport { AlephaServer, type FetchOptions } from \"alepha/server\";\nimport { $basicAuth } from \"./descriptors/$basicAuth.ts\";\nimport { ServerBasicAuthProvider } from \"./providers/ServerBasicAuthProvider.ts\";\nimport {\n type ServerRouteSecure,\n ServerSecurityProvider,\n} from \"./providers/ServerSecurityProvider.ts\";\n\n// ---------------------------------------------------------------------------------------------------------------------\n\nexport * from \"./descriptors/$basicAuth.ts\";\nexport * from \"./providers/ServerBasicAuthProvider.ts\";\nexport * from \"./providers/ServerSecurityProvider.ts\";\n\n// ---------------------------------------------------------------------------------------------------------------------\n\ndeclare module \"alepha\" {\n interface State {\n /**\n * Real (or fake) user account, used for internal actions.\n *\n * If you define this, you assume that all actions are executed by this user by default.\n * > To force a different user, you need to pass it explicitly in the options.\n */\n\n \"alepha.server.security.system.user\"?: UserAccountToken;\n\n /**\n * The authenticated user account attached to the server request state.\n *\n * @internal\n */\n \"alepha.server.request.user\"?: UserAccount;\n }\n}\n\ndeclare module \"alepha/server\" {\n interface ServerRequest<TConfig> {\n user?: UserAccountToken; // for all routes, user is maybe present\n }\n\n interface ServerActionRequest<TConfig> {\n user: UserAccountToken; // for actions, user is always present\n }\n\n interface ServerRoute {\n /**\n * If true, the route will be protected by the security provider.\n * All actions are secure by default, but you can disable it for specific actions.\n */\n secure?: boolean | ServerRouteSecure;\n }\n\n interface ClientRequestOptions extends FetchOptions {\n /**\n * Forward user from the previous request.\n * If \"system\", use system user. @see {ServerSecurityProvider.localSystemUser}\n * If \"context\", use the user from the current context (e.g. request).\n *\n * @default \"system\" if provided, else \"context\" if available.\n */\n user?: UserAccountToken | \"system\" | \"context\";\n }\n}\n\n// ---------------------------------------------------------------------------------------------------------------------\n\n/**\n * Plugin for Alepha Server that provides security features. Based on the Alepha Security module.\n *\n * By default, all $action will be guarded by a permission check.\n *\n * @see {@link ServerSecurityProvider}\n * @module alepha.server.security\n */\nexport const AlephaServerSecurity = $module({\n name: \"alepha.server.security\",\n descriptors: [$realm, $role, $permission, $basicAuth],\n services: [\n AlephaServer,\n AlephaSecurity,\n ServerSecurityProvider,\n ServerBasicAuthProvider,\n ],\n});\n"],"mappings":";;;;;;;AAyBA,IAAa,0BAAb,MAAqC;CACnC,AAAmB,6BAAiBA,cAAO;CAC3C,AAAmB,kCAAe;CAClC,AAAmB,qCAAyBC,mCAAqB;CACjE,AAAmB,QAAQ;;;;CAK3B,AAAgB,kBAA+C,EAAE;;;;CAKjE,AAAO,aAAa,QAAyC;AAC3D,OAAK,gBAAgB,KAAK,OAAO;;CAGnC,AAAgB,4BAAgB;EAC9B,IAAI;EACJ,SAAS,YAAY;AACnB,QAAK,MAAM,QAAQ,KAAK,gBACtB,KAAI,KAAK,MACP,MAAK,MAAM,WAAW,KAAK,OAAO;IAChC,MAAM,gBAAgB,KAAK,eAAe,UAAU,QAAQ;AAC5D,SAAK,MAAM,SAAS,cAClB,OAAM,SAAS,EACb,OAAO;KACL,UAAU,KAAK;KACf,UAAU,KAAK;KAChB,EACF;;AAMT,OAAI,KAAK,gBAAgB,SAAS,EAChC,MAAK,IAAI,KACP,oBAAoB,KAAK,gBAAgB,OAAO,wCACjD;;EAGN,CAAC;;;;CAKF,AAAgB,8BAAkB;EAChC,IAAI;EACJ,SAAS,OAAO,EAAE,OAAO,cAAc;GACrC,MAAM,YAAY,MAAM;AACxB,OACE,OAAO,cAAc,YACrB,WAAW,aACX,UAAU,MAEV,MAAK,UAAU,SAAS,UAAU,MAAM;;EAG7C,CAAC;;;;CAKF,AAAgB,oCAAwB;EACtC,IAAI;EACJ,SAAS,OAAO,EAAE,QAAQ,cAAc;GACtC,MAAM,YAAY,OAAO,MAAM;AAC/B,OAAI,YAAY,UAAU,CACxB,MAAK,UAAU,SAAS,UAAU,MAAM;;EAG7C,CAAC;;;;CAKF,AAAO,UAAU,SAAwB,SAAiC;EACxE,MAAM,aAAa,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