@guren/server 1.0.0-rc.9 → 1.0.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/{Application-DtWDHXr1.d.ts → Application-BnsyCKXY.d.ts} +79 -8
- package/dist/AuthManager-SfhCNkAU.d.ts +79 -0
- package/dist/{BroadcastManager-AkIWUGJo.d.ts → BroadcastManager-CGWl9rUO.d.ts} +5 -0
- package/dist/{ConsoleKernel-CqCVrdZs.d.ts → ConsoleKernel-BDtBETjm.d.ts} +1 -1
- package/dist/{Gate-CNkBYf8m.d.ts → Gate-CynjZCtS.d.ts} +5 -0
- package/dist/{I18nManager-Dtgzsf5n.d.ts → I18nManager-BiSoczfV.d.ts} +6 -1
- package/dist/McpServiceProvider-JW6PDVMD.js +7 -0
- package/dist/api-token-BSSCLlFW.d.ts +541 -0
- package/dist/auth/index.d.ts +9 -327
- package/dist/auth/index.js +59 -6684
- package/dist/authorization/index.d.ts +2 -2
- package/dist/authorization/index.js +19 -604
- package/dist/broadcasting/index.d.ts +2 -2
- package/dist/broadcasting/index.js +12 -895
- package/dist/cache/index.js +8 -809
- package/dist/chunk-2T6JN4VR.js +1563 -0
- package/dist/chunk-44F7JQ7I.js +950 -0
- package/dist/chunk-74HTZG3V.js +331 -0
- package/dist/chunk-A3ISJVEV.js +598 -0
- package/dist/chunk-CSDKWLFD.js +652 -0
- package/dist/chunk-CSRQTEQA.js +839 -0
- package/dist/chunk-DAQKYKLH.js +182 -0
- package/dist/chunk-EDRGAM6G.js +647 -0
- package/dist/chunk-EGU5KB7V.js +818 -0
- package/dist/chunk-H32L2NE3.js +372 -0
- package/dist/chunk-HKQSAFSN.js +837 -0
- package/dist/chunk-IOTWFHZU.js +558 -0
- package/dist/chunk-ONSDE37A.js +125 -0
- package/dist/chunk-QQKTH5KX.js +114 -0
- package/dist/chunk-R2TCP7D7.js +409 -0
- package/dist/chunk-SIP34GBE.js +380 -0
- package/dist/chunk-THSX7OOR.js +454 -0
- package/dist/chunk-UY3AZSYL.js +14 -0
- package/dist/chunk-VT5KRDPH.js +134 -0
- package/dist/chunk-VXXZIXAP.js +255 -0
- package/dist/chunk-WJJ5CTNI.js +907 -0
- package/dist/chunk-WVY45EIW.js +359 -0
- package/dist/chunk-ZRBLZY3M.js +462 -0
- package/dist/client-CKXJLsTe.d.ts +232 -0
- package/dist/email-verification-CAeArjui.d.ts +327 -0
- package/dist/encryption/index.js +48 -556
- package/dist/errors-JOOPDDQ6.js +34 -0
- package/dist/events/index.js +14 -316
- package/dist/health/index.js +12 -367
- package/dist/i18n/index.d.ts +2 -2
- package/dist/i18n/index.js +14 -583
- package/dist/index.d.ts +37 -239
- package/dist/index.js +2873 -19166
- package/dist/lambda/index.d.ts +9 -7
- package/dist/lambda/index.js +4 -9
- package/dist/logging/index.js +12 -545
- package/dist/mail/index.d.ts +29 -1
- package/dist/mail/index.js +15 -684
- package/dist/mcp/index.d.ts +7 -5
- package/dist/mcp/index.js +5 -378
- package/dist/notifications/index.d.ts +8 -6
- package/dist/notifications/index.js +13 -730
- package/dist/queue/index.d.ts +37 -7
- package/dist/queue/index.js +22 -940
- package/dist/redis/index.d.ts +366 -0
- package/dist/redis/index.js +597 -0
- package/dist/runtime/index.d.ts +8 -6
- package/dist/runtime/index.js +26 -244
- package/dist/scheduling/index.js +14 -822
- package/dist/storage/index.d.ts +1 -0
- package/dist/storage/index.js +6 -824
- package/package.json +15 -7
- package/dist/api-token-JOif2CtG.d.ts +0 -1792
|
@@ -0,0 +1,541 @@
|
|
|
1
|
+
import { MiddlewareHandler, Context } from 'hono';
|
|
2
|
+
|
|
3
|
+
type SessionData = Record<string, unknown>;
|
|
4
|
+
interface SessionStore {
|
|
5
|
+
/**
|
|
6
|
+
* Read a session by its opaque identifier.
|
|
7
|
+
* Returns undefined when the session does not exist or has expired.
|
|
8
|
+
*/
|
|
9
|
+
read(id: string): Promise<SessionData | undefined>;
|
|
10
|
+
/**
|
|
11
|
+
* Persist a session using upsert semantics for the given opaque identifier.
|
|
12
|
+
*/
|
|
13
|
+
write(id: string, data: SessionData, ttlSeconds: number): Promise<void>;
|
|
14
|
+
/**
|
|
15
|
+
* Destroy a session. Implementations must treat repeated calls as safe.
|
|
16
|
+
*/
|
|
17
|
+
destroy(id: string): Promise<void>;
|
|
18
|
+
}
|
|
19
|
+
declare class MemorySessionStore implements SessionStore {
|
|
20
|
+
private readonly now;
|
|
21
|
+
private readonly store;
|
|
22
|
+
constructor(now?: () => number);
|
|
23
|
+
read(id: string): Promise<SessionData | undefined>;
|
|
24
|
+
write(id: string, data: SessionData, ttlSeconds: number): Promise<void>;
|
|
25
|
+
destroy(id: string): Promise<void>;
|
|
26
|
+
}
|
|
27
|
+
interface SessionOptions {
|
|
28
|
+
cookieName?: string;
|
|
29
|
+
cookiePath?: string;
|
|
30
|
+
cookieDomain?: string;
|
|
31
|
+
cookieSecure?: boolean;
|
|
32
|
+
cookieSameSite?: 'Strict' | 'Lax' | 'None';
|
|
33
|
+
cookieHttpOnly?: boolean;
|
|
34
|
+
cookieMaxAgeSeconds?: number;
|
|
35
|
+
ttlSeconds?: number;
|
|
36
|
+
store?: SessionStore;
|
|
37
|
+
}
|
|
38
|
+
interface Session {
|
|
39
|
+
readonly id: string;
|
|
40
|
+
readonly isNew: boolean;
|
|
41
|
+
get<T = unknown>(key: string): T | undefined;
|
|
42
|
+
set<T = unknown>(key: string, value: T): void;
|
|
43
|
+
has(key: string): boolean;
|
|
44
|
+
forget(key: string): void;
|
|
45
|
+
flush(): void;
|
|
46
|
+
all(): SessionData;
|
|
47
|
+
regenerate(): void;
|
|
48
|
+
invalidate(): void;
|
|
49
|
+
flash(key: string, value: unknown): void;
|
|
50
|
+
getFlash<T = unknown>(key: string): T | undefined;
|
|
51
|
+
reflash(): void;
|
|
52
|
+
keep(...keys: string[]): void;
|
|
53
|
+
}
|
|
54
|
+
interface CreateSessionMiddlewareOptions extends SessionOptions {
|
|
55
|
+
}
|
|
56
|
+
declare function createSessionMiddleware(options?: CreateSessionMiddlewareOptions): MiddlewareHandler;
|
|
57
|
+
declare function getSessionFromContext<T extends Session = Session>(ctx: {
|
|
58
|
+
get: (key: string) => unknown;
|
|
59
|
+
}): T | undefined;
|
|
60
|
+
|
|
61
|
+
type AuthCredentials = Record<string, unknown>;
|
|
62
|
+
interface Authenticatable {
|
|
63
|
+
getAuthIdentifier(): unknown;
|
|
64
|
+
getAuthPassword(): string | null | undefined;
|
|
65
|
+
getRememberToken?(): string | null | undefined;
|
|
66
|
+
setRememberToken?(token: string | null): void | Promise<void>;
|
|
67
|
+
}
|
|
68
|
+
interface Guard<User = Authenticatable> {
|
|
69
|
+
check(): Promise<boolean>;
|
|
70
|
+
guest(): Promise<boolean>;
|
|
71
|
+
user<T = User>(): Promise<T | null>;
|
|
72
|
+
id(): Promise<unknown>;
|
|
73
|
+
login<T = User>(user: T, remember?: boolean): Promise<void>;
|
|
74
|
+
logout(): Promise<void>;
|
|
75
|
+
attempt(credentials: AuthCredentials, remember?: boolean): Promise<boolean>;
|
|
76
|
+
validate(credentials: AuthCredentials): Promise<User | null>;
|
|
77
|
+
session<T extends Session = Session>(): T | undefined;
|
|
78
|
+
}
|
|
79
|
+
interface UserProvider<User = Authenticatable> {
|
|
80
|
+
retrieveById(identifier: unknown): Promise<User | null>;
|
|
81
|
+
retrieveByCredentials(credentials: AuthCredentials): Promise<User | null>;
|
|
82
|
+
validateCredentials(user: User, credentials: AuthCredentials): Promise<boolean>;
|
|
83
|
+
getId(user: User): unknown;
|
|
84
|
+
setRememberToken?(user: User, token: string | null): Promise<void> | void;
|
|
85
|
+
getRememberToken?(user: User): Promise<string | null> | string | null;
|
|
86
|
+
/**
|
|
87
|
+
* Strip fields that must never leave the auth layer (password hashes,
|
|
88
|
+
* remember tokens, model `hidden` fields) from a user record before it
|
|
89
|
+
* is cached or handed to application code via guard.user().
|
|
90
|
+
*/
|
|
91
|
+
sanitize?(user: User): User;
|
|
92
|
+
}
|
|
93
|
+
interface GuardContext {
|
|
94
|
+
ctx: Context;
|
|
95
|
+
session: Session | undefined;
|
|
96
|
+
manager: AuthManagerContract;
|
|
97
|
+
}
|
|
98
|
+
type GuardFactory<User = Authenticatable> = (context: GuardContext) => Guard<User>;
|
|
99
|
+
interface ProviderFactory<User = Authenticatable> {
|
|
100
|
+
(manager: AuthManagerContract): UserProvider<User>;
|
|
101
|
+
}
|
|
102
|
+
interface AuthManagerOptions {
|
|
103
|
+
defaultGuard?: string;
|
|
104
|
+
}
|
|
105
|
+
interface AttachContextOptions {
|
|
106
|
+
guard?: string;
|
|
107
|
+
}
|
|
108
|
+
interface AuthContext<User = Authenticatable> {
|
|
109
|
+
check(): Promise<boolean>;
|
|
110
|
+
guest(): Promise<boolean>;
|
|
111
|
+
user<T = User>(): Promise<T | null>;
|
|
112
|
+
userOrFail<T = User>(): Promise<T>;
|
|
113
|
+
id(): Promise<unknown>;
|
|
114
|
+
login<T = User>(user: T, remember?: boolean): Promise<void>;
|
|
115
|
+
attempt(credentials: AuthCredentials, remember?: boolean): Promise<boolean>;
|
|
116
|
+
logout(): Promise<void>;
|
|
117
|
+
guard<T = User>(name?: string): Guard<T>;
|
|
118
|
+
session<T extends Session = Session>(): T | undefined;
|
|
119
|
+
}
|
|
120
|
+
interface AuthManagerContract {
|
|
121
|
+
registerGuard<User = Authenticatable>(name: string, factory: GuardFactory<User>): void;
|
|
122
|
+
registerProvider<User = Authenticatable>(name: string, factory: ProviderFactory<User>): void;
|
|
123
|
+
getProvider<User = Authenticatable>(name: string): UserProvider<User>;
|
|
124
|
+
createGuard<User = Authenticatable>(name: string, context: GuardContext): Guard<User>;
|
|
125
|
+
guardNames(): string[];
|
|
126
|
+
setDefaultGuard(name: string): void;
|
|
127
|
+
getDefaultGuard(): string;
|
|
128
|
+
createAuthContext(ctx: Context, options?: AttachContextOptions): AuthContext;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
interface OAuthProviderConfig {
|
|
132
|
+
clientId: string;
|
|
133
|
+
clientSecret: string;
|
|
134
|
+
redirectUri: string;
|
|
135
|
+
authorizeUrl: string;
|
|
136
|
+
tokenUrl: string;
|
|
137
|
+
userInfoUrl: string;
|
|
138
|
+
scopes?: string[];
|
|
139
|
+
tokenAuthMethod?: 'client_secret_post' | 'client_secret_basic';
|
|
140
|
+
userInfoMethod?: 'GET' | 'POST';
|
|
141
|
+
mapProfile?: (raw: Record<string, unknown>, token: OAuthTokenResult) => OAuthUserProfile;
|
|
142
|
+
}
|
|
143
|
+
interface OAuthTokenResult {
|
|
144
|
+
accessToken: string;
|
|
145
|
+
tokenType?: string;
|
|
146
|
+
refreshToken?: string;
|
|
147
|
+
expiresIn?: number;
|
|
148
|
+
scope?: string;
|
|
149
|
+
raw: Record<string, unknown>;
|
|
150
|
+
}
|
|
151
|
+
interface OAuthUserProfile {
|
|
152
|
+
id: string;
|
|
153
|
+
email?: string;
|
|
154
|
+
name?: string;
|
|
155
|
+
avatar?: string;
|
|
156
|
+
token: OAuthTokenResult;
|
|
157
|
+
raw: Record<string, unknown>;
|
|
158
|
+
}
|
|
159
|
+
interface OAuthStatePayload {
|
|
160
|
+
provider: string;
|
|
161
|
+
redirectTo?: string;
|
|
162
|
+
expiresAt: Date;
|
|
163
|
+
}
|
|
164
|
+
interface OAuthStateStore {
|
|
165
|
+
store(stateHash: string, payload: OAuthStatePayload): Promise<void>;
|
|
166
|
+
find(stateHash: string): Promise<OAuthStatePayload | null>;
|
|
167
|
+
delete(stateHash: string): Promise<void>;
|
|
168
|
+
}
|
|
169
|
+
interface OAuthStateConfig {
|
|
170
|
+
expiresIn?: number;
|
|
171
|
+
stateLength?: number;
|
|
172
|
+
hashAlgorithm?: 'sha256' | 'sha512';
|
|
173
|
+
}
|
|
174
|
+
interface OAuthAuthorizeOptions {
|
|
175
|
+
scope?: string[];
|
|
176
|
+
redirectTo?: string;
|
|
177
|
+
state?: string;
|
|
178
|
+
extraParams?: Record<string, string>;
|
|
179
|
+
}
|
|
180
|
+
interface OAuthCallbackPayload {
|
|
181
|
+
code: string;
|
|
182
|
+
state: string;
|
|
183
|
+
}
|
|
184
|
+
interface OAuthManagerOptions {
|
|
185
|
+
stateStore?: OAuthStateStore;
|
|
186
|
+
stateConfig?: OAuthStateConfig;
|
|
187
|
+
}
|
|
188
|
+
declare class MemoryOAuthStateStore implements OAuthStateStore {
|
|
189
|
+
private readonly states;
|
|
190
|
+
private readonly maxEntries;
|
|
191
|
+
constructor(options?: {
|
|
192
|
+
maxEntries?: number;
|
|
193
|
+
});
|
|
194
|
+
store(stateHash: string, payload: OAuthStatePayload): Promise<void>;
|
|
195
|
+
private sweepExpired;
|
|
196
|
+
find(stateHash: string): Promise<OAuthStatePayload | null>;
|
|
197
|
+
delete(stateHash: string): Promise<void>;
|
|
198
|
+
clear(): void;
|
|
199
|
+
}
|
|
200
|
+
declare class OAuthManager {
|
|
201
|
+
private readonly providers;
|
|
202
|
+
private readonly stateStore;
|
|
203
|
+
private readonly stateConfig;
|
|
204
|
+
constructor(options?: OAuthManagerOptions);
|
|
205
|
+
registerProvider(name: string, config: OAuthProviderConfig): void;
|
|
206
|
+
providerNames(): string[];
|
|
207
|
+
getProvider(name: string): OAuthProviderConfig;
|
|
208
|
+
authorize(providerName: string, options?: OAuthAuthorizeOptions): Promise<{
|
|
209
|
+
url: string;
|
|
210
|
+
state: string;
|
|
211
|
+
expiresAt: Date;
|
|
212
|
+
}>;
|
|
213
|
+
user(providerName: string, payload: OAuthCallbackPayload): Promise<OAuthUserProfile>;
|
|
214
|
+
}
|
|
215
|
+
declare function createOAuthManager(options?: OAuthManagerOptions): OAuthManager;
|
|
216
|
+
declare function createOAuthState(provider: string, store: OAuthStateStore, config?: OAuthStateConfig, redirectTo?: string, fixedState?: string): Promise<{
|
|
217
|
+
state: string;
|
|
218
|
+
expiresAt: Date;
|
|
219
|
+
}>;
|
|
220
|
+
declare function verifyOAuthState(state: string, provider: string, store: OAuthStateStore, config?: OAuthStateConfig): Promise<OAuthStatePayload | null>;
|
|
221
|
+
declare function buildOAuthAuthorizeUrl(provider: OAuthProviderConfig, state: string, options?: {
|
|
222
|
+
scope?: string[];
|
|
223
|
+
extraParams?: Record<string, string>;
|
|
224
|
+
}): string;
|
|
225
|
+
declare function exchangeOAuthCode(provider: OAuthProviderConfig, code: string): Promise<OAuthTokenResult>;
|
|
226
|
+
declare function fetchOAuthUserProfile(provider: OAuthProviderConfig, token: OAuthTokenResult): Promise<OAuthUserProfile>;
|
|
227
|
+
interface OAuthProviderFactoryInput {
|
|
228
|
+
clientId: string;
|
|
229
|
+
clientSecret: string;
|
|
230
|
+
redirectUri: string;
|
|
231
|
+
scopes?: string[];
|
|
232
|
+
}
|
|
233
|
+
declare function createGitHubOAuthProviderConfig(input: OAuthProviderFactoryInput): OAuthProviderConfig;
|
|
234
|
+
declare function createGoogleOAuthProviderConfig(input: OAuthProviderFactoryInput): OAuthProviderConfig;
|
|
235
|
+
declare function createDiscordOAuthProviderConfig(input: OAuthProviderFactoryInput): OAuthProviderConfig;
|
|
236
|
+
declare function buildOAuthRedirectUrl(baseUrl: string, token: string, email?: string): string;
|
|
237
|
+
declare function parseOAuthRedirectUrl(url: string): {
|
|
238
|
+
token: string | null;
|
|
239
|
+
email: string | null;
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* API token data stored in the backing store.
|
|
244
|
+
*/
|
|
245
|
+
interface ApiToken {
|
|
246
|
+
id: string;
|
|
247
|
+
name: string;
|
|
248
|
+
hashedToken: string;
|
|
249
|
+
userId: string | number;
|
|
250
|
+
abilities: string[];
|
|
251
|
+
lastUsedAt: Date | null;
|
|
252
|
+
expiresAt: Date | null;
|
|
253
|
+
createdAt: Date;
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Store interface for API tokens.
|
|
257
|
+
* Implement this for database-backed storage.
|
|
258
|
+
*/
|
|
259
|
+
interface ApiTokenStore {
|
|
260
|
+
/**
|
|
261
|
+
* Store a new API token.
|
|
262
|
+
*/
|
|
263
|
+
store(token: ApiToken): Promise<void>;
|
|
264
|
+
/**
|
|
265
|
+
* Find a token by its hashed value.
|
|
266
|
+
*/
|
|
267
|
+
findByHashedToken(hashedToken: string): Promise<ApiToken | null>;
|
|
268
|
+
/**
|
|
269
|
+
* Find all tokens for a user.
|
|
270
|
+
*/
|
|
271
|
+
findByUserId(userId: string | number): Promise<ApiToken[]>;
|
|
272
|
+
/**
|
|
273
|
+
* Delete a token by its ID.
|
|
274
|
+
*/
|
|
275
|
+
delete(id: string): Promise<void>;
|
|
276
|
+
/**
|
|
277
|
+
* Delete all tokens for a user.
|
|
278
|
+
*/
|
|
279
|
+
deleteForUser(userId: string | number): Promise<void>;
|
|
280
|
+
/**
|
|
281
|
+
* Update the last used timestamp.
|
|
282
|
+
*/
|
|
283
|
+
updateLastUsed(id: string, timestamp: Date): Promise<void>;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* In-memory token store for testing.
|
|
287
|
+
* Do NOT use in production - tokens will be lost on restart.
|
|
288
|
+
*/
|
|
289
|
+
declare class MemoryApiTokenStore implements ApiTokenStore {
|
|
290
|
+
private tokens;
|
|
291
|
+
private byHash;
|
|
292
|
+
store(token: ApiToken): Promise<void>;
|
|
293
|
+
findByHashedToken(hashedToken: string): Promise<ApiToken | null>;
|
|
294
|
+
findByUserId(userId: string | number): Promise<ApiToken[]>;
|
|
295
|
+
delete(id: string): Promise<void>;
|
|
296
|
+
deleteForUser(userId: string | number): Promise<void>;
|
|
297
|
+
updateLastUsed(id: string, timestamp: Date): Promise<void>;
|
|
298
|
+
/**
|
|
299
|
+
* Clear all tokens (useful for testing).
|
|
300
|
+
*/
|
|
301
|
+
clear(): void;
|
|
302
|
+
/**
|
|
303
|
+
* Get the number of stored tokens.
|
|
304
|
+
*/
|
|
305
|
+
get size(): number;
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Configuration for API token creation.
|
|
309
|
+
*/
|
|
310
|
+
interface CreateApiTokenOptions {
|
|
311
|
+
/**
|
|
312
|
+
* Human-readable name for the token.
|
|
313
|
+
*/
|
|
314
|
+
name: string;
|
|
315
|
+
/**
|
|
316
|
+
* User ID the token belongs to.
|
|
317
|
+
*/
|
|
318
|
+
userId: string | number;
|
|
319
|
+
/**
|
|
320
|
+
* Token abilities/scopes.
|
|
321
|
+
* @default ['*'] (all abilities)
|
|
322
|
+
*/
|
|
323
|
+
abilities?: string[];
|
|
324
|
+
/**
|
|
325
|
+
* Token expiration time in milliseconds from now.
|
|
326
|
+
* @default null (never expires)
|
|
327
|
+
*/
|
|
328
|
+
expiresIn?: number | null;
|
|
329
|
+
/**
|
|
330
|
+
* Token byte length (before encoding).
|
|
331
|
+
* @default 32
|
|
332
|
+
*/
|
|
333
|
+
tokenLength?: number;
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Result of creating an API token.
|
|
337
|
+
*/
|
|
338
|
+
interface CreateApiTokenResult {
|
|
339
|
+
/**
|
|
340
|
+
* The full token to give to the user.
|
|
341
|
+
* Format: {id}|{plainToken}
|
|
342
|
+
* This is the ONLY time the plain token is available.
|
|
343
|
+
*/
|
|
344
|
+
plainTextToken: string;
|
|
345
|
+
/**
|
|
346
|
+
* The token record (without the plain token).
|
|
347
|
+
*/
|
|
348
|
+
token: ApiToken;
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Create a new API token.
|
|
352
|
+
*
|
|
353
|
+
* @example
|
|
354
|
+
* ```ts
|
|
355
|
+
* const { plainTextToken, token } = await createApiToken(store, {
|
|
356
|
+
* name: 'My App Token',
|
|
357
|
+
* userId: user.id,
|
|
358
|
+
* abilities: ['read', 'write'],
|
|
359
|
+
* expiresIn: 30 * 24 * 60 * 60 * 1000, // 30 days
|
|
360
|
+
* })
|
|
361
|
+
*
|
|
362
|
+
* // Return plainTextToken to user - this is the only time it's available
|
|
363
|
+
* return ctx.json({ token: plainTextToken })
|
|
364
|
+
* ```
|
|
365
|
+
*/
|
|
366
|
+
declare function createApiToken(store: ApiTokenStore, options: CreateApiTokenOptions): Promise<CreateApiTokenResult>;
|
|
367
|
+
/**
|
|
368
|
+
* Parse a plain text token into its components.
|
|
369
|
+
*/
|
|
370
|
+
declare function parseApiToken(plainTextToken: string): {
|
|
371
|
+
id: string;
|
|
372
|
+
token: string;
|
|
373
|
+
} | null;
|
|
374
|
+
/**
|
|
375
|
+
* Verify an API token and return the associated user ID.
|
|
376
|
+
*
|
|
377
|
+
* @example
|
|
378
|
+
* ```ts
|
|
379
|
+
* const result = await verifyApiToken(plainTextToken, store)
|
|
380
|
+
*
|
|
381
|
+
* if (!result) {
|
|
382
|
+
* return ctx.json({ error: 'Invalid token' }, 401)
|
|
383
|
+
* }
|
|
384
|
+
*
|
|
385
|
+
* console.log(`User ${result.userId} authenticated with abilities:`, result.abilities)
|
|
386
|
+
* ```
|
|
387
|
+
*/
|
|
388
|
+
declare function verifyApiToken(plainTextToken: string, store: ApiTokenStore, options?: {
|
|
389
|
+
updateLastUsed?: boolean;
|
|
390
|
+
}): Promise<{
|
|
391
|
+
token: ApiToken;
|
|
392
|
+
userId: string | number;
|
|
393
|
+
abilities: string[];
|
|
394
|
+
} | null>;
|
|
395
|
+
/**
|
|
396
|
+
* Check if a token has a specific ability.
|
|
397
|
+
*/
|
|
398
|
+
declare function tokenCan(token: ApiToken | {
|
|
399
|
+
abilities: string[];
|
|
400
|
+
}, ability: string): boolean;
|
|
401
|
+
/**
|
|
402
|
+
* Check if a token has all specified abilities.
|
|
403
|
+
*/
|
|
404
|
+
declare function tokenCanAll(token: ApiToken | {
|
|
405
|
+
abilities: string[];
|
|
406
|
+
}, abilities: string[]): boolean;
|
|
407
|
+
/**
|
|
408
|
+
* Check if a token has any of the specified abilities.
|
|
409
|
+
*/
|
|
410
|
+
declare function tokenCanAny(token: ApiToken | {
|
|
411
|
+
abilities: string[];
|
|
412
|
+
}, abilities: string[]): boolean;
|
|
413
|
+
/**
|
|
414
|
+
* Revoke (delete) an API token.
|
|
415
|
+
*
|
|
416
|
+
* @example
|
|
417
|
+
* ```ts
|
|
418
|
+
* await revokeApiToken(tokenId, store)
|
|
419
|
+
* ```
|
|
420
|
+
*/
|
|
421
|
+
declare function revokeApiToken(id: string, store: ApiTokenStore): Promise<void>;
|
|
422
|
+
/**
|
|
423
|
+
* Revoke all API tokens for a user.
|
|
424
|
+
*
|
|
425
|
+
* @example
|
|
426
|
+
* ```ts
|
|
427
|
+
* // Revoke all tokens when user changes password
|
|
428
|
+
* await revokeAllApiTokens(user.id, store)
|
|
429
|
+
* ```
|
|
430
|
+
*/
|
|
431
|
+
declare function revokeAllApiTokens(userId: string | number, store: ApiTokenStore): Promise<void>;
|
|
432
|
+
/**
|
|
433
|
+
* Get all API tokens for a user.
|
|
434
|
+
*
|
|
435
|
+
* @example
|
|
436
|
+
* ```ts
|
|
437
|
+
* const tokens = await getUserApiTokens(user.id, store)
|
|
438
|
+
* // Returns tokens without the plain text (only metadata)
|
|
439
|
+
* ```
|
|
440
|
+
*/
|
|
441
|
+
declare function getUserApiTokens(userId: string | number, store: ApiTokenStore): Promise<ApiToken[]>;
|
|
442
|
+
/**
|
|
443
|
+
* Context key for storing the authenticated token.
|
|
444
|
+
*/
|
|
445
|
+
declare const API_TOKEN_KEY = "guren:api-token";
|
|
446
|
+
/**
|
|
447
|
+
* Options for the bearer token middleware.
|
|
448
|
+
*/
|
|
449
|
+
interface BearerTokenMiddlewareOptions {
|
|
450
|
+
/**
|
|
451
|
+
* Token store implementation.
|
|
452
|
+
*/
|
|
453
|
+
store: ApiTokenStore;
|
|
454
|
+
/**
|
|
455
|
+
* Function to load the user from the user ID.
|
|
456
|
+
*/
|
|
457
|
+
loadUser?: (userId: string | number) => Promise<unknown>;
|
|
458
|
+
/**
|
|
459
|
+
* Required abilities for this route.
|
|
460
|
+
* If not specified, any valid token is accepted.
|
|
461
|
+
*/
|
|
462
|
+
abilities?: string[];
|
|
463
|
+
/**
|
|
464
|
+
* Custom handler when authentication fails.
|
|
465
|
+
*/
|
|
466
|
+
onUnauthorized?: (ctx: Context) => Response | Promise<Response>;
|
|
467
|
+
/**
|
|
468
|
+
* Custom handler when token lacks required abilities.
|
|
469
|
+
*/
|
|
470
|
+
onForbidden?: (ctx: Context, required: string[]) => Response | Promise<Response>;
|
|
471
|
+
/**
|
|
472
|
+
* Header name to extract the token from.
|
|
473
|
+
* @default 'Authorization'
|
|
474
|
+
*/
|
|
475
|
+
headerName?: string;
|
|
476
|
+
/**
|
|
477
|
+
* Whether to update the token's lastUsedAt timestamp.
|
|
478
|
+
* @default true
|
|
479
|
+
*/
|
|
480
|
+
updateLastUsed?: boolean;
|
|
481
|
+
}
|
|
482
|
+
/**
|
|
483
|
+
* Create middleware that authenticates requests using Bearer tokens.
|
|
484
|
+
*
|
|
485
|
+
* @example
|
|
486
|
+
* ```ts
|
|
487
|
+
* // Basic usage
|
|
488
|
+
* app.use('/api/*', createBearerTokenMiddleware({ store }))
|
|
489
|
+
*
|
|
490
|
+
* // With ability requirement
|
|
491
|
+
* router.delete('/api/posts/:id', [PostController, 'destroy'],
|
|
492
|
+
* createBearerTokenMiddleware({
|
|
493
|
+
* store,
|
|
494
|
+
* abilities: ['posts:delete'],
|
|
495
|
+
* })
|
|
496
|
+
* )
|
|
497
|
+
*
|
|
498
|
+
* // With user loading
|
|
499
|
+
* app.use('/api/*', createBearerTokenMiddleware({
|
|
500
|
+
* store,
|
|
501
|
+
* loadUser: async (userId) => User.find(userId),
|
|
502
|
+
* }))
|
|
503
|
+
* ```
|
|
504
|
+
*/
|
|
505
|
+
declare function createBearerTokenMiddleware(options: BearerTokenMiddlewareOptions): MiddlewareHandler;
|
|
506
|
+
/**
|
|
507
|
+
* Get the authenticated API token from the request context.
|
|
508
|
+
*
|
|
509
|
+
* @example
|
|
510
|
+
* ```ts
|
|
511
|
+
* router.get('/api/me', async (ctx) => {
|
|
512
|
+
* const { token, userId, abilities } = getApiToken(ctx)!
|
|
513
|
+
* return ctx.json({ userId, tokenName: token.name, abilities })
|
|
514
|
+
* })
|
|
515
|
+
* ```
|
|
516
|
+
*/
|
|
517
|
+
declare function getApiToken(ctx: Context): {
|
|
518
|
+
token: ApiToken;
|
|
519
|
+
userId: string | number;
|
|
520
|
+
abilities: string[];
|
|
521
|
+
} | null;
|
|
522
|
+
/**
|
|
523
|
+
* Get the authenticated API token from the request context, or throw.
|
|
524
|
+
*
|
|
525
|
+
* @throws {AuthenticationException} When no token is present in the context.
|
|
526
|
+
*
|
|
527
|
+
* @example
|
|
528
|
+
* ```ts
|
|
529
|
+
* router.get('/api/me', async (ctx) => {
|
|
530
|
+
* const { token, userId, abilities } = getApiTokenOrFail(ctx)
|
|
531
|
+
* return ctx.json({ userId, tokenName: token.name, abilities })
|
|
532
|
+
* })
|
|
533
|
+
* ```
|
|
534
|
+
*/
|
|
535
|
+
declare function getApiTokenOrFail(ctx: Context): {
|
|
536
|
+
token: ApiToken;
|
|
537
|
+
userId: string | number;
|
|
538
|
+
abilities: string[];
|
|
539
|
+
};
|
|
540
|
+
|
|
541
|
+
export { revokeAllApiTokens as $, type Authenticatable as A, type BearerTokenMiddlewareOptions as B, type CreateSessionMiddlewareOptions as C, buildOAuthAuthorizeUrl as D, buildOAuthRedirectUrl as E, createApiToken as F, type GuardFactory as G, createBearerTokenMiddleware as H, createDiscordOAuthProviderConfig as I, createGitHubOAuthProviderConfig as J, createGoogleOAuthProviderConfig as K, createOAuthManager as L, MemoryApiTokenStore as M, createOAuthState as N, type OAuthStateStore as O, type ProviderFactory as P, createSessionMiddleware as Q, exchangeOAuthCode as R, type SessionStore as S, fetchOAuthUserProfile as T, type UserProvider as U, getApiToken as V, getApiTokenOrFail as W, getSessionFromContext as X, getUserApiTokens as Y, parseApiToken as Z, parseOAuthRedirectUrl as _, type AuthCredentials as a, revokeApiToken as a0, tokenCan as a1, tokenCanAll as a2, tokenCanAny as a3, verifyApiToken as a4, verifyOAuthState as a5, type AuthManagerContract as b, type AuthManagerOptions as c, type GuardContext as d, type Guard as e, type AttachContextOptions as f, type AuthContext as g, type SessionData as h, type ApiTokenStore as i, type ApiToken as j, type OAuthStatePayload as k, OAuthManager as l, API_TOKEN_KEY as m, type CreateApiTokenOptions as n, type CreateApiTokenResult as o, MemoryOAuthStateStore as p, MemorySessionStore as q, type OAuthAuthorizeOptions as r, type OAuthCallbackPayload as s, type OAuthManagerOptions as t, type OAuthProviderConfig as u, type OAuthProviderFactoryInput as v, type OAuthStateConfig as w, type OAuthTokenResult as x, type OAuthUserProfile as y, type Session as z };
|