@adonisjs/auth 10.0.0-next.1 → 10.0.0-next.3

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.
@@ -0,0 +1 @@
1
+ export {};
@@ -1,245 +1,115 @@
1
- import {
2
- E_UNAUTHORIZED_ACCESS
3
- } from "../../chunk-S5G5RTJX.js";
4
- import "../../chunk-UXA4FHST.js";
5
-
6
- // modules/basic_auth_guard/guard.ts
7
- import auth from "basic-auth";
1
+ import { n as E_UNAUTHORIZED_ACCESS } from "../../errors-BQxhZmkE.js";
2
+ import "../../symbols-Ct253Khf.js";
3
+ import { RuntimeException } from "@adonisjs/core/exceptions";
8
4
  import { base64 } from "@adonisjs/core/helpers";
5
+ import auth from "basic-auth";
9
6
  var BasicAuthGuard = class {
10
- /**
11
- * A unique name for the guard.
12
- */
13
- #name;
14
- /**
15
- * Reference to the current HTTP context
16
- */
17
- #ctx;
18
- /**
19
- * Provider to lookup user details
20
- */
21
- #userProvider;
22
- /**
23
- * Emitter to emit events
24
- */
25
- #emitter;
26
- /**
27
- * Driver name of the guard
28
- */
29
- driverName = "basic_auth";
30
- /**
31
- * Whether or not the authentication has been attempted
32
- * during the current request.
33
- */
34
- authenticationAttempted = false;
35
- /**
36
- * A boolean to know if the current request has
37
- * been authenticated
38
- */
39
- isAuthenticated = false;
40
- /**
41
- * Reference to an instance of the authenticated user.
42
- * The value only exists after calling one of the
43
- * following methods.
44
- *
45
- * - authenticate
46
- * - check
47
- *
48
- * You can use the "getUserOrFail" method to throw an exception if
49
- * the request is not authenticated.
50
- */
51
- user;
52
- constructor(name, ctx, emitter, userProvider) {
53
- this.#name = name;
54
- this.#ctx = ctx;
55
- this.#emitter = emitter;
56
- this.#userProvider = userProvider;
57
- }
58
- /**
59
- * Emits authentication failure, updates the local state,
60
- * and returns an exception to end the authentication
61
- * cycle.
62
- */
63
- #authenticationFailed() {
64
- this.isAuthenticated = false;
65
- this.user = void 0;
66
- const error = new E_UNAUTHORIZED_ACCESS("Invalid basic auth credentials", {
67
- guardDriverName: this.driverName
68
- });
69
- this.#emitter.emit("basic_auth:authentication_failed", {
70
- ctx: this.#ctx,
71
- guardName: this.#name,
72
- error
73
- });
74
- return error;
75
- }
76
- /**
77
- * Emits the authentication succeeded event and updates
78
- * the local state to reflect successful authentication
79
- */
80
- #authenticationSucceeded(user) {
81
- this.isAuthenticated = true;
82
- this.user = user;
83
- this.#emitter.emit("basic_auth:authentication_succeeded", {
84
- ctx: this.#ctx,
85
- guardName: this.#name,
86
- user
87
- });
88
- }
89
- /**
90
- * Returns an instance of the authenticated user. Or throws
91
- * an exception if the request is not authenticated.
92
- */
93
- getUserOrFail() {
94
- if (!this.user) {
95
- throw new E_UNAUTHORIZED_ACCESS("Invalid basic auth credentials", {
96
- guardDriverName: this.driverName
97
- });
98
- }
99
- return this.user;
100
- }
101
- /**
102
- * Authenticates the incoming HTTP request by looking for BasicAuth
103
- * credentials inside the request authorization header.
104
- *
105
- * Returns the authenticated user or throws an exception.
106
- */
107
- async authenticate() {
108
- if (this.authenticationAttempted) {
109
- return this.getUserOrFail();
110
- }
111
- this.authenticationAttempted = true;
112
- this.#emitter.emit("basic_auth:authentication_attempted", {
113
- ctx: this.#ctx,
114
- guardName: this.#name
115
- });
116
- const credentials = auth(this.#ctx.request.request);
117
- if (!credentials) {
118
- throw this.#authenticationFailed();
119
- }
120
- const user = await this.#userProvider.verifyCredentials(credentials.name, credentials.pass);
121
- if (!user) {
122
- throw this.#authenticationFailed();
123
- }
124
- this.#authenticationSucceeded(user.getOriginal());
125
- return this.getUserOrFail();
126
- }
127
- /**
128
- * Silently attempt to authenticate the user.
129
- *
130
- * The method returns a boolean indicating if the authentication
131
- * succeeded or failed.
132
- */
133
- async check() {
134
- try {
135
- await this.authenticate();
136
- return true;
137
- } catch (error) {
138
- if (error instanceof E_UNAUTHORIZED_ACCESS) {
139
- return false;
140
- }
141
- throw error;
142
- }
143
- }
144
- /**
145
- * Does not support authenticating as client. Instead use "basicAuth"
146
- * helper on Japa APIClient
147
- */
148
- async authenticateAsClient(uid, password) {
149
- return {
150
- headers: {
151
- authorization: `Basic ${base64.encode(`${uid}:${password}`)}`
152
- }
153
- };
154
- }
7
+ #name;
8
+ #ctx;
9
+ #userProvider;
10
+ #emitter;
11
+ driverName = "basic_auth";
12
+ authenticationAttempted = false;
13
+ isAuthenticated = false;
14
+ user;
15
+ constructor(name, ctx, emitter, userProvider) {
16
+ this.#name = name;
17
+ this.#ctx = ctx;
18
+ this.#emitter = emitter;
19
+ this.#userProvider = userProvider;
20
+ }
21
+ #authenticationFailed() {
22
+ this.isAuthenticated = false;
23
+ this.user = void 0;
24
+ const error = new E_UNAUTHORIZED_ACCESS("Invalid basic auth credentials", { guardDriverName: this.driverName });
25
+ this.#emitter.emit("basic_auth:authentication_failed", {
26
+ ctx: this.#ctx,
27
+ guardName: this.#name,
28
+ error
29
+ });
30
+ return error;
31
+ }
32
+ #authenticationSucceeded(user) {
33
+ this.isAuthenticated = true;
34
+ this.user = user;
35
+ this.#emitter.emit("basic_auth:authentication_succeeded", {
36
+ ctx: this.#ctx,
37
+ guardName: this.#name,
38
+ user
39
+ });
40
+ }
41
+ getUserOrFail() {
42
+ if (!this.user) throw new E_UNAUTHORIZED_ACCESS("Invalid basic auth credentials", { guardDriverName: this.driverName });
43
+ return this.user;
44
+ }
45
+ async authenticate() {
46
+ if (this.authenticationAttempted) return this.getUserOrFail();
47
+ this.authenticationAttempted = true;
48
+ this.#emitter.emit("basic_auth:authentication_attempted", {
49
+ ctx: this.#ctx,
50
+ guardName: this.#name
51
+ });
52
+ const credentials = auth(this.#ctx.request.request);
53
+ if (!credentials) throw this.#authenticationFailed();
54
+ const user = await this.#userProvider.verifyCredentials(credentials.name, credentials.pass);
55
+ if (!user) throw this.#authenticationFailed();
56
+ this.#authenticationSucceeded(user.getOriginal());
57
+ return this.getUserOrFail();
58
+ }
59
+ async check() {
60
+ try {
61
+ await this.authenticate();
62
+ return true;
63
+ } catch (error) {
64
+ if (error instanceof E_UNAUTHORIZED_ACCESS) return false;
65
+ throw error;
66
+ }
67
+ }
68
+ async authenticateAsClient(uid, password) {
69
+ return { headers: { authorization: `Basic ${base64.encode(`${uid}:${password}`)}` } };
70
+ }
155
71
  };
