@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.
Files changed (270) hide show
  1. package/dist/{McpServiceProvider-7BB5FPF3.js → McpServiceProvider-GRDYNEQG.js} +1 -1
  2. package/dist/auth/AuthManager.d.ts +30 -0
  3. package/dist/auth/AuthenticatableModel.d.ts +25 -0
  4. package/dist/auth/RequestAuthContext.d.ts +24 -0
  5. package/dist/auth/SessionGuard.d.ts +31 -0
  6. package/dist/auth/api-token.d.ts +299 -0
  7. package/dist/auth/email-verification.d.ts +203 -0
  8. package/dist/auth/index.d.ts +18 -110
  9. package/dist/auth/index.js +3 -1
  10. package/dist/auth/oauth/index.d.ts +239 -0
  11. package/dist/auth/password/DefaultHasher.d.ts +13 -0
  12. package/dist/auth/password/NodeHasher.d.ts +22 -0
  13. package/dist/auth/password/PasswordHasher.d.ts +5 -0
  14. package/dist/auth/password/ScryptHasher.d.ts +26 -0
  15. package/dist/auth/password-reset.d.ts +91 -0
  16. package/dist/auth/providers/ModelUserProvider.d.ts +36 -0
  17. package/dist/auth/providers/UserProvider.d.ts +9 -0
  18. package/dist/auth/types.d.ts +102 -0
  19. package/dist/auth/utils.d.ts +39 -0
  20. package/dist/{Gate-CynjZCtS.d.ts → authorization/Gate.d.ts} +30 -111
  21. package/dist/authorization/Policy.d.ts +77 -0
  22. package/dist/authorization/index.d.ts +5 -129
  23. package/dist/authorization/index.js +3 -1
  24. package/dist/authorization/middleware.d.ts +49 -0
  25. package/dist/authorization/types.d.ts +108 -0
  26. package/dist/broadcasting/BroadcastManager.d.ts +205 -0
  27. package/dist/broadcasting/channels/Channel.d.ts +28 -0
  28. package/dist/broadcasting/channels/PresenceChannel.d.ts +72 -0
  29. package/dist/broadcasting/channels/PrivateChannel.d.ts +32 -0
  30. package/dist/broadcasting/channels/index.d.ts +3 -0
  31. package/dist/broadcasting/drivers/MemoryDriver.d.ts +93 -0
  32. package/dist/broadcasting/drivers/RedisDriver.d.ts +112 -0
  33. package/dist/broadcasting/drivers/index.d.ts +2 -0
  34. package/dist/broadcasting/index.d.ts +6 -233
  35. package/dist/broadcasting/index.js +1 -1
  36. package/dist/broadcasting/typed.d.ts +23 -0
  37. package/dist/broadcasting/types.d.ts +155 -0
  38. package/dist/cache/CacheManager.d.ts +87 -0
  39. package/dist/cache/TaggedCache.d.ts +62 -0
  40. package/dist/cache/index.d.ts +6 -233
  41. package/dist/cache/index.js +1 -1
  42. package/dist/cache/stores/FileStore.d.ts +64 -0
  43. package/dist/cache/stores/MemoryStore.d.ts +52 -0
  44. package/dist/cache/stores/RedisStore.d.ts +53 -0
  45. package/dist/{CacheManager-BmDzUKrF.d.ts → cache/types.d.ts} +26 -99
  46. package/dist/{chunk-TVY72CLV.js → chunk-4VE3LYOM.js} +3 -2
  47. package/dist/{chunk-FZD2AKJI.js → chunk-7OB2YXRE.js} +56 -28
  48. package/dist/{chunk-HONCNQXZ.js → chunk-BUQXOZCT.js} +97 -10
  49. package/dist/{chunk-DSDX3ASM.js → chunk-CDZUNKI6.js} +65 -83
  50. package/dist/{chunk-CWI4AJ47.js → chunk-HPET6XY5.js} +13 -9
  51. package/dist/{chunk-A3ISJVEV.js → chunk-HR2KZ3WL.js} +23 -5
  52. package/dist/{chunk-EARA7JM4.js → chunk-PZKUDFMW.js} +11 -8
  53. package/dist/console/Command.d.ts +179 -0
  54. package/dist/{ConsoleKernel-CJg975mp.d.ts → console/ConsoleKernel.d.ts} +4 -6
  55. package/dist/console/Input.d.ts +140 -0
  56. package/dist/console/Output.d.ts +161 -0
  57. package/dist/console/index.d.ts +5 -0
  58. package/dist/console/types.d.ts +106 -0
  59. package/dist/container/Container.d.ts +148 -0
  60. package/dist/container/ServiceProvider.d.ts +103 -0
  61. package/dist/container/bindings.d.ts +55 -0
  62. package/dist/container/defineModule.d.ts +88 -0
  63. package/dist/container/definePlugin.d.ts +62 -0
  64. package/dist/container/index.d.ts +8 -0
  65. package/dist/container/types.d.ts +73 -0
  66. package/dist/database/Factory.d.ts +82 -0
  67. package/dist/database/Seeder.d.ts +35 -0
  68. package/dist/database/SeederRunner.d.ts +41 -0
  69. package/dist/database/index.d.ts +4 -0
  70. package/dist/database/types.d.ts +49 -0
  71. package/dist/discovery/AutoDiscovery.d.ts +113 -0
  72. package/dist/discovery/index.d.ts +2 -0
  73. package/dist/docs-viewer/DocsViewerServiceProvider.d.ts +12 -0
  74. package/dist/docs-viewer/endpoint.d.ts +19 -0
  75. package/dist/encryption/Encrypter.d.ts +96 -0
  76. package/dist/encryption/Hash.d.ts +69 -0
  77. package/dist/encryption/MessageSigner.d.ts +21 -0
  78. package/dist/encryption/Random.d.ts +119 -0
  79. package/dist/encryption/app-key.d.ts +12 -0
  80. package/dist/encryption/index.d.ts +10 -222
  81. package/dist/encryption/signed-url.d.ts +9 -0
  82. package/dist/encryption/types.d.ts +103 -0
  83. package/dist/errors/ExceptionHandler.d.ts +118 -0
  84. package/dist/errors/HttpException.d.ts +106 -0
  85. package/dist/errors/debug-page.d.ts +22 -0
  86. package/dist/errors/error-page.d.ts +1 -0
  87. package/dist/errors/exceptions/AuthenticationException.d.ts +32 -0
  88. package/dist/errors/exceptions/AuthorizationException.d.ts +32 -0
  89. package/dist/errors/exceptions/MethodNotAllowedException.d.ts +31 -0
  90. package/dist/errors/exceptions/NotFoundHttpException.d.ts +36 -0
  91. package/dist/errors/exceptions/ValidationException.d.ts +60 -0
  92. package/dist/errors/exceptions/index.d.ts +5 -0
  93. package/dist/errors/index.d.ts +5 -0
  94. package/dist/errors/types.d.ts +43 -0
  95. package/dist/events/Event.d.ts +33 -0
  96. package/dist/{EventManager-CmIoLt7r.d.ts → events/EventManager.d.ts} +4 -86
  97. package/dist/events/Listener.d.ts +79 -0
  98. package/dist/events/builtin.d.ts +73 -0
  99. package/dist/events/index.d.ts +6 -155
  100. package/dist/events/types.d.ts +48 -0
  101. package/dist/facades/index.d.ts +31 -0
  102. package/dist/health/HealthCheck.d.ts +34 -0
  103. package/dist/health/HealthManager.d.ts +53 -0
  104. package/dist/health/checks/CacheCheck.d.ts +35 -0
  105. package/dist/health/checks/CustomCheck.d.ts +23 -0
  106. package/dist/health/checks/DatabaseCheck.d.ts +33 -0
  107. package/dist/health/checks/MemoryCheck.d.ts +32 -0
  108. package/dist/health/checks/RedisCheck.d.ts +27 -0
  109. package/dist/health/checks/StorageCheck.d.ts +35 -0
  110. package/dist/health/checks/index.d.ts +12 -0
  111. package/dist/health/index.d.ts +5 -185
  112. package/dist/health/types.d.ts +52 -0
  113. package/dist/hot-reload/hot-disposables.d.ts +96 -0
  114. package/dist/hot-reload/testing.d.ts +14 -0
  115. package/dist/http/Application.d.ts +187 -0
  116. package/dist/http/FormRequest.d.ts +73 -0
  117. package/dist/http/dev-assets.d.ts +51 -0
  118. package/dist/http/dev-banner.d.ts +7 -0
  119. package/dist/http/inertia-assets.d.ts +22 -0
  120. package/dist/http/middleware/auth.d.ts +13 -0
  121. package/dist/http/middleware/capabilities.d.ts +22 -0
  122. package/dist/http/middleware/cors.d.ts +40 -0
  123. package/dist/http/middleware/csp.d.ts +59 -0
  124. package/dist/http/middleware/csrf.d.ts +121 -0
  125. package/dist/http/middleware/detect-locale.d.ts +65 -0
  126. package/dist/http/middleware/force-https.d.ts +34 -0
  127. package/dist/http/middleware/host-authorization.d.ts +24 -0
  128. package/dist/http/middleware/index.d.ts +19 -0
  129. package/dist/http/middleware/loopback-guard.d.ts +38 -0
  130. package/dist/{rate-limit-D9J2b4S1.d.ts → http/middleware/rate-limit.d.ts} +22 -50
  131. package/dist/http/middleware/redirect-safety.d.ts +29 -0
  132. package/dist/http/middleware/request-id.d.ts +19 -0
  133. package/dist/http/middleware/request-logging.d.ts +16 -0
  134. package/dist/http/middleware/security-headers.d.ts +49 -0
  135. package/dist/http/middleware/session.d.ts +93 -0
  136. package/dist/http/middleware/validation.d.ts +94 -0
  137. package/dist/http/public-assets.d.ts +15 -0
  138. package/dist/http/request.d.ts +24 -0
  139. package/dist/http/resources/CursorPaginator.d.ts +106 -0
  140. package/dist/http/resources/Paginator.d.ts +130 -0
  141. package/dist/http/resources/Resource.d.ts +67 -0
  142. package/dist/http/resources/ResourceCollection.d.ts +60 -0
  143. package/dist/http/resources/index.d.ts +6 -0
  144. package/dist/http/resources/types.d.ts +91 -0
  145. package/dist/http/validation/Validator.d.ts +128 -0
  146. package/dist/http/validation/index.d.ts +3 -0
  147. package/dist/http/validation/rules.d.ts +196 -0
  148. package/dist/http/validation/types.d.ts +96 -0
  149. package/dist/http/vite-dev-server.d.ts +27 -0
  150. package/dist/i18n/I18nManager.d.ts +114 -0
  151. package/dist/i18n/Translator.d.ts +80 -0
  152. package/dist/i18n/index.d.ts +6 -101
  153. package/dist/i18n/index.js +5 -3
  154. package/dist/i18n/loaders/JsonLoader.d.ts +47 -0
  155. package/dist/i18n/loaders/MemoryLoader.d.ts +37 -0
  156. package/dist/i18n/pluralization.d.ts +19 -0
  157. package/dist/i18n/types.d.ts +106 -0
  158. package/dist/index.d.ts +69 -2605
  159. package/dist/index.js +332 -152
  160. package/dist/lambda/index.d.ts +17 -37
  161. package/dist/logging/LogManager.d.ts +83 -0
  162. package/dist/logging/Logger.d.ts +76 -0
  163. package/dist/logging/channels/ConsoleChannel.d.ts +11 -0
  164. package/dist/logging/channels/DailyFileChannel.d.ts +20 -0
  165. package/dist/logging/channels/FileChannel.d.ts +15 -0
  166. package/dist/logging/index.d.ts +8 -50
  167. package/dist/logging/types.d.ts +96 -0
  168. package/dist/mail/Mail.d.ts +138 -0
  169. package/dist/mail/MailManager.d.ts +79 -0
  170. package/dist/mail/address.d.ts +8 -0
  171. package/dist/mail/index.d.ts +7 -316
  172. package/dist/mail/transports/LogTransport.d.ts +19 -0
  173. package/dist/mail/transports/MemoryTransport.d.ts +84 -0
  174. package/dist/mail/transports/ResendTransport.d.ts +32 -0
  175. package/dist/mail/transports/SmtpTransport.d.ts +40 -0
  176. package/dist/{MailManager-DpMvYiP9.d.ts → mail/types.d.ts} +11 -92
  177. package/dist/mcp/McpServiceProvider.d.ts +17 -0
  178. package/dist/mcp/create-mcp-server.d.ts +144 -0
  179. package/dist/mcp/endpoint.d.ts +29 -0
  180. package/dist/mcp/index.d.ts +3 -185
  181. package/dist/mcp/index.js +1 -1
  182. package/dist/mvc/Controller.d.ts +326 -0
  183. package/dist/mvc/Router.d.ts +239 -0
  184. package/dist/mvc/ViewEngine.d.ts +11 -0
  185. package/dist/mvc/inertia/InertiaEngine.d.ts +114 -0
  186. package/dist/mvc/inertia/shared.d.ts +90 -0
  187. package/dist/mvc/mount-route.d.ts +10 -0
  188. package/dist/notifications/Notification.d.ts +99 -0
  189. package/dist/notifications/NotificationManager.d.ts +150 -0
  190. package/dist/notifications/channels/DatabaseChannel.d.ts +55 -0
  191. package/dist/notifications/channels/MailChannel.d.ts +45 -0
  192. package/dist/notifications/channels/MemoryChannel.d.ts +85 -0
  193. package/dist/notifications/channels/SlackChannel.d.ts +73 -0
  194. package/dist/notifications/channels/index.d.ts +4 -0
  195. package/dist/notifications/index.d.ts +6 -322
  196. package/dist/notifications/notifiable-type.d.ts +10 -0
  197. package/dist/notifications/registry.d.ts +39 -0
  198. package/dist/notifications/types.d.ts +162 -0
  199. package/dist/providers/AuthServiceProvider.d.ts +7 -0
  200. package/dist/providers/AuthorizationServiceProvider.d.ts +9 -0
  201. package/dist/providers/BroadcastServiceProvider.d.ts +7 -0
  202. package/dist/providers/CacheServiceProvider.d.ts +7 -0
  203. package/dist/providers/EncryptionServiceProvider.d.ts +7 -0
  204. package/dist/providers/ErrorServiceProvider.d.ts +9 -0
  205. package/dist/providers/EventServiceProvider.d.ts +7 -0
  206. package/dist/providers/HealthServiceProvider.d.ts +7 -0
  207. package/dist/providers/I18nServiceProvider.d.ts +27 -0
  208. package/dist/providers/InertiaServiceProvider.d.ts +20 -0
  209. package/dist/providers/LogServiceProvider.d.ts +7 -0
  210. package/dist/providers/MailServiceProvider.d.ts +7 -0
  211. package/dist/providers/NotificationServiceProvider.d.ts +8 -0
  212. package/dist/providers/OAuthServiceProvider.d.ts +7 -0
  213. package/dist/providers/QueueServiceProvider.d.ts +7 -0
  214. package/dist/providers/SchedulingServiceProvider.d.ts +7 -0
  215. package/dist/providers/StorageServiceProvider.d.ts +7 -0
  216. package/dist/providers/index.d.ts +17 -0
  217. package/dist/queue/FailedJobReporter.d.ts +40 -0
  218. package/dist/queue/Job.d.ts +140 -0
  219. package/dist/queue/QueueManager.d.ts +79 -0
  220. package/dist/queue/Worker.d.ts +99 -0
  221. package/dist/queue/drivers/MemoryDriver.d.ts +60 -0
  222. package/dist/queue/drivers/RedisDriver.d.ts +96 -0
  223. package/dist/queue/drivers/SqsDriver.d.ts +108 -0
  224. package/dist/queue/drivers/SyncDriver.d.ts +28 -0
  225. package/dist/queue/index.d.ts +9 -453
  226. package/dist/queue/types.d.ts +171 -0
  227. package/dist/redis/RedisApiTokenStore.d.ts +78 -0
  228. package/dist/redis/RedisEmailVerificationStore.d.ts +54 -0
  229. package/dist/redis/RedisOAuthStateStore.d.ts +26 -0
  230. package/dist/redis/RedisPasswordResetStore.d.ts +57 -0
  231. package/dist/redis/RedisRateLimitStore.d.ts +77 -0
  232. package/dist/redis/RedisSessionStore.d.ts +61 -0
  233. package/dist/redis/client.d.ts +41 -0
  234. package/dist/redis/index.d.ts +14 -356
  235. package/dist/redis/index.js +5 -2
  236. package/dist/redis/scan-keys.d.ts +6 -0
  237. package/dist/runtime/index.d.ts +8 -95
  238. package/dist/runtime/index.js +1 -1
  239. package/dist/scheduling/CronParser.d.ts +37 -0
  240. package/dist/scheduling/Schedule.d.ts +229 -0
  241. package/dist/scheduling/ScheduledTask.d.ts +47 -0
  242. package/dist/scheduling/Scheduler.d.ts +99 -0
  243. package/dist/scheduling/index.d.ts +5 -41
  244. package/dist/scheduling/types.d.ts +97 -0
  245. package/dist/storage/StorageManager.d.ts +65 -0
  246. package/dist/storage/drivers/LocalDriver.d.ts +55 -0
  247. package/dist/storage/drivers/MemoryDriver.d.ts +66 -0
  248. package/dist/storage/drivers/S3Driver.d.ts +72 -0
  249. package/dist/storage/index.d.ts +5 -197
  250. package/dist/{StorageManager-oZTHqaza.d.ts → storage/types.d.ts} +10 -77
  251. package/dist/support/error-polyfill.d.ts +9 -0
  252. package/dist/support/import-map.d.ts +13 -0
  253. package/dist/support/redirect-target.d.ts +11 -0
  254. package/dist/support/stack-frames.d.ts +56 -0
  255. package/dist/support/trim-slashes.d.ts +11 -0
  256. package/dist/vite/index.d.ts +2 -35
  257. package/dist/vite/index.js +0 -3
  258. package/dist/vite/plugin.d.ts +34 -0
  259. package/package.json +3 -3
  260. package/dist/AuthManager-DBP4KYsx.d.ts +0 -79
  261. package/dist/BroadcastManager-D8KoJxiV.d.ts +0 -492
  262. package/dist/HealthManager-DUyMIzsZ.d.ts +0 -141
  263. package/dist/I18nManager-BiSoczfV.d.ts +0 -275
  264. package/dist/LogManager-7mxnkaPM.d.ts +0 -256
  265. package/dist/Scheduler-pZiT8i57.d.ts +0 -471
  266. package/dist/api-token-D24VPr_E.d.ts +0 -663
  267. package/dist/app-key-CsBfRC_Q.d.ts +0 -214
  268. package/dist/email-verification-D60YF2ZO.d.ts +0 -329
  269. package/dist/index-9_Jzj5jo.d.ts +0 -7
  270. 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 'hono';
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
- * User type for authorization.
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
- interface PolicyRegistration {
82
- modelClass: unknown;
83
- policyClass: PolicyClass;
84
- }
6
+ export declare const Response: ResponseBuilder;
85
7
  /**
86
- * Resource actions for policies.
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
- type ResourceAction = 'viewAny' | 'view' | 'create' | 'update' | 'delete' | 'restore' | 'forceDelete';
13
+ export declare function isAuthorizationResponse(value: unknown): value is AuthorizationResponse;
89
14
  /**
90
- * Response builder for authorization.
15
+ * Build the exception for a denial, honouring `denyWithStatus()` /
16
+ * `denyAsNotFound()` so a policy can hide a record as a 404.
91
17
  */
92
- interface ResponseBuilder {
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
- import { f as Policy$1, A as AuthUser, a as AuthorizationResponse, G as Gate, b as AuthorizeOptions } from '../Gate-CynjZCtS.js';
2
- export { c as GateCallback, d as GateDefinition, e as GateOptions, P as PolicyClass, g as PolicyMethod, h as PolicyRegistration, i as ResourceAction, R as Response, j as ResponseBuilder, k as authorize, l as can, m as cannot, n as createGate, o as defineGate, p as getGate, s as setGate } from '../Gate-CynjZCtS.js';
3
- import { Context } from 'hono';
4
- import { M as Middleware } from '../index-9_Jzj5jo.js';
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-FZD2AKJI.js";
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
+ }