@adonisjs/auth 10.0.0-next.6 → 10.1.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/build/auth_manager-Cp_ofh4p.js +384 -0
- package/build/debug-CrTUB4zl.js +12 -0
- package/build/{errors-Ns_FO2np.js → errors-eDV8ejO0.js} +116 -4
- package/build/index.js +46 -4
- package/build/modules/access_tokens_guard/main.js +758 -3
- package/build/modules/basic_auth_guard/main.js +224 -2
- package/build/modules/session_guard/main.js +716 -2
- package/build/providers/auth_provider.js +24 -3
- package/build/services/auth.js +5 -0
- package/build/src/middleware/initialize_auth_middleware.js +29 -0
- package/build/src/mixins/lucid.js +94 -1
- package/build/src/plugins/japa/api_client.js +26 -1
- package/build/src/plugins/japa/browser_client.js +24 -1
- package/build/{symbols-BwcLqNln.js → symbols-C5QEqFvJ.js} +10 -1
- package/package.json +34 -34
- package/build/auth_manager-CJvMGOzX.js +0 -129
- package/build/debug-Ckko95-M.js +0 -3
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
import { n as E_UNAUTHORIZED_ACCESS } from "./errors-eDV8ejO0.js";
|
|
2
|
+
import { t as debug_default } from "./debug-CrTUB4zl.js";
|
|
3
|
+
import { RuntimeException } from "@adonisjs/core/exceptions";
|
|
4
|
+
import { HttpContextFactory } from "@adonisjs/core/factories/http";
|
|
5
|
+
//#region src/authenticator.ts
|
|
6
|
+
/**
|
|
7
|
+
* Authenticator is used to authenticate incoming HTTP requests
|
|
8
|
+
* using one or more known guards.
|
|
9
|
+
*/
|
|
10
|
+
var Authenticator = class {
|
|
11
|
+
/**
|
|
12
|
+
* Registered guards
|
|
13
|
+
*/
|
|
14
|
+
#config;
|
|
15
|
+
/**
|
|
16
|
+
* Cache of guards created during the HTTP request
|
|
17
|
+
*/
|
|
18
|
+
#guardsCache = {};
|
|
19
|
+
/**
|
|
20
|
+
* Last guard that was used to perform the authentication via
|
|
21
|
+
* the "authenticateUsing" method.
|
|
22
|
+
*
|
|
23
|
+
* @note
|
|
24
|
+
* Reset on every call made to "authenticate", "check" and
|
|
25
|
+
* "authenticateUsing" method.
|
|
26
|
+
*/
|
|
27
|
+
#authenticationAttemptedViaGuard;
|
|
28
|
+
/**
|
|
29
|
+
* Name of the guard using which the request has
|
|
30
|
+
* been authenticated successfully.
|
|
31
|
+
*
|
|
32
|
+
* @note
|
|
33
|
+
* Reset on every call made to "authenticate", "check" and
|
|
34
|
+
* "authenticateUsing" method.
|
|
35
|
+
*/
|
|
36
|
+
#authenticatedViaGuard;
|
|
37
|
+
/**
|
|
38
|
+
* Reference to HTTP context
|
|
39
|
+
*/
|
|
40
|
+
#ctx;
|
|
41
|
+
/**
|
|
42
|
+
* Name of the default guard configured in the auth configuration
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* const defaultGuard = auth.defaultGuard
|
|
46
|
+
* console.log(defaultGuard) // 'web'
|
|
47
|
+
*/
|
|
48
|
+
get defaultGuard() {
|
|
49
|
+
return this.#config.default;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Reference to the guard using which the current
|
|
53
|
+
* request has been authenticated. Returns undefined if
|
|
54
|
+
* authentication has not been attempted or failed.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* await auth.authenticate()
|
|
58
|
+
* console.log(auth.authenticatedViaGuard) // 'web'
|
|
59
|
+
*/
|
|
60
|
+
get authenticatedViaGuard() {
|
|
61
|
+
return this.#authenticatedViaGuard;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* A boolean to know if the current request has been authenticated. The
|
|
65
|
+
* property returns false when "authenticate" or "authenticateUsing"
|
|
66
|
+
* methods are not used.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* await auth.authenticate()
|
|
70
|
+
* console.log(auth.isAuthenticated) // true
|
|
71
|
+
*/
|
|
72
|
+
get isAuthenticated() {
|
|
73
|
+
if (!this.#authenticationAttemptedViaGuard) return false;
|
|
74
|
+
return this.use(this.#authenticationAttemptedViaGuard).isAuthenticated;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Reference to the currently authenticated user. The property returns
|
|
78
|
+
* undefined when "authenticate" or "authenticateUsing" methods are
|
|
79
|
+
* not used.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* await auth.authenticate()
|
|
83
|
+
* console.log(auth.user?.email)
|
|
84
|
+
*/
|
|
85
|
+
get user() {
|
|
86
|
+
if (!this.#authenticationAttemptedViaGuard) return;
|
|
87
|
+
return this.use(this.#authenticationAttemptedViaGuard).user;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Whether or not the authentication has been attempted during
|
|
91
|
+
* the current request. The property returns false when the
|
|
92
|
+
* "authenticate" or "authenticateUsing" methods are not
|
|
93
|
+
* used.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* await auth.check()
|
|
97
|
+
* console.log(auth.authenticationAttempted) // true
|
|
98
|
+
*/
|
|
99
|
+
get authenticationAttempted() {
|
|
100
|
+
if (!this.#authenticationAttemptedViaGuard) return false;
|
|
101
|
+
return this.use(this.#authenticationAttemptedViaGuard).authenticationAttempted;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Creates a new Authenticator instance
|
|
105
|
+
*
|
|
106
|
+
* @param ctx - The HTTP context for the current request
|
|
107
|
+
* @param config - Configuration object containing default guard and available guards
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* const authenticator = new Authenticator(ctx, {
|
|
111
|
+
* default: 'web',
|
|
112
|
+
* guards: { web: sessionGuard }
|
|
113
|
+
* })
|
|
114
|
+
*/
|
|
115
|
+
constructor(ctx, config) {
|
|
116
|
+
this.#ctx = ctx;
|
|
117
|
+
this.#config = config;
|
|
118
|
+
debug_default("creating authenticator. config %O", this.#config);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Returns an instance of the logged-in user or throws an exception
|
|
122
|
+
*
|
|
123
|
+
* @throws {RuntimeException} When authentication has not been attempted
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* const user = auth.getUserOrFail()
|
|
127
|
+
* console.log(user.id)
|
|
128
|
+
*/
|
|
129
|
+
getUserOrFail() {
|
|
130
|
+
if (!this.#authenticationAttemptedViaGuard) throw new RuntimeException("Cannot access authenticated user. Please call \"auth.authenticate\" method first.");
|
|
131
|
+
return this.use(this.#authenticationAttemptedViaGuard).getUserOrFail();
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Returns an instance of a known guard. Guards instances are
|
|
135
|
+
* cached during the lifecycle of an HTTP request.
|
|
136
|
+
*
|
|
137
|
+
* @param guard - Optional guard name. Uses default guard if not provided
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* const sessionGuard = auth.use('session')
|
|
141
|
+
* const defaultGuard = auth.use()
|
|
142
|
+
*/
|
|
143
|
+
use(guard) {
|
|
144
|
+
const guardToUse = guard || this.#config.default;
|
|
145
|
+
/**
|
|
146
|
+
* Use cached copy if exists
|
|
147
|
+
*/
|
|
148
|
+
const cachedGuard = this.#guardsCache[guardToUse];
|
|
149
|
+
if (cachedGuard) {
|
|
150
|
+
debug_default("authenticator: using guard from cache. name: \"%s\"", guardToUse);
|
|
151
|
+
return cachedGuard;
|
|
152
|
+
}
|
|
153
|
+
const guardFactory = this.#config.guards[guardToUse];
|
|
154
|
+
/**
|
|
155
|
+
* Construct guard and cache it
|
|
156
|
+
*/
|
|
157
|
+
debug_default("authenticator: creating guard. name: \"%s\"", guardToUse);
|
|
158
|
+
const guardInstance = guardFactory(this.#ctx);
|
|
159
|
+
this.#guardsCache[guardToUse] = guardInstance;
|
|
160
|
+
return guardInstance;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Authenticate current request using the default guard. Calling this
|
|
164
|
+
* method multiple times triggers multiple authentication with the
|
|
165
|
+
* guard.
|
|
166
|
+
*
|
|
167
|
+
* @throws {E_UNAUTHORIZED_ACCESS} When authentication fails
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* const user = await auth.authenticate()
|
|
171
|
+
* console.log('Authenticated user:', user.email)
|
|
172
|
+
*/
|
|
173
|
+
async authenticate() {
|
|
174
|
+
await this.authenticateUsing();
|
|
175
|
+
return this.getUserOrFail();
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Silently attempt to authenticate the request using the default
|
|
179
|
+
* guard. Calling this method multiple times triggers multiple
|
|
180
|
+
* authentication with the guard.
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* const isAuthenticated = await auth.check()
|
|
184
|
+
* if (isAuthenticated) {
|
|
185
|
+
* console.log('User is authenticated')
|
|
186
|
+
* }
|
|
187
|
+
*/
|
|
188
|
+
async check() {
|
|
189
|
+
this.#authenticationAttemptedViaGuard = this.defaultGuard;
|
|
190
|
+
const isAuthenticated = await this.use().check();
|
|
191
|
+
if (isAuthenticated) this.#authenticatedViaGuard = this.defaultGuard;
|
|
192
|
+
return isAuthenticated;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Authenticate the request using all of the mentioned guards
|
|
196
|
+
* or the default guard.
|
|
197
|
+
*
|
|
198
|
+
* The authentication process will stop after any of the mentioned
|
|
199
|
+
* guards is able to authenticate the request successfully.
|
|
200
|
+
*
|
|
201
|
+
* Otherwise, "E_UNAUTHORIZED_ACCESS" will be raised.
|
|
202
|
+
*
|
|
203
|
+
* @param guards - Array of guard names to try for authentication
|
|
204
|
+
* @param options - Options object with optional loginRoute for redirects
|
|
205
|
+
*
|
|
206
|
+
* @throws {E_UNAUTHORIZED_ACCESS} When none of the guards can authenticate
|
|
207
|
+
*
|
|
208
|
+
* @example
|
|
209
|
+
* const user = await auth.authenticateUsing(['session', 'api'])
|
|
210
|
+
* const userWithRedirect = await auth.authenticateUsing(['web'], { loginRoute: '/login' })
|
|
211
|
+
*/
|
|
212
|
+
async authenticateUsing(guards, options) {
|
|
213
|
+
const guardsToUse = guards || [this.defaultGuard];
|
|
214
|
+
let lastUsedDriver;
|
|
215
|
+
for (let guardName of guardsToUse) {
|
|
216
|
+
debug_default("attempting to authenticate using guard \"%s\"", guardName);
|
|
217
|
+
this.#authenticationAttemptedViaGuard = guardName;
|
|
218
|
+
const guard = this.use(guardName);
|
|
219
|
+
lastUsedDriver = guard.driverName;
|
|
220
|
+
if (await guard.check()) {
|
|
221
|
+
this.#authenticatedViaGuard = guardName;
|
|
222
|
+
return this.getUserOrFail();
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
throw new E_UNAUTHORIZED_ACCESS("Unauthorized access", {
|
|
226
|
+
guardDriverName: lastUsedDriver,
|
|
227
|
+
redirectTo: options?.loginRoute
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Silently attempt to authenticate the request using all of the mentioned guards
|
|
232
|
+
* or the default guard. Calling this method multiple times triggers multiple
|
|
233
|
+
* authentication with the guard.
|
|
234
|
+
*
|
|
235
|
+
* @param guards - Array of guard names to check. Defaults to default guard
|
|
236
|
+
*
|
|
237
|
+
* @example
|
|
238
|
+
* const isAuthenticated = await auth.checkUsing(['session', 'api'])
|
|
239
|
+
* if (isAuthenticated) {
|
|
240
|
+
* const user = auth.user
|
|
241
|
+
* }
|
|
242
|
+
*/
|
|
243
|
+
async checkUsing(guards = [this.defaultGuard]) {
|
|
244
|
+
for (const name of guards) {
|
|
245
|
+
this.#authenticationAttemptedViaGuard = name;
|
|
246
|
+
if (await this.use(name).check()) {
|
|
247
|
+
this.#authenticatedViaGuard = name;
|
|
248
|
+
return true;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return false;
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
//#endregion
|
|
255
|
+
//#region src/authenticator_client.ts
|
|
256
|
+
/**
|
|
257
|
+
* Authenticator client is used to create guard instances for testing.
|
|
258
|
+
* It passes a fake HTTPContext to the guards, so make sure to not
|
|
259
|
+
* call server side APIs that might be relying on a real
|
|
260
|
+
* HTTPContext instance.
|
|
261
|
+
*/
|
|
262
|
+
var AuthenticatorClient = class {
|
|
263
|
+
/**
|
|
264
|
+
* Registered guards
|
|
265
|
+
*/
|
|
266
|
+
#config;
|
|
267
|
+
/**
|
|
268
|
+
* Cache of guards
|
|
269
|
+
*/
|
|
270
|
+
#guardsCache = {};
|
|
271
|
+
/**
|
|
272
|
+
* Name of the default guard configured in the auth configuration
|
|
273
|
+
*
|
|
274
|
+
* @example
|
|
275
|
+
* const client = new AuthenticatorClient({ default: 'web', guards: {} })
|
|
276
|
+
* console.log(client.defaultGuard) // 'web'
|
|
277
|
+
*/
|
|
278
|
+
get defaultGuard() {
|
|
279
|
+
return this.#config.default;
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Creates a new AuthenticatorClient instance for testing
|
|
283
|
+
*
|
|
284
|
+
* @param config - Configuration object containing default guard and available guards
|
|
285
|
+
*
|
|
286
|
+
* @example
|
|
287
|
+
* const client = new AuthenticatorClient({
|
|
288
|
+
* default: 'web',
|
|
289
|
+
* guards: { web: sessionGuard }
|
|
290
|
+
* })
|
|
291
|
+
*/
|
|
292
|
+
constructor(config) {
|
|
293
|
+
this.#config = config;
|
|
294
|
+
debug_default("creating authenticator client. config %O", this.#config);
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Returns an instance of a known guard. Guards instances are
|
|
298
|
+
* cached during the lifecycle of an HTTP request.
|
|
299
|
+
*
|
|
300
|
+
* @param guard - Optional guard name. Uses default guard if not provided
|
|
301
|
+
*
|
|
302
|
+
* @example
|
|
303
|
+
* const sessionGuard = client.use('session')
|
|
304
|
+
* const defaultGuard = client.use()
|
|
305
|
+
*/
|
|
306
|
+
use(guard) {
|
|
307
|
+
const guardToUse = guard || this.#config.default;
|
|
308
|
+
/**
|
|
309
|
+
* Use cached copy if exists
|
|
310
|
+
*/
|
|
311
|
+
const cachedGuard = this.#guardsCache[guardToUse];
|
|
312
|
+
if (cachedGuard) {
|
|
313
|
+
debug_default("authenticator client: using guard from cache. name: \"%s\"", guardToUse);
|
|
314
|
+
return cachedGuard;
|
|
315
|
+
}
|
|
316
|
+
const guardFactory = this.#config.guards[guardToUse];
|
|
317
|
+
/**
|
|
318
|
+
* Construct guard and cache it
|
|
319
|
+
*/
|
|
320
|
+
debug_default("authenticator client: creating guard. name: \"%s\"", guardToUse);
|
|
321
|
+
const guardInstance = guardFactory(new HttpContextFactory().create());
|
|
322
|
+
this.#guardsCache[guardToUse] = guardInstance;
|
|
323
|
+
return guardInstance;
|
|
324
|
+
}
|
|
325
|
+
};
|
|
326
|
+
//#endregion
|
|
327
|
+
//#region src/auth_manager.ts
|
|
328
|
+
/**
|
|
329
|
+
* Auth manager exposes the API to register and manage authentication
|
|
330
|
+
* guards from the config
|
|
331
|
+
*/
|
|
332
|
+
var AuthManager = class {
|
|
333
|
+
/**
|
|
334
|
+
* Name of the default guard configured in the auth configuration
|
|
335
|
+
*
|
|
336
|
+
* @example
|
|
337
|
+
* const manager = new AuthManager({ default: 'web', guards: {} })
|
|
338
|
+
* console.log(manager.defaultGuard) // 'web'
|
|
339
|
+
*/
|
|
340
|
+
get defaultGuard() {
|
|
341
|
+
return this.config.default;
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Creates a new AuthManager instance
|
|
345
|
+
*
|
|
346
|
+
* @param config - Configuration object containing default guard and available guards
|
|
347
|
+
*
|
|
348
|
+
* @example
|
|
349
|
+
* const manager = new AuthManager({
|
|
350
|
+
* default: 'web',
|
|
351
|
+
* guards: { web: sessionGuard, api: tokenGuard }
|
|
352
|
+
* })
|
|
353
|
+
*/
|
|
354
|
+
constructor(config) {
|
|
355
|
+
this.config = config;
|
|
356
|
+
this.config = config;
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Create an authenticator for a given HTTP request. The authenticator
|
|
360
|
+
* is used to authenticate incoming HTTP requests
|
|
361
|
+
*
|
|
362
|
+
* @param ctx - The HTTP context for the current request
|
|
363
|
+
*
|
|
364
|
+
* @example
|
|
365
|
+
* const authenticator = manager.createAuthenticator(ctx)
|
|
366
|
+
* const user = await authenticator.authenticate()
|
|
367
|
+
*/
|
|
368
|
+
createAuthenticator(ctx) {
|
|
369
|
+
return new Authenticator(ctx, this.config);
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Creates an instance of the authenticator client. The client is
|
|
373
|
+
* used to setup authentication state during testing.
|
|
374
|
+
*
|
|
375
|
+
* @example
|
|
376
|
+
* const client = manager.createAuthenticatorClient()
|
|
377
|
+
* const guard = client.use('session')
|
|
378
|
+
*/
|
|
379
|
+
createAuthenticatorClient() {
|
|
380
|
+
return new AuthenticatorClient(this.config);
|
|
381
|
+
}
|
|
382
|
+
};
|
|
383
|
+
//#endregion
|
|
384
|
+
export { AuthenticatorClient as n, Authenticator as r, AuthManager as t };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { debuglog } from "node:util";
|
|
2
|
+
//#region src/debug.ts
|
|
3
|
+
/**
|
|
4
|
+
* Debug logger instance for the AdonisJS auth package.
|
|
5
|
+
* Set NODE_DEBUG=adonisjs:auth environment variable to enable debug logging.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* debug('authenticating user %s', user.email)
|
|
9
|
+
*/
|
|
10
|
+
var debug_default = debuglog("adonisjs:auth");
|
|
11
|
+
//#endregion
|
|
12
|
+
export { debug_default as t };
|
|
@@ -1,25 +1,59 @@
|
|
|
1
|
-
import "node:module";
|
|
2
1
|
import { Exception } from "@adonisjs/core/exceptions";
|
|
2
|
+
//#region \0rolldown/runtime.js
|
|
3
3
|
var __defProp = Object.defineProperty;
|
|
4
|
-
var __exportAll = (all,
|
|
4
|
+
var __exportAll = (all, no_symbols) => {
|
|
5
5
|
let target = {};
|
|
6
6
|
for (var name in all) __defProp(target, name, {
|
|
7
7
|
get: all[name],
|
|
8
8
|
enumerable: true
|
|
9
9
|
});
|
|
10
|
-
if (
|
|
10
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
11
11
|
return target;
|
|
12
12
|
};
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region src/errors.ts
|
|
13
15
|
var errors_exports = /* @__PURE__ */ __exportAll({
|
|
14
16
|
E_INVALID_CREDENTIALS: () => E_INVALID_CREDENTIALS,
|
|
15
17
|
E_UNAUTHORIZED_ACCESS: () => E_UNAUTHORIZED_ACCESS
|
|
16
18
|
});
|
|
19
|
+
/**
|
|
20
|
+
* The "E_UNAUTHORIZED_ACCESS" exception is raised when unable to
|
|
21
|
+
* authenticate an incoming HTTP request.
|
|
22
|
+
*
|
|
23
|
+
* The "error.guardDriverName" can be used to know the driver which
|
|
24
|
+
* raised the error.
|
|
25
|
+
*/
|
|
17
26
|
const E_UNAUTHORIZED_ACCESS = class extends Exception {
|
|
27
|
+
/**
|
|
28
|
+
* HTTP status code for unauthorized access
|
|
29
|
+
*/
|
|
18
30
|
static status = 401;
|
|
31
|
+
/**
|
|
32
|
+
* Error code identifier
|
|
33
|
+
*/
|
|
19
34
|
static code = "E_UNAUTHORIZED_ACCESS";
|
|
35
|
+
/**
|
|
36
|
+
* Endpoint to redirect to. Only used by "session" driver
|
|
37
|
+
* renderer
|
|
38
|
+
*/
|
|
20
39
|
redirectTo;
|
|
40
|
+
/**
|
|
41
|
+
* Translation identifier. Can be customized
|
|
42
|
+
*/
|
|
21
43
|
identifier = "errors.E_UNAUTHORIZED_ACCESS";
|
|
44
|
+
/**
|
|
45
|
+
* The guard name reference that raised the exception. It allows
|
|
46
|
+
* us to customize the logic of handling the exception.
|
|
47
|
+
*/
|
|
22
48
|
guardDriverName;
|
|
49
|
+
/**
|
|
50
|
+
* A collection of renderers to render the exception to a
|
|
51
|
+
* response.
|
|
52
|
+
*
|
|
53
|
+
* The collection is a key-value pair, where the key is
|
|
54
|
+
* the guard driver name and value is a factory function
|
|
55
|
+
* to respond to the request.
|
|
56
|
+
*/
|
|
23
57
|
renderers = {
|
|
24
58
|
session: (message, error, ctx) => {
|
|
25
59
|
switch (ctx.request.accepts([
|
|
@@ -31,8 +65,11 @@ const E_UNAUTHORIZED_ACCESS = class extends Exception {
|
|
|
31
65
|
case null:
|
|
32
66
|
ctx.session.flashExcept(["_csrf"]);
|
|
33
67
|
ctx.session.flash("error", message);
|
|
68
|
+
/**
|
|
69
|
+
* The "flashErrors" call must be removed in the future
|
|
70
|
+
*/
|
|
34
71
|
ctx.session.flashErrors({ [error.code]: message });
|
|
35
|
-
ctx.response.redirect(error.redirectTo || "/"
|
|
72
|
+
ctx.response.redirect().withIntendedUrl().withQs().toPath(error.redirectTo || "/");
|
|
36
73
|
break;
|
|
37
74
|
case "json":
|
|
38
75
|
ctx.response.status(error.status).send({ errors: [{ message }] });
|
|
@@ -70,15 +107,49 @@ const E_UNAUTHORIZED_ACCESS = class extends Exception {
|
|
|
70
107
|
}
|
|
71
108
|
}
|
|
72
109
|
};
|
|
110
|
+
/**
|
|
111
|
+
* Returns the message to be sent in the HTTP response.
|
|
112
|
+
* Feel free to override this method and return a custom
|
|
113
|
+
* response.
|
|
114
|
+
*
|
|
115
|
+
* @param error - The error instance
|
|
116
|
+
* @param ctx - The HTTP context
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* const message = error.getResponseMessage(error, ctx)
|
|
120
|
+
* console.log('Error message:', message)
|
|
121
|
+
*/
|
|
73
122
|
getResponseMessage(error, ctx) {
|
|
74
123
|
if ("i18n" in ctx) return ctx.i18n.t(error.identifier, {}, error.message);
|
|
75
124
|
return error.message;
|
|
76
125
|
}
|
|
126
|
+
/**
|
|
127
|
+
* Creates a new E_UNAUTHORIZED_ACCESS exception
|
|
128
|
+
*
|
|
129
|
+
* @param message - The error message
|
|
130
|
+
* @param options - Options including redirectTo and guardDriverName
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* throw new E_UNAUTHORIZED_ACCESS('Access denied', {
|
|
134
|
+
* guardDriverName: 'session',
|
|
135
|
+
* redirectTo: '/login'
|
|
136
|
+
* })
|
|
137
|
+
*/
|
|
77
138
|
constructor(message, options) {
|
|
78
139
|
super(message, {});
|
|
79
140
|
this.guardDriverName = options.guardDriverName;
|
|
80
141
|
this.redirectTo = options.redirectTo;
|
|
81
142
|
}
|
|
143
|
+
/**
|
|
144
|
+
* Converts exception to an HTTP response
|
|
145
|
+
*
|
|
146
|
+
* @param error - The error instance
|
|
147
|
+
* @param ctx - The HTTP context
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* // This method is called automatically by AdonisJS
|
|
151
|
+
* await error.handle(error, ctx)
|
|
152
|
+
*/
|
|
82
153
|
async handle(error, ctx) {
|
|
83
154
|
const renderer = this.renderers[this.guardDriverName];
|
|
84
155
|
const message = error.getResponseMessage(error, ctx);
|
|
@@ -86,14 +157,51 @@ const E_UNAUTHORIZED_ACCESS = class extends Exception {
|
|
|
86
157
|
return renderer(message, error, ctx);
|
|
87
158
|
}
|
|
88
159
|
};
|
|
160
|
+
/**
|
|
161
|
+
* Exception is raised when user credentials are invalid
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* throw new E_INVALID_CREDENTIALS('Invalid email or password')
|
|
165
|
+
*/
|
|
89
166
|
const E_INVALID_CREDENTIALS = class extends Exception {
|
|
167
|
+
/**
|
|
168
|
+
* HTTP status code for invalid credentials
|
|
169
|
+
*/
|
|
90
170
|
static status = 400;
|
|
171
|
+
/**
|
|
172
|
+
* Error code identifier
|
|
173
|
+
*/
|
|
91
174
|
static code = "E_INVALID_CREDENTIALS";
|
|
175
|
+
/**
|
|
176
|
+
* Translation identifier. Can be customized
|
|
177
|
+
*/
|
|
92
178
|
identifier = "errors.E_INVALID_CREDENTIALS";
|
|
179
|
+
/**
|
|
180
|
+
* Returns the message to be sent in the HTTP response.
|
|
181
|
+
* Feel free to override this method and return a custom
|
|
182
|
+
* response.
|
|
183
|
+
*
|
|
184
|
+
* @param error - The error instance
|
|
185
|
+
* @param ctx - The HTTP context
|
|
186
|
+
*
|
|
187
|
+
* @example
|
|
188
|
+
* const message = error.getResponseMessage(error, ctx)
|
|
189
|
+
* console.log('Error message:', message)
|
|
190
|
+
*/
|
|
93
191
|
getResponseMessage(error, ctx) {
|
|
94
192
|
if ("i18n" in ctx) return ctx.i18n.t(error.identifier, {}, error.message);
|
|
95
193
|
return error.message;
|
|
96
194
|
}
|
|
195
|
+
/**
|
|
196
|
+
* Converts exception to an HTTP response
|
|
197
|
+
*
|
|
198
|
+
* @param error - The error instance
|
|
199
|
+
* @param ctx - The HTTP context
|
|
200
|
+
*
|
|
201
|
+
* @example
|
|
202
|
+
* // This method is called automatically by AdonisJS
|
|
203
|
+
* await error.handle(error, ctx)
|
|
204
|
+
*/
|
|
97
205
|
async handle(error, ctx) {
|
|
98
206
|
const message = this.getResponseMessage(error, ctx);
|
|
99
207
|
switch (ctx.request.accepts([
|
|
@@ -111,6 +219,9 @@ const E_INVALID_CREDENTIALS = class extends Exception {
|
|
|
111
219
|
"password_confirmation"
|
|
112
220
|
]);
|
|
113
221
|
ctx.session.flash("error", message);
|
|
222
|
+
/**
|
|
223
|
+
* The "flashErrors" call must be removed in the future
|
|
224
|
+
*/
|
|
114
225
|
ctx.session.flashErrors({ [error.code]: message });
|
|
115
226
|
ctx.response.redirect("back", true);
|
|
116
227
|
} else ctx.response.status(error.status).send(message);
|
|
@@ -127,4 +238,5 @@ const E_INVALID_CREDENTIALS = class extends Exception {
|
|
|
127
238
|
}
|
|
128
239
|
}
|
|
129
240
|
};
|
|
241
|
+
//#endregion
|
|
130
242
|
export { __exportAll as i, E_UNAUTHORIZED_ACCESS as n, errors_exports as r, E_INVALID_CREDENTIALS as t };
|
package/build/index.js
CHANGED
|
@@ -1,12 +1,19 @@
|
|
|
1
|
-
import { r as errors_exports } from "./errors-
|
|
2
|
-
import { t as symbols_exports } from "./symbols-
|
|
3
|
-
import "./
|
|
4
|
-
import { n as AuthenticatorClient, r as Authenticator, t as AuthManager } from "./auth_manager-CJvMGOzX.js";
|
|
1
|
+
import { r as errors_exports } from "./errors-eDV8ejO0.js";
|
|
2
|
+
import { t as symbols_exports } from "./symbols-C5QEqFvJ.js";
|
|
3
|
+
import { n as AuthenticatorClient, r as Authenticator, t as AuthManager } from "./auth_manager-Cp_ofh4p.js";
|
|
5
4
|
import { presetAuth } from "@adonisjs/presets/auth";
|
|
6
5
|
import { configProvider } from "@adonisjs/core";
|
|
6
|
+
//#region configure.ts
|
|
7
|
+
/**
|
|
8
|
+
* Configures the auth package
|
|
9
|
+
*/
|
|
7
10
|
async function configure(command) {
|
|
8
11
|
const codemods = await command.createCodemods();
|
|
9
12
|
let guard = command.parsedFlags.guard;
|
|
13
|
+
/**
|
|
14
|
+
* Prompts user to select a guard when not mentioned via
|
|
15
|
+
* the CLI
|
|
16
|
+
*/
|
|
10
17
|
if (guard === void 0) guard = await command.prompt.choice("Select the auth guard you want to use", [
|
|
11
18
|
{
|
|
12
19
|
name: "session",
|
|
@@ -23,6 +30,10 @@ async function configure(command) {
|
|
|
23
30
|
], { validate(value) {
|
|
24
31
|
return !!value;
|
|
25
32
|
} });
|
|
33
|
+
/**
|
|
34
|
+
* Ensure selected or guard defined via the CLI flag is
|
|
35
|
+
* valid
|
|
36
|
+
*/
|
|
26
37
|
if (![
|
|
27
38
|
"session",
|
|
28
39
|
"access_tokens",
|
|
@@ -37,6 +48,31 @@ async function configure(command) {
|
|
|
37
48
|
userProvider: "lucid"
|
|
38
49
|
});
|
|
39
50
|
}
|
|
51
|
+
//#endregion
|
|
52
|
+
//#region src/define_config.ts
|
|
53
|
+
/**
|
|
54
|
+
* Define configuration for the auth package. The function returns
|
|
55
|
+
* a config provider that is invoked inside the auth service
|
|
56
|
+
* provider
|
|
57
|
+
*
|
|
58
|
+
* @param config - Configuration object with default guard and available guards
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* import { defineConfig } from '@adonisjs/auth'
|
|
62
|
+
* import { sessionGuard, sessionUserProvider } from '@adonisjs/auth/session'
|
|
63
|
+
*
|
|
64
|
+
* const authConfig = defineConfig({
|
|
65
|
+
* default: 'web',
|
|
66
|
+
* guards: {
|
|
67
|
+
* web: sessionGuard({
|
|
68
|
+
* useRememberMeTokens: false,
|
|
69
|
+
* provider: sessionUserProvider({
|
|
70
|
+
* model: () => import('#models/user')
|
|
71
|
+
* })
|
|
72
|
+
* })
|
|
73
|
+
* }
|
|
74
|
+
* })
|
|
75
|
+
*/
|
|
40
76
|
function defineConfig(config) {
|
|
41
77
|
return configProvider.create(async (app) => {
|
|
42
78
|
const guardsList = Object.keys(config.guards);
|
|
@@ -52,6 +88,8 @@ function defineConfig(config) {
|
|
|
52
88
|
};
|
|
53
89
|
});
|
|
54
90
|
}
|
|
91
|
+
//#endregion
|
|
92
|
+
//#region index.ts
|
|
55
93
|
function isModuleInstalled(moduleName) {
|
|
56
94
|
try {
|
|
57
95
|
import.meta.resolve(moduleName);
|
|
@@ -60,9 +98,13 @@ function isModuleInstalled(moduleName) {
|
|
|
60
98
|
return false;
|
|
61
99
|
}
|
|
62
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* @deprecated Import `withAuthFinder` from `@adonisjs/auth/mixins/lucid` instead
|
|
103
|
+
*/
|
|
63
104
|
let withAuthFinder;
|
|
64
105
|
if (isModuleInstalled("@adonisjs/lucid")) {
|
|
65
106
|
const { withAuthFinder: withAuthFinderFn } = await import("./src/mixins/lucid.js");
|
|
66
107
|
withAuthFinder = withAuthFinderFn;
|
|
67
108
|
}
|
|
109
|
+
//#endregion
|
|
68
110
|
export { AuthManager, Authenticator, AuthenticatorClient, configure, defineConfig, errors_exports as errors, symbols_exports as symbols, withAuthFinder };
|