156
-
157
- // modules/basic_auth_guard/user_providers/lucid.ts
158
- import { RuntimeException } from "@adonisjs/core/exceptions";
159
72
  var BasicAuthLucidUserProvider = class {
160
- /**
161
- * Creates a new BasicAuthLucidUserProvider instance
162
- *
163
- * @param options - Configuration options for the user provider
164
- *
165
- * @example
166
- * const provider = new BasicAuthLucidUserProvider({
167
- * model: () => import('#models/user')
168
- * })
169
- */
170
- constructor(options) {
171
- this.options = options;
172
- }
173
- /**
174
- * Reference to the lazily imported model
175
- */
176
- model;
177
- /**
178
- * Imports the model from the provider, returns and caches it
179
- * for further operations.
180
- */
181
- async getModel() {
182
- if (this.model && !("hot" in import.meta)) {
183
- return this.model;
184
- }
185
- const importedModel = await this.options.model();
186
- this.model = importedModel.default;
187
- return this.model;
188
- }
189
- /**
190
- * Creates an adapter user for the guard
191
- */
192
- async createUserForGuard(user) {
193
- const model = await this.getModel();
194
- if (user instanceof model === false) {
195
- throw new RuntimeException(
196
- `Invalid user object. It must be an instance of the "${model.name}" model`
197
- );
198
- }
199
- return {
200
- getId() {
201
- if (!user.$primaryKeyValue) {
202
- throw new RuntimeException(
203
- `Cannot use "${model.name}" model for authentication. The value of column "${model.primaryKey}" is undefined or null`
204
- );
205
- }
206
- return user.$primaryKeyValue;
207
- },
208
- getOriginal() {
209
- return user;
210
- }
211
- };
212
- }
213
- /**
214
- * Verifies credentials using the underlying model
215
- */
216
- async verifyCredentials(uid, password) {
217
- const model = await this.getModel();
218
- try {
219
- const user = await model.verifyCredentials(uid, password);
220
- return this.createUserForGuard(user);
221
- } catch {
222
- return null;
223
- }
224
- }
73
+ model;
74
+ constructor(options) {
75
+ this.options = options;
76
+ }
77
+ async getModel() {
78
+ if (this.model && !("hot" in import.meta)) return this.model;
79
+ this.model = (await this.options.model()).default;
80
+ return this.model;
81
+ }
82
+ async createUserForGuard(user) {
83
+ const model = await this.getModel();
84
+ if (user instanceof model === false) throw new RuntimeException(`Invalid user object. It must be an instance of the "${model.name}" model`);
85
+ return {
86
+ getId() {
87
+ if (!user.$primaryKeyValue) throw new RuntimeException(`Cannot use "${model.name}" model for authentication. The value of column "${model.primaryKey}" is undefined or null`);
88
+ return user.$primaryKeyValue;
89
+ },
90
+ getOriginal() {
91
+ return user;
92
+ }
93
+ };
94
+ }
95
+ async verifyCredentials(uid, password) {
96
+ const model = await this.getModel();
97
+ try {
98
+ const user = await model.verifyCredentials(uid, password);
99
+ return this.createUserForGuard(user);
100
+ } catch {
101
+ return null;
102
+ }
103
+ }
225
104
  };
