alepha 0.6.7 → 0.6.9

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/security.d.ts CHANGED
@@ -1 +1,651 @@
1
- export * from '@alepha/security';
1
+ import * as _alepha_core from '@alepha/core';
2
+ import { Static as Static$1, KIND, OPTIONS, Alepha } from '@alepha/core';
3
+ import { JWSHeaderParameters, FlattenedJWSInput, CryptoKey, KeyObject, JSONWebKeySet, JWTVerifyResult, JWTPayload, JWTHeaderParameters } from 'jose';
4
+
5
+ /**
6
+ * Represents a User Account extracted from JWT.
7
+ */
8
+ interface UserAccountInfo {
9
+ /**
10
+ * ID of user account. Based on JWT.sub.
11
+ */
12
+ id: string;
13
+ /**
14
+ * Represents the roles assigned to a user.
15
+ */
16
+ roles?: string[];
17
+ /**
18
+ * User full name, if available.
19
+ */
20
+ name?: string;
21
+ /**
22
+ * Organization ID, if available.
23
+ */
24
+ organization?: string;
25
+ }
26
+
27
+ /** Symbol key applied to readonly types */
28
+ declare const ReadonlyKind: unique symbol;
29
+ /** Symbol key applied to optional types */
30
+ declare const OptionalKind: unique symbol;
31
+ /** Symbol key applied to types */
32
+ declare const Hint: unique symbol;
33
+ /** Symbol key applied to types */
34
+ declare const Kind: unique symbol;
35
+
36
+ type TReadonly<T extends TSchema> = T & {
37
+ [ReadonlyKind]: 'Readonly';
38
+ };
39
+
40
+ type StringFormatOption = 'date-time' | 'time' | 'date' | 'email' | 'idn-email' | 'hostname' | 'idn-hostname' | 'ipv4' | 'ipv6' | 'uri' | 'uri-reference' | 'iri' | 'uuid' | 'iri-reference' | 'uri-template' | 'json-pointer' | 'relative-json-pointer' | 'regex' | ({} & string);
41
+ type StringContentEncodingOption = '7bit' | '8bit' | 'binary' | 'quoted-printable' | 'base64' | ({} & string);
42
+ interface StringOptions extends SchemaOptions {
43
+ /** The maximum string length */
44
+ maxLength?: number;
45
+ /** The minimum string length */
46
+ minLength?: number;
47
+ /** A regular expression pattern this string should match */
48
+ pattern?: string;
49
+ /** A format this string should match */
50
+ format?: StringFormatOption;
51
+ /** The content encoding for this string */
52
+ contentEncoding?: StringContentEncodingOption;
53
+ /** The content media type for this string */
54
+ contentMediaType?: string;
55
+ }
56
+ interface TString extends TSchema, StringOptions {
57
+ [Kind]: 'String';
58
+ static: string;
59
+ type: 'string';
60
+ }
61
+
62
+ interface TBoolean extends TSchema {
63
+ [Kind]: 'Boolean';
64
+ static: boolean;
65
+ type: 'boolean';
66
+ }
67
+
68
+ type TOptional<T extends TSchema> = T & {
69
+ [OptionalKind]: 'Optional';
70
+ };
71
+
72
+ /** Creates a static type from a TypeBox type */
73
+ type Static<Type extends TSchema, Params extends unknown[] = [], Result = (Type & {
74
+ params: Params;
75
+ })['static']> = Result;
76
+
77
+ type ReadonlyOptionalPropertyKeys<T extends TProperties> = {
78
+ [K in keyof T]: T[K] extends TReadonly<TSchema> ? (T[K] extends TOptional<T[K]> ? K : never) : never;
79
+ }[keyof T];
80
+ type ReadonlyPropertyKeys<T extends TProperties> = {
81
+ [K in keyof T]: T[K] extends TReadonly<TSchema> ? (T[K] extends TOptional<T[K]> ? never : K) : never;
82
+ }[keyof T];
83
+ type OptionalPropertyKeys<T extends TProperties> = {
84
+ [K in keyof T]: T[K] extends TOptional<TSchema> ? (T[K] extends TReadonly<T[K]> ? never : K) : never;
85
+ }[keyof T];
86
+ type RequiredPropertyKeys<T extends TProperties> = keyof Omit<T, ReadonlyOptionalPropertyKeys<T> | ReadonlyPropertyKeys<T> | OptionalPropertyKeys<T>>;
87
+ type ObjectStaticProperties<T extends TProperties, R extends Record<keyof any, unknown>> = Evaluate<(Readonly<Partial<Pick<R, ReadonlyOptionalPropertyKeys<T>>>> & Readonly<Pick<R, ReadonlyPropertyKeys<T>>> & Partial<Pick<R, OptionalPropertyKeys<T>>> & Required<Pick<R, RequiredPropertyKeys<T>>>)>;
88
+ type ObjectStatic<T extends TProperties, P extends unknown[]> = ObjectStaticProperties<T, {
89
+ [K in keyof T]: Static<T[K], P>;
90
+ }>;
91
+ type TPropertyKey = string | number;
92
+ type TProperties = Record<TPropertyKey, TSchema>;
93
+ type TAdditionalProperties = undefined | TSchema | boolean;
94
+ interface ObjectOptions extends SchemaOptions {
95
+ /** Additional property constraints for this object */
96
+ additionalProperties?: TAdditionalProperties;
97
+ /** The minimum number of properties allowed on this object */
98
+ minProperties?: number;
99
+ /** The maximum number of properties allowed on this object */
100
+ maxProperties?: number;
101
+ }
102
+ interface TObject<T extends TProperties = TProperties> extends TSchema, ObjectOptions {
103
+ [Kind]: 'Object';
104
+ static: ObjectStatic<T, this['params']>;
105
+ additionalProperties?: TAdditionalProperties;
106
+ type: 'object';
107
+ properties: T;
108
+ required?: string[];
109
+ }
110
+
111
+ type Evaluate<T> = T extends infer O ? {
112
+ [K in keyof O]: O[K];
113
+ } : never;
114
+ type Ensure<T> = T extends infer U ? U : never;
115
+
116
+ interface ArrayOptions extends SchemaOptions {
117
+ /** The minimum number of items in this array */
118
+ minItems?: number;
119
+ /** The maximum number of items in this array */
120
+ maxItems?: number;
121
+ /** Should this schema contain unique items */
122
+ uniqueItems?: boolean;
123
+ /** A schema for which some elements should match */
124
+ contains?: TSchema;
125
+ /** A minimum number of contains schema matches */
126
+ minContains?: number;
127
+ /** A maximum number of contains schema matches */
128
+ maxContains?: number;
129
+ }
130
+ type ArrayStatic<T extends TSchema, P extends unknown[]> = Ensure<Static<T, P>[]>;
131
+ interface TArray<T extends TSchema = TSchema> extends TSchema, ArrayOptions {
132
+ [Kind]: 'Array';
133
+ static: ArrayStatic<T, this['params']>;
134
+ type: 'array';
135
+ items: T;
136
+ }
137
+
138
+ interface SchemaOptions {
139
+ $schema?: string;
140
+ /** Id for this schema */
141
+ $id?: string;
142
+ /** Title of this schema */
143
+ title?: string;
144
+ /** Description of this schema */
145
+ description?: string;
146
+ /** Default value for this schema */
147
+ default?: any;
148
+ /** Example values matching this schema */
149
+ examples?: any;
150
+ /** Optional annotation for readOnly */
151
+ readOnly?: boolean;
152
+ /** Optional annotation for writeOnly */
153
+ writeOnly?: boolean;
154
+ [prop: string]: any;
155
+ }
156
+ interface TKind {
157
+ [Kind]: string;
158
+ }
159
+ interface TSchema extends TKind, SchemaOptions {
160
+ [ReadonlyKind]?: string;
161
+ [OptionalKind]?: string;
162
+ [Hint]?: string;
163
+ params: unknown[];
164
+ static: unknown;
165
+ }
166
+
167
+ declare const permissionSchema: TObject<{
168
+ name: TString;
169
+ group: TOptional<TString>;
170
+ description: TOptional<TString>;
171
+ method: TOptional<TString>;
172
+ path: TOptional<TString>;
173
+ contentType: TOptional<TString>;
174
+ }>;
175
+ type Permission = Static$1<typeof permissionSchema>;
176
+
177
+ declare const KEY$2 = "PERMISSION";
178
+ interface PermissionDescriptorOptions {
179
+ /**
180
+ * Name of the permission. Use Property name is not provided.
181
+ */
182
+ name?: string;
183
+ /**
184
+ * Group of the permission. Use Class name is not provided.
185
+ */
186
+ group?: string;
187
+ /**
188
+ * Describe the permission.
189
+ */
190
+ description?: string;
191
+ /**
192
+ * HTTP method of the permission. When available.
193
+ */
194
+ method?: string;
195
+ /**
196
+ * URL of the permission. When available.
197
+ */
198
+ url?: string;
199
+ }
200
+ interface PermissionDescriptor {
201
+ [KIND]: typeof KEY$2;
202
+ [OPTIONS]: PermissionDescriptorOptions;
203
+ /**
204
+ * Get the permission object.
205
+ */
206
+ (): Permission;
207
+ /**
208
+ * Check if the user has the permission.
209
+ */
210
+ can(user: UserAccountInfo): boolean;
211
+ }
212
+ declare const $permission: {
213
+ (options?: PermissionDescriptorOptions): PermissionDescriptor;
214
+ [KIND]: string;
215
+ };
216
+
217
+ interface UserAccountToken extends UserAccountInfo {
218
+ /**
219
+ * Access token for the user.
220
+ */
221
+ token?: string;
222
+ /**
223
+ *
224
+ */
225
+ realm?: string;
226
+ /**
227
+ * Is user dedicated to his own resources for this scope ?
228
+ * Mostly, Admin is false and Customer is true.
229
+ */
230
+ ownership?: string | boolean;
231
+ }
232
+
233
+ declare const roleSchema: TObject<{
234
+ name: TString;
235
+ description: TOptional<TString>;
236
+ default: TOptional<TBoolean>;
237
+ permissions: TArray<TObject<{
238
+ name: TString;
239
+ ownership: TOptional<TBoolean>;
240
+ }>>;
241
+ }>;
242
+ type Role = Static$1<typeof roleSchema>;
243
+
244
+ /**
245
+ * Provides utilities for working with JSON Web Tokens (JWT).
246
+ */
247
+ declare class JwtProvider {
248
+ protected readonly log: _alepha_core.Logger;
249
+ protected readonly keystore: KeyLoaderHolder[];
250
+ /**
251
+ * Adds a key loader to the embedded keystore.
252
+ *
253
+ * @param name
254
+ * @param secretKeyOrJwks
255
+ */
256
+ setKeyLoader(name: string, secretKeyOrJwks: string | JSONWebKeySet): void;
257
+ /**
258
+ * Retrieves the payload from a JSON Web Token (JWT).
259
+ *
260
+ * @param token - The JWT to extract the payload from.
261
+ *
262
+ * @return A Promise that resolves with the payload object from the token.
263
+ */
264
+ parse(token: string): Promise<JwtParseResult>;
265
+ /**
266
+ * Creates a JWT token with the provided payload and secret key.
267
+ *
268
+ * @param payload - The payload to be encoded in the token.
269
+ * It should include the `realm_access` property which contains an array of roles.
270
+ * @param keyName - The name of the key to use when signing the token.
271
+ * @param signOptions - The options to use when signing the token.
272
+ *
273
+ * @returns The signed JWT token.
274
+ */
275
+ create(payload: ExtendedJWTPayload, keyName?: string, signOptions?: JwtSignOptions): Promise<string>;
276
+ /**
277
+ * Retrieves the options to use when signing a JWT token.
278
+ *
279
+ * @returns The JWT sign options.
280
+ */
281
+ signOptions(): JwtSignOptions;
282
+ /**
283
+ * Retrieves the first secret key from the keystore.
284
+ *
285
+ * @protected
286
+ */
287
+ protected getFirstSecretKey(): string | undefined;
288
+ /**
289
+ * Determines if the provided key is a secret key.
290
+ *
291
+ * @param key
292
+ * @protected
293
+ */
294
+ protected isSecretKey(key: string): boolean;
295
+ /**
296
+ * Try to find a realm name or something similar in the token.
297
+ *
298
+ * This is useful when the token is not encrypted and API has multiple realms.
299
+ * Instead of trying to verify the token with all keys, we can try to find the key !
300
+ *
301
+ * @param token
302
+ * @protected
303
+ */
304
+ protected tryToGetKeyLoaderFromToken(token: string): KeyLoaderHolder | undefined;
305
+ }
306
+ type KeyLoader = (protectedHeader?: JWSHeaderParameters, token?: FlattenedJWSInput) => Promise<CryptoKey | KeyObject>;
307
+ interface KeyLoaderHolder {
308
+ name: string;
309
+ keyLoader: KeyLoader;
310
+ secretKey?: string;
311
+ }
312
+ interface JwtSignOptions {
313
+ issuedAt?: boolean;
314
+ protectedHeader?: JWTHeaderParameters;
315
+ expiresIn?: string | number;
316
+ }
317
+ interface ExtendedJWTPayload extends JWTPayload {
318
+ name?: string;
319
+ roles?: string[];
320
+ realm_access?: {
321
+ roles: string[];
322
+ };
323
+ }
324
+ interface JwtParseResult {
325
+ keyName: string;
326
+ result: JWTVerifyResult<ExtendedJWTPayload>;
327
+ }
328
+
329
+ declare const envSchema: _alepha_core.TObject<{
330
+ SECURITY_SECRET_KEY: TString;
331
+ }>;
332
+ declare module "alepha" {
333
+ interface Env extends Partial<Static$1<typeof envSchema>> {
334
+ }
335
+ }
336
+ declare class SecurityProvider {
337
+ protected readonly UNKNOWN_USER_NAME = "Unknown User";
338
+ protected readonly PERMISSION_REGEXP: RegExp;
339
+ protected readonly PERMISSION_REGEXP_WILDCARD: RegExp;
340
+ protected readonly log: _alepha_core.Logger;
341
+ protected readonly jwt: JwtProvider;
342
+ protected readonly env: {
343
+ SECURITY_SECRET_KEY: string;
344
+ };
345
+ protected readonly alepha: Alepha;
346
+ /**
347
+ * The permissions configured for the security provider.
348
+ */
349
+ protected readonly permissions: Permission[];
350
+ /**
351
+ * The realms configured for the security provider.
352
+ */
353
+ protected readonly realms: Realm[];
354
+ /**
355
+ * Create realms.
356
+ */
357
+ protected createRealms(): Realm[];
358
+ protected configure: _alepha_core.HookDescriptor<"configure">;
359
+ /**
360
+ * Processes all $permission descriptors.
361
+ */
362
+ protected processPermissionDescriptors(): void;
363
+ /**
364
+ * Processes all $realm descriptors.
365
+ */
366
+ protected processRealmDescriptors(): void;
367
+ /**
368
+ * Processes all $role descriptors.
369
+ */
370
+ protected processRoleDescriptors(): void;
371
+ protected ready: _alepha_core.HookDescriptor<"ready">;
372
+ /**
373
+ * Updates the roles for a realm then synchronizes the user account provider if available.
374
+ *
375
+ * Only available when the app is started.
376
+ *
377
+ * @param realm - The realm to update the roles for.
378
+ * @param roles - The roles to update.
379
+ */
380
+ updateRealm(realm: string, roles: Role[]): Promise<void>;
381
+ /**
382
+ * Adds a role to one or more realms.
383
+ *
384
+ * @param role
385
+ * @param realms
386
+ */
387
+ createRole(role: Role, ...realms: string[]): Role;
388
+ /**
389
+ * Adds a permission to the security provider.
390
+ *
391
+ * @param raw - The permission to add.
392
+ */
393
+ createPermission(raw: Permission | string): Permission;
394
+ /**
395
+ * Creates a user account from the provided payload.
396
+ *
397
+ * @param payload - The payload to create the user account from.
398
+ * @param [realm] - The realm containing the roles. Default is all.
399
+ *
400
+ * @returns The user info created from the payload.
401
+ */
402
+ createInfoFromPayload(payload: JWTPayload, realm?: string): UserAccountInfo;
403
+ /**
404
+ * Checks if the user has the specified permission.
405
+ *
406
+ * Bonus: we check also if the user has "ownership" flag.
407
+ *
408
+ * @param permissionLike - The permission to check for.
409
+ * @param roleEntries - The roles to check for the permission.
410
+ */
411
+ checkPermission(permissionLike: string | Permission, ...roleEntries: string[]): SecurityCheckResult;
412
+ /**
413
+ * Creates a user account from the provided payload.
414
+ *
415
+ * @param headerOrToken
416
+ * @param permissionLike
417
+ */
418
+ createUserFromToken(headerOrToken?: string, permissionLike?: Permission | string): Promise<UserAccountToken>;
419
+ /**
420
+ * Checks if a user has a specific role.
421
+ *
422
+ * @param roleName - The role to check for.
423
+ * @param permission - The permission to check for.
424
+ * @returns True if the user has the role, false otherwise.
425
+ */
426
+ can(role: string, permission: string | Permission): boolean;
427
+ /**
428
+ * Converts a permission object to a string.
429
+ *
430
+ * @param permission
431
+ */
432
+ permissionToString(permission: Permission | string): string;
433
+ getRealms(): Realm[];
434
+ /**
435
+ * Retrieves the user account from the provided user ID.
436
+ *
437
+ * @param realm
438
+ */
439
+ getRoles(realm?: string): Role[];
440
+ /**
441
+ * Returns all permissions.
442
+ *
443
+ * @param user - Filter permissions by user.
444
+ *
445
+ * @return An array containing all permissions.
446
+ */
447
+ getPermissions(user?: {
448
+ roles?: Array<Role | string>;
449
+ realm?: string;
450
+ }): Permission[];
451
+ /**
452
+ * Retrieves the user ID from the provided payload object.
453
+ *
454
+ * @param payload - The payload object from which to extract the user ID.
455
+ * @return The user ID as a string.
456
+ */
457
+ getIdFromPayload(payload: Record<string, any>): string;
458
+ /**
459
+ * Retrieves the roles from the provided payload object.
460
+ * @param payload - The payload object from which to extract the roles.
461
+ * @return An array of role strings.
462
+ */
463
+ getRolesFromPayload(payload: Record<string, any>): string[];
464
+ /**
465
+ * Returns the name from the given payload.
466
+ *
467
+ * @param payload - The payload object.
468
+ * @returns The name extracted from the payload, or an empty string if the payload is falsy or no name is found.
469
+ */
470
+ getNameFromPayload(payload: Record<string, any>): string;
471
+ getOrganizationFromPayload(payload: Record<string, any>): string | undefined;
472
+ }
473
+ /**
474
+ * A realm definition.
475
+ */
476
+ interface Realm {
477
+ /**
478
+ *
479
+ */
480
+ name: string;
481
+ /**
482
+ *
483
+ */
484
+ roles: Role[];
485
+ /**
486
+ * The secret key for the realm.
487
+ *
488
+ * Can be also a JWKS URL.
489
+ */
490
+ secret?: string | JSONWebKeySet;
491
+ /**
492
+ * Attach a user provider to the realm.
493
+ *
494
+ * This is useful when you want to use a custom user provider for a specific realm.
495
+ */
496
+ userAccountProvider?: SecurityUserAccountProvider;
497
+ }
498
+ interface SecurityUserAccountProvider {
499
+ jwks: string | undefined;
500
+ synchronize(config: RealmConfig): Promise<void>;
501
+ }
502
+ interface SecurityCheckResult {
503
+ isAuthorized: boolean;
504
+ ownership: string | boolean | undefined;
505
+ }
506
+ interface RealmConfig {
507
+ roles?: Array<Role>;
508
+ smtp?: {
509
+ host?: string;
510
+ };
511
+ }
512
+
513
+ declare const KEY$1 = "REALM";
514
+ interface RealmDescriptorOptions {
515
+ /**
516
+ * Define the realm name.
517
+ *
518
+ * @default key name
519
+ */
520
+ name?: string;
521
+ /**
522
+ * Describe the realm.
523
+ */
524
+ description?: string;
525
+ /**
526
+ * All roles available in the realm. Role is a string (role name) or a Role object (embedded role).
527
+ */
528
+ roles?: Array<string | Role>;
529
+ /**
530
+ * In order to verify user of the realm, a secret is required.
531
+ * Can be a string based secret or a JWKS URL.
532
+ *
533
+ * Note: You can skip this if you are using a user account provider with JWKS.
534
+ */
535
+ secret?: string | JSONWebKeySet;
536
+ /**
537
+ * Attach a user account provider to the realm to manage roles.
538
+ * For example, you can use a KeycloakUserProvider to automatically create realm roles inside Keycloak.
539
+ */
540
+ userAccountProvider?: SecurityUserAccountProvider | (() => SecurityUserAccountProvider);
541
+ }
542
+ interface RealmDescriptor {
543
+ [KIND]: typeof KEY$1;
544
+ [OPTIONS]: RealmDescriptorOptions;
545
+ /**
546
+ * Get all roles in the realm.
547
+ */
548
+ getRoles(): Role[];
549
+ /**
550
+ * Set all roles in the realm.
551
+ */
552
+ setRoles(roles: Role[]): Promise<void>;
553
+ /**
554
+ * Get a role by name, throws an error if not found.
555
+ */
556
+ getRoleByName(name: string): Role;
557
+ /**
558
+ * Create a token for the subject.
559
+ */
560
+ createToken(subject: string, roles?: string[]): Promise<string>;
561
+ }
562
+ declare const $realm: {
563
+ (options?: RealmDescriptorOptions): RealmDescriptor;
564
+ [KIND]: string;
565
+ };
566
+
567
+ declare const KEY = "ROLE";
568
+ interface RoleDescriptorOptions {
569
+ /**
570
+ * Name of the role.
571
+ */
572
+ name?: string;
573
+ /**
574
+ * Describe the role.
575
+ */
576
+ description?: string;
577
+ /**
578
+ *
579
+ */
580
+ permissions?: Array<string | {
581
+ name: string;
582
+ ownership?: boolean;
583
+ }>;
584
+ }
585
+ interface RoleDescriptor {
586
+ [KIND]: typeof KEY;
587
+ [OPTIONS]: RoleDescriptorOptions;
588
+ /**
589
+ * Get the role object.
590
+ */
591
+ (): Role;
592
+ }
593
+ declare const $role: {
594
+ (options?: RoleDescriptorOptions): RoleDescriptor;
595
+ [KIND]: string;
596
+ };
597
+
598
+ /**
599
+ * Create a service account that can be used to authenticate with a OAUTH2 server.
600
+ *
601
+ * @param options
602
+ */
603
+ declare const $serviceAccount: (options: ServiceAccountDescriptorOptions) => ServiceAccountDescriptor;
604
+ interface ServiceAccountDescriptorOptions {
605
+ /**
606
+ * Get Token URL.
607
+ */
608
+ url: string;
609
+ /**
610
+ * Client ID.
611
+ */
612
+ clientId: string;
613
+ /**
614
+ * Client Secret.
615
+ */
616
+ clientSecret: string;
617
+ /**
618
+ * Scopes to request.
619
+ */
620
+ scope?: string;
621
+ }
622
+ interface ServiceAccountDescriptor {
623
+ options: ServiceAccountDescriptorOptions;
624
+ store: ServiceAccountStore;
625
+ token: () => Promise<string>;
626
+ fetch(url: string, options?: RequestInit): Promise<Response>;
627
+ }
628
+ interface AccessTokenResponse {
629
+ access_token: string;
630
+ expires_in: number;
631
+ at: number;
632
+ }
633
+ interface ServiceAccountStore {
634
+ response?: AccessTokenResponse;
635
+ }
636
+
637
+ declare class InvalidPermissionError extends Error {
638
+ constructor(name: string);
639
+ }
640
+
641
+ declare class SecurityError extends Error {
642
+ readonly status = 403;
643
+ readonly code = "ERR_SECURITY";
644
+ }
645
+
646
+ declare class SecurityModule {
647
+ protected readonly alepha: Alepha;
648
+ constructor();
649
+ }
650
+
651
+ export { $permission, $realm, $role, $serviceAccount, type AccessTokenResponse, type ExtendedJWTPayload, InvalidPermissionError, type JwtParseResult, JwtProvider, type JwtSignOptions, type KeyLoader, type KeyLoaderHolder, type Permission, type PermissionDescriptor, type PermissionDescriptorOptions, type Realm, type RealmConfig, type RealmDescriptor, type RealmDescriptorOptions, type Role, type RoleDescriptor, type RoleDescriptorOptions, type SecurityCheckResult, SecurityError, SecurityModule, SecurityProvider, type SecurityUserAccountProvider, type ServiceAccountDescriptor, type ServiceAccountDescriptorOptions, type ServiceAccountStore, type UserAccountInfo, type UserAccountToken, permissionSchema, roleSchema };
@@ -1 +1,56 @@
1
- export * from '@alepha/server-cookies';
1
+ import * as _alepha_core from '@alepha/core';
2
+ import { TSchema, KIND, OPTIONS, Static } from '@alepha/core';
3
+ import { DurationLike } from '@alepha/datetime';
4
+
5
+ interface CookieDescriptorOptions<T extends TSchema> {
6
+ schema: T;
7
+ name: string;
8
+ path?: string;
9
+ ttl?: DurationLike;
10
+ secure?: boolean;
11
+ httpOnly?: boolean;
12
+ sameSite?: "strict" | "lax" | "none";
13
+ domain?: string;
14
+ compress?: boolean;
15
+ encrypt?: boolean;
16
+ sign?: boolean;
17
+ }
18
+ interface CookieDescriptor<T extends TSchema> {
19
+ [KIND]: "COOKIE";
20
+ [OPTIONS]: CookieDescriptorOptions<T>;
21
+ set: (cookies: Cookies, value: Static<T>) => void;
22
+ get: (cookies: Cookies) => Static<T> | undefined;
23
+ del: (cookies: Cookies) => void;
24
+ }
25
+ declare const $cookie: {
26
+ <T extends TSchema>(options: CookieDescriptorOptions<T>): CookieDescriptor<T>;
27
+ [KIND]: string;
28
+ };
29
+ interface Cookies {
30
+ req: Record<string, string>;
31
+ res: Record<string, Cookie | null>;
32
+ }
33
+ interface Cookie {
34
+ value: string;
35
+ path?: string;
36
+ maxAge?: number;
37
+ secure?: boolean;
38
+ httpOnly?: boolean;
39
+ sameSite?: "strict" | "lax" | "none";
40
+ domain?: string;
41
+ }
42
+
43
+ declare class ServerCookiesProvider {
44
+ readonly onRequest: _alepha_core.HookDescriptor<"server:onRequest">;
45
+ readonly onSend: _alepha_core.HookDescriptor<"server:onSend">;
46
+ fromHeader(header: string): Record<string, string>;
47
+ toHeader(cookies: Record<string, Cookie | null>): string[];
48
+ }
49
+
50
+ declare module "alepha/server" {
51
+ interface ServerRequest {
52
+ cookies: Cookies;
53
+ }
54
+ }
55
+
56
+ export { $cookie, type Cookie, type CookieDescriptor, type CookieDescriptorOptions, type Cookies, ServerCookiesProvider };