@adonisjs/auth 10.0.0-next.2 → 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.
- package/build/auth_manager-BMz_OJXq.js +129 -0
- package/build/debug-Ckko95-M.js +3 -0
- package/build/errors-BQxhZmkE.js +128 -0
- package/build/index.js +59 -100
- package/build/modules/access_tokens_guard/main.js +647 -1309
- package/build/modules/access_tokens_guard/types.js +1 -0
- package/build/modules/basic_auth_guard/main.js +106 -236
- package/build/modules/basic_auth_guard/types.js +1 -0
- package/build/modules/session_guard/main.js +377 -966
- package/build/modules/session_guard/types.js +1 -0
- package/build/providers/auth_provider.js +16 -40
- package/build/services/auth.js +3 -8
- package/build/src/debug.d.ts +1 -1
- package/build/src/middleware/initialize_auth_middleware.js +6 -24
- package/build/src/mixins/lucid.js +43 -95
- package/build/src/plugins/japa/api_client.js +43 -56
- package/build/src/plugins/japa/browser_client.js +45 -64
- package/build/src/types.js +1 -0
- package/build/symbols-Ct253Khf.js +8 -0
- package/package.json +40 -37
- package/build/chunk-2VRS2VHB.js +0 -7
- package/build/chunk-MSPAYMZE.js +0 -351
- package/build/chunk-S5G5RTJX.js +0 -236
- package/build/chunk-UXA4FHST.js +0 -19
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,245 +1,115 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
} from "
|
|
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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
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
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
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
|
-
|
|
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 {};
|