@guren/server 2.1.0 → 2.2.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/{McpServiceProvider-7BB5FPF3.js → McpServiceProvider-GRDYNEQG.js} +1 -1
- package/dist/auth/AuthManager.d.ts +30 -0
- package/dist/auth/AuthenticatableModel.d.ts +25 -0
- package/dist/auth/RequestAuthContext.d.ts +24 -0
- package/dist/auth/SessionGuard.d.ts +31 -0
- package/dist/auth/api-token.d.ts +299 -0
- package/dist/auth/email-verification.d.ts +203 -0
- package/dist/auth/index.d.ts +18 -110
- package/dist/auth/index.js +3 -1
- package/dist/auth/oauth/index.d.ts +239 -0
- package/dist/auth/password/DefaultHasher.d.ts +13 -0
- package/dist/auth/password/NodeHasher.d.ts +22 -0
- package/dist/auth/password/PasswordHasher.d.ts +5 -0
- package/dist/auth/password/ScryptHasher.d.ts +26 -0
- package/dist/auth/password-reset.d.ts +91 -0
- package/dist/auth/providers/ModelUserProvider.d.ts +36 -0
- package/dist/auth/providers/UserProvider.d.ts +9 -0
- package/dist/auth/types.d.ts +102 -0
- package/dist/auth/utils.d.ts +39 -0
- package/dist/{Gate-CynjZCtS.d.ts → authorization/Gate.d.ts} +30 -111
- package/dist/authorization/Policy.d.ts +77 -0
- package/dist/authorization/index.d.ts +5 -129
- package/dist/authorization/index.js +3 -1
- package/dist/authorization/middleware.d.ts +49 -0
- package/dist/authorization/types.d.ts +108 -0
- package/dist/broadcasting/BroadcastManager.d.ts +205 -0
- package/dist/broadcasting/channels/Channel.d.ts +28 -0
- package/dist/broadcasting/channels/PresenceChannel.d.ts +72 -0
- package/dist/broadcasting/channels/PrivateChannel.d.ts +32 -0
- package/dist/broadcasting/channels/index.d.ts +3 -0
- package/dist/broadcasting/drivers/MemoryDriver.d.ts +93 -0
- package/dist/broadcasting/drivers/RedisDriver.d.ts +112 -0
- package/dist/broadcasting/drivers/index.d.ts +2 -0
- package/dist/broadcasting/index.d.ts +6 -233
- package/dist/broadcasting/index.js +1 -1
- package/dist/broadcasting/typed.d.ts +23 -0
- package/dist/broadcasting/types.d.ts +155 -0
- package/dist/cache/CacheManager.d.ts +87 -0
- package/dist/cache/TaggedCache.d.ts +62 -0
- package/dist/cache/index.d.ts +6 -233
- package/dist/cache/index.js +1 -1
- package/dist/cache/stores/FileStore.d.ts +64 -0
- package/dist/cache/stores/MemoryStore.d.ts +52 -0
- package/dist/cache/stores/RedisStore.d.ts +53 -0
- package/dist/{CacheManager-BmDzUKrF.d.ts → cache/types.d.ts} +26 -99
- package/dist/{chunk-TVY72CLV.js → chunk-4VE3LYOM.js} +3 -2
- package/dist/{chunk-FZD2AKJI.js → chunk-7OB2YXRE.js} +56 -28
- package/dist/{chunk-HONCNQXZ.js → chunk-BUQXOZCT.js} +97 -10
- package/dist/{chunk-DSDX3ASM.js → chunk-CDZUNKI6.js} +65 -83
- package/dist/{chunk-CWI4AJ47.js → chunk-HPET6XY5.js} +13 -9
- package/dist/{chunk-A3ISJVEV.js → chunk-HR2KZ3WL.js} +23 -5
- package/dist/{chunk-EARA7JM4.js → chunk-PZKUDFMW.js} +11 -8
- package/dist/console/Command.d.ts +179 -0
- package/dist/{ConsoleKernel-CJg975mp.d.ts → console/ConsoleKernel.d.ts} +4 -6
- package/dist/console/Input.d.ts +140 -0
- package/dist/console/Output.d.ts +161 -0
- package/dist/console/index.d.ts +5 -0
- package/dist/console/types.d.ts +106 -0
- package/dist/container/Container.d.ts +148 -0
- package/dist/container/ServiceProvider.d.ts +103 -0
- package/dist/container/bindings.d.ts +55 -0
- package/dist/container/defineModule.d.ts +88 -0
- package/dist/container/definePlugin.d.ts +62 -0
- package/dist/container/index.d.ts +8 -0
- package/dist/container/types.d.ts +73 -0
- package/dist/database/Factory.d.ts +82 -0
- package/dist/database/Seeder.d.ts +35 -0
- package/dist/database/SeederRunner.d.ts +41 -0
- package/dist/database/index.d.ts +4 -0
- package/dist/database/types.d.ts +49 -0
- package/dist/discovery/AutoDiscovery.d.ts +113 -0
- package/dist/discovery/index.d.ts +2 -0
- package/dist/docs-viewer/DocsViewerServiceProvider.d.ts +12 -0
- package/dist/docs-viewer/endpoint.d.ts +19 -0
- package/dist/encryption/Encrypter.d.ts +96 -0
- package/dist/encryption/Hash.d.ts +69 -0
- package/dist/encryption/MessageSigner.d.ts +21 -0
- package/dist/encryption/Random.d.ts +119 -0
- package/dist/encryption/app-key.d.ts +12 -0
- package/dist/encryption/index.d.ts +10 -222
- package/dist/encryption/signed-url.d.ts +9 -0
- package/dist/encryption/types.d.ts +103 -0
- package/dist/errors/ExceptionHandler.d.ts +118 -0
- package/dist/errors/HttpException.d.ts +106 -0
- package/dist/errors/debug-page.d.ts +22 -0
- package/dist/errors/error-page.d.ts +1 -0
- package/dist/errors/exceptions/AuthenticationException.d.ts +32 -0
- package/dist/errors/exceptions/AuthorizationException.d.ts +32 -0
- package/dist/errors/exceptions/MethodNotAllowedException.d.ts +31 -0
- package/dist/errors/exceptions/NotFoundHttpException.d.ts +36 -0
- package/dist/errors/exceptions/ValidationException.d.ts +60 -0
- package/dist/errors/exceptions/index.d.ts +5 -0
- package/dist/errors/index.d.ts +5 -0
- package/dist/errors/types.d.ts +43 -0
- package/dist/events/Event.d.ts +33 -0
- package/dist/{EventManager-CmIoLt7r.d.ts → events/EventManager.d.ts} +4 -86
- package/dist/events/Listener.d.ts +79 -0
- package/dist/events/builtin.d.ts +73 -0
- package/dist/events/index.d.ts +6 -155
- package/dist/events/types.d.ts +48 -0
- package/dist/facades/index.d.ts +31 -0
- package/dist/health/HealthCheck.d.ts +34 -0
- package/dist/health/HealthManager.d.ts +53 -0
- package/dist/health/checks/CacheCheck.d.ts +35 -0
- package/dist/health/checks/CustomCheck.d.ts +23 -0
- package/dist/health/checks/DatabaseCheck.d.ts +33 -0
- package/dist/health/checks/MemoryCheck.d.ts +32 -0
- package/dist/health/checks/RedisCheck.d.ts +27 -0
- package/dist/health/checks/StorageCheck.d.ts +35 -0
- package/dist/health/checks/index.d.ts +12 -0
- package/dist/health/index.d.ts +5 -185
- package/dist/health/types.d.ts +52 -0
- package/dist/hot-reload/hot-disposables.d.ts +96 -0
- package/dist/hot-reload/testing.d.ts +14 -0
- package/dist/http/Application.d.ts +187 -0
- package/dist/http/FormRequest.d.ts +73 -0
- package/dist/http/dev-assets.d.ts +51 -0
- package/dist/http/dev-banner.d.ts +7 -0
- package/dist/http/inertia-assets.d.ts +22 -0
- package/dist/http/middleware/auth.d.ts +13 -0
- package/dist/http/middleware/capabilities.d.ts +22 -0
- package/dist/http/middleware/cors.d.ts +40 -0
- package/dist/http/middleware/csp.d.ts +59 -0
- package/dist/http/middleware/csrf.d.ts +121 -0
- package/dist/http/middleware/detect-locale.d.ts +65 -0
- package/dist/http/middleware/force-https.d.ts +34 -0
- package/dist/http/middleware/host-authorization.d.ts +24 -0
- package/dist/http/middleware/index.d.ts +19 -0
- package/dist/http/middleware/loopback-guard.d.ts +38 -0
- package/dist/{rate-limit-D9J2b4S1.d.ts → http/middleware/rate-limit.d.ts} +22 -50
- package/dist/http/middleware/redirect-safety.d.ts +29 -0
- package/dist/http/middleware/request-id.d.ts +19 -0
- package/dist/http/middleware/request-logging.d.ts +16 -0
- package/dist/http/middleware/security-headers.d.ts +49 -0
- package/dist/http/middleware/session.d.ts +93 -0
- package/dist/http/middleware/validation.d.ts +94 -0
- package/dist/http/public-assets.d.ts +15 -0
- package/dist/http/request.d.ts +24 -0
- package/dist/http/resources/CursorPaginator.d.ts +106 -0
- package/dist/http/resources/Paginator.d.ts +130 -0
- package/dist/http/resources/Resource.d.ts +67 -0
- package/dist/http/resources/ResourceCollection.d.ts +60 -0
- package/dist/http/resources/index.d.ts +6 -0
- package/dist/http/resources/types.d.ts +91 -0
- package/dist/http/validation/Validator.d.ts +128 -0
- package/dist/http/validation/index.d.ts +3 -0
- package/dist/http/validation/rules.d.ts +196 -0
- package/dist/http/validation/types.d.ts +96 -0
- package/dist/http/vite-dev-server.d.ts +27 -0
- package/dist/i18n/I18nManager.d.ts +114 -0
- package/dist/i18n/Translator.d.ts +80 -0
- package/dist/i18n/index.d.ts +6 -101
- package/dist/i18n/index.js +5 -3
- package/dist/i18n/loaders/JsonLoader.d.ts +47 -0
- package/dist/i18n/loaders/MemoryLoader.d.ts +37 -0
- package/dist/i18n/pluralization.d.ts +19 -0
- package/dist/i18n/types.d.ts +106 -0
- package/dist/index.d.ts +69 -2605
- package/dist/index.js +332 -152
- package/dist/lambda/index.d.ts +17 -37
- package/dist/logging/LogManager.d.ts +83 -0
- package/dist/logging/Logger.d.ts +76 -0
- package/dist/logging/channels/ConsoleChannel.d.ts +11 -0
- package/dist/logging/channels/DailyFileChannel.d.ts +20 -0
- package/dist/logging/channels/FileChannel.d.ts +15 -0
- package/dist/logging/index.d.ts +8 -50
- package/dist/logging/types.d.ts +96 -0
- package/dist/mail/Mail.d.ts +138 -0
- package/dist/mail/MailManager.d.ts +79 -0
- package/dist/mail/address.d.ts +8 -0
- package/dist/mail/index.d.ts +7 -316
- package/dist/mail/transports/LogTransport.d.ts +19 -0
- package/dist/mail/transports/MemoryTransport.d.ts +84 -0
- package/dist/mail/transports/ResendTransport.d.ts +32 -0
- package/dist/mail/transports/SmtpTransport.d.ts +40 -0
- package/dist/{MailManager-DpMvYiP9.d.ts → mail/types.d.ts} +11 -92
- package/dist/mcp/McpServiceProvider.d.ts +17 -0
- package/dist/mcp/create-mcp-server.d.ts +144 -0
- package/dist/mcp/endpoint.d.ts +29 -0
- package/dist/mcp/index.d.ts +3 -185
- package/dist/mcp/index.js +1 -1
- package/dist/mvc/Controller.d.ts +326 -0
- package/dist/mvc/Router.d.ts +239 -0
- package/dist/mvc/ViewEngine.d.ts +11 -0
- package/dist/mvc/inertia/InertiaEngine.d.ts +114 -0
- package/dist/mvc/inertia/shared.d.ts +90 -0
- package/dist/mvc/mount-route.d.ts +10 -0
- package/dist/notifications/Notification.d.ts +99 -0
- package/dist/notifications/NotificationManager.d.ts +150 -0
- package/dist/notifications/channels/DatabaseChannel.d.ts +55 -0
- package/dist/notifications/channels/MailChannel.d.ts +45 -0
- package/dist/notifications/channels/MemoryChannel.d.ts +85 -0
- package/dist/notifications/channels/SlackChannel.d.ts +73 -0
- package/dist/notifications/channels/index.d.ts +4 -0
- package/dist/notifications/index.d.ts +6 -322
- package/dist/notifications/notifiable-type.d.ts +10 -0
- package/dist/notifications/registry.d.ts +39 -0
- package/dist/notifications/types.d.ts +162 -0
- package/dist/providers/AuthServiceProvider.d.ts +7 -0
- package/dist/providers/AuthorizationServiceProvider.d.ts +9 -0
- package/dist/providers/BroadcastServiceProvider.d.ts +7 -0
- package/dist/providers/CacheServiceProvider.d.ts +7 -0
- package/dist/providers/EncryptionServiceProvider.d.ts +7 -0
- package/dist/providers/ErrorServiceProvider.d.ts +9 -0
- package/dist/providers/EventServiceProvider.d.ts +7 -0
- package/dist/providers/HealthServiceProvider.d.ts +7 -0
- package/dist/providers/I18nServiceProvider.d.ts +27 -0
- package/dist/providers/InertiaServiceProvider.d.ts +20 -0
- package/dist/providers/LogServiceProvider.d.ts +7 -0
- package/dist/providers/MailServiceProvider.d.ts +7 -0
- package/dist/providers/NotificationServiceProvider.d.ts +8 -0
- package/dist/providers/OAuthServiceProvider.d.ts +7 -0
- package/dist/providers/QueueServiceProvider.d.ts +7 -0
- package/dist/providers/SchedulingServiceProvider.d.ts +7 -0
- package/dist/providers/StorageServiceProvider.d.ts +7 -0
- package/dist/providers/index.d.ts +17 -0
- package/dist/queue/FailedJobReporter.d.ts +40 -0
- package/dist/queue/Job.d.ts +140 -0
- package/dist/queue/QueueManager.d.ts +79 -0
- package/dist/queue/Worker.d.ts +99 -0
- package/dist/queue/drivers/MemoryDriver.d.ts +60 -0
- package/dist/queue/drivers/RedisDriver.d.ts +96 -0
- package/dist/queue/drivers/SqsDriver.d.ts +108 -0
- package/dist/queue/drivers/SyncDriver.d.ts +28 -0
- package/dist/queue/index.d.ts +9 -453
- package/dist/queue/types.d.ts +171 -0
- package/dist/redis/RedisApiTokenStore.d.ts +78 -0
- package/dist/redis/RedisEmailVerificationStore.d.ts +54 -0
- package/dist/redis/RedisOAuthStateStore.d.ts +26 -0
- package/dist/redis/RedisPasswordResetStore.d.ts +57 -0
- package/dist/redis/RedisRateLimitStore.d.ts +77 -0
- package/dist/redis/RedisSessionStore.d.ts +61 -0
- package/dist/redis/client.d.ts +41 -0
- package/dist/redis/index.d.ts +14 -356
- package/dist/redis/index.js +5 -2
- package/dist/redis/scan-keys.d.ts +6 -0
- package/dist/runtime/index.d.ts +8 -95
- package/dist/runtime/index.js +1 -1
- package/dist/scheduling/CronParser.d.ts +37 -0
- package/dist/scheduling/Schedule.d.ts +229 -0
- package/dist/scheduling/ScheduledTask.d.ts +47 -0
- package/dist/scheduling/Scheduler.d.ts +99 -0
- package/dist/scheduling/index.d.ts +5 -41
- package/dist/scheduling/types.d.ts +97 -0
- package/dist/storage/StorageManager.d.ts +65 -0
- package/dist/storage/drivers/LocalDriver.d.ts +55 -0
- package/dist/storage/drivers/MemoryDriver.d.ts +66 -0
- package/dist/storage/drivers/S3Driver.d.ts +72 -0
- package/dist/storage/index.d.ts +5 -197
- package/dist/{StorageManager-oZTHqaza.d.ts → storage/types.d.ts} +10 -77
- package/dist/support/error-polyfill.d.ts +9 -0
- package/dist/support/import-map.d.ts +13 -0
- package/dist/support/redirect-target.d.ts +11 -0
- package/dist/support/stack-frames.d.ts +56 -0
- package/dist/support/trim-slashes.d.ts +11 -0
- package/dist/vite/index.d.ts +2 -35
- package/dist/vite/index.js +0 -3
- package/dist/vite/plugin.d.ts +34 -0
- package/package.json +3 -3
- package/dist/AuthManager-DBP4KYsx.d.ts +0 -79
- package/dist/BroadcastManager-D8KoJxiV.d.ts +0 -492
- package/dist/HealthManager-DUyMIzsZ.d.ts +0 -141
- package/dist/I18nManager-BiSoczfV.d.ts +0 -275
- package/dist/LogManager-7mxnkaPM.d.ts +0 -256
- package/dist/Scheduler-pZiT8i57.d.ts +0 -471
- package/dist/api-token-D24VPr_E.d.ts +0 -663
- package/dist/app-key-CsBfRC_Q.d.ts +0 -214
- package/dist/email-verification-D60YF2ZO.d.ts +0 -329
- package/dist/index-9_Jzj5jo.d.ts +0 -7
- package/dist/validation-yAL53e1S.d.ts +0 -2610
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import type { Context } from 'hono';
|
|
2
|
+
import type { Session } from '../http/middleware/index.js';
|
|
3
|
+
export type AuthCredentials = Record<string, unknown>;
|
|
4
|
+
export interface Authenticatable {
|
|
5
|
+
getAuthIdentifier(): unknown;
|
|
6
|
+
getAuthPassword(): string | null | undefined;
|
|
7
|
+
getRememberToken?(): string | null | undefined;
|
|
8
|
+
setRememberToken?(token: string | null): void | Promise<void>;
|
|
9
|
+
}
|
|
10
|
+
export interface Guard<User = Authenticatable> {
|
|
11
|
+
check(): Promise<boolean>;
|
|
12
|
+
guest(): Promise<boolean>;
|
|
13
|
+
user<T = User>(): Promise<T | null>;
|
|
14
|
+
id(): Promise<unknown>;
|
|
15
|
+
login<T = User>(user: T, remember?: boolean): Promise<void>;
|
|
16
|
+
logout(): Promise<void>;
|
|
17
|
+
attempt(credentials: AuthCredentials, remember?: boolean): Promise<boolean>;
|
|
18
|
+
validate(credentials: AuthCredentials): Promise<User | null>;
|
|
19
|
+
session<T extends Session = Session>(): T | undefined;
|
|
20
|
+
}
|
|
21
|
+
export interface UserProvider<User = Authenticatable> {
|
|
22
|
+
retrieveById(identifier: unknown): Promise<User | null>;
|
|
23
|
+
retrieveByCredentials(credentials: AuthCredentials): Promise<User | null>;
|
|
24
|
+
validateCredentials(user: User, credentials: AuthCredentials): Promise<boolean>;
|
|
25
|
+
getId(user: User): unknown;
|
|
26
|
+
setRememberToken?(user: User, token: string | null): Promise<void> | void;
|
|
27
|
+
getRememberToken?(user: User): Promise<string | null> | string | null;
|
|
28
|
+
/**
|
|
29
|
+
* Strip fields that must never leave the auth layer (password hashes,
|
|
30
|
+
* remember tokens, model `hidden` fields) from a user record before it
|
|
31
|
+
* is cached or handed to application code via guard.user().
|
|
32
|
+
*/
|
|
33
|
+
sanitize?(user: User): User;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The conventional names for the credential columns that
|
|
37
|
+
* `ModelUserProvider.sanitize()` strips: the password column (`password`
|
|
38
|
+
* by default, commonly configured as `passwordHash`/`password_hash`) and
|
|
39
|
+
* the remember-token column (`remember_token` by default, commonly
|
|
40
|
+
* `rememberToken`).
|
|
41
|
+
*/
|
|
42
|
+
export type DefaultSanitizedKeys = 'password' | 'passwordHash' | 'password_hash' | 'rememberToken' | 'remember_token';
|
|
43
|
+
/**
|
|
44
|
+
* The shape of a user record after auth-layer sanitization — what
|
|
45
|
+
* `auth.user()` returns at runtime when the provider's column names
|
|
46
|
+
* follow the {@link DefaultSanitizedKeys} conventions. Removes those
|
|
47
|
+
* conventional credential keys plus any extra keys passed as `Hidden`.
|
|
48
|
+
*
|
|
49
|
+
* The runtime strips exactly the *configured* password/remember-token
|
|
50
|
+
* columns plus the model's `static hidden` — a static type cannot see
|
|
51
|
+
* that configuration. If your columns use other names, or the model
|
|
52
|
+
* hides additional fields, list them in `Hidden` explicitly. Only
|
|
53
|
+
* meaningful for concrete record types (index-signature records like
|
|
54
|
+
* `Record<string, string>` cannot have individual keys removed).
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```ts
|
|
58
|
+
* const user = await this.auth.userOrFail<Sanitized<UserRecord>>()
|
|
59
|
+
* user.passwordHash // compile error — stripped at runtime
|
|
60
|
+
*
|
|
61
|
+
* // With additional hidden fields or custom column names:
|
|
62
|
+
* type SafeUser = Sanitized<UserRecord, 'twoFactorSecret' | 'credentialDigest'>
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
export type Sanitized<User, Hidden extends string = never> = User extends unknown ? Omit<User, Extract<keyof User, DefaultSanitizedKeys | Hidden>> : never;
|
|
66
|
+
export interface GuardContext {
|
|
67
|
+
ctx: Context;
|
|
68
|
+
session: Session | undefined;
|
|
69
|
+
manager: AuthManagerContract;
|
|
70
|
+
}
|
|
71
|
+
export type GuardFactory<User = Authenticatable> = (context: GuardContext) => Guard<User>;
|
|
72
|
+
export interface ProviderFactory<User = Authenticatable> {
|
|
73
|
+
(manager: AuthManagerContract): UserProvider<User>;
|
|
74
|
+
}
|
|
75
|
+
export interface AuthManagerOptions {
|
|
76
|
+
defaultGuard?: string;
|
|
77
|
+
}
|
|
78
|
+
export interface AttachContextOptions {
|
|
79
|
+
guard?: string;
|
|
80
|
+
}
|
|
81
|
+
export interface AuthContext<User = Authenticatable> {
|
|
82
|
+
check(): Promise<boolean>;
|
|
83
|
+
guest(): Promise<boolean>;
|
|
84
|
+
user<T = User>(): Promise<T | null>;
|
|
85
|
+
userOrFail<T = User>(): Promise<T>;
|
|
86
|
+
id(): Promise<unknown>;
|
|
87
|
+
login<T = User>(user: T, remember?: boolean): Promise<void>;
|
|
88
|
+
attempt(credentials: AuthCredentials, remember?: boolean): Promise<boolean>;
|
|
89
|
+
logout(): Promise<void>;
|
|
90
|
+
guard<T = User>(name?: string): Guard<T>;
|
|
91
|
+
session<T extends Session = Session>(): T | undefined;
|
|
92
|
+
}
|
|
93
|
+
export interface AuthManagerContract {
|
|
94
|
+
registerGuard<User = Authenticatable>(name: string, factory: GuardFactory<User>): void;
|
|
95
|
+
registerProvider<User = Authenticatable>(name: string, factory: ProviderFactory<User>): void;
|
|
96
|
+
getProvider<User = Authenticatable>(name: string): UserProvider<User>;
|
|
97
|
+
createGuard<User = Authenticatable>(name: string, context: GuardContext): Guard<User>;
|
|
98
|
+
guardNames(): string[];
|
|
99
|
+
setDefaultGuard(name: string): void;
|
|
100
|
+
getDefaultGuard(): string;
|
|
101
|
+
createAuthContext(ctx: Context, options?: AttachContextOptions): AuthContext;
|
|
102
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hash a token using SHA-256 or SHA-512.
|
|
3
|
+
*/
|
|
4
|
+
export declare function hashToken(token: string, algorithm?: 'sha256' | 'sha512'): string;
|
|
5
|
+
/**
|
|
6
|
+
* Generate a secure random token.
|
|
7
|
+
*/
|
|
8
|
+
export declare function generateToken(length?: number): string;
|
|
9
|
+
/**
|
|
10
|
+
* Generate a random ID (16 bytes = 32 hex chars).
|
|
11
|
+
*/
|
|
12
|
+
export declare function generateId(): string;
|
|
13
|
+
/**
|
|
14
|
+
* Securely compare two hex strings using timing-safe comparison.
|
|
15
|
+
*
|
|
16
|
+
* Input that is not strict, even-length hex is rejected rather than compared:
|
|
17
|
+
* `Buffer.from(value, 'hex')` stops at the first invalid pair, so 'zzzz' and
|
|
18
|
+
* 'yyyy' both decode to nothing — and 'abcz' and 'abdz' both decode to the
|
|
19
|
+
* one byte they share — and would compare equal. A short decode is how the
|
|
20
|
+
* decoder reports that, which beats restating its grammar in a regex here.
|
|
21
|
+
* Use `secureStringCompare` for values that are not hex.
|
|
22
|
+
*/
|
|
23
|
+
export declare function secureCompare(a: string, b: string): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Securely compare two arbitrary strings using timing-safe comparison.
|
|
26
|
+
* Unlike secureCompare, this works with any string encoding (UUIDs, tokens, etc.).
|
|
27
|
+
*/
|
|
28
|
+
export declare function secureStringCompare(a: string, b: string): boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Build a URL with token and optional email parameters.
|
|
31
|
+
*/
|
|
32
|
+
export declare function buildTokenUrl(baseUrl: string, token: string, email?: string): string;
|
|
33
|
+
/**
|
|
34
|
+
* Parse a URL to extract token and email parameters.
|
|
35
|
+
*/
|
|
36
|
+
export declare function parseTokenUrl(url: string): {
|
|
37
|
+
token: string | null;
|
|
38
|
+
email: string | null;
|
|
39
|
+
};
|
|
@@ -1,109 +1,21 @@
|
|
|
1
|
-
import { Context } from '
|
|
2
|
-
|
|
1
|
+
import type { Context } from '../http/Application.js';
|
|
2
|
+
import type { AuthUser, GateCallback, GateBeforeCallback, GateDefinition, GateOptions, PolicyClass, Policy, AuthorizationResponse, ResponseBuilder } from './types.js';
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
*/
|
|
6
|
-
interface AuthUser {
|
|
7
|
-
id: string | number;
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* Gate callback function.
|
|
11
|
-
*/
|
|
12
|
-
type GateCallback<Args extends unknown[] = unknown[]> = {
|
|
13
|
-
bivarianceHack(user: AuthUser | null, ...args: Args): boolean | Promise<boolean>;
|
|
14
|
-
}['bivarianceHack'];
|
|
15
|
-
/**
|
|
16
|
-
* Gate before callbacks can short-circuit authorization.
|
|
17
|
-
* Return true to allow, false to deny, or undefined to continue.
|
|
18
|
-
*/
|
|
19
|
-
type GateBeforeCallback<Args extends unknown[] = unknown[]> = {
|
|
20
|
-
bivarianceHack(user: AuthUser | null, ability: string, ...args: Args): boolean | undefined | Promise<boolean | undefined>;
|
|
21
|
-
}['bivarianceHack'];
|
|
22
|
-
/**
|
|
23
|
-
* Gate definition with optional callback.
|
|
24
|
-
*/
|
|
25
|
-
interface GateDefinition {
|
|
26
|
-
callback: GateCallback;
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Policy method type.
|
|
30
|
-
*/
|
|
31
|
-
type PolicyMethod<T = unknown> = (user: AuthUser | null, model?: T, ...args: unknown[]) => boolean | Promise<boolean>;
|
|
32
|
-
/**
|
|
33
|
-
* Policy class interface.
|
|
34
|
-
*/
|
|
35
|
-
interface Policy {
|
|
36
|
-
/**
|
|
37
|
-
* Run before all other authorization checks.
|
|
38
|
-
* Return true to allow, false to deny, undefined to continue to specific check.
|
|
39
|
-
*/
|
|
40
|
-
before?(user: AuthUser | null, ability: string): boolean | undefined | Promise<boolean | undefined>;
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Policy class constructor.
|
|
44
|
-
*/
|
|
45
|
-
interface PolicyClass {
|
|
46
|
-
new (): Policy;
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Authorization response.
|
|
50
|
-
*/
|
|
51
|
-
interface AuthorizationResponse {
|
|
52
|
-
allowed: boolean;
|
|
53
|
-
message?: string;
|
|
54
|
-
code?: string;
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Gate options.
|
|
58
|
-
*/
|
|
59
|
-
interface GateOptions {
|
|
60
|
-
/**
|
|
61
|
-
* Default user resolver from context.
|
|
62
|
-
*/
|
|
63
|
-
userResolver?: (ctx: Context) => AuthUser | null | Promise<AuthUser | null>;
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Authorization check options.
|
|
67
|
-
*/
|
|
68
|
-
interface AuthorizeOptions {
|
|
69
|
-
/**
|
|
70
|
-
* Custom message for denial.
|
|
71
|
-
*/
|
|
72
|
-
message?: string;
|
|
73
|
-
/**
|
|
74
|
-
* HTTP status code for denial.
|
|
75
|
-
*/
|
|
76
|
-
status?: number;
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* Policy registration.
|
|
4
|
+
* Response builder for authorization checks.
|
|
80
5
|
*/
|
|
81
|
-
|
|
82
|
-
modelClass: unknown;
|
|
83
|
-
policyClass: PolicyClass;
|
|
84
|
-
}
|
|
6
|
+
export declare const Response: ResponseBuilder;
|
|
85
7
|
/**
|
|
86
|
-
*
|
|
8
|
+
* Narrow a policy/gate return value to an `AuthorizationResponse`.
|
|
9
|
+
*
|
|
10
|
+
* Policies may return either a boolean or one of the `Response` objects
|
|
11
|
+
* produced by `allow()`/`deny()`/`denyWithStatus()`/`denyAsNotFound()`.
|
|
87
12
|
*/
|
|
88
|
-
|
|
13
|
+
export declare function isAuthorizationResponse(value: unknown): value is AuthorizationResponse;
|
|
89
14
|
/**
|
|
90
|
-
*
|
|
15
|
+
* Build the exception for a denial, honouring `denyWithStatus()` /
|
|
16
|
+
* `denyAsNotFound()` so a policy can hide a record as a 404.
|
|
91
17
|
*/
|
|
92
|
-
|
|
93
|
-
allow(message?: string): AuthorizationResponse;
|
|
94
|
-
deny(message?: string, code?: string): AuthorizationResponse;
|
|
95
|
-
denyWithStatus(status: number, message?: string): AuthorizationResponse & {
|
|
96
|
-
status: number;
|
|
97
|
-
};
|
|
98
|
-
denyAsNotFound(message?: string): AuthorizationResponse & {
|
|
99
|
-
status: 404;
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Response builder for authorization checks.
|
|
105
|
-
*/
|
|
106
|
-
declare const Response: ResponseBuilder;
|
|
18
|
+
export declare function denialToException(response: AuthorizationResponse): Error;
|
|
107
19
|
/**
|
|
108
20
|
* Authorization gate for defining and checking abilities.
|
|
109
21
|
*
|
|
@@ -124,7 +36,7 @@ declare const Response: ResponseBuilder;
|
|
|
124
36
|
* await gate.authorize('edit-post', post) // throws if not allowed
|
|
125
37
|
* ```
|
|
126
38
|
*/
|
|
127
|
-
declare class Gate {
|
|
39
|
+
export declare class Gate {
|
|
128
40
|
/**
|
|
129
41
|
* Defined gates.
|
|
130
42
|
*/
|
|
@@ -211,6 +123,15 @@ declare class Gate {
|
|
|
211
123
|
* Check the ability with a specific user.
|
|
212
124
|
*/
|
|
213
125
|
check(ability: string, user: AuthUser | null, ...args: unknown[]): Promise<boolean>;
|
|
126
|
+
/**
|
|
127
|
+
* Check the ability with a specific user and keep the full response.
|
|
128
|
+
*
|
|
129
|
+
* Policies may answer with a boolean or with a `Response` object. Both
|
|
130
|
+
* shapes are normalized here so no caller has to truthy-test a
|
|
131
|
+
* `{ allowed: false }` object.
|
|
132
|
+
*/
|
|
133
|
+
checkResponse(ability: string, user: AuthUser | null, ...args: unknown[]): Promise<AuthorizationResponse>;
|
|
134
|
+
protected settle(user: AuthUser | null, ability: string, response: AuthorizationResponse, args: unknown[]): Promise<AuthorizationResponse>;
|
|
214
135
|
/**
|
|
215
136
|
* Check a policy for the given ability.
|
|
216
137
|
*
|
|
@@ -219,7 +140,7 @@ declare class Gate {
|
|
|
219
140
|
* required for plain records returned by the ORM, which carry no constructor
|
|
220
141
|
* information.
|
|
221
142
|
*/
|
|
222
|
-
protected checkPolicy(ability: string, user: AuthUser | null, model: unknown, additionalArgs: unknown[]): Promise<boolean | undefined>;
|
|
143
|
+
protected checkPolicy(ability: string, user: AuthUser | null, model: unknown, additionalArgs: unknown[]): Promise<boolean | AuthorizationResponse | undefined>;
|
|
223
144
|
/**
|
|
224
145
|
* Get or create a policy instance.
|
|
225
146
|
*/
|
|
@@ -244,30 +165,28 @@ declare class Gate {
|
|
|
244
165
|
/**
|
|
245
166
|
* Create a new gate instance.
|
|
246
167
|
*/
|
|
247
|
-
declare function createGate(options?: GateOptions): Gate;
|
|
168
|
+
export declare function createGate(options?: GateOptions): Gate;
|
|
248
169
|
/**
|
|
249
170
|
* Set the global gate instance.
|
|
250
171
|
*/
|
|
251
|
-
declare function setGate(gate: Gate): void;
|
|
172
|
+
export declare function setGate(gate: Gate): void;
|
|
252
173
|
/**
|
|
253
174
|
* Get the global gate instance.
|
|
254
175
|
*/
|
|
255
|
-
declare function getGate(): Gate;
|
|
176
|
+
export declare function getGate(): Gate;
|
|
256
177
|
/**
|
|
257
178
|
* Define a gate on the global instance.
|
|
258
179
|
*/
|
|
259
|
-
declare function defineGate(ability: string, callback: GateCallback): void;
|
|
180
|
+
export declare function defineGate(ability: string, callback: GateCallback): void;
|
|
260
181
|
/**
|
|
261
182
|
* Check if the current user can perform an ability.
|
|
262
183
|
*/
|
|
263
|
-
declare function can(ability: string, ...args: unknown[]): Promise<boolean>;
|
|
184
|
+
export declare function can(ability: string, ...args: unknown[]): Promise<boolean>;
|
|
264
185
|
/**
|
|
265
186
|
* Check if the current user cannot perform an ability.
|
|
266
187
|
*/
|
|
267
|
-
declare function cannot(ability: string, ...args: unknown[]): Promise<boolean>;
|
|
188
|
+
export declare function cannot(ability: string, ...args: unknown[]): Promise<boolean>;
|
|
268
189
|
/**
|
|
269
190
|
* Authorize or throw.
|
|
270
191
|
*/
|
|
271
|
-
declare function authorize(ability: string, ...args: unknown[]): Promise<void>;
|
|
272
|
-
|
|
273
|
-
export { type AuthUser as A, Gate as G, type PolicyClass as P, Response as R, type AuthorizationResponse as a, type AuthorizeOptions as b, type GateCallback as c, type GateDefinition as d, type GateOptions as e, type Policy as f, type PolicyMethod as g, type PolicyRegistration as h, type ResourceAction as i, type ResponseBuilder as j, authorize as k, can as l, cannot as m, createGate as n, defineGate as o, getGate as p, setGate as s };
|
|
192
|
+
export declare function authorize(ability: string, ...args: unknown[]): Promise<void>;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { AuthUser, Policy as PolicyInterface, AuthorizationResponse, PolicyResult } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Base policy class for model authorization.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```typescript
|
|
7
|
+
* class PostPolicy extends Policy {
|
|
8
|
+
* // Runs before all checks - return true/false to short-circuit
|
|
9
|
+
* before(user: AuthUser | null, ability: string) {
|
|
10
|
+
* if (user?.role === 'admin') {
|
|
11
|
+
* return true // Admins can do anything
|
|
12
|
+
* }
|
|
13
|
+
* return undefined // Continue to specific check
|
|
14
|
+
* }
|
|
15
|
+
*
|
|
16
|
+
* viewAny(user: AuthUser | null) {
|
|
17
|
+
* return true // Anyone can view posts list
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
* view(user: AuthUser | null, post: Post) {
|
|
21
|
+
* return post.published || user?.id === post.authorId
|
|
22
|
+
* }
|
|
23
|
+
*
|
|
24
|
+
* create(user: AuthUser | null) {
|
|
25
|
+
* return user !== null
|
|
26
|
+
* }
|
|
27
|
+
*
|
|
28
|
+
* update(user: AuthUser | null, post: Post) {
|
|
29
|
+
* return user?.id === post.authorId
|
|
30
|
+
* }
|
|
31
|
+
*
|
|
32
|
+
* delete(user: AuthUser | null, post: Post) {
|
|
33
|
+
* return user?.id === post.authorId
|
|
34
|
+
* }
|
|
35
|
+
* }
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export declare abstract class Policy implements PolicyInterface {
|
|
39
|
+
/**
|
|
40
|
+
* Perform pre-authorization checks.
|
|
41
|
+
* Return true to allow, false to deny, undefined to continue to specific check.
|
|
42
|
+
*/
|
|
43
|
+
before?(user: AuthUser | null, ability: string): PolicyResult | undefined | Promise<PolicyResult | undefined>;
|
|
44
|
+
/**
|
|
45
|
+
* Allow the action.
|
|
46
|
+
*/
|
|
47
|
+
protected allow(message?: string): AuthorizationResponse;
|
|
48
|
+
/**
|
|
49
|
+
* Deny the action.
|
|
50
|
+
*/
|
|
51
|
+
protected deny(message?: string, code?: string): AuthorizationResponse;
|
|
52
|
+
/**
|
|
53
|
+
* Deny with a specific HTTP status.
|
|
54
|
+
*/
|
|
55
|
+
protected denyWithStatus(status: number, message?: string): AuthorizationResponse & {
|
|
56
|
+
status: number;
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Deny as not found (404).
|
|
60
|
+
*/
|
|
61
|
+
protected denyAsNotFound(message?: string): AuthorizationResponse & {
|
|
62
|
+
status: 404;
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Create a simple policy from an object.
|
|
67
|
+
*/
|
|
68
|
+
export declare function definePolicy<T>(definition: {
|
|
69
|
+
before?: (user: AuthUser | null, ability: string) => PolicyResult | undefined | Promise<PolicyResult | undefined>;
|
|
70
|
+
viewAny?: (user: AuthUser | null) => PolicyResult | Promise<PolicyResult>;
|
|
71
|
+
view?: (user: AuthUser | null, model: T) => PolicyResult | Promise<PolicyResult>;
|
|
72
|
+
create?: (user: AuthUser | null) => PolicyResult | Promise<PolicyResult>;
|
|
73
|
+
update?: (user: AuthUser | null, model: T) => PolicyResult | Promise<PolicyResult>;
|
|
74
|
+
delete?: (user: AuthUser | null, model: T) => PolicyResult | Promise<PolicyResult>;
|
|
75
|
+
restore?: (user: AuthUser | null, model: T) => PolicyResult | Promise<PolicyResult>;
|
|
76
|
+
forceDelete?: (user: AuthUser | null, model: T) => PolicyResult | Promise<PolicyResult>;
|
|
77
|
+
} & Record<string, ((user: AuthUser | null, ...args: any[]) => PolicyResult | undefined | Promise<PolicyResult | undefined>) | undefined>): new () => Policy;
|
|
@@ -1,129 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
export {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Base policy class for model authorization.
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* ```typescript
|
|
11
|
-
* class PostPolicy extends Policy {
|
|
12
|
-
* // Runs before all checks - return true/false to short-circuit
|
|
13
|
-
* before(user: AuthUser | null, ability: string) {
|
|
14
|
-
* if (user?.role === 'admin') {
|
|
15
|
-
* return true // Admins can do anything
|
|
16
|
-
* }
|
|
17
|
-
* return undefined // Continue to specific check
|
|
18
|
-
* }
|
|
19
|
-
*
|
|
20
|
-
* viewAny(user: AuthUser | null) {
|
|
21
|
-
* return true // Anyone can view posts list
|
|
22
|
-
* }
|
|
23
|
-
*
|
|
24
|
-
* view(user: AuthUser | null, post: Post) {
|
|
25
|
-
* return post.published || user?.id === post.authorId
|
|
26
|
-
* }
|
|
27
|
-
*
|
|
28
|
-
* create(user: AuthUser | null) {
|
|
29
|
-
* return user !== null
|
|
30
|
-
* }
|
|
31
|
-
*
|
|
32
|
-
* update(user: AuthUser | null, post: Post) {
|
|
33
|
-
* return user?.id === post.authorId
|
|
34
|
-
* }
|
|
35
|
-
*
|
|
36
|
-
* delete(user: AuthUser | null, post: Post) {
|
|
37
|
-
* return user?.id === post.authorId
|
|
38
|
-
* }
|
|
39
|
-
* }
|
|
40
|
-
* ```
|
|
41
|
-
*/
|
|
42
|
-
declare abstract class Policy implements Policy$1 {
|
|
43
|
-
/**
|
|
44
|
-
* Perform pre-authorization checks.
|
|
45
|
-
* Return true to allow, false to deny, undefined to continue to specific check.
|
|
46
|
-
*/
|
|
47
|
-
before?(user: AuthUser | null, ability: string): boolean | undefined | Promise<boolean | undefined>;
|
|
48
|
-
/**
|
|
49
|
-
* Allow the action.
|
|
50
|
-
*/
|
|
51
|
-
protected allow(message?: string): AuthorizationResponse;
|
|
52
|
-
/**
|
|
53
|
-
* Deny the action.
|
|
54
|
-
*/
|
|
55
|
-
protected deny(message?: string, code?: string): AuthorizationResponse;
|
|
56
|
-
/**
|
|
57
|
-
* Deny with a specific HTTP status.
|
|
58
|
-
*/
|
|
59
|
-
protected denyWithStatus(status: number, message?: string): AuthorizationResponse & {
|
|
60
|
-
status: number;
|
|
61
|
-
};
|
|
62
|
-
/**
|
|
63
|
-
* Deny as not found (404).
|
|
64
|
-
*/
|
|
65
|
-
protected denyAsNotFound(message?: string): AuthorizationResponse & {
|
|
66
|
-
status: 404;
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Create a simple policy from an object.
|
|
71
|
-
*/
|
|
72
|
-
declare function definePolicy<T>(definition: {
|
|
73
|
-
before?: (user: AuthUser | null, ability: string) => boolean | undefined | Promise<boolean | undefined>;
|
|
74
|
-
viewAny?: (user: AuthUser | null) => boolean | Promise<boolean>;
|
|
75
|
-
view?: (user: AuthUser | null, model: T) => boolean | Promise<boolean>;
|
|
76
|
-
create?: (user: AuthUser | null) => boolean | Promise<boolean>;
|
|
77
|
-
update?: (user: AuthUser | null, model: T) => boolean | Promise<boolean>;
|
|
78
|
-
delete?: (user: AuthUser | null, model: T) => boolean | Promise<boolean>;
|
|
79
|
-
restore?: (user: AuthUser | null, model: T) => boolean | Promise<boolean>;
|
|
80
|
-
forceDelete?: (user: AuthUser | null, model: T) => boolean | Promise<boolean>;
|
|
81
|
-
} & Record<string, ((user: AuthUser | null, ...args: any[]) => boolean | undefined | Promise<boolean | undefined>) | undefined>): new () => Policy;
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Middleware to authorize an ability.
|
|
85
|
-
*
|
|
86
|
-
* @example
|
|
87
|
-
* ```typescript
|
|
88
|
-
* // Authorize a simple ability
|
|
89
|
-
* app.get('/admin', authorizeMiddleware('admin'), adminHandler)
|
|
90
|
-
*
|
|
91
|
-
* // Authorize with model (from route param)
|
|
92
|
-
* app.put('/posts/:id', authorizeMiddleware('update', getPost), updateHandler)
|
|
93
|
-
*
|
|
94
|
-
* // Multiple abilities (any)
|
|
95
|
-
* app.get('/dashboard', authorizeMiddleware(['admin', 'moderator']), dashboardHandler)
|
|
96
|
-
* ```
|
|
97
|
-
*/
|
|
98
|
-
declare function authorizeMiddleware(ability: string | string[], modelResolver?: (ctx: Context) => unknown | Promise<unknown>, options?: AuthorizeOptions): Middleware;
|
|
99
|
-
/**
|
|
100
|
-
* Middleware to authorize all given abilities.
|
|
101
|
-
*/
|
|
102
|
-
declare function authorizeAllMiddleware(abilities: string[], modelResolver?: (ctx: Context) => unknown | Promise<unknown>, options?: AuthorizeOptions): Middleware;
|
|
103
|
-
/**
|
|
104
|
-
* Middleware factory for resource authorization.
|
|
105
|
-
* Automatically maps HTTP methods to policy abilities.
|
|
106
|
-
*
|
|
107
|
-
* @example
|
|
108
|
-
* ```typescript
|
|
109
|
-
* // Authorize resource actions based on HTTP method
|
|
110
|
-
* app.use('/posts/:id', authorizeResourceMiddleware(getPost))
|
|
111
|
-
* // GET -> view, POST -> create, PUT/PATCH -> update, DELETE -> delete
|
|
112
|
-
* ```
|
|
113
|
-
*/
|
|
114
|
-
declare function authorizeResourceMiddleware(modelResolver: (ctx: Context) => unknown | Promise<unknown>, options?: AuthorizeOptions): Middleware;
|
|
115
|
-
/**
|
|
116
|
-
* Create a gate-aware context with authorization helpers.
|
|
117
|
-
*/
|
|
118
|
-
declare function withAuthorization(gate: Gate): Middleware;
|
|
119
|
-
/**
|
|
120
|
-
* Helper type for context with authorization.
|
|
121
|
-
*/
|
|
122
|
-
interface AuthorizedContext extends Context {
|
|
123
|
-
get(key: 'gate'): Gate;
|
|
124
|
-
get(key: 'can'): (ability: string, ...args: unknown[]) => Promise<boolean>;
|
|
125
|
-
get(key: 'cannot'): (ability: string, ...args: unknown[]) => Promise<boolean>;
|
|
126
|
-
get(key: 'authorize'): (ability: string, ...args: unknown[]) => Promise<void>;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
export { AuthUser, AuthorizationResponse, AuthorizeOptions, type AuthorizedContext, Gate, Policy, Policy$1 as PolicyInterface, authorizeAllMiddleware, authorizeMiddleware, authorizeResourceMiddleware, definePolicy, withAuthorization };
|
|
1
|
+
export type { AuthUser, GateCallback, GateDefinition, PolicyMethod, PolicyResult, Policy as PolicyInterface, PolicyClass, AuthorizationResponse, GateOptions, AuthorizeOptions, PolicyRegistration, ResourceAction, ResponseBuilder, } from './types.js';
|
|
2
|
+
export { Gate, Response, isAuthorizationResponse, createGate, setGate, getGate, defineGate, can, cannot, authorize, } from './Gate.js';
|
|
3
|
+
export { Policy, definePolicy } from './Policy.js';
|
|
4
|
+
export { authorizeMiddleware, authorizeAllMiddleware, authorizeResourceMiddleware, withAuthorization, } from './middleware.js';
|
|
5
|
+
export type { AuthorizedContext } from './middleware.js';
|
|
@@ -12,9 +12,10 @@ import {
|
|
|
12
12
|
defineGate,
|
|
13
13
|
definePolicy,
|
|
14
14
|
getGate,
|
|
15
|
+
isAuthorizationResponse,
|
|
15
16
|
setGate,
|
|
16
17
|
withAuthorization
|
|
17
|
-
} from "../chunk-
|
|
18
|
+
} from "../chunk-7OB2YXRE.js";
|
|
18
19
|
import "../chunk-63SKW5PS.js";
|
|
19
20
|
import "../chunk-DAQKYKLH.js";
|
|
20
21
|
export {
|
|
@@ -31,6 +32,7 @@ export {
|
|
|
31
32
|
defineGate,
|
|
32
33
|
definePolicy,
|
|
33
34
|
getGate,
|
|
35
|
+
isAuthorizationResponse,
|
|
34
36
|
setGate,
|
|
35
37
|
withAuthorization
|
|
36
38
|
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { Context } from '../http/Application.js';
|
|
2
|
+
import type { Middleware } from '../http/middleware/index.js';
|
|
3
|
+
import type { AuthorizeOptions } from './types.js';
|
|
4
|
+
import { Gate } from './Gate.js';
|
|
5
|
+
/**
|
|
6
|
+
* Middleware to authorize an ability.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* // Authorize a simple ability
|
|
11
|
+
* app.get('/admin', authorizeMiddleware('admin'), adminHandler)
|
|
12
|
+
*
|
|
13
|
+
* // Authorize with model (from route param)
|
|
14
|
+
* app.put('/posts/:id', authorizeMiddleware('update', getPost), updateHandler)
|
|
15
|
+
*
|
|
16
|
+
* // Multiple abilities (any)
|
|
17
|
+
* app.get('/dashboard', authorizeMiddleware(['admin', 'moderator']), dashboardHandler)
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare function authorizeMiddleware(ability: string | string[], modelResolver?: (ctx: Context) => unknown | Promise<unknown>, options?: AuthorizeOptions): Middleware;
|
|
21
|
+
/**
|
|
22
|
+
* Middleware to authorize all given abilities.
|
|
23
|
+
*/
|
|
24
|
+
export declare function authorizeAllMiddleware(abilities: string[], modelResolver?: (ctx: Context) => unknown | Promise<unknown>, options?: AuthorizeOptions): Middleware;
|
|
25
|
+
/**
|
|
26
|
+
* Middleware factory for resource authorization.
|
|
27
|
+
* Automatically maps HTTP methods to policy abilities.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* // Authorize resource actions based on HTTP method
|
|
32
|
+
* app.use('/posts/:id', authorizeResourceMiddleware(getPost))
|
|
33
|
+
* // GET -> view, POST -> create, PUT/PATCH -> update, DELETE -> delete
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare function authorizeResourceMiddleware(modelResolver: (ctx: Context) => unknown | Promise<unknown>, options?: AuthorizeOptions): Middleware;
|
|
37
|
+
/**
|
|
38
|
+
* Create a gate-aware context with authorization helpers.
|
|
39
|
+
*/
|
|
40
|
+
export declare function withAuthorization(gate: Gate): Middleware;
|
|
41
|
+
/**
|
|
42
|
+
* Helper type for context with authorization.
|
|
43
|
+
*/
|
|
44
|
+
export interface AuthorizedContext extends Context {
|
|
45
|
+
get(key: 'gate'): Gate;
|
|
46
|
+
get(key: 'can'): (ability: string, ...args: unknown[]) => Promise<boolean>;
|
|
47
|
+
get(key: 'cannot'): (ability: string, ...args: unknown[]) => Promise<boolean>;
|
|
48
|
+
get(key: 'authorize'): (ability: string, ...args: unknown[]) => Promise<void>;
|
|
49
|
+
}
|