@adonisjs/auth 9.0.0-9 → 9.0.1
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/chunk-3HZHOWKL.js +8 -0
- package/build/chunk-3HZHOWKL.js.map +1 -0
- package/build/chunk-CZCFTIBB.js +21 -0
- package/build/chunk-CZCFTIBB.js.map +1 -0
- package/build/chunk-OL2Z3AO5.js +246 -0
- package/build/chunk-OL2Z3AO5.js.map +1 -0
- package/build/chunk-UGHJLKDI.js +197 -0
- package/build/chunk-UGHJLKDI.js.map +1 -0
- package/build/index.d.ts +7 -6
- package/build/index.js +149 -15
- package/build/index.js.map +1 -0
- package/build/modules/access_tokens_guard/access_token.d.ts +137 -0
- package/build/modules/access_tokens_guard/crc32.d.ts +16 -0
- package/build/modules/access_tokens_guard/define_config.d.ts +17 -0
- package/build/modules/access_tokens_guard/guard.d.ts +81 -0
- package/build/modules/access_tokens_guard/main.d.ts +5 -0
- package/build/modules/access_tokens_guard/main.js +929 -0
- package/build/modules/access_tokens_guard/main.js.map +1 -0
- package/build/modules/access_tokens_guard/token_providers/db.d.ts +76 -0
- package/build/modules/access_tokens_guard/types.d.ts +208 -0
- package/build/modules/access_tokens_guard/types.js +1 -0
- package/build/modules/access_tokens_guard/types.js.map +1 -0
- package/build/modules/access_tokens_guard/user_providers/lucid.d.ts +53 -0
- package/build/modules/session_guard/define_config.d.ts +17 -0
- package/build/modules/session_guard/guard.d.ts +98 -0
- package/build/modules/session_guard/main.d.ts +5 -0
- package/build/modules/session_guard/main.js +741 -0
- package/build/modules/session_guard/main.js.map +1 -0
- package/build/modules/session_guard/remember_me_token.d.ts +88 -0
- package/build/modules/session_guard/token_providers/db.d.ts +69 -0
- package/build/modules/session_guard/types.d.ts +243 -0
- package/build/modules/session_guard/types.js +1 -0
- package/build/modules/session_guard/types.js.map +1 -0
- package/build/modules/session_guard/user_providers/lucid.d.ts +57 -0
- package/build/providers/auth_provider.d.ts +1 -1
- package/build/providers/auth_provider.js +31 -27
- package/build/providers/auth_provider.js.map +1 -0
- package/build/services/auth.d.ts +1 -1
- package/build/services/auth.js +10 -15
- package/build/services/auth.js.map +1 -0
- package/build/src/{auth/auth_manager.d.ts → auth_manager.d.ts} +8 -3
- package/build/src/authenticator.d.ts +81 -0
- package/build/src/{auth/authenticator_client.d.ts → authenticator_client.d.ts} +4 -4
- package/build/src/{auth/define_config.d.ts → define_config.d.ts} +0 -10
- package/build/src/errors.d.ts +105 -0
- package/build/src/{auth/middleware → middleware}/initialize_auth_middleware.d.ts +3 -2
- package/build/src/middleware/initialize_auth_middleware.js +17 -0
- package/build/src/middleware/initialize_auth_middleware.js.map +1 -0
- package/build/src/mixins/with_auth_finder.d.ts +156 -0
- package/build/src/{auth/plugins → plugins}/japa/api_client.d.ts +8 -8
- package/build/src/plugins/japa/api_client.js +59 -0
- package/build/src/plugins/japa/api_client.js.map +1 -0
- package/build/src/{auth/plugins → plugins}/japa/browser_client.d.ts +10 -7
- package/build/src/plugins/japa/browser_client.js +67 -0
- package/build/src/plugins/japa/browser_client.js.map +1 -0
- package/build/src/{auth/types.d.ts → types.d.ts} +31 -26
- package/build/src/types.js +1 -0
- package/build/src/types.js.map +1 -0
- package/package.json +88 -68
- package/build/configure.js +0 -47
- package/build/factories/basic_auth_guard_factory.d.ts +0 -12
- package/build/factories/basic_auth_guard_factory.js +0 -22
- package/build/factories/database_token_factory.d.ts +0 -36
- package/build/factories/database_token_factory.js +0 -54
- package/build/factories/database_user_provider.d.ts +0 -14
- package/build/factories/database_user_provider.js +0 -27
- package/build/factories/lucid_user_provider.d.ts +0 -28
- package/build/factories/lucid_user_provider.js +0 -68
- package/build/factories/main.d.ts +0 -4
- package/build/factories/main.js +0 -12
- package/build/factories/session_guard_factory.d.ts +0 -13
- package/build/factories/session_guard_factory.js +0 -24
- package/build/src/auth/auth_manager.js +0 -41
- package/build/src/auth/authenticator.d.ts +0 -63
- package/build/src/auth/authenticator.js +0 -129
- package/build/src/auth/authenticator_client.js +0 -59
- package/build/src/auth/debug.js +0 -10
- package/build/src/auth/define_config.js +0 -54
- package/build/src/auth/errors.d.ts +0 -90
- package/build/src/auth/errors.js +0 -201
- package/build/src/auth/middleware/initialize_auth_middleware.js +0 -25
- package/build/src/auth/plugins/japa/api_client.js +0 -63
- package/build/src/auth/plugins/japa/browser_client.js +0 -64
- package/build/src/auth/symbols.js +0 -17
- package/build/src/auth/types.js +0 -9
- package/build/src/auth/user_providers/main.d.ts +0 -15
- package/build/src/auth/user_providers/main.js +0 -22
- package/build/src/core/guard_user.d.ts +0 -26
- package/build/src/core/guard_user.js +0 -29
- package/build/src/core/token.d.ts +0 -89
- package/build/src/core/token.js +0 -114
- package/build/src/core/token_providers/database.d.ts +0 -77
- package/build/src/core/token_providers/database.js +0 -113
- package/build/src/core/types.d.ts +0 -178
- package/build/src/core/types.js +0 -9
- package/build/src/core/user_providers/database.d.ts +0 -78
- package/build/src/core/user_providers/database.js +0 -117
- package/build/src/core/user_providers/lucid.d.ts +0 -61
- package/build/src/core/user_providers/lucid.js +0 -122
- package/build/src/guards/basic_auth/define_config.d.ts +0 -16
- package/build/src/guards/basic_auth/define_config.js +0 -38
- package/build/src/guards/basic_auth/guard.d.ts +0 -70
- package/build/src/guards/basic_auth/guard.js +0 -190
- package/build/src/guards/basic_auth/main.d.ts +0 -2
- package/build/src/guards/basic_auth/main.js +0 -10
- package/build/src/guards/basic_auth/types.d.ts +0 -35
- package/build/src/guards/basic_auth/types.js +0 -9
- package/build/src/guards/session/define_config.d.ts +0 -23
- package/build/src/guards/session/define_config.js +0 -56
- package/build/src/guards/session/guard.d.ts +0 -123
- package/build/src/guards/session/guard.js +0 -510
- package/build/src/guards/session/main.d.ts +0 -3
- package/build/src/guards/session/main.js +0 -11
- package/build/src/guards/session/token.d.ts +0 -57
- package/build/src/guards/session/token.js +0 -58
- package/build/src/guards/session/token_providers/main.d.ts +0 -33
- package/build/src/guards/session/token_providers/main.js +0 -42
- package/build/src/guards/session/types.d.ts +0 -104
- package/build/src/guards/session/types.js +0 -9
- package/build/stubs/main.d.ts +0 -1
- package/build/stubs/main.js +0 -10
- package/build/stubs/middleware/auth_middleware.stub +0 -30
- package/build/stubs/middleware/guest_middleware.stub +0 -36
- /package/build/src/{auth/debug.d.ts → debug.d.ts} +0 -0
- /package/build/src/{auth/symbols.d.ts → symbols.d.ts} +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/debug.ts"],"sourcesContent":["/*\n * @adonisjs/auth\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport { debuglog } from 'node:util'\n\nexport default debuglog('adonisjs:auth')\n"],"mappings":";AASA,SAAS,gBAAgB;AAEzB,IAAO,gBAAQ,SAAS,eAAe;","names":[]}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __export = (target, all) => {
|
|
4
|
+
for (var name in all)
|
|
5
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
6
|
+
};
|
|
7
|
+
var __decorateClass = (decorators, target, key, kind) => {
|
|
8
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
9
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
10
|
+
if (decorator = decorators[i])
|
|
11
|
+
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
12
|
+
if (kind && result)
|
|
13
|
+
__defProp(target, key, result);
|
|
14
|
+
return result;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export {
|
|
18
|
+
__export,
|
|
19
|
+
__decorateClass
|
|
20
|
+
};
|
|
21
|
+
//# sourceMappingURL=chunk-CZCFTIBB.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import {
|
|
2
|
+
debug_default
|
|
3
|
+
} from "./chunk-3HZHOWKL.js";
|
|
4
|
+
import {
|
|
5
|
+
E_UNAUTHORIZED_ACCESS
|
|
6
|
+
} from "./chunk-UGHJLKDI.js";
|
|
7
|
+
|
|
8
|
+
// src/authenticator.ts
|
|
9
|
+
import { RuntimeException } from "@adonisjs/core/exceptions";
|
|
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
|
|
43
|
+
*/
|
|
44
|
+
get defaultGuard() {
|
|
45
|
+
return this.#config.default;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Reference to the guard using which the current
|
|
49
|
+
* request has been authenticated.
|
|
50
|
+
*/
|
|
51
|
+
get authenticatedViaGuard() {
|
|
52
|
+
return this.#authenticatedViaGuard;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* A boolean to know if the current request has been authenticated. The
|
|
56
|
+
* property returns false when "authenticate" or "authenticateUsing"
|
|
57
|
+
* methods are not used.
|
|
58
|
+
*/
|
|
59
|
+
get isAuthenticated() {
|
|
60
|
+
if (!this.#authenticationAttemptedViaGuard) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
return this.use(this.#authenticationAttemptedViaGuard).isAuthenticated;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Reference to the currently authenticated user. The property returns
|
|
67
|
+
* undefined when "authenticate" or "authenticateUsing" methods are
|
|
68
|
+
* not used.
|
|
69
|
+
*/
|
|
70
|
+
get user() {
|
|
71
|
+
if (!this.#authenticationAttemptedViaGuard) {
|
|
72
|
+
return void 0;
|
|
73
|
+
}
|
|
74
|
+
return this.use(this.#authenticationAttemptedViaGuard).user;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Whether or not the authentication has been attempted during
|
|
78
|
+
* the current request. The property returns false when the
|
|
79
|
+
* "authenticate" or "authenticateUsing" methods are not
|
|
80
|
+
* used.
|
|
81
|
+
*/
|
|
82
|
+
get authenticationAttempted() {
|
|
83
|
+
if (!this.#authenticationAttemptedViaGuard) {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
return this.use(this.#authenticationAttemptedViaGuard).authenticationAttempted;
|
|
87
|
+
}
|
|
88
|
+
constructor(ctx, config) {
|
|
89
|
+
this.#ctx = ctx;
|
|
90
|
+
this.#config = config;
|
|
91
|
+
debug_default("creating authenticator. config %O", this.#config);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Returns an instance of the logged-in user or throws an
|
|
95
|
+
* exception
|
|
96
|
+
*/
|
|
97
|
+
getUserOrFail() {
|
|
98
|
+
if (!this.#authenticationAttemptedViaGuard) {
|
|
99
|
+
throw new RuntimeException(
|
|
100
|
+
'Cannot access authenticated user. Please call "auth.authenticate" method first.'
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
return this.use(this.#authenticationAttemptedViaGuard).getUserOrFail();
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Returns an instance of a known guard. Guards instances are
|
|
107
|
+
* cached during the lifecycle of an HTTP request.
|
|
108
|
+
*/
|
|
109
|
+
use(guard) {
|
|
110
|
+
const guardToUse = guard || this.#config.default;
|
|
111
|
+
const cachedGuard = this.#guardsCache[guardToUse];
|
|
112
|
+
if (cachedGuard) {
|
|
113
|
+
debug_default('authenticator: using guard from cache. name: "%s"', guardToUse);
|
|
114
|
+
return cachedGuard;
|
|
115
|
+
}
|
|
116
|
+
const guardFactory = this.#config.guards[guardToUse];
|
|
117
|
+
debug_default('authenticator: creating guard. name: "%s"', guardToUse);
|
|
118
|
+
const guardInstance = guardFactory(this.#ctx);
|
|
119
|
+
this.#guardsCache[guardToUse] = guardInstance;
|
|
120
|
+
return guardInstance;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Authenticate current request using the default guard. Calling this
|
|
124
|
+
* method multiple times triggers multiple authentication with the
|
|
125
|
+
* guard.
|
|
126
|
+
*/
|
|
127
|
+
async authenticate() {
|
|
128
|
+
await this.authenticateUsing();
|
|
129
|
+
return this.getUserOrFail();
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Silently attempt to authenticate the request using the default
|
|
133
|
+
* guard. Calling this method multiple times triggers multiple
|
|
134
|
+
* authentication with the guard.
|
|
135
|
+
*/
|
|
136
|
+
async check() {
|
|
137
|
+
this.#authenticationAttemptedViaGuard = this.defaultGuard;
|
|
138
|
+
const isAuthenticated = await this.use().check();
|
|
139
|
+
if (isAuthenticated) {
|
|
140
|
+
this.#authenticatedViaGuard = this.defaultGuard;
|
|
141
|
+
}
|
|
142
|
+
return isAuthenticated;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Authenticate the request using all of the mentioned guards
|
|
146
|
+
* or the default guard.
|
|
147
|
+
*
|
|
148
|
+
* The authentication process will stop after any of the mentioned
|
|
149
|
+
* guards is able to authenticate the request successfully.
|
|
150
|
+
*
|
|
151
|
+
* Otherwise, "E_UNAUTHORIZED_ACCESS" will be raised.
|
|
152
|
+
*/
|
|
153
|
+
async authenticateUsing(guards, options) {
|
|
154
|
+
const guardsToUse = guards || [this.defaultGuard];
|
|
155
|
+
let lastUsedDriver;
|
|
156
|
+
for (let guardName of guardsToUse) {
|
|
157
|
+
debug_default('attempting to authenticate using guard "%s"', guardName);
|
|
158
|
+
this.#authenticationAttemptedViaGuard = guardName;
|
|
159
|
+
const guard = this.use(guardName);
|
|
160
|
+
lastUsedDriver = guard.driverName;
|
|
161
|
+
if (await guard.check()) {
|
|
162
|
+
this.#authenticatedViaGuard = guardName;
|
|
163
|
+
return this.getUserOrFail();
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
throw new E_UNAUTHORIZED_ACCESS("Unauthorized access", {
|
|
167
|
+
guardDriverName: lastUsedDriver,
|
|
168
|
+
redirectTo: options?.loginRoute
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
// src/authenticator_client.ts
|
|
174
|
+
import { HttpContextFactory } from "@adonisjs/core/factories/http";
|
|
175
|
+
var AuthenticatorClient = class {
|
|
176
|
+
/**
|
|
177
|
+
* Registered guards
|
|
178
|
+
*/
|
|
179
|
+
#config;
|
|
180
|
+
/**
|
|
181
|
+
* Cache of guards
|
|
182
|
+
*/
|
|
183
|
+
#guardsCache = {};
|
|
184
|
+
/**
|
|
185
|
+
* Name of the default guard
|
|
186
|
+
*/
|
|
187
|
+
get defaultGuard() {
|
|
188
|
+
return this.#config.default;
|
|
189
|
+
}
|
|
190
|
+
constructor(config) {
|
|
191
|
+
this.#config = config;
|
|
192
|
+
debug_default("creating authenticator client. config %O", this.#config);
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Returns an instance of a known guard. Guards instances are
|
|
196
|
+
* cached during the lifecycle of an HTTP request.
|
|
197
|
+
*/
|
|
198
|
+
use(guard) {
|
|
199
|
+
const guardToUse = guard || this.#config.default;
|
|
200
|
+
const cachedGuard = this.#guardsCache[guardToUse];
|
|
201
|
+
if (cachedGuard) {
|
|
202
|
+
debug_default('authenticator client: using guard from cache. name: "%s"', guardToUse);
|
|
203
|
+
return cachedGuard;
|
|
204
|
+
}
|
|
205
|
+
const guardFactory = this.#config.guards[guardToUse];
|
|
206
|
+
debug_default('authenticator client: creating guard. name: "%s"', guardToUse);
|
|
207
|
+
const guardInstance = guardFactory(new HttpContextFactory().create());
|
|
208
|
+
this.#guardsCache[guardToUse] = guardInstance;
|
|
209
|
+
return guardInstance;
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
// src/auth_manager.ts
|
|
214
|
+
var AuthManager = class {
|
|
215
|
+
constructor(config) {
|
|
216
|
+
this.config = config;
|
|
217
|
+
this.config = config;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Name of the default guard
|
|
221
|
+
*/
|
|
222
|
+
get defaultGuard() {
|
|
223
|
+
return this.config.default;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Create an authenticator for a given HTTP request. The authenticator
|
|
227
|
+
* is used to authenticated in incoming HTTP request
|
|
228
|
+
*/
|
|
229
|
+
createAuthenticator(ctx) {
|
|
230
|
+
return new Authenticator(ctx, this.config);
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Creates an instance of the authenticator client. The client is
|
|
234
|
+
* used to setup authentication state during testing.
|
|
235
|
+
*/
|
|
236
|
+
createAuthenticatorClient() {
|
|
237
|
+
return new AuthenticatorClient(this.config);
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
export {
|
|
242
|
+
Authenticator,
|
|
243
|
+
AuthenticatorClient,
|
|
244
|
+
AuthManager
|
|
245
|
+
};
|
|
246
|
+
//# sourceMappingURL=chunk-OL2Z3AO5.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/authenticator.ts","../src/authenticator_client.ts","../src/auth_manager.ts"],"sourcesContent":["/*\n * @adonisjs/auth\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport type { HttpContext } from '@adonisjs/core/http'\nimport { RuntimeException } from '@adonisjs/core/exceptions'\n\nimport debug from './debug.js'\nimport type { GuardFactory } from './types.js'\nimport { E_UNAUTHORIZED_ACCESS } from './errors.js'\n\n/**\n * Authenticator is used to authenticate incoming HTTP requests\n * using one or more known guards.\n */\nexport class Authenticator<KnownGuards extends Record<string, GuardFactory>> {\n /**\n * Registered guards\n */\n #config: {\n default: keyof KnownGuards\n guards: KnownGuards\n }\n\n /**\n * Cache of guards created during the HTTP request\n */\n #guardsCache: Partial<Record<keyof KnownGuards, unknown>> = {}\n\n /**\n * Last guard that was used to perform the authentication via\n * the \"authenticateUsing\" method.\n *\n * @note\n * Reset on every call made to \"authenticate\", \"check\" and\n * \"authenticateUsing\" method.\n */\n #authenticationAttemptedViaGuard?: keyof KnownGuards\n\n /**\n * Name of the guard using which the request has\n * been authenticated successfully.\n *\n * @note\n * Reset on every call made to \"authenticate\", \"check\" and\n * \"authenticateUsing\" method.\n */\n #authenticatedViaGuard?: keyof KnownGuards\n\n /**\n * Reference to HTTP context\n */\n #ctx: HttpContext\n\n /**\n * Name of the default guard\n */\n get defaultGuard(): keyof KnownGuards {\n return this.#config.default\n }\n\n /**\n * Reference to the guard using which the current\n * request has been authenticated.\n */\n get authenticatedViaGuard(): keyof KnownGuards | undefined {\n return this.#authenticatedViaGuard\n }\n\n /**\n * A boolean to know if the current request has been authenticated. The\n * property returns false when \"authenticate\" or \"authenticateUsing\"\n * methods are not used.\n */\n get isAuthenticated(): boolean {\n if (!this.#authenticationAttemptedViaGuard) {\n return false\n }\n\n return this.use(this.#authenticationAttemptedViaGuard).isAuthenticated\n }\n\n /**\n * Reference to the currently authenticated user. The property returns\n * undefined when \"authenticate\" or \"authenticateUsing\" methods are\n * not used.\n */\n get user(): {\n [K in keyof KnownGuards]: ReturnType<KnownGuards[K]>['user']\n }[keyof KnownGuards] {\n if (!this.#authenticationAttemptedViaGuard) {\n return undefined\n }\n\n return this.use(this.#authenticationAttemptedViaGuard).user\n }\n\n /**\n * Whether or not the authentication has been attempted during\n * the current request. The property returns false when the\n * \"authenticate\" or \"authenticateUsing\" methods are not\n * used.\n */\n get authenticationAttempted(): boolean {\n if (!this.#authenticationAttemptedViaGuard) {\n return false\n }\n\n return this.use(this.#authenticationAttemptedViaGuard).authenticationAttempted\n }\n\n constructor(ctx: HttpContext, config: { default: keyof KnownGuards; guards: KnownGuards }) {\n this.#ctx = ctx\n this.#config = config\n debug('creating authenticator. config %O', this.#config)\n }\n\n /**\n * Returns an instance of the logged-in user or throws an\n * exception\n */\n getUserOrFail(): {\n [K in keyof KnownGuards]: ReturnType<ReturnType<KnownGuards[K]>['getUserOrFail']>\n }[keyof KnownGuards] {\n if (!this.#authenticationAttemptedViaGuard) {\n throw new RuntimeException(\n 'Cannot access authenticated user. Please call \"auth.authenticate\" method first.'\n )\n }\n\n return this.use(this.#authenticationAttemptedViaGuard).getUserOrFail() as {\n [K in keyof KnownGuards]: ReturnType<ReturnType<KnownGuards[K]>['getUserOrFail']>\n }[keyof KnownGuards]\n }\n\n /**\n * Returns an instance of a known guard. Guards instances are\n * cached during the lifecycle of an HTTP request.\n */\n use<Guard extends keyof KnownGuards>(guard?: Guard): ReturnType<KnownGuards[Guard]> {\n const guardToUse = guard || this.#config.default\n\n /**\n * Use cached copy if exists\n */\n const cachedGuard = this.#guardsCache[guardToUse]\n if (cachedGuard) {\n debug('authenticator: using guard from cache. name: \"%s\"', guardToUse)\n return cachedGuard as ReturnType<KnownGuards[Guard]>\n }\n\n const guardFactory = this.#config.guards[guardToUse]\n\n /**\n * Construct guard and cache it\n */\n debug('authenticator: creating guard. name: \"%s\"', guardToUse)\n const guardInstance = guardFactory(this.#ctx)\n this.#guardsCache[guardToUse] = guardInstance\n\n return guardInstance as ReturnType<KnownGuards[Guard]>\n }\n\n /**\n * Authenticate current request using the default guard. Calling this\n * method multiple times triggers multiple authentication with the\n * guard.\n */\n async authenticate() {\n await this.authenticateUsing()\n return this.getUserOrFail()\n }\n\n /**\n * Silently attempt to authenticate the request using the default\n * guard. Calling this method multiple times triggers multiple\n * authentication with the guard.\n */\n async check() {\n this.#authenticationAttemptedViaGuard = this.defaultGuard\n const isAuthenticated = await this.use().check()\n if (isAuthenticated) {\n this.#authenticatedViaGuard = this.defaultGuard\n }\n\n return isAuthenticated\n }\n\n /**\n * Authenticate the request using all of the mentioned guards\n * or the default guard.\n *\n * The authentication process will stop after any of the mentioned\n * guards is able to authenticate the request successfully.\n *\n * Otherwise, \"E_UNAUTHORIZED_ACCESS\" will be raised.\n */\n async authenticateUsing(\n guards?: (keyof KnownGuards)[],\n options?: { loginRoute?: string }\n ): Promise<\n {\n [K in keyof KnownGuards]: ReturnType<ReturnType<KnownGuards[K]>['getUserOrFail']>\n }[keyof KnownGuards]\n > {\n const guardsToUse = guards || [this.defaultGuard]\n let lastUsedDriver: string | undefined\n\n for (let guardName of guardsToUse) {\n debug('attempting to authenticate using guard \"%s\"', guardName)\n\n this.#authenticationAttemptedViaGuard = guardName\n const guard = this.use(guardName)\n lastUsedDriver = guard.driverName\n\n if (await guard.check()) {\n this.#authenticatedViaGuard = guardName\n return this.getUserOrFail()\n }\n }\n\n throw new E_UNAUTHORIZED_ACCESS('Unauthorized access', {\n guardDriverName: lastUsedDriver!,\n redirectTo: options?.loginRoute,\n })\n }\n}\n","/*\n * @adonisjs/auth\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport debug from './debug.js'\nimport type { GuardFactory } from './types.js'\nimport { HttpContextFactory } from '@adonisjs/core/factories/http'\n\n/**\n * Authenticator client is used to create guard instances for testing.\n * It passes a fake HTTPContext to the guards, so make sure to not\n * call server side APIs that might be relying on a real\n * HTTPContext instance.\n */\nexport class AuthenticatorClient<KnownGuards extends Record<string, GuardFactory>> {\n /**\n * Registered guards\n */\n #config: {\n default: keyof KnownGuards\n guards: KnownGuards\n }\n\n /**\n * Cache of guards\n */\n #guardsCache: Partial<Record<keyof KnownGuards, unknown>> = {}\n\n /**\n * Name of the default guard\n */\n get defaultGuard(): keyof KnownGuards {\n return this.#config.default\n }\n\n constructor(config: { default: keyof KnownGuards; guards: KnownGuards }) {\n this.#config = config\n debug('creating authenticator client. config %O', this.#config)\n }\n\n /**\n * Returns an instance of a known guard. Guards instances are\n * cached during the lifecycle of an HTTP request.\n */\n use<Guard extends keyof KnownGuards>(guard?: Guard): ReturnType<KnownGuards[Guard]> {\n const guardToUse = guard || this.#config.default\n\n /**\n * Use cached copy if exists\n */\n const cachedGuard = this.#guardsCache[guardToUse]\n if (cachedGuard) {\n debug('authenticator client: using guard from cache. name: \"%s\"', guardToUse)\n return cachedGuard as ReturnType<KnownGuards[Guard]>\n }\n\n const guardFactory = this.#config.guards[guardToUse]\n\n /**\n * Construct guard and cache it\n */\n debug('authenticator client: creating guard. name: \"%s\"', guardToUse)\n const guardInstance = guardFactory(new HttpContextFactory().create())\n this.#guardsCache[guardToUse] = guardInstance\n\n return guardInstance as ReturnType<KnownGuards[Guard]>\n }\n}\n","/*\n * @adonisjs/auth\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport type { HttpContext } from '@adonisjs/core/http'\n\nimport type { GuardFactory } from './types.js'\nimport { Authenticator } from './authenticator.js'\nimport { AuthenticatorClient } from './authenticator_client.js'\n\n/**\n * Auth manager exposes the API to register and manage authentication\n * guards from the config\n */\nexport class AuthManager<KnownGuards extends Record<string, GuardFactory>> {\n /**\n * Name of the default guard\n */\n get defaultGuard() {\n return this.config.default\n }\n\n constructor(public config: { default: keyof KnownGuards; guards: KnownGuards }) {\n this.config = config\n }\n\n /**\n * Create an authenticator for a given HTTP request. The authenticator\n * is used to authenticated in incoming HTTP request\n */\n createAuthenticator(ctx: HttpContext) {\n return new Authenticator<KnownGuards>(ctx, this.config)\n }\n\n /**\n * Creates an instance of the authenticator client. The client is\n * used to setup authentication state during testing.\n */\n createAuthenticatorClient() {\n return new AuthenticatorClient<KnownGuards>(this.config)\n }\n}\n"],"mappings":";;;;;;;;AAUA,SAAS,wBAAwB;AAU1B,IAAM,gBAAN,MAAsE;AAAA;AAAA;AAAA;AAAA,EAI3E;AAAA;AAAA;AAAA;AAAA,EAQA,eAA4D,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAU7D;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA;AAAA;AAAA;AAAA;AAAA,EAKA;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,eAAkC;AACpC,WAAO,KAAK,QAAQ;AAAA,EACtB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,IAAI,wBAAuD;AACzD,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,IAAI,kBAA2B;AAC7B,QAAI,CAAC,KAAK,kCAAkC;AAC1C,aAAO;AAAA,IACT;AAEA,WAAO,KAAK,IAAI,KAAK,gCAAgC,EAAE;AAAA,EACzD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,IAAI,OAEiB;AACnB,QAAI,CAAC,KAAK,kCAAkC;AAC1C,aAAO;AAAA,IACT;AAEA,WAAO,KAAK,IAAI,KAAK,gCAAgC,EAAE;AAAA,EACzD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,IAAI,0BAAmC;AACrC,QAAI,CAAC,KAAK,kCAAkC;AAC1C,aAAO;AAAA,IACT;AAEA,WAAO,KAAK,IAAI,KAAK,gCAAgC,EAAE;AAAA,EACzD;AAAA,EAEA,YAAY,KAAkB,QAA6D;AACzF,SAAK,OAAO;AACZ,SAA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iC,KAAK,MAAM;AAAA,EACzD;AACF;","names":[]}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__export
|
|
3
|
+
} from "./chunk-CZCFTIBB.js";
|
|
4
|
+
|
|
5
|
+
// src/errors.ts
|
|
6
|
+
var errors_exports = {};
|
|
7
|
+
__export(errors_exports, {
|
|
8
|
+
E_INVALID_CREDENTIALS: () => E_INVALID_CREDENTIALS,
|
|
9
|
+
E_UNAUTHORIZED_ACCESS: () => E_UNAUTHORIZED_ACCESS
|
|
10
|
+
});
|
|
11
|
+
import { Exception } from "@adonisjs/core/exceptions";
|
|
12
|
+
var E_UNAUTHORIZED_ACCESS = class extends Exception {
|
|
13
|
+
static status = 401;
|
|
14
|
+
static code = "E_UNAUTHORIZED_ACCESS";
|
|
15
|
+
/**
|
|
16
|
+
* Endpoint to redirect to. Only used by "session" driver
|
|
17
|
+
* renderer
|
|
18
|
+
*/
|
|
19
|
+
redirectTo;
|
|
20
|
+
/**
|
|
21
|
+
* Translation identifier. Can be customized
|
|
22
|
+
*/
|
|
23
|
+
identifier = "errors.E_UNAUTHORIZED_ACCESS";
|
|
24
|
+
/**
|
|
25
|
+
* The guard name reference that raised the exception. It allows
|
|
26
|
+
* us to customize the logic of handling the exception.
|
|
27
|
+
*/
|
|
28
|
+
guardDriverName;
|
|
29
|
+
/**
|
|
30
|
+
* A collection of renderers to render the exception to a
|
|
31
|
+
* response.
|
|
32
|
+
*
|
|
33
|
+
* The collection is a key-value pair, where the key is
|
|
34
|
+
* the guard driver name and value is a factory function
|
|
35
|
+
* to respond to the request.
|
|
36
|
+
*/
|
|
37
|
+
renderers = {
|
|
38
|
+
/**
|
|
39
|
+
* Response when session driver is used
|
|
40
|
+
*/
|
|
41
|
+
session: (message, error, ctx) => {
|
|
42
|
+
switch (ctx.request.accepts(["html", "application/vnd.api+json", "json"])) {
|
|
43
|
+
case "html":
|
|
44
|
+
case null:
|
|
45
|
+
ctx.session.flashExcept(["_csrf"]);
|
|
46
|
+
ctx.session.flashErrors({ [error.code]: message });
|
|
47
|
+
ctx.response.redirect(error.redirectTo || "/", true);
|
|
48
|
+
break;
|
|
49
|
+
case "json":
|
|
50
|
+
ctx.response.status(error.status).send({
|
|
51
|
+
errors: [
|
|
52
|
+
{
|
|
53
|
+
message
|
|
54
|
+
}
|
|
55
|
+
]
|
|
56
|
+
});
|
|
57
|
+
break;
|
|
58
|
+
case "application/vnd.api+json":
|
|
59
|
+
ctx.response.status(error.status).send({
|
|
60
|
+
errors: [
|
|
61
|
+
{
|
|
62
|
+
code: error.code,
|
|
63
|
+
title: message
|
|
64
|
+
}
|
|
65
|
+
]
|
|
66
|
+
});
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
/**
|
|
71
|
+
* Response when basic auth driver is used
|
|
72
|
+
*/
|
|
73
|
+
basic_auth: (message, _, ctx) => {
|
|
74
|
+
ctx.response.status(this.status).header("WWW-Authenticate", `Basic realm="Authenticate", charset="UTF-8"`).send(message);
|
|
75
|
+
},
|
|
76
|
+
/**
|
|
77
|
+
* Response when access tokens driver is used
|
|
78
|
+
*/
|
|
79
|
+
access_tokens: (message, error, ctx) => {
|
|
80
|
+
switch (ctx.request.accepts(["html", "application/vnd.api+json", "json"])) {
|
|
81
|
+
case "html":
|
|
82
|
+
case null:
|
|
83
|
+
ctx.response.status(error.status).send(message);
|
|
84
|
+
break;
|
|
85
|
+
case "json":
|
|
86
|
+
ctx.response.status(error.status).send({
|
|
87
|
+
errors: [
|
|
88
|
+
{
|
|
89
|
+
message
|
|
90
|
+
}
|
|
91
|
+
]
|
|
92
|
+
});
|
|
93
|
+
break;
|
|
94
|
+
case "application/vnd.api+json":
|
|
95
|
+
ctx.response.status(error.status).send({
|
|
96
|
+
errors: [
|
|
97
|
+
{
|
|
98
|
+
code: error.code,
|
|
99
|
+
title: message
|
|
100
|
+
}
|
|
101
|
+
]
|
|
102
|
+
});
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* Returns the message to be sent in the HTTP response.
|
|
109
|
+
* Feel free to override this method and return a custom
|
|
110
|
+
* response.
|
|
111
|
+
*/
|
|
112
|
+
getResponseMessage(error, ctx) {
|
|
113
|
+
if ("i18n" in ctx) {
|
|
114
|
+
return ctx.i18n.t(error.identifier, {}, error.message);
|
|
115
|
+
}
|
|
116
|
+
return error.message;
|
|
117
|
+
}
|
|
118
|
+
constructor(message, options) {
|
|
119
|
+
super(message, {});
|
|
120
|
+
this.guardDriverName = options.guardDriverName;
|
|
121
|
+
this.redirectTo = options.redirectTo;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Converts exception to an HTTP response
|
|
125
|
+
*/
|
|
126
|
+
async handle(error, ctx) {
|
|
127
|
+
const renderer = this.renderers[this.guardDriverName];
|
|
128
|
+
const message = error.getResponseMessage(error, ctx);
|
|
129
|
+
if (!renderer) {
|
|
130
|
+
return ctx.response.status(error.status).send(message);
|
|
131
|
+
}
|
|
132
|
+
return renderer(message, error, ctx);
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
var E_INVALID_CREDENTIALS = class extends Exception {
|
|
136
|
+
static status = 400;
|
|
137
|
+
static code = "E_INVALID_CREDENTIALS";
|
|
138
|
+
/**
|
|
139
|
+
* Translation identifier. Can be customized
|
|
140
|
+
*/
|
|
141
|
+
identifier = "errors.E_INVALID_CREDENTIALS";
|
|
142
|
+
/**
|
|
143
|
+
* Returns the message to be sent in the HTTP response.
|
|
144
|
+
* Feel free to override this method and return a custom
|
|
145
|
+
* response.
|
|
146
|
+
*/
|
|
147
|
+
getResponseMessage(error, ctx) {
|
|
148
|
+
if ("i18n" in ctx) {
|
|
149
|
+
return ctx.i18n.t(error.identifier, {}, error.message);
|
|
150
|
+
}
|
|
151
|
+
return error.message;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Converts exception to an HTTP response
|
|
155
|
+
*/
|
|
156
|
+
async handle(error, ctx) {
|
|
157
|
+
const message = this.getResponseMessage(error, ctx);
|
|
158
|
+
switch (ctx.request.accepts(["html", "application/vnd.api+json", "json"])) {
|
|
159
|
+
case "html":
|
|
160
|
+
case null:
|
|
161
|
+
if (ctx.session) {
|
|
162
|
+
ctx.session.flashExcept(["_csrf", "_method", "password", "password_confirmation"]);
|
|
163
|
+
ctx.session.flashErrors({ [error.code]: message });
|
|
164
|
+
ctx.response.redirect("back", true);
|
|
165
|
+
} else {
|
|
166
|
+
ctx.response.status(error.status).send(message);
|
|
167
|
+
}
|
|
168
|
+
break;
|
|
169
|
+
case "json":
|
|
170
|
+
ctx.response.status(error.status).send({
|
|
171
|
+
errors: [
|
|
172
|
+
{
|
|
173
|
+
message
|
|
174
|
+
}
|
|
175
|
+
]
|
|
176
|
+
});
|
|
177
|
+
break;
|
|
178
|
+
case "application/vnd.api+json":
|
|
179
|
+
ctx.response.status(error.status).send({
|
|
180
|
+
errors: [
|
|
181
|
+
{
|
|
182
|
+
code: error.code,
|
|
183
|
+
title: message
|
|
184
|
+
}
|
|
185
|
+
]
|
|
186
|
+
});
|
|
187
|
+
break;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
export {
|
|
193
|
+
E_UNAUTHORIZED_ACCESS,
|
|
194
|
+
E_INVALID_CREDENTIALS,
|
|
195
|
+
errors_exports
|
|
196
|
+
};
|
|
197
|
+
//# sourceMappingURL=chunk-UGHJLKDI.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/errors.ts"],"sourcesContent":["/*\n * @adonisjs/auth\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport type { I18n } from '@adonisjs/i18n'\nimport { Exception } from '@adonisjs/core/exceptions'\nimport type { HttpContext } from '@adonisjs/core/http'\n\n/**\n * The \"E_UNAUTHORIZED_ACCESS\" exception is raised when unable to\n * authenticate an incoming HTTP request.\n *\n * The \"error.guardDriverName\" can be used to know the driver which\n * raised the error.\n */\nexport const E_UNAUTHORIZED_ACCESS = class extends Exception {\n static status: number = 401\n static code: string = 'E_UNAUTHORIZED_ACCESS'\n\n /**\n * Endpoint to redirect to. Only used by \"session\" driver\n * renderer\n */\n redirectTo?: string\n\n /**\n * Translation identifier. Can be customized\n */\n identifier: string = 'errors.E_UNAUTHORIZED_ACCESS'\n\n /**\n * The guard name reference that raised the exception. It allows\n * us to customize the logic of handling the exception.\n */\n guardDriverName: string\n\n /**\n * A collection of renderers to render the exception to a\n * response.\n *\n * The collection is a key-value pair, where the key is\n * the guard driver name and value is a factory function\n * to respond to the request.\n */\n renderers: Record<\n string,\n (message: string, error: this, ctx: HttpContext) => Promise<void> | void\n > = {\n /**\n * Response when session driver is used\n */\n session: (message, error, ctx) => {\n switch (ctx.request.accepts(['html', 'application/vnd.api+json', 'json'])) {\n case 'html':\n case null:\n ctx.session.flashExcept(['_csrf'])\n ctx.session.flashErrors({ [error.code!]: message })\n ctx.response.redirect(error.redirectTo || '/', true)\n break\n case 'json':\n ctx.response.status(error.status).send({\n errors: [\n {\n message,\n },\n ],\n })\n break\n case 'application/vnd.api+json':\n ctx.response.status(error.status).send({\n errors: [\n {\n code: error.code,\n title: message,\n },\n ],\n })\n break\n }\n },\n\n /**\n * Response when basic auth driver is used\n */\n basic_auth: (message, _, ctx) => {\n ctx.response\n .status(this.status)\n .header('WWW-Authenticate', `Basic realm=\"Authenticate\", charset=\"UTF-8\"`)\n .send(message)\n },\n\n /**\n * Response when access tokens driver is used\n */\n access_tokens: (message, error, ctx) => {\n switch (ctx.request.accepts(['html', 'application/vnd.api+json', 'json'])) {\n case 'html':\n case null:\n ctx.response.status(error.status).send(message)\n break\n case 'json':\n ctx.response.status(error.status).send({\n errors: [\n {\n message,\n },\n ],\n })\n break\n case 'application/vnd.api+json':\n ctx.response.status(error.status).send({\n errors: [\n {\n code: error.code,\n title: message,\n },\n ],\n })\n break\n }\n },\n }\n\n /**\n * Returns the message to be sent in the HTTP response.\n * Feel free to override this method and return a custom\n * response.\n */\n getResponseMessage(error: this, ctx: HttpContext) {\n if ('i18n' in ctx) {\n return (ctx.i18n as I18n).t(error.identifier, {}, error.message)\n }\n return error.message\n }\n\n constructor(\n message: string,\n options: {\n redirectTo?: string\n guardDriverName: string\n }\n ) {\n super(message, {})\n this.guardDriverName = options.guardDriverName\n this.redirectTo = options.redirectTo\n }\n\n /**\n * Converts exception to an HTTP response\n */\n async handle(error: this, ctx: HttpContext) {\n const renderer = this.renderers[this.guardDriverName]\n const message = error.getResponseMessage(error, ctx)\n\n if (!renderer) {\n return ctx.response.status(error.status).send(message)\n }\n\n return renderer(message, error, ctx)\n }\n}\n\n/**\n * Exception is raised when user credentials are invalid\n */\nexport const E_INVALID_CREDENTIALS = class extends Exception {\n static status: number = 400\n static code: string = 'E_INVALID_CREDENTIALS'\n\n /**\n * Translation identifier. Can be customized\n */\n identifier: string = 'errors.E_INVALID_CREDENTIALS'\n\n /**\n * Returns the message to be sent in the HTTP response.\n * Feel free to override this method and return a custom\n * response.\n */\n getResponseMessage(error: this, ctx: HttpContext) {\n if ('i18n' in ctx) {\n return (ctx.i18n as I18n).t(error.identifier, {}, error.message)\n }\n return error.message\n }\n\n /**\n * Converts exception to an HTTP response\n */\n async handle(error: this, ctx: HttpContext) {\n const message = this.getResponseMessage(error, ctx)\n\n switch (ctx.request.accepts(['html', 'application/vnd.api+json', 'json'])) {\n case 'html':\n case null:\n if (ctx.session) {\n ctx.session.flashExcept(['_csrf', '_method', 'password', 'password_confirmation'])\n ctx.session.flashErrors({ [error.code!]: message })\n ctx.response.redirect('back', true)\n } else {\n ctx.response.status(error.status).send(message)\n }\n break\n case 'json':\n ctx.response.status(error.status).send({\n errors: [\n {\n message,\n },\n ],\n })\n break\n case 'application/vnd.api+json':\n ctx.response.status(error.status).send({\n errors: [\n {\n code: error.code,\n title: message,\n },\n ],\n })\n break\n }\n }\n}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A;AAAA;AAAA;AAAA,EAKtB,aAAqB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOrB,mBAAmB,OAAa,KAAkB;AAChD,QAAI,UAAU,KAAK;AACjB,aAAQ,IAAI,KAAc,EAAE,MAAM,YAAY,CAAC,GAAG,MAAM,OAAO;AAAA,IACjE;AACA,WAAO,MAAM;AAAA,EACf;AAAA;AAAA;AAAA;AAAA,EAKA,MAAM,OAAO,OAAa,KAAkB;AAC1C,UAAM,UAAU,KAAK,mBAAmB,OAAO,GAAG;AAElD,YAAQ,IAAI,QAAQ,QAAQ,CAAC,QAAQ,4BAA4B,MAAM,CAAC,GAAG;AAAA,MACzE,KAAK;AAAA,MACL,KAAK;AACH,YAAI,IAAI,SAAS;AACf,cAAI,QAAQ,YAAY,CAAC,SAAS,WAAW,YAAY,uBAAuB,CAAC;AACjF,cAAI,QAAQ,YAAY,EAAE,CAAC,MAAM,IAAK,GAAG,QAAQ,CAAC;AAClD,cAAI,SAAS,SAAS,QAAQ,IAAI;AAAA,QACpC,OAAO;AACL,cAAI,SAAS,OAAO,MAAM,MAAM,EAAE,KAAK,OAAO;AAAA,QAChD;AACA;AAAA,MACF,KAAK;AACH,YAAI,SAAS,OAAO,MAAM,MAAM,EAAE,KAAK;AAAA,UACrC,QAAQ;AAAA,YACN;AAAA,cACE;AAAA,YACF;AAAA,UACF;AAAA,QACF,CAAC;AACD;AAAA,MACF,KAAK;AACH,YAAI,SAAS,OAAO,MAAM,MAAM,EAAE,KAAK;AAAA,UACrC,QAAQ;AAAA,YACN;AAAA,cACE,MAAM,MAAM;AAAA,cACZ,OAAO;AAAA,YACT;AAAA,UACF;AAAA,QACF,CAAC;AACD;AAAA,IACJ;AAAA,EACF;AACF;","names":[]}
|
package/build/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
export * as errors from './src/errors.js';
|
|
1
2
|
export { configure } from './configure.js';
|
|
2
|
-
export
|
|
3
|
-
export
|
|
4
|
-
export {
|
|
5
|
-
export { Authenticator } from './src/
|
|
6
|
-
export {
|
|
7
|
-
export {
|
|
3
|
+
export * as symbols from './src/symbols.js';
|
|
4
|
+
export { AuthManager } from './src/auth_manager.js';
|
|
5
|
+
export { defineConfig } from './src/define_config.js';
|
|
6
|
+
export { Authenticator } from './src/authenticator.js';
|
|
7
|
+
export { withAuthFinder } from './src/mixins/with_auth_finder.js';
|
|
8
|
+
export { AuthenticatorClient } from './src/authenticator_client.js';
|