226
-
227
- // modules/basic_auth_guard/define_config.ts
228
105
  function basicAuthGuard(config) {
229
- return {
230
- async resolver(name, app) {
231
- const emitter = await app.container.make("emitter");
232
- const provider = "resolver" in config.provider ? await config.provider.resolver(app) : config.provider;
233
- return (ctx) => new BasicAuthGuard(name, ctx, emitter, provider);
234
- }
235
- };
106
+ return { async resolver(name, app) {
107
+ const emitter = await app.container.make("emitter");
108
+ const provider = "resolver" in config.provider ? await config.provider.resolver(app) : config.provider;
109
+ return (ctx) => new BasicAuthGuard(name, ctx, emitter, provider);
110
+ } };
236
111
  }
237
112
  function basicAuthUserProvider(config) {
238
- return new BasicAuthLucidUserProvider(config);
113
+ return new BasicAuthLucidUserProvider(config);
239
114
  }
240
- export {
241
- BasicAuthGuard,
242
- BasicAuthLucidUserProvider,
243
- basicAuthGuard,
244
- basicAuthUserProvider
245
- };
115
+ export { BasicAuthGuard, BasicAuthLucidUserProvider, basicAuthGuard, basicAuthUserProvider };
@@ -0,0 +1 @@
1
+ export {};