@morojs/moro 1.0.3 → 1.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/README.md +57 -2
- package/dist/core/auth/morojs-adapter.d.ts +94 -0
- package/dist/core/auth/morojs-adapter.js +288 -0
- package/dist/core/auth/morojs-adapter.js.map +1 -0
- package/dist/core/config/file-loader.d.ts +18 -0
- package/dist/core/config/file-loader.js +345 -0
- package/dist/core/config/file-loader.js.map +1 -0
- package/dist/core/config/index.d.ts +6 -0
- package/dist/core/config/index.js +15 -0
- package/dist/core/config/index.js.map +1 -1
- package/dist/core/config/loader.d.ts +2 -1
- package/dist/core/config/loader.js +15 -2
- package/dist/core/config/loader.js.map +1 -1
- package/dist/core/config/utils.js +50 -3
- package/dist/core/config/utils.js.map +1 -1
- package/dist/core/http/http-server.d.ts +2 -0
- package/dist/core/http/http-server.js +52 -9
- package/dist/core/http/http-server.js.map +1 -1
- package/dist/core/middleware/built-in/auth-helpers.d.ts +124 -0
- package/dist/core/middleware/built-in/auth-helpers.js +338 -0
- package/dist/core/middleware/built-in/auth-helpers.js.map +1 -0
- package/dist/core/middleware/built-in/auth-providers.d.ts +125 -0
- package/dist/core/middleware/built-in/auth-providers.js +394 -0
- package/dist/core/middleware/built-in/auth-providers.js.map +1 -0
- package/dist/core/middleware/built-in/auth.d.ts +29 -1
- package/dist/core/middleware/built-in/auth.js +259 -16
- package/dist/core/middleware/built-in/auth.js.map +1 -1
- package/dist/core/middleware/built-in/index.d.ts +3 -1
- package/dist/core/middleware/built-in/index.js +19 -1
- package/dist/core/middleware/built-in/index.js.map +1 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.js +11 -2
- package/dist/index.js.map +1 -1
- package/dist/moro.d.ts +1 -0
- package/dist/moro.js +19 -1
- package/dist/moro.js.map +1 -1
- package/dist/types/auth.d.ts +367 -0
- package/dist/types/auth.js +28 -0
- package/dist/types/auth.js.map +1 -0
- package/package.json +6 -2
- package/src/core/auth/README.md +339 -0
- package/src/core/auth/morojs-adapter.ts +402 -0
- package/src/core/config/file-loader.ts +398 -0
- package/src/core/config/index.ts +18 -0
- package/src/core/config/loader.ts +18 -2
- package/src/core/config/utils.ts +53 -3
- package/src/core/http/http-server.ts +61 -10
- package/src/core/middleware/built-in/auth-helpers.ts +401 -0
- package/src/core/middleware/built-in/auth-providers.ts +480 -0
- package/src/core/middleware/built-in/auth.ts +306 -16
- package/src/core/middleware/built-in/index.ts +22 -0
- package/src/index.ts +30 -1
- package/src/moro.ts +29 -1
- package/src/types/auth.ts +440 -0
- package/tsconfig.json +1 -1
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
// Auth.js Types for MoroJS
|
|
2
|
+
export interface AuthProvider {
|
|
3
|
+
id: string;
|
|
4
|
+
name: string;
|
|
5
|
+
type: 'oauth' | 'oidc' | 'credentials' | 'email';
|
|
6
|
+
|
|
7
|
+
// OAuth/OIDC specific
|
|
8
|
+
authorization?: string | { url: string; params?: Record<string, any> };
|
|
9
|
+
token?: string | { url: string; params?: Record<string, any> };
|
|
10
|
+
userinfo?: string | { url: string; params?: Record<string, any> };
|
|
11
|
+
issuer?: string;
|
|
12
|
+
wellKnown?: string;
|
|
13
|
+
|
|
14
|
+
// Client configuration
|
|
15
|
+
clientId?: string;
|
|
16
|
+
clientSecret?: string;
|
|
17
|
+
|
|
18
|
+
// Scope and claims
|
|
19
|
+
scope?: string;
|
|
20
|
+
claims?: Record<string, any>;
|
|
21
|
+
|
|
22
|
+
// Profile mapping
|
|
23
|
+
profile?: (profile: any, tokens: any) => Promise<any> | any;
|
|
24
|
+
|
|
25
|
+
// Custom authorization parameters
|
|
26
|
+
authorization_params?: Record<string, any>;
|
|
27
|
+
|
|
28
|
+
// Token handling
|
|
29
|
+
token_endpoint_auth_method?: 'client_secret_post' | 'client_secret_basic';
|
|
30
|
+
|
|
31
|
+
// Additional provider-specific options
|
|
32
|
+
[key: string]: any;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface OAuthProvider extends AuthProvider {
|
|
36
|
+
type: 'oauth';
|
|
37
|
+
authorization: string | { url: string; params?: Record<string, any> };
|
|
38
|
+
token: string | { url: string; params?: Record<string, any> };
|
|
39
|
+
userinfo?: string | { url: string; params?: Record<string, any> };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface OIDCProvider extends AuthProvider {
|
|
43
|
+
type: 'oidc';
|
|
44
|
+
issuer: string;
|
|
45
|
+
wellKnown?: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface CredentialsProvider extends AuthProvider {
|
|
49
|
+
type: 'credentials';
|
|
50
|
+
credentials: Record<
|
|
51
|
+
string,
|
|
52
|
+
{
|
|
53
|
+
label?: string;
|
|
54
|
+
type?: string;
|
|
55
|
+
placeholder?: string;
|
|
56
|
+
[key: string]: any;
|
|
57
|
+
}
|
|
58
|
+
>;
|
|
59
|
+
authorize: (credentials: Record<string, any>, req: any) => Promise<any> | any;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface EmailProvider extends AuthProvider {
|
|
63
|
+
type: 'email';
|
|
64
|
+
server:
|
|
65
|
+
| string
|
|
66
|
+
| {
|
|
67
|
+
host: string;
|
|
68
|
+
port: number;
|
|
69
|
+
auth: {
|
|
70
|
+
user: string;
|
|
71
|
+
pass: string;
|
|
72
|
+
};
|
|
73
|
+
secure?: boolean;
|
|
74
|
+
tls?: any;
|
|
75
|
+
};
|
|
76
|
+
from: string;
|
|
77
|
+
sendVerificationRequest?: (params: {
|
|
78
|
+
identifier: string;
|
|
79
|
+
url: string;
|
|
80
|
+
expires: Date;
|
|
81
|
+
provider: EmailProvider;
|
|
82
|
+
token: string;
|
|
83
|
+
theme: any;
|
|
84
|
+
request: any;
|
|
85
|
+
}) => Promise<void>;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export interface AuthUser {
|
|
89
|
+
id: string;
|
|
90
|
+
name?: string | null;
|
|
91
|
+
email?: string | null;
|
|
92
|
+
image?: string | null;
|
|
93
|
+
emailVerified?: Date | null;
|
|
94
|
+
[key: string]: any;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export interface AuthAccount {
|
|
98
|
+
userId: string;
|
|
99
|
+
type: 'oauth' | 'oidc' | 'email' | 'credentials';
|
|
100
|
+
provider: string;
|
|
101
|
+
providerAccountId: string;
|
|
102
|
+
access_token?: string;
|
|
103
|
+
expires_at?: number;
|
|
104
|
+
id_token?: string;
|
|
105
|
+
refresh_token?: string;
|
|
106
|
+
refresh_token_expires_in?: number;
|
|
107
|
+
scope?: string;
|
|
108
|
+
token_type?: string;
|
|
109
|
+
session_state?: string;
|
|
110
|
+
[key: string]: any;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export interface AuthSession {
|
|
114
|
+
sessionToken: string;
|
|
115
|
+
userId: string;
|
|
116
|
+
expires: Date;
|
|
117
|
+
user: AuthUser;
|
|
118
|
+
[key: string]: any;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export interface VerificationToken {
|
|
122
|
+
identifier: string;
|
|
123
|
+
token: string;
|
|
124
|
+
expires: Date;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export interface AuthJWT {
|
|
128
|
+
name?: string | null;
|
|
129
|
+
email?: string | null;
|
|
130
|
+
picture?: string | null;
|
|
131
|
+
sub?: string;
|
|
132
|
+
iat?: number;
|
|
133
|
+
exp?: number;
|
|
134
|
+
jti?: string;
|
|
135
|
+
[key: string]: any;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export interface AuthCallbacks {
|
|
139
|
+
signIn?: (params: {
|
|
140
|
+
user: AuthUser;
|
|
141
|
+
account: AuthAccount | null;
|
|
142
|
+
profile?: any;
|
|
143
|
+
email?: { verificationRequest?: boolean };
|
|
144
|
+
credentials?: Record<string, any>;
|
|
145
|
+
}) => Awaitable<boolean | string>;
|
|
146
|
+
|
|
147
|
+
redirect?: (params: { url: string; baseUrl: string }) => Awaitable<string>;
|
|
148
|
+
|
|
149
|
+
session?: (params: {
|
|
150
|
+
session: AuthSession;
|
|
151
|
+
user: AuthUser;
|
|
152
|
+
token: AuthJWT;
|
|
153
|
+
}) => Awaitable<AuthSession>;
|
|
154
|
+
|
|
155
|
+
jwt?: (params: {
|
|
156
|
+
token: AuthJWT;
|
|
157
|
+
user?: AuthUser;
|
|
158
|
+
account?: AuthAccount;
|
|
159
|
+
profile?: any;
|
|
160
|
+
trigger?: 'signIn' | 'signUp' | 'update';
|
|
161
|
+
isNewUser?: boolean;
|
|
162
|
+
session?: any;
|
|
163
|
+
}) => Awaitable<AuthJWT>;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export interface AuthPages {
|
|
167
|
+
signIn?: string;
|
|
168
|
+
signOut?: string;
|
|
169
|
+
error?: string;
|
|
170
|
+
verifyRequest?: string;
|
|
171
|
+
newUser?: string;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export interface AuthEvents {
|
|
175
|
+
signIn?: (message: {
|
|
176
|
+
user: AuthUser;
|
|
177
|
+
account: AuthAccount | null;
|
|
178
|
+
profile?: any;
|
|
179
|
+
isNewUser?: boolean;
|
|
180
|
+
}) => Awaitable<void>;
|
|
181
|
+
signOut?: (message: { session: AuthSession; token: AuthJWT }) => Awaitable<void>;
|
|
182
|
+
createUser?: (message: { user: AuthUser }) => Awaitable<void>;
|
|
183
|
+
updateUser?: (message: { user: AuthUser }) => Awaitable<void>;
|
|
184
|
+
linkAccount?: (message: {
|
|
185
|
+
user: AuthUser;
|
|
186
|
+
account: AuthAccount;
|
|
187
|
+
profile: any;
|
|
188
|
+
}) => Awaitable<void>;
|
|
189
|
+
session?: (message: { session: AuthSession; token: AuthJWT }) => Awaitable<void>;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export interface AuthCookies {
|
|
193
|
+
sessionToken: {
|
|
194
|
+
name: string;
|
|
195
|
+
options?: CookieOptions;
|
|
196
|
+
};
|
|
197
|
+
callbackUrl: {
|
|
198
|
+
name: string;
|
|
199
|
+
options?: CookieOptions;
|
|
200
|
+
};
|
|
201
|
+
csrfToken: {
|
|
202
|
+
name: string;
|
|
203
|
+
options?: CookieOptions;
|
|
204
|
+
};
|
|
205
|
+
pkceCodeVerifier: {
|
|
206
|
+
name: string;
|
|
207
|
+
options?: CookieOptions;
|
|
208
|
+
};
|
|
209
|
+
state: {
|
|
210
|
+
name: string;
|
|
211
|
+
options?: CookieOptions;
|
|
212
|
+
};
|
|
213
|
+
nonce: {
|
|
214
|
+
name: string;
|
|
215
|
+
options?: CookieOptions;
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export interface CookieOptions {
|
|
220
|
+
domain?: string;
|
|
221
|
+
expires?: Date;
|
|
222
|
+
httpOnly?: boolean;
|
|
223
|
+
maxAge?: number;
|
|
224
|
+
path?: string;
|
|
225
|
+
sameSite?: 'strict' | 'lax' | 'none';
|
|
226
|
+
secure?: boolean;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export interface AuthTheme {
|
|
230
|
+
colorScheme?: 'light' | 'dark' | 'auto';
|
|
231
|
+
logo?: string;
|
|
232
|
+
brandColor?: string;
|
|
233
|
+
buttonText?: string;
|
|
234
|
+
[key: string]: any;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export interface AuthLogger {
|
|
238
|
+
error: (code: string, metadata?: any) => void;
|
|
239
|
+
warn: (code: string) => void;
|
|
240
|
+
debug: (code: string, metadata?: any) => void;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
export interface AuthAdapter {
|
|
244
|
+
createUser?: (user: Omit<AuthUser, 'id'>) => Awaitable<AuthUser>;
|
|
245
|
+
getUser?: (id: string) => Awaitable<AuthUser | null>;
|
|
246
|
+
getUserByEmail?: (email: string) => Awaitable<AuthUser | null>;
|
|
247
|
+
getUserByAccount?: (
|
|
248
|
+
providerAccountId: Pick<AuthAccount, 'provider' | 'providerAccountId'>
|
|
249
|
+
) => Awaitable<AuthUser | null>;
|
|
250
|
+
updateUser?: (user: Partial<AuthUser> & Pick<AuthUser, 'id'>) => Awaitable<AuthUser>;
|
|
251
|
+
deleteUser?: (userId: string) => Awaitable<void>;
|
|
252
|
+
linkAccount?: (account: AuthAccount) => Awaitable<void>;
|
|
253
|
+
unlinkAccount?: (
|
|
254
|
+
providerAccountId: Pick<AuthAccount, 'provider' | 'providerAccountId'>
|
|
255
|
+
) => Awaitable<void>;
|
|
256
|
+
createSession?: (session: {
|
|
257
|
+
sessionToken: string;
|
|
258
|
+
userId: string;
|
|
259
|
+
expires: Date;
|
|
260
|
+
}) => Awaitable<AuthSession>;
|
|
261
|
+
getSessionAndUser?: (
|
|
262
|
+
sessionToken: string
|
|
263
|
+
) => Awaitable<{ session: AuthSession; user: AuthUser } | null>;
|
|
264
|
+
updateSession?: (
|
|
265
|
+
session: Partial<AuthSession> & Pick<AuthSession, 'sessionToken'>
|
|
266
|
+
) => Awaitable<AuthSession | null | undefined>;
|
|
267
|
+
deleteSession?: (sessionToken: string) => Awaitable<void>;
|
|
268
|
+
createVerificationToken?: (
|
|
269
|
+
verificationToken: VerificationToken
|
|
270
|
+
) => Awaitable<VerificationToken | null | undefined>;
|
|
271
|
+
useVerificationToken?: (params: {
|
|
272
|
+
identifier: string;
|
|
273
|
+
token: string;
|
|
274
|
+
}) => Awaitable<VerificationToken | null>;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
export interface AuthOptions {
|
|
278
|
+
// Core configuration
|
|
279
|
+
providers: AuthProvider[];
|
|
280
|
+
secret?: string;
|
|
281
|
+
|
|
282
|
+
// Session configuration
|
|
283
|
+
session?: {
|
|
284
|
+
strategy?: 'jwt' | 'database';
|
|
285
|
+
maxAge?: number; // in seconds
|
|
286
|
+
updateAge?: number; // in seconds
|
|
287
|
+
generateSessionToken?: () => string;
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
// JWT configuration
|
|
291
|
+
jwt?: {
|
|
292
|
+
secret?: string;
|
|
293
|
+
maxAge?: number;
|
|
294
|
+
encode?: (params: { token?: AuthJWT; secret: string; maxAge?: number }) => Awaitable<string>;
|
|
295
|
+
decode?: (params: { token?: string; secret: string }) => Awaitable<AuthJWT | null>;
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
// Callbacks
|
|
299
|
+
callbacks?: AuthCallbacks;
|
|
300
|
+
|
|
301
|
+
// Events
|
|
302
|
+
events?: AuthEvents;
|
|
303
|
+
|
|
304
|
+
// Adapter
|
|
305
|
+
adapter?: AuthAdapter;
|
|
306
|
+
|
|
307
|
+
// Pages
|
|
308
|
+
pages?: AuthPages;
|
|
309
|
+
|
|
310
|
+
// Cookies
|
|
311
|
+
cookies?: Partial<AuthCookies>;
|
|
312
|
+
|
|
313
|
+
// Theme
|
|
314
|
+
theme?: AuthTheme;
|
|
315
|
+
|
|
316
|
+
// Logger
|
|
317
|
+
logger?: AuthLogger;
|
|
318
|
+
|
|
319
|
+
// Configuration
|
|
320
|
+
debug?: boolean;
|
|
321
|
+
basePath?: string;
|
|
322
|
+
useSecureCookies?: boolean;
|
|
323
|
+
trustHost?: boolean;
|
|
324
|
+
|
|
325
|
+
// CSRF
|
|
326
|
+
skipCSRFCheck?: string[];
|
|
327
|
+
|
|
328
|
+
// Experimental features
|
|
329
|
+
experimental?: {
|
|
330
|
+
enableWebAuthn?: boolean;
|
|
331
|
+
[key: string]: any;
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
export interface AuthRequest {
|
|
336
|
+
user?: AuthUser;
|
|
337
|
+
session?: AuthSession;
|
|
338
|
+
token?: string;
|
|
339
|
+
isAuthenticated: boolean;
|
|
340
|
+
|
|
341
|
+
// Auth methods
|
|
342
|
+
signIn: (
|
|
343
|
+
provider?: string,
|
|
344
|
+
options?: {
|
|
345
|
+
callbackUrl?: string;
|
|
346
|
+
redirect?: boolean;
|
|
347
|
+
[key: string]: any;
|
|
348
|
+
}
|
|
349
|
+
) => Promise<any>;
|
|
350
|
+
|
|
351
|
+
signOut: (options?: { callbackUrl?: string; redirect?: boolean }) => Promise<any>;
|
|
352
|
+
|
|
353
|
+
getSession: () => Promise<AuthSession | null>;
|
|
354
|
+
getToken: () => Promise<AuthJWT | null>;
|
|
355
|
+
|
|
356
|
+
// CSRF protection
|
|
357
|
+
getCsrfToken: () => Promise<string>;
|
|
358
|
+
|
|
359
|
+
// Providers
|
|
360
|
+
getProviders: () => Promise<Record<string, AuthProvider>>;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
export interface AuthResponse {
|
|
364
|
+
status: number;
|
|
365
|
+
headers?: Record<string, string>;
|
|
366
|
+
body?: any;
|
|
367
|
+
redirect?: string;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
export interface AuthConfig extends AuthOptions {
|
|
371
|
+
// Runtime configuration
|
|
372
|
+
basePath: string;
|
|
373
|
+
baseUrl: string;
|
|
374
|
+
|
|
375
|
+
// Internal state
|
|
376
|
+
providers: AuthProvider[];
|
|
377
|
+
|
|
378
|
+
// Computed values
|
|
379
|
+
skipCSRFCheck: string[];
|
|
380
|
+
useSecureCookies: boolean;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
// Utility types
|
|
384
|
+
export type Awaitable<T> = T | Promise<T>;
|
|
385
|
+
|
|
386
|
+
export type ProviderType = 'oauth' | 'oidc' | 'credentials' | 'email';
|
|
387
|
+
|
|
388
|
+
export type SignInOptions = {
|
|
389
|
+
callbackUrl?: string;
|
|
390
|
+
redirect?: boolean;
|
|
391
|
+
[key: string]: any;
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
export type SignOutOptions = {
|
|
395
|
+
callbackUrl?: string;
|
|
396
|
+
redirect?: boolean;
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
// Error types
|
|
400
|
+
export interface AuthError extends Error {
|
|
401
|
+
type: string;
|
|
402
|
+
code?: string;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
export class SignInError extends Error implements AuthError {
|
|
406
|
+
type = 'SignInError';
|
|
407
|
+
code?: string;
|
|
408
|
+
|
|
409
|
+
constructor(message: string, code?: string) {
|
|
410
|
+
super(message);
|
|
411
|
+
this.code = code;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
export class CallbackError extends Error implements AuthError {
|
|
416
|
+
type = 'CallbackError';
|
|
417
|
+
code?: string;
|
|
418
|
+
|
|
419
|
+
constructor(message: string, code?: string) {
|
|
420
|
+
super(message);
|
|
421
|
+
this.code = code;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
export class SessionError extends Error implements AuthError {
|
|
426
|
+
type = 'SessionError';
|
|
427
|
+
code?: string;
|
|
428
|
+
|
|
429
|
+
constructor(message: string, code?: string) {
|
|
430
|
+
super(message);
|
|
431
|
+
this.code = code;
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
// Re-export for convenience
|
|
436
|
+
export type { AuthProvider as Provider };
|
|
437
|
+
export type { AuthUser as User };
|
|
438
|
+
export type { AuthSession as Session };
|
|
439
|
+
export type { AuthAccount as Account };
|
|
440
|
+
export type { AuthJWT as JWT };
|
package/tsconfig.json
CHANGED