@adonisjs/auth 9.0.3 → 9.1.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.
@@ -0,0 +1,17 @@
1
+ import type { HttpContext } from '@adonisjs/core/http';
2
+ import type { ConfigProvider } from '@adonisjs/core/types';
3
+ import { BasicAuthGuard } from './guard.js';
4
+ import type { GuardConfigProvider } from '../../src/types.js';
5
+ import { BasicAuthLucidUserProvider } from './user_providers/lucid.js';
6
+ import type { LucidAuthenticatable, BasicAuthUserProviderContract, BasicAuthLucidUserProviderOptions } from './types.js';
7
+ /**
8
+ * Configures basic auth guard for authentication
9
+ */
10
+ export declare function basicAuthGuard<UserProvider extends BasicAuthUserProviderContract<unknown>>(config: {
11
+ provider: UserProvider | ConfigProvider<UserProvider>;
12
+ }): GuardConfigProvider<(ctx: HttpContext) => BasicAuthGuard<UserProvider>>;
13
+ /**
14
+ * Configures user provider that uses Lucid models to authenticate
15
+ * users using basic auth
16
+ */
17
+ export declare function basicAuthUserProvider<Model extends LucidAuthenticatable>(config: BasicAuthLucidUserProviderOptions<Model>): BasicAuthLucidUserProvider<Model>;
@@ -0,0 +1,66 @@
1
+ import type { HttpContext } from '@adonisjs/core/http';
2
+ import type { EmitterLike } from '@adonisjs/core/types/events';
3
+ import type { AuthClientResponse, GuardContract } from '../../src/types.js';
4
+ import { GUARD_KNOWN_EVENTS, PROVIDER_REAL_USER } from '../../src/symbols.js';
5
+ import type { BasicAuthGuardEvents, BasicAuthUserProviderContract } from './types.js';
6
+ /**
7
+ * BasicAuth guard implements the HTTP Authentication protocol
8
+ */
9
+ export declare class BasicAuthGuard<UserProvider extends BasicAuthUserProviderContract<unknown>> implements GuardContract<UserProvider[typeof PROVIDER_REAL_USER]> {
10
+ #private;
11
+ /**
12
+ * Events emitted by the guard
13
+ */
14
+ [GUARD_KNOWN_EVENTS]: BasicAuthGuardEvents<UserProvider[typeof PROVIDER_REAL_USER]>;
15
+ /**
16
+ * Driver name of the guard
17
+ */
18
+ driverName: 'basic_auth';
19
+ /**
20
+ * Whether or not the authentication has been attempted
21
+ * during the current request.
22
+ */
23
+ authenticationAttempted: boolean;
24
+ /**
25
+ * A boolean to know if the current request has
26
+ * been authenticated
27
+ */
28
+ isAuthenticated: boolean;
29
+ /**
30
+ * Reference to an instance of the authenticated user.
31
+ * The value only exists after calling one of the
32
+ * following methods.
33
+ *
34
+ * - authenticate
35
+ * - check
36
+ *
37
+ * You can use the "getUserOrFail" method to throw an exception if
38
+ * the request is not authenticated.
39
+ */
40
+ user?: UserProvider[typeof PROVIDER_REAL_USER];
41
+ constructor(name: string, ctx: HttpContext, emitter: EmitterLike<BasicAuthGuardEvents<UserProvider[typeof PROVIDER_REAL_USER]>>, userProvider: UserProvider);
42
+ /**
43
+ * Returns an instance of the authenticated user. Or throws
44
+ * an exception if the request is not authenticated.
45
+ */
46
+ getUserOrFail(): UserProvider[typeof PROVIDER_REAL_USER];
47
+ /**
48
+ * Authenticates the incoming HTTP request by looking for BasicAuth
49
+ * credentials inside the request authorization header.
50
+ *
51
+ * Returns the authenticated user or throws an exception.
52
+ */
53
+ authenticate(): Promise<UserProvider[typeof PROVIDER_REAL_USER]>;
54
+ /**
55
+ * Silently attempt to authenticate the user.
56
+ *
57
+ * The method returns a boolean indicating if the authentication
58
+ * succeeded or failed.
59
+ */
60
+ check(): Promise<boolean>;
61
+ /**
62
+ * Does not support authenticating as client. Instead use "basicAuth"
63
+ * helper on Japa APIClient
64
+ */
65
+ authenticateAsClient(uid: string, password: string): Promise<AuthClientResponse>;
66
+ }
@@ -0,0 +1,3 @@
1
+ export { BasicAuthGuard } from './guard.js';
2
+ export { basicAuthGuard, basicAuthUserProvider } from './define_config.js';
3
+ export { BasicAuthLucidUserProvider } from './user_providers/lucid.js';
@@ -0,0 +1,236 @@
1
+ import {
2
+ E_UNAUTHORIZED_ACCESS
3
+ } from "../../chunk-UGHJLKDI.js";
4
+ import "../../chunk-CZCFTIBB.js";
5
+
6
+ // modules/basic_auth_guard/guard.ts
7
+ import auth from "basic-auth";
8
+ import { base64 } from "@adonisjs/core/helpers";
9
+ 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
+ }
155
+ };
156
+
157
+ // modules/basic_auth_guard/user_providers/lucid.ts
158
+ import { RuntimeException } from "@adonisjs/core/exceptions";
159
+ var BasicAuthLucidUserProvider = class {
160
+ constructor(options) {
161
+ this.options = options;
162
+ }
163
+ /**
164
+ * Reference to the lazily imported model
165
+ */
166
+ model;
167
+ /**
168
+ * Imports the model from the provider, returns and caches it
169
+ * for further operations.
170
+ */
171
+ async getModel() {
172
+ if (this.model) {
173
+ return this.model;
174
+ }
175
+ const importedModel = await this.options.model();
176
+ this.model = importedModel.default;
177
+ return this.model;
178
+ }
179
+ /**
180
+ * Creates an adapter user for the guard
181
+ */
182
+ async createUserForGuard(user) {
183
+ const model = await this.getModel();
184
+ if (user instanceof model === false) {
185
+ throw new RuntimeException(
186
+ `Invalid user object. It must be an instance of the "${model.name}" model`
187
+ );
188
+ }
189
+ return {
190
+ getId() {
191
+ if (!user.$primaryKeyValue) {
192
+ throw new RuntimeException(
193
+ `Cannot use "${model.name}" model for authentication. The value of column "${model.primaryKey}" is undefined or null`
194
+ );
195
+ }
196
+ return user.$primaryKeyValue;
197
+ },
198
+ getOriginal() {
199
+ return user;
200
+ }
201
+ };
202
+ }
203
+ /**
204
+ * Verifies credentials using the underlying model
205
+ */
206
+ async verifyCredentials(uid, password) {
207
+ const model = await this.getModel();
208
+ try {
209
+ const user = await model.verifyCredentials(uid, password);
210
+ return this.createUserForGuard(user);
211
+ } catch {
212
+ return null;
213
+ }
214
+ }
215
+ };
216
+
217
+ // modules/basic_auth_guard/define_config.ts
218
+ function basicAuthGuard(config) {
219
+ return {
220
+ async resolver(name, app) {
221
+ const emitter = await app.container.make("emitter");
222
+ const provider = "resolver" in config.provider ? await config.provider.resolver(app) : config.provider;
223
+ return (ctx) => new BasicAuthGuard(name, ctx, emitter, provider);
224
+ }
225
+ };
226
+ }
227
+ function basicAuthUserProvider(config) {
228
+ return new BasicAuthLucidUserProvider(config);
229
+ }
230
+ export {
231
+ BasicAuthGuard,
232
+ BasicAuthLucidUserProvider,
233
+ basicAuthGuard,
234
+ basicAuthUserProvider
235
+ };
236
+ //# sourceMappingURL=main.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../modules/basic_auth_guard/guard.ts","../../../modules/basic_auth_guard/user_providers/lucid.ts","../../../modules/basic_auth_guard/define_config.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 auth from 'basic-auth'\nimport { base64 } from '@adonisjs/core/helpers'\nimport type { HttpContext } from '@adonisjs/core/http'\nimport type { EmitterLike } from '@adonisjs/core/types/events'\n\nimport { E_UNAUTHORIZED_ACCESS } from '../../src/errors.js'\nimport type { AuthClientResponse, GuardContract } from '../../src/types.js'\nimport { GUARD_KNOWN_EVENTS, PROVIDER_REAL_USER } from '../../src/symbols.js'\nimport type { BasicAuthGuardEvents, BasicAuthUserProviderContract } from './types.js'\n\n/**\n * BasicAuth guard implements the HTTP Authentication protocol\n */\nexport class BasicAuthGuard<UserProvider extends BasicAuthUserProviderContract<unknown>>\n implements GuardContract<UserProvider[typeof PROVIDER_REAL_USER]>\n{\n /**\n * Events emitted by the guard\n */\n declare [GUARD_KNOWN_EVENTS]: BasicAuthGuardEvents<UserProvider[typeof PROVIDER_REAL_USER]>\n\n /**\n * A unique name for the guard.\n */\n #name: string\n\n /**\n * Reference to the current HTTP context\n */\n #ctx: HttpContext\n\n /**\n * Provider to lookup user details\n */\n #userProvider: UserProvider\n\n /**\n * Emitter to emit events\n */\n #emitter: EmitterLike<BasicAuthGuardEvents<UserProvider[typeof PROVIDER_REAL_USER]>>\n\n /**\n * Driver name of the guard\n */\n driverName: 'basic_auth' = 'basic_auth'\n\n /**\n * Whether or not the authentication has been attempted\n * during the current request.\n */\n authenticationAttempted = false\n\n /**\n * A boolean to know if the current request has\n * been authenticated\n */\n isAuthenticated = false\n\n /**\n * Reference to an instance of the authenticated user.\n * The value only exists after calling one of the\n * following methods.\n *\n * - authenticate\n * - check\n *\n * You can use the \"getUserOrFail\" method to throw an exception if\n * the request is not authenticated.\n */\n user?: UserProvider[typeof PROVIDER_REAL_USER]\n\n constructor(\n name: string,\n ctx: HttpContext,\n emitter: EmitterLike<BasicAuthGuardEvents<UserProvider[typeof PROVIDER_REAL_USER]>>,\n userProvider: UserProvider\n ) {\n this.#name = name\n this.#ctx = ctx\n this.#emitter = emitter\n this.#userProvider = userProvider\n }\n\n /**\n * Emits authentication failure, updates the local state,\n * and returns an exception to end the authentication\n * cycle.\n */\n #authenticationFailed() {\n this.isAuthenticated = false\n this.user = undefined\n\n const error = new E_UNAUTHORIZED_ACCESS('Invalid basic auth credentials', {\n guardDriverName: this.driverName,\n })\n\n this.#emitter.emit('basic_auth:authentication_failed', {\n ctx: this.#ctx,\n guardName: this.#name,\n error,\n })\n\n return error\n }\n\n /**\n * Emits the authentication succeeded event and updates\n * the local state to reflect successful authentication\n */\n #authenticationSucceeded(user: UserProvider[typeof PROVIDER_REAL_USER]) {\n this.isAuthenticated = true\n this.user = user\n\n this.#emitter.emit('basic_auth:authentication_succeeded', {\n ctx: this.#ctx,\n guardName: this.#name,\n user,\n })\n }\n\n /**\n * Returns an instance of the authenticated user. Or throws\n * an exception if the request is not authenticated.\n */\n getUserOrFail(): UserProvider[typeof PROVIDER_REAL_USER] {\n if (!this.user) {\n throw new E_UNAUTHORIZED_ACCESS('Invalid basic auth credentials', {\n guardDriverName: this.driverName,\n })\n }\n\n return this.user\n }\n\n /**\n * Authenticates the incoming HTTP request by looking for BasicAuth\n * credentials inside the request authorization header.\n *\n * Returns the authenticated user or throws an exception.\n */\n async authenticate(): Promise<UserProvider[typeof PROVIDER_REAL_USER]> {\n /**\n * Avoid re-authenticating when already authenticated\n */\n if (this.authenticationAttempted) {\n return this.getUserOrFail()\n }\n\n /**\n * Beginning authentication attempt\n */\n this.authenticationAttempted = true\n this.#emitter.emit('basic_auth:authentication_attempted', {\n ctx: this.#ctx,\n guardName: this.#name,\n })\n\n /**\n * Fetch credentials from the header or fail\n */\n const credentials = auth(this.#ctx.request.request)\n if (!credentials) {\n throw this.#authenticationFailed()\n }\n\n /**\n * Verify user credentials or fail\n */\n const user = await this.#userProvider.verifyCredentials(credentials.name, credentials.pass)\n if (!user) {\n throw this.#authenticationFailed()\n }\n\n /**\n * Mark user as authenticated\n */\n this.#authenticationSucceeded(user.getOriginal())\n return this.getUserOrFail()\n }\n\n /**\n * Silently attempt to authenticate the user.\n *\n * The method returns a boolean indicating if the authentication\n * succeeded or failed.\n */\n async check(): Promise<boolean> {\n try {\n await this.authenticate()\n return true\n } catch (error) {\n if (error instanceof E_UNAUTHORIZED_ACCESS) {\n return false\n }\n\n throw error\n }\n }\n\n /**\n * Does not support authenticating as client. Instead use \"basicAuth\"\n * helper on Japa APIClient\n */\n async authenticateAsClient(uid: string, password: string): Promise<AuthClientResponse> {\n return {\n headers: {\n authorization: `Basic ${base64.encode(`${uid}:${password}`)}`,\n },\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 { RuntimeException } from '@adonisjs/core/exceptions'\nimport { PROVIDER_REAL_USER } from '../../../src/symbols.js'\nimport type {\n BasicAuthGuardUser,\n LucidAuthenticatable,\n BasicAuthUserProviderContract,\n BasicAuthLucidUserProviderOptions,\n} from '../types.js'\n\n/**\n * Uses a Lucid model to verify access tokens and find a user during\n * authentication\n */\nexport class BasicAuthLucidUserProvider<UserModel extends LucidAuthenticatable>\n implements BasicAuthUserProviderContract<InstanceType<UserModel>>\n{\n declare [PROVIDER_REAL_USER]: InstanceType<UserModel>\n\n /**\n * Reference to the lazily imported model\n */\n protected model?: UserModel\n\n constructor(\n /**\n * Lucid provider options\n */\n protected options: BasicAuthLucidUserProviderOptions<UserModel>\n ) {}\n\n /**\n * Imports the model from the provider, returns and caches it\n * for further operations.\n */\n protected async getModel() {\n if (this.model) {\n return this.model\n }\n\n const importedModel = await this.options.model()\n this.model = importedModel.default\n return this.model\n }\n\n /**\n * Creates an adapter user for the guard\n */\n async createUserForGuard(\n user: InstanceType<UserModel>\n ): Promise<BasicAuthGuardUser<InstanceType<UserModel>>> {\n const model = await this.getModel()\n if (user instanceof model === false) {\n throw new RuntimeException(\n `Invalid user object. It must be an instance of the \"${model.name}\" model`\n )\n }\n\n return {\n getId() {\n /**\n * Ensure user has a primary key\n */\n if (!user.$primaryKeyValue) {\n throw new RuntimeException(\n `Cannot use \"${model.name}\" model for authentication. The value of column \"${model.primaryKey}\" is undefined or null`\n )\n }\n\n return user.$primaryKeyValue\n },\n getOriginal() {\n return user\n },\n }\n }\n\n /**\n * Verifies credentials using the underlying model\n */\n async verifyCredentials(\n uid: string,\n password: string\n ): Promise<BasicAuthGuardUser<InstanceType<UserModel>> | null> {\n const model = await this.getModel()\n try {\n const user = await model.verifyCredentials(uid, password)\n return this.createUserForGuard(user as InstanceType<UserModel>)\n } catch {\n return null\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 type { HttpContext } from '@adonisjs/core/http'\nimport type { ConfigProvider } from '@adonisjs/core/types'\n\nimport { BasicAuthGuard } from './guard.js'\nimport type { GuardConfigProvider } from '../../src/types.js'\nimport { BasicAuthLucidUserProvider } from './user_providers/lucid.js'\nimport type {\n LucidAuthenticatable,\n BasicAuthUserProviderContract,\n BasicAuthLucidUserProviderOptions,\n} from './types.js'\n\n/**\n * Configures basic auth guard for authentication\n */\nexport function basicAuthGuard<\n UserProvider extends BasicAuthUserProviderContract<unknown>,\n>(config: {\n provider: UserProvider | ConfigProvider<UserProvider>\n}): GuardConfigProvider<(ctx: HttpContext) => BasicAuthGuard<UserProvider>> {\n return {\n async resolver(name, app) {\n const emitter = await app.container.make('emitter')\n const provider =\n 'resolver' in config.provider ? await config.provider.resolver(app) : config.provider\n return (ctx) => new BasicAuthGuard(name, ctx, emitter as any, provider)\n },\n }\n}\n\n/**\n * Configures user provider that uses Lucid models to authenticate\n * users using basic auth\n */\nexport function basicAuthUserProvider<Model extends LucidAuthenticatable>(\n config: BasicAuthLucidUserProviderOptions<Model>\n): BasicAuthLucidUserProvider<Model> {\n return new BasicAuthLucidUserProvider(config)\n}\n"],"mappings":";;;;;;AASA,OAAO,UAAU;AACjB,SAAS,cAAc;AAYhB,IAAM,iBAAN,MAEP;AAAA;AAAA;AAAA;AAAA,EASE;AAAA;AAAA;AAAA;AAAA,EAKA;AAAA;AAAA;AAAA;AAAA,EAKA;AAAA;AAAA;AAAA;AAAA,EAKA;AAAA;AAAA;AAAA;AAAA,EAKA,aAA2B;AAAA;AAAA;AAAA;AAAA;AAAA,EAM3B,0BAA0B;AAAA;AAAA;AAAA;AAAA;AAAA,EAM1B,kBAAkB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAalB;AAAA,EAEA,YACE,MACA,KACA,SACA,cACA;AACA,SAAK,QAAQ;AACb,SAAK,OAAO;AACZ,SAAK,WAAW;AAChB,SAAK,gBAAgB;AAAA,EACvB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,wBAAwB;AACtB,SAAK,kBAAkB;AACvB,SAAK,OAAO;AAEZ,UAAM,QAAQ,IAAI,sBAAsB,kCAAkC;AAAA,MACxE,iBAAiB,KAAK;AAAA,IACxB,CAAC;AAED,SAAK,SAAS,KAAK,oCAAoC;AAAA,MACrD,KAAK,KAAK;AAAA,MACV,WAAW,KAAK;AAAA,MAChB;AAAA,IACF,CAAC;AAED,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,yBAAyB,MAA+C;AACtE,SAAK,kBAAkB;AACvB,SAAK,OAAO;AAEZ,SAAK,SAAS,KAAK,uCAAuC;AAAA,MACxD,KAAK,KAAK;AAAA,MACV,WAAW,KAAK;AAAA,MAChB;AAAA,IACF,CAAC;AAAA,EACH;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,gBAAyD;AACvD,QAAI,CAAC,KAAK,MAAM;AACd,YAAM,I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}
@@ -0,0 +1,85 @@
1
+ import type { HttpContext } from '@adonisjs/core/http';
2
+ import type { Exception } from '@adonisjs/core/exceptions';
3
+ import type { LucidModel } from '@adonisjs/lucid/types/model';
4
+ import type { PROVIDER_REAL_USER } from '../../src/symbols.js';
5
+ /**
6
+ * A lucid model with verify credentials method to verify user
7
+ * credentials during authentication.
8
+ */
9
+ export type LucidAuthenticatable = LucidModel & {
10
+ /**
11
+ * Verify credentials method should return the user instance
12
+ * or throw an exception
13
+ */
14
+ verifyCredentials(uid: string, password: string): Promise<InstanceType<LucidAuthenticatable>>;
15
+ };
16
+ /**
17
+ * Options accepted by the user provider that uses a lucid
18
+ * model to lookup a user during authentication and verify
19
+ * credentials
20
+ */
21
+ export type BasicAuthLucidUserProviderOptions<Model extends LucidAuthenticatable> = {
22
+ /**
23
+ * The model to use for users lookup
24
+ */
25
+ model: () => Promise<{
26
+ default: Model;
27
+ }>;
28
+ };
29
+ /**
30
+ * Guard user is an adapter between the user provider
31
+ * and the guard.
32
+ *
33
+ * The guard is user provider agnostic and therefore it
34
+ * needs a adapter to known some basic info about the
35
+ * user.
36
+ */
37
+ export type BasicAuthGuardUser<RealUser> = {
38
+ getId(): string | number | BigInt;
39
+ getOriginal(): RealUser;
40
+ };
41
+ /**
42
+ * The user provider used by basic auth guard to lookup users
43
+ * during authentication
44
+ */
45
+ export interface BasicAuthUserProviderContract<RealUser> {
46
+ [PROVIDER_REAL_USER]: RealUser;
47
+ /**
48
+ * Create a user object that acts as an adapter between
49
+ * the guard and real user value.
50
+ */
51
+ createUserForGuard(user: RealUser): Promise<BasicAuthGuardUser<RealUser>>;
52
+ /**
53
+ * Verify user credentials and must return an instance of the
54
+ * user back or null when the credentials are invalid
55
+ */
56
+ verifyCredentials(uid: string, password: string): Promise<BasicAuthGuardUser<RealUser> | null>;
57
+ }
58
+ /**
59
+ * Events emitted by the basic auth guard
60
+ */
61
+ export type BasicAuthGuardEvents<User> = {
62
+ /**
63
+ * Attempting to authenticate the user
64
+ */
65
+ 'basic_auth:authentication_attempted': {
66
+ ctx: HttpContext;
67
+ guardName: string;
68
+ };
69
+ /**
70
+ * Authentication was successful
71
+ */
72
+ 'basic_auth:authentication_succeeded': {
73
+ ctx: HttpContext;
74
+ guardName: string;
75
+ user: User;
76
+ };
77
+ /**
78
+ * Authentication failed
79
+ */
80
+ 'basic_auth:authentication_failed': {
81
+ ctx: HttpContext;
82
+ guardName: string;
83
+ error: Exception;
84
+ };
85
+ };
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -0,0 +1,35 @@
1
+ import { PROVIDER_REAL_USER } from '../../../src/symbols.js';
2
+ import type { BasicAuthGuardUser, LucidAuthenticatable, BasicAuthUserProviderContract, BasicAuthLucidUserProviderOptions } from '../types.js';
3
+ /**
4
+ * Uses a Lucid model to verify access tokens and find a user during
5
+ * authentication
6
+ */
7
+ export declare class BasicAuthLucidUserProvider<UserModel extends LucidAuthenticatable> implements BasicAuthUserProviderContract<InstanceType<UserModel>> {
8
+ /**
9
+ * Lucid provider options
10
+ */
11
+ protected options: BasicAuthLucidUserProviderOptions<UserModel>;
12
+ [PROVIDER_REAL_USER]: InstanceType<UserModel>;
13
+ /**
14
+ * Reference to the lazily imported model
15
+ */
16
+ protected model?: UserModel;
17
+ constructor(
18
+ /**
19
+ * Lucid provider options
20
+ */
21
+ options: BasicAuthLucidUserProviderOptions<UserModel>);
22
+ /**
23
+ * Imports the model from the provider, returns and caches it
24
+ * for further operations.
25
+ */
26
+ protected getModel(): Promise<UserModel>;
27
+ /**
28
+ * Creates an adapter user for the guard
29
+ */
30
+ createUserForGuard(user: InstanceType<UserModel>): Promise<BasicAuthGuardUser<InstanceType<UserModel>>>;
31
+ /**
32
+ * Verifies credentials using the underlying model
33
+ */
34
+ verifyCredentials(uid: string, password: string): Promise<BasicAuthGuardUser<InstanceType<UserModel>> | null>;
35
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adonisjs/auth",
3
- "version": "9.0.3",
3
+ "version": "9.1.1",
4
4
  "description": "Official authentication provider for Adonis framework",
5
5
  "type": "module",
6
6
  "main": "build/index.js",
@@ -24,11 +24,13 @@
24
24
  "./access_tokens": "./build/modules/access_tokens_guard/main.js",
25
25
  "./types/access_tokens": "./build/modules/access_tokens_guard/types.js",
26
26
  "./session": "./build/modules/session_guard/main.js",
27
- "./types/session": "./build/modules/session_guard/types.js"
27
+ "./types/session": "./build/modules/session_guard/types.js",
28
+ "./basic_auth": "./build/modules/basic_auth_guard/main.js",
29
+ "./types/basic_auth": "./build/modules/basic_auth_guard/types.js"
28
30
  },
