@adonisjs/auth 10.0.0 → 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-sGy-K8pd.js → errors-eDV8ejO0.js} +114 -2
- package/build/index.js +46 -4
- package/build/modules/access_tokens_guard/main.js +757 -2
- 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-BQLDWwuQ.js → symbols-C5QEqFvJ.js} +10 -1
- package/package.json +20 -20
- package/build/auth_manager-hJTiBA2V.js +0 -129
- package/build/debug-Ckko95-M.js +0 -3
|
@@ -1,23 +1,95 @@
|
|
|
1
|
-
import { n as E_UNAUTHORIZED_ACCESS } from "../../errors-
|
|
2
|
-
import "../../symbols-
|
|
1
|
+
import { n as E_UNAUTHORIZED_ACCESS } from "../../errors-eDV8ejO0.js";
|
|
2
|
+
import "../../symbols-C5QEqFvJ.js";
|
|
3
3
|
import { RuntimeException } from "@adonisjs/core/exceptions";
|
|
4
4
|
import { base64 } from "@adonisjs/core/helpers";
|
|
5
5
|
import auth from "basic-auth";
|
|
6
|
+
//#region modules/basic_auth_guard/guard.ts
|
|
7
|
+
/**
|
|
8
|
+
* BasicAuth guard implements the HTTP Authentication protocol
|
|
9
|
+
*
|
|
10
|
+
* @template UserProvider - The user provider contract
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const guard = new BasicAuthGuard(
|
|
14
|
+
* 'basic',
|
|
15
|
+
* ctx,
|
|
16
|
+
* emitter,
|
|
17
|
+
* userProvider
|
|
18
|
+
* )
|
|
19
|
+
*
|
|
20
|
+
* const user = await guard.authenticate()
|
|
21
|
+
* console.log('Authenticated user:', user.email)
|
|
22
|
+
*/
|
|
6
23
|
var BasicAuthGuard = class {
|
|
24
|
+
/**
|
|
25
|
+
* A unique name for the guard.
|
|
26
|
+
*/
|
|
7
27
|
#name;
|
|
28
|
+
/**
|
|
29
|
+
* Reference to the current HTTP context
|
|
30
|
+
*/
|
|
8
31
|
#ctx;
|
|
32
|
+
/**
|
|
33
|
+
* Provider to lookup user details
|
|
34
|
+
*/
|
|
9
35
|
#userProvider;
|
|
36
|
+
/**
|
|
37
|
+
* Emitter to emit events
|
|
38
|
+
*/
|
|
10
39
|
#emitter;
|
|
40
|
+
/**
|
|
41
|
+
* Driver name of the guard
|
|
42
|
+
*/
|
|
11
43
|
driverName = "basic_auth";
|
|
44
|
+
/**
|
|
45
|
+
* Whether or not the authentication has been attempted
|
|
46
|
+
* during the current request.
|
|
47
|
+
*/
|
|
12
48
|
authenticationAttempted = false;
|
|
49
|
+
/**
|
|
50
|
+
* A boolean to know if the current request has
|
|
51
|
+
* been authenticated
|
|
52
|
+
*/
|
|
13
53
|
isAuthenticated = false;
|
|
54
|
+
/**
|
|
55
|
+
* Reference to an instance of the authenticated user.
|
|
56
|
+
* The value only exists after calling one of the
|
|
57
|
+
* following methods.
|
|
58
|
+
*
|
|
59
|
+
* - authenticate
|
|
60
|
+
* - check
|
|
61
|
+
*
|
|
62
|
+
* You can use the "getUserOrFail" method to throw an exception if
|
|
63
|
+
* the request is not authenticated.
|
|
64
|
+
*/
|
|
14
65
|
user;
|
|
66
|
+
/**
|
|
67
|
+
* Creates a new BasicAuthGuard instance
|
|
68
|
+
*
|
|
69
|
+
* @param name - Unique name for the guard instance
|
|
70
|
+
* @param ctx - HTTP context for the current request
|
|
71
|
+
* @param emitter - Event emitter for guard events
|
|
72
|
+
* @param userProvider - User provider for credential verification
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* const guard = new BasicAuthGuard(
|
|
76
|
+
* 'basic',
|
|
77
|
+
* ctx,
|
|
78
|
+
* emitter,
|
|
79
|
+
* new BasicAuthLucidUserProvider()
|
|
80
|
+
* )
|
|
81
|
+
*/
|
|
15
82
|
constructor(name, ctx, emitter, userProvider) {
|
|
16
83
|
this.#name = name;
|
|
17
84
|
this.#ctx = ctx;
|
|
18
85
|
this.#emitter = emitter;
|
|
19
86
|
this.#userProvider = userProvider;
|
|
20
87
|
}
|
|
88
|
+
/**
|
|
89
|
+
* Emits authentication failure, updates the local state,
|
|
90
|
+
* and returns an exception to end the authentication
|
|
91
|
+
* cycle.
|
|
92
|
+
*/
|
|
21
93
|
#authenticationFailed() {
|
|
22
94
|
this.isAuthenticated = false;
|
|
23
95
|
this.user = void 0;
|
|
@@ -29,6 +101,10 @@ var BasicAuthGuard = class {
|
|
|
29
101
|
});
|
|
30
102
|
return error;
|
|
31
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* Emits the authentication succeeded event and updates
|
|
106
|
+
* the local state to reflect successful authentication
|
|
107
|
+
*/
|
|
32
108
|
#authenticationSucceeded(user) {
|
|
33
109
|
this.isAuthenticated = true;
|
|
34
110
|
this.user = user;
|
|
@@ -38,24 +114,75 @@ var BasicAuthGuard = class {
|
|
|
38
114
|
user
|
|
39
115
|
});
|
|
40
116
|
}
|
|
117
|
+
/**
|
|
118
|
+
* Returns an instance of the authenticated user. Or throws
|
|
119
|
+
* an exception if the request is not authenticated.
|
|
120
|
+
*
|
|
121
|
+
* @throws {E_UNAUTHORIZED_ACCESS} When user is not authenticated
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* const user = guard.getUserOrFail()
|
|
125
|
+
* console.log('User:', user.email)
|
|
126
|
+
*/
|
|
41
127
|
getUserOrFail() {
|
|
42
128
|
if (!this.user) throw new E_UNAUTHORIZED_ACCESS("Invalid basic auth credentials", { guardDriverName: this.driverName });
|
|
43
129
|
return this.user;
|
|
44
130
|
}
|
|
131
|
+
/**
|
|
132
|
+
* Authenticates the incoming HTTP request by looking for BasicAuth
|
|
133
|
+
* credentials inside the request authorization header.
|
|
134
|
+
*
|
|
135
|
+
* @throws {E_UNAUTHORIZED_ACCESS} When authentication fails
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* try {
|
|
139
|
+
* const user = await guard.authenticate()
|
|
140
|
+
* console.log('Authenticated as:', user.email)
|
|
141
|
+
* } catch (error) {
|
|
142
|
+
* console.log('Authentication failed')
|
|
143
|
+
* }
|
|
144
|
+
*/
|
|
45
145
|
async authenticate() {
|
|
146
|
+
/**
|
|
147
|
+
* Avoid re-authenticating when already authenticated
|
|
148
|
+
*/
|
|
46
149
|
if (this.authenticationAttempted) return this.getUserOrFail();
|
|
150
|
+
/**
|
|
151
|
+
* Beginning authentication attempt
|
|
152
|
+
*/
|
|
47
153
|
this.authenticationAttempted = true;
|
|
48
154
|
this.#emitter.emit("basic_auth:authentication_attempted", {
|
|
49
155
|
ctx: this.#ctx,
|
|
50
156
|
guardName: this.#name
|
|
51
157
|
});
|
|
158
|
+
/**
|
|
159
|
+
* Fetch credentials from the header or fail
|
|
160
|
+
*/
|
|
52
161
|
const credentials = auth(this.#ctx.request.request);
|
|
53
162
|
if (!credentials) throw this.#authenticationFailed();
|
|
163
|
+
/**
|
|
164
|
+
* Verify user credentials or fail
|
|
165
|
+
*/
|
|
54
166
|
const user = await this.#userProvider.verifyCredentials(credentials.name, credentials.pass);
|
|
55
167
|
if (!user) throw this.#authenticationFailed();
|
|
168
|
+
/**
|
|
169
|
+
* Mark user as authenticated
|
|
170
|
+
*/
|
|
56
171
|
this.#authenticationSucceeded(user.getOriginal());
|
|
57
172
|
return this.getUserOrFail();
|
|
58
173
|
}
|
|
174
|
+
/**
|
|
175
|
+
* Silently attempt to authenticate the user.
|
|
176
|
+
*
|
|
177
|
+
* The method returns a boolean indicating if the authentication
|
|
178
|
+
* succeeded or failed.
|
|
179
|
+
*
|
|
180
|
+
* @example
|
|
181
|
+
* const isAuthenticated = await guard.check()
|
|
182
|
+
* if (isAuthenticated) {
|
|
183
|
+
* console.log('User is authenticated:', guard.user.email)
|
|
184
|
+
* }
|
|
185
|
+
*/
|
|
59
186
|
async check() {
|
|
60
187
|
try {
|
|
61
188
|
await this.authenticate();
|
|
@@ -65,25 +192,82 @@ var BasicAuthGuard = class {
|
|
|
65
192
|
throw error;
|
|
66
193
|
}
|
|
67
194
|
}
|
|
195
|
+
/**
|
|
196
|
+
* Returns the Authorization header clients can use to authenticate
|
|
197
|
+
* the request using basic auth.
|
|
198
|
+
*
|
|
199
|
+
* @param uid - The username or user identifier
|
|
200
|
+
* @param password - The user's password
|
|
201
|
+
*
|
|
202
|
+
* @example
|
|
203
|
+
* const clientAuth = await guard.authenticateAsClient('user@example.com', 'secret')
|
|
204
|
+
* // Use clientAuth.headers.authorization in API tests
|
|
205
|
+
*/
|
|
68
206
|
async authenticateAsClient(uid, password) {
|
|
69
207
|
return { headers: { authorization: `Basic ${base64.encode(`${uid}:${password}`)}` } };
|
|
70
208
|
}
|
|
71
209
|
};
|
|
210
|
+
//#endregion
|
|
211
|
+
//#region modules/basic_auth_guard/user_providers/lucid.ts
|
|
212
|
+
/**
|
|
213
|
+
* Uses a Lucid model to verify access tokens and find a user during
|
|
214
|
+
* authentication
|
|
215
|
+
*
|
|
216
|
+
* @template UserModel - The Lucid model representing the user
|
|
217
|
+
*
|
|
218
|
+
* @example
|
|
219
|
+
* const userProvider = new BasicAuthLucidUserProvider({
|
|
220
|
+
* model: () => import('#models/user')
|
|
221
|
+
* })
|
|
222
|
+
*/
|
|
72
223
|
var BasicAuthLucidUserProvider = class {
|
|
224
|
+
/**
|
|
225
|
+
* Reference to the lazily imported model
|
|
226
|
+
*/
|
|
73
227
|
model;
|
|
228
|
+
/**
|
|
229
|
+
* Creates a new BasicAuthLucidUserProvider instance
|
|
230
|
+
*
|
|
231
|
+
* @param options - Configuration options for the user provider
|
|
232
|
+
*
|
|
233
|
+
* @example
|
|
234
|
+
* const provider = new BasicAuthLucidUserProvider({
|
|
235
|
+
* model: () => import('#models/user')
|
|
236
|
+
* })
|
|
237
|
+
*/
|
|
74
238
|
constructor(options) {
|
|
75
239
|
this.options = options;
|
|
76
240
|
}
|
|
241
|
+
/**
|
|
242
|
+
* Imports the model from the provider, returns and caches it
|
|
243
|
+
* for further operations.
|
|
244
|
+
*
|
|
245
|
+
* @example
|
|
246
|
+
* const UserModel = await provider.getModel()
|
|
247
|
+
* const user = await UserModel.find(1)
|
|
248
|
+
*/
|
|
77
249
|
async getModel() {
|
|
78
250
|
if (this.model && !("hot" in import.meta)) return this.model;
|
|
79
251
|
this.model = (await this.options.model()).default;
|
|
80
252
|
return this.model;
|
|
81
253
|
}
|
|
254
|
+
/**
|
|
255
|
+
* Creates an adapter user for the guard
|
|
256
|
+
*
|
|
257
|
+
* @param user - The user model instance
|
|
258
|
+
*
|
|
259
|
+
* @example
|
|
260
|
+
* const guardUser = await provider.createUserForGuard(user)
|
|
261
|
+
* console.log('User ID:', guardUser.getId())
|
|
262
|
+
*/
|
|
82
263
|
async createUserForGuard(user) {
|
|
83
264
|
const model = await this.getModel();
|
|
84
265
|
if (user instanceof model === false) throw new RuntimeException(`Invalid user object. It must be an instance of the "${model.name}" model`);
|
|
85
266
|
return {
|
|
86
267
|
getId() {
|
|
268
|
+
/**
|
|
269
|
+
* Ensure user has a primary key
|
|
270
|
+
*/
|
|
87
271
|
if (!user.$primaryKeyValue) throw new RuntimeException(`Cannot use "${model.name}" model for authentication. The value of column "${model.primaryKey}" is undefined or null`);
|
|
88
272
|
return user.$primaryKeyValue;
|
|
89
273
|
},
|
|
@@ -92,6 +276,18 @@ var BasicAuthLucidUserProvider = class {
|
|
|
92
276
|
}
|
|
93
277
|
};
|
|
94
278
|
}
|
|
279
|
+
/**
|
|
280
|
+
* Verifies credentials using the underlying model
|
|
281
|
+
*
|
|
282
|
+
* @param uid - The username or user identifier
|
|
283
|
+
* @param password - The password to verify
|
|
284
|
+
*
|
|
285
|
+
* @example
|
|
286
|
+
* const guardUser = await provider.verifyCredentials('user@example.com', 'secret')
|
|
287
|
+
* if (guardUser) {
|
|
288
|
+
* console.log('Valid credentials')
|
|
289
|
+
* }
|
|
290
|
+
*/
|
|
95
291
|
async verifyCredentials(uid, password) {
|
|
96
292
|
const model = await this.getModel();
|
|
97
293
|
try {
|
|
@@ -102,6 +298,20 @@ var BasicAuthLucidUserProvider = class {
|
|
|
102
298
|
}
|
|
103
299
|
}
|
|
104
300
|
};
|
|
301
|
+
//#endregion
|
|
302
|
+
//#region modules/basic_auth_guard/define_config.ts
|
|
303
|
+
/**
|
|
304
|
+
* Configures basic auth guard for authentication using HTTP Basic Authentication
|
|
305
|
+
*
|
|
306
|
+
* @param config - Configuration object containing the user provider
|
|
307
|
+
*
|
|
308
|
+
* @example
|
|
309
|
+
* const guard = basicAuthGuard({
|
|
310
|
+
* provider: basicAuthUserProvider({
|
|
311
|
+
* model: () => import('#models/user')
|
|
312
|
+
* })
|
|
313
|
+
* })
|
|
314
|
+
*/
|
|
105
315
|
function basicAuthGuard(config) {
|
|
106
316
|
return { async resolver(name, app) {
|
|
107
317
|
const emitter = await app.container.make("emitter");
|
|
@@ -109,7 +319,19 @@ function basicAuthGuard(config) {
|
|
|
109
319
|
return (ctx) => new BasicAuthGuard(name, ctx, emitter, provider);
|
|
110
320
|
} };
|
|
111
321
|
}
|
|
322
|
+
/**
|
|
323
|
+
* Configures user provider that uses Lucid models to authenticate
|
|
324
|
+
* users using basic auth credentials
|
|
325
|
+
*
|
|
326
|
+
* @param config - Configuration options for the Lucid user provider
|
|
327
|
+
*
|
|
328
|
+
* @example
|
|
329
|
+
* const userProvider = basicAuthUserProvider({
|
|
330
|
+
* model: () => import('#models/user')
|
|
331
|
+
* })
|
|
332
|
+
*/
|
|
112
333
|
function basicAuthUserProvider(config) {
|
|
113
334
|
return new BasicAuthLucidUserProvider(config);
|
|
114
335
|
}
|
|
336
|
+
//#endregion
|
|
115
337
|
export { BasicAuthGuard, BasicAuthLucidUserProvider, basicAuthGuard, basicAuthUserProvider };
|