29
31
  "scripts": {
30
32
  "pretest": "npm run lint",
31
- "test": "c8 npm run test:sqlite && npm run test:mysql && npm run test:pg && npm run test:mssql",
33
+ "test": "npm run test:mysql && npm run test:pg && npm run test:mssql && c8 npm run test:sqlite",
32
34
  "test:sqlite": "npm run quick:test",
33
35
  "test:mysql": "cross-env DB=mysql npm run quick:test",
34
36
  "test:mssql": "cross-env DB=mssql npm run quick:test",
@@ -64,11 +66,11 @@
64
66
  "url": "https://github.com/adonisjs/auth/issues"
65
67
  },
66
68
  "devDependencies": {
67
- "@adonisjs/assembler": "^7.1.0",
69
+ "@adonisjs/assembler": "^7.1.1",
68
70
  "@adonisjs/core": "^6.2.1",
69
71
  "@adonisjs/eslint-config": "^1.2.1",
70
72
  "@adonisjs/i18n": "^2.0.0",
71
- "@adonisjs/lucid": "^20.0.0",
73
+ "@adonisjs/lucid": "^20.1.0",
72
74
  "@adonisjs/prettier-config": "^1.2.1",
73
75
  "@adonisjs/session": "^7.1.1",
74
76
  "@adonisjs/tsconfig": "^1.2.1",
@@ -96,9 +98,9 @@
96
98
  "dotenv": "^16.4.1",
97
99
  "eslint": "^8.56.0",
98
100
  "github-label-sync": "^2.3.1",
99
- "husky": "^9.0.1",
101
+ "husky": "^9.0.7",
100
102
  "luxon": "^3.4.4",
101
- "mysql2": "^3.8.0",
103
+ "mysql2": "^3.9.1",
102
104
  "nock": "^13.5.0",
103
105
  "np": "^9.2.0",
104
106
  "pg": "^8.11.3",
@@ -148,9 +150,9 @@
148
150
  "basic-auth": "^2.0.1"
149
151
  },
150
152
  "peerDependencies": {
151
- "@adonisjs/core": "^6.2.0",
152
- "@adonisjs/lucid": "^19.0.0",
153
- "@adonisjs/session": "^7.0.0",
153
+ "@adonisjs/core": "^6.2.1",
154
+ "@adonisjs/lucid": "^20.1.0",
155
+ "@adonisjs/session": "^7.1.1",
154
156
  "@japa/api-client": "^2.0.2",
155
157
  "@japa/browser-client": "^2.0.2",
156
158
  "@japa/plugin-adonisjs": "^3.0.0"
@@ -184,7 +186,9 @@
184
186
  "./modules/access_tokens_guard/main.ts",
185
187
  "./modules/access_tokens_guard/types.ts",
186
188
  "./modules/session_guard/main.ts",
187
- "./modules/session_guard/types.ts"
189
+ "./modules/session_guard/types.ts",
190
+ "./modules/basic_auth_guard/main.ts",
191
+ "./modules/basic_auth_guard/types.ts"
188
192
  ],
189
193
  "outDir": "./build",
190
194
  "clean": true,