@adonisjs/auth 9.0.0-9 → 9.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/chunk-3HZHOWKL.js +8 -0
- package/build/chunk-3HZHOWKL.js.map +1 -0
- package/build/chunk-52DVKHJR.js +246 -0
- package/build/chunk-52DVKHJR.js.map +1 -0
- package/build/chunk-BHB55PBJ.js +197 -0
- package/build/chunk-BHB55PBJ.js.map +1 -0
- package/build/chunk-CZCFTIBB.js +21 -0
- package/build/chunk-CZCFTIBB.js.map +1 -0
- package/build/index.d.ts +7 -6
- package/build/index.js +146 -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
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* @adonisjs/auth
|
|
3
|
-
*
|
|
4
|
-
* (c) AdonisJS
|
|
5
|
-
*
|
|
6
|
-
* For the full copyright and license information, please view the LICENSE
|
|
7
|
-
* file that was distributed with this source code.
|
|
8
|
-
*/
|
|
9
|
-
import { RuntimeException } from '@poppinss/utils';
|
|
10
|
-
import debug from '../../auth/debug.js';
|
|
11
|
-
import { GuardUser } from '../guard_user.js';
|
|
12
|
-
import { PROVIDER_REAL_USER } from '../../auth/symbols.js';
|
|
13
|
-
/**
|
|
14
|
-
* Lucid user represents a guard user, used by authentication guards
|
|
15
|
-
* to perform authentication.
|
|
16
|
-
*/
|
|
17
|
-
class LucidUser extends GuardUser {
|
|
18
|
-
/**
|
|
19
|
-
* @inheritdoc
|
|
20
|
-
*/
|
|
21
|
-
getId() {
|
|
22
|
-
const id = this.realUser.$primaryKeyValue;
|
|
23
|
-
/**
|
|
24
|
-
* Ensure id exists
|
|
25
|
-
*/
|
|
26
|
-
if (!id) {
|
|
27
|
-
const model = this.realUser.constructor;
|
|
28
|
-
const modelName = model.name;
|
|
29
|
-
const primaryKey = model.primaryKey;
|
|
30
|
-
throw new RuntimeException(`Cannot use "${modelName}" model for authentication. The value of column "${primaryKey}" is undefined or null`);
|
|
31
|
-
}
|
|
32
|
-
return id;
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* @inheritdoc
|
|
36
|
-
*/
|
|
37
|
-
async verifyPassword(plainTextPassword) {
|
|
38
|
-
return this.realUser.verifyPasswordForAuth(plainTextPassword);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Lucid user provider is used to lookup user for authentication
|
|
43
|
-
* using a Lucid model.
|
|
44
|
-
*/
|
|
45
|
-
export class BaseLucidUserProvider {
|
|
46
|
-
options;
|
|
47
|
-
/**
|
|
48
|
-
* Reference to the lazily imported model
|
|
49
|
-
*/
|
|
50
|
-
model;
|
|
51
|
-
constructor(
|
|
52
|
-
/**
|
|
53
|
-
* Lucid provider options
|
|
54
|
-
*/
|
|
55
|
-
options) {
|
|
56
|
-
this.options = options;
|
|
57
|
-
debug('lucid_user_provider: options %O', options);
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Imports the model from the provider, returns and caches it
|
|
61
|
-
* for further operations.
|
|
62
|
-
*/
|
|
63
|
-
async getModel() {
|
|
64
|
-
if (this.model) {
|
|
65
|
-
return this.model;
|
|
66
|
-
}
|
|
67
|
-
const importedModel = await this.options.model();
|
|
68
|
-
this.model = importedModel.default;
|
|
69
|
-
debug('lucid_user_provider: using model %O', this.model);
|
|
70
|
-
return this.model;
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Returns an instance of the query builder
|
|
74
|
-
*/
|
|
75
|
-
getQueryBuilder(model) {
|
|
76
|
-
return model.query({
|
|
77
|
-
client: this.options.client,
|
|
78
|
-
connection: this.options.connection,
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* Returns an instance of the "LucidUser" that guards
|
|
83
|
-
* can use for authentication
|
|
84
|
-
*/
|
|
85
|
-
async createUserForGuard(user) {
|
|
86
|
-
const model = await this.getModel();
|
|
87
|
-
if (user instanceof model === false) {
|
|
88
|
-
throw new RuntimeException(`Invalid user object. It must be an instance of the "${model.name}" model`);
|
|
89
|
-
}
|
|
90
|
-
debug('lucid_user_provider: converting user object to guard user %O', user);
|
|
91
|
-
return new LucidUser(user);
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* Finds a user by id using the configured model.
|
|
95
|
-
*/
|
|
96
|
-
async findById(value) {
|
|
97
|
-
debug('lucid_user_provider: finding user by id %s', value);
|
|
98
|
-
const model = await this.getModel();
|
|
99
|
-
const user = await model.find(value, {
|
|
100
|
-
client: this.options.client,
|
|
101
|
-
connection: this.options.connection,
|
|
102
|
-
});
|
|
103
|
-
if (!user) {
|
|
104
|
-
return null;
|
|
105
|
-
}
|
|
106
|
-
return new LucidUser(user);
|
|
107
|
-
}
|
|
108
|
-
/**
|
|
109
|
-
* Finds a user using one of the pre-configured unique
|
|
110
|
-
* ids, via the configured model.
|
|
111
|
-
*/
|
|
112
|
-
async findByUid(value) {
|
|
113
|
-
const query = this.getQueryBuilder(await this.getModel());
|
|
114
|
-
this.options.uids.forEach((uid) => query.orWhere(uid, value));
|
|
115
|
-
debug('lucid_user_provider: finding user by uids, uids: %O, value: %s', this.options.uids, value);
|
|
116
|
-
const user = await query.limit(1).first();
|
|
117
|
-
if (!user) {
|
|
118
|
-
return null;
|
|
119
|
-
}
|
|
120
|
-
return new LucidUser(user);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type { HttpContext } from '@adonisjs/core/http';
|
|
2
|
-
import type { ConfigProvider } from '@adonisjs/core/types';
|
|
3
|
-
import type { GuardConfigProvider } from '../../auth/types.js';
|
|
4
|
-
import type { UserProviderContract } from '../../core/types.js';
|
|
5
|
-
import { BasicAuthGuard } from './guard.js';
|
|
6
|
-
/**
|
|
7
|
-
* Helper function to configure the basic auth guard for
|
|
8
|
-
* authentication.
|
|
9
|
-
*
|
|
10
|
-
* This method returns a config builder, which internally
|
|
11
|
-
* returns a factory function to construct a guard
|
|
12
|
-
* during HTTP requests.
|
|
13
|
-
*/
|
|
14
|
-
export declare function basicAuthGuard<UserProvider extends UserProviderContract<unknown>>(config: {
|
|
15
|
-
provider: ConfigProvider<UserProvider>;
|
|
16
|
-
}): GuardConfigProvider<(ctx: HttpContext) => BasicAuthGuard<UserProvider>>;
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* @adonisjs/auth
|
|
3
|
-
*
|
|
4
|
-
* (c) AdonisJS
|
|
5
|
-
*
|
|
6
|
-
* For the full copyright and license information, please view the LICENSE
|
|
7
|
-
* file that was distributed with this source code.
|
|
8
|
-
*/
|
|
9
|
-
import { configProvider } from '@adonisjs/core';
|
|
10
|
-
import { RuntimeException } from '@poppinss/utils';
|
|
11
|
-
import { BasicAuthGuard } from './guard.js';
|
|
12
|
-
/**
|
|
13
|
-
* Helper function to configure the basic auth guard for
|
|
14
|
-
* authentication.
|
|
15
|
-
*
|
|
16
|
-
* This method returns a config builder, which internally
|
|
17
|
-
* returns a factory function to construct a guard
|
|
18
|
-
* during HTTP requests.
|
|
19
|
-
*/
|
|
20
|
-
export function basicAuthGuard(config) {
|
|
21
|
-
return {
|
|
22
|
-
async resolver(guardName, app) {
|
|
23
|
-
const provider = await configProvider.resolve(app, config.provider);
|
|
24
|
-
if (!provider) {
|
|
25
|
-
throw new RuntimeException(`Invalid user provider defined on "${guardName}" guard`);
|
|
26
|
-
}
|
|
27
|
-
const emitter = await app.container.make('emitter');
|
|
28
|
-
/**
|
|
29
|
-
* Factory function needed by Authenticator to switch
|
|
30
|
-
* between guards and perform authentication
|
|
31
|
-
*/
|
|
32
|
-
return (ctx) => {
|
|
33
|
-
const guard = new BasicAuthGuard(guardName, ctx, provider);
|
|
34
|
-
return guard.withEmitter(emitter);
|
|
35
|
-
};
|
|
36
|
-
},
|
|
37
|
-
};
|
|
38
|
-
}
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import type { Emitter } from '@adonisjs/core/events';
|
|
2
|
-
import type { HttpContext } from '@adonisjs/core/http';
|
|
3
|
-
import type { BasicAuthGuardEvents } from './types.js';
|
|
4
|
-
import type { GuardContract } from '../../auth/types.js';
|
|
5
|
-
import type { UserProviderContract } from '../../core/types.js';
|
|
6
|
-
import { PROVIDER_REAL_USER, GUARD_KNOWN_EVENTS } from '../../auth/symbols.js';
|
|
7
|
-
/**
|
|
8
|
-
* Implementation of basic auth as an authentication guard
|
|
9
|
-
*/
|
|
10
|
-
export declare class BasicAuthGuard<UserProvider extends UserProviderContract<unknown>> implements GuardContract<UserProvider[typeof PROVIDER_REAL_USER]> {
|
|
11
|
-
#private;
|
|
12
|
-
[GUARD_KNOWN_EVENTS]: BasicAuthGuardEvents<UserProvider[typeof PROVIDER_REAL_USER]>;
|
|
13
|
-
/**
|
|
14
|
-
* Driver name of the guard
|
|
15
|
-
*/
|
|
16
|
-
driverName: 'basic_auth';
|
|
17
|
-
/**
|
|
18
|
-
* Whether or not the authentication has been attempted
|
|
19
|
-
* during the current request
|
|
20
|
-
*/
|
|
21
|
-
authenticationAttempted: boolean;
|
|
22
|
-
/**
|
|
23
|
-
* A boolean to know if the current request has
|
|
24
|
-
* been authenticated
|
|
25
|
-
*/
|
|
26
|
-
isAuthenticated: boolean;
|
|
27
|
-
/**
|
|
28
|
-
* Reference to an instance of the authenticated or logged-in
|
|
29
|
-
* user. The value only exists after calling one of the
|
|
30
|
-
* following methods.
|
|
31
|
-
*
|
|
32
|
-
* - authenticate
|
|
33
|
-
*
|
|
34
|
-
* You can use the "getUserOrFail" method to throw an exception if
|
|
35
|
-
* the request is not authenticated.
|
|
36
|
-
*/
|
|
37
|
-
user?: UserProvider[typeof PROVIDER_REAL_USER];
|
|
38
|
-
constructor(name: string, ctx: HttpContext, userProvider: UserProvider);
|
|
39
|
-
/**
|
|
40
|
-
* Register an event emitter to listen for global events for
|
|
41
|
-
* authentication lifecycle.
|
|
42
|
-
*/
|
|
43
|
-
withEmitter(emitter: Emitter<any>): this;
|
|
44
|
-
/**
|
|
45
|
-
* Returns an instance of the authenticated user. Or throws
|
|
46
|
-
* an exception if the request is not authenticated.
|
|
47
|
-
*/
|
|
48
|
-
getUserOrFail(): UserProvider[typeof PROVIDER_REAL_USER];
|
|
49
|
-
/**
|
|
50
|
-
* Verifies user credentials and returns an instance of
|
|
51
|
-
* the user or throws "E_INVALID_BASIC_AUTH_CREDENTIALS" exception.
|
|
52
|
-
*/
|
|
53
|
-
verifyCredentials(uid: string, password: string): Promise<UserProvider[typeof PROVIDER_REAL_USER]>;
|
|
54
|
-
/**
|
|
55
|
-
* Authenticates the current HTTP request for basic
|
|
56
|
-
* auth credentials
|
|
57
|
-
*/
|
|
58
|
-
authenticate(): Promise<UserProvider[typeof PROVIDER_REAL_USER]>;
|
|
59
|
-
/**
|
|
60
|
-
* Silently attempt to authenticate the user.
|
|
61
|
-
*
|
|
62
|
-
* The method returns a boolean indicating if the authentication
|
|
63
|
-
* succeeded or failed.
|
|
64
|
-
*/
|
|
65
|
-
check(): Promise<boolean>;
|
|
66
|
-
/**
|
|
67
|
-
* Not support
|
|
68
|
-
*/
|
|
69
|
-
authenticateAsClient(_: UserProvider[typeof PROVIDER_REAL_USER]): Promise<never>;
|
|
70
|
-
}
|
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* @adonisjs/auth
|
|
3
|
-
*
|
|
4
|
-
* (c) AdonisJS
|
|
5
|
-
*
|
|
6
|
-
* For the full copyright and license information, please view the LICENSE
|
|
7
|
-
* file that was distributed with this source code.
|
|
8
|
-
*/
|
|
9
|
-
import auth from 'basic-auth';
|
|
10
|
-
import { RuntimeException } from '@poppinss/utils';
|
|
11
|
-
import debug from '../../auth/debug.js';
|
|
12
|
-
import { AuthenticationException } from '../../auth/errors.js';
|
|
13
|
-
import { GUARD_KNOWN_EVENTS } from '../../auth/symbols.js';
|
|
14
|
-
/**
|
|
15
|
-
* Implementation of basic auth as an authentication guard
|
|
16
|
-
*/
|
|
17
|
-
export class BasicAuthGuard {
|
|
18
|
-
/**
|
|
19
|
-
* A unique name for the guard. It is used while
|
|
20
|
-
* emitting events
|
|
21
|
-
*/
|
|
22
|
-
#name;
|
|
23
|
-
/**
|
|
24
|
-
* Reference to the current HTTP context
|
|
25
|
-
*/
|
|
26
|
-
#ctx;
|
|
27
|
-
/**
|
|
28
|
-
* Provider to lookup user details
|
|
29
|
-
*/
|
|
30
|
-
#userProvider;
|
|
31
|
-
/**
|
|
32
|
-
* Emitter to emit events
|
|
33
|
-
*/
|
|
34
|
-
#emitter;
|
|
35
|
-
/**
|
|
36
|
-
* Driver name of the guard
|
|
37
|
-
*/
|
|
38
|
-
driverName = 'basic_auth';
|
|
39
|
-
/**
|
|
40
|
-
* Whether or not the authentication has been attempted
|
|
41
|
-
* during the current request
|
|
42
|
-
*/
|
|
43
|
-
authenticationAttempted = false;
|
|
44
|
-
/**
|
|
45
|
-
* A boolean to know if the current request has
|
|
46
|
-
* been authenticated
|
|
47
|
-
*/
|
|
48
|
-
isAuthenticated = false;
|
|
49
|
-
/**
|
|
50
|
-
* Reference to an instance of the authenticated or logged-in
|
|
51
|
-
* user. The value only exists after calling one of the
|
|
52
|
-
* following methods.
|
|
53
|
-
*
|
|
54
|
-
* - authenticate
|
|
55
|
-
*
|
|
56
|
-
* You can use the "getUserOrFail" method to throw an exception if
|
|
57
|
-
* the request is not authenticated.
|
|
58
|
-
*/
|
|
59
|
-
user;
|
|
60
|
-
constructor(name, ctx, userProvider) {
|
|
61
|
-
this.#ctx = ctx;
|
|
62
|
-
this.#name = name;
|
|
63
|
-
this.#userProvider = userProvider;
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Notifies about authentication failure and throws the exception
|
|
67
|
-
*/
|
|
68
|
-
#authenticationFailed(error) {
|
|
69
|
-
if (this.#emitter) {
|
|
70
|
-
this.#emitter.emit('basic_auth:authentication_failed', {
|
|
71
|
-
guardName: this.#name,
|
|
72
|
-
error,
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
throw error;
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* Register an event emitter to listen for global events for
|
|
79
|
-
* authentication lifecycle.
|
|
80
|
-
*/
|
|
81
|
-
withEmitter(emitter) {
|
|
82
|
-
this.#emitter = emitter;
|
|
83
|
-
return this;
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Returns an instance of the authenticated user. Or throws
|
|
87
|
-
* an exception if the request is not authenticated.
|
|
88
|
-
*/
|
|
89
|
-
getUserOrFail() {
|
|
90
|
-
if (!this.user) {
|
|
91
|
-
throw AuthenticationException.E_INVALID_BASIC_AUTH_CREDENTIALS();
|
|
92
|
-
}
|
|
93
|
-
return this.user;
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Verifies user credentials and returns an instance of
|
|
97
|
-
* the user or throws "E_INVALID_BASIC_AUTH_CREDENTIALS" exception.
|
|
98
|
-
*/
|
|
99
|
-
async verifyCredentials(uid, password) {
|
|
100
|
-
debug('basic_auth_guard: attempting to verify credentials for uid "%s"', uid);
|
|
101
|
-
/**
|
|
102
|
-
* Attempt to find a user by the uid and raise
|
|
103
|
-
* error when unable to find one
|
|
104
|
-
*/
|
|
105
|
-
const providerUser = await this.#userProvider.findByUid(uid);
|
|
106
|
-
if (!providerUser) {
|
|
107
|
-
this.#authenticationFailed(AuthenticationException.E_INVALID_BASIC_AUTH_CREDENTIALS());
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Raise error when unable to verify password
|
|
111
|
-
*/
|
|
112
|
-
const user = providerUser.getOriginal();
|
|
113
|
-
/**
|
|
114
|
-
* Raise error when unable to verify password
|
|
115
|
-
*/
|
|
116
|
-
if (!(await providerUser.verifyPassword(password))) {
|
|
117
|
-
this.#authenticationFailed(AuthenticationException.E_INVALID_BASIC_AUTH_CREDENTIALS());
|
|
118
|
-
}
|
|
119
|
-
return user;
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* Authenticates the current HTTP request for basic
|
|
123
|
-
* auth credentials
|
|
124
|
-
*/
|
|
125
|
-
async authenticate() {
|
|
126
|
-
/**
|
|
127
|
-
* Avoid re-authenticating when already authenticated
|
|
128
|
-
*/
|
|
129
|
-
if (this.authenticationAttempted) {
|
|
130
|
-
return this.getUserOrFail();
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Beginning authentication attempt
|
|
134
|
-
*/
|
|
135
|
-
this.authenticationAttempted = true;
|
|
136
|
-
if (this.#emitter) {
|
|
137
|
-
this.#emitter.emit('basic_auth:authentication_attempted', {
|
|
138
|
-
guardName: this.#name,
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* Fetch credentials from the header
|
|
143
|
-
*/
|
|
144
|
-
const credentials = auth(this.#ctx.request.request);
|
|
145
|
-
if (!credentials) {
|
|
146
|
-
this.#authenticationFailed(AuthenticationException.E_INVALID_BASIC_AUTH_CREDENTIALS());
|
|
147
|
-
}
|
|
148
|
-
debug('basic_auth_guard: authenticating user using credentials');
|
|
149
|
-
/**
|
|
150
|
-
* Verifying user credentials
|
|
151
|
-
*/
|
|
152
|
-
this.user = await this.verifyCredentials(credentials.name, credentials.pass);
|
|
153
|
-
this.isAuthenticated = true;
|
|
154
|
-
debug('basic_auth_guard: marking user as authenticated');
|
|
155
|
-
if (this.#emitter) {
|
|
156
|
-
this.#emitter.emit('basic_auth:authentication_succeeded', {
|
|
157
|
-
guardName: this.#name,
|
|
158
|
-
user: this.user,
|
|
159
|
-
});
|
|
160
|
-
}
|
|
161
|
-
/**
|
|
162
|
-
* Return user
|
|
163
|
-
*/
|
|
164
|
-
return this.getUserOrFail();
|
|
165
|
-
}
|
|
166
|
-
/**
|
|
167
|
-
* Silently attempt to authenticate the user.
|
|
168
|
-
*
|
|
169
|
-
* The method returns a boolean indicating if the authentication
|
|
170
|
-
* succeeded or failed.
|
|
171
|
-
*/
|
|
172
|
-
async check() {
|
|
173
|
-
try {
|
|
174
|
-
await this.authenticate();
|
|
175
|
-
return true;
|
|
176
|
-
}
|
|
177
|
-
catch (error) {
|
|
178
|
-
if (error instanceof AuthenticationException) {
|
|
179
|
-
return false;
|
|
180
|
-
}
|
|
181
|
-
throw error;
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
/**
|
|
185
|
-
* Not support
|
|
186
|
-
*/
|
|
187
|
-
async authenticateAsClient(_) {
|
|
188
|
-
throw new RuntimeException('Cannot authenticate as a client when using basic auth');
|
|
189
|
-
}
|
|
190
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* @adonisjs/auth
|
|
3
|
-
*
|
|
4
|
-
* (c) AdonisJS
|
|
5
|
-
*
|
|
6
|
-
* For the full copyright and license information, please view the LICENSE
|
|
7
|
-
* file that was distributed with this source code.
|
|
8
|
-
*/
|
|
9
|
-
export { BasicAuthGuard } from './guard.js';
|
|
10
|
-
export { basicAuthGuard } from './define_config.js';
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { Exception } from '@poppinss/utils';
|
|
2
|
-
/**
|
|
3
|
-
* Events emitted by the basic auth guard
|
|
4
|
-
*/
|
|
5
|
-
export type BasicAuthGuardEvents<User> = {
|
|
6
|
-
/**
|
|
7
|
-
* The event is emitted when the user credentials
|
|
8
|
-
* have been verified successfully.
|
|
9
|
-
*/
|
|
10
|
-
'basic_auth:credentials_verified': {
|
|
11
|
-
guardName: string;
|
|
12
|
-
uid: string;
|
|
13
|
-
user: User;
|
|
14
|
-
};
|
|
15
|
-
/**
|
|
16
|
-
* Attempting to authenticate the user
|
|
17
|
-
*/
|
|
18
|
-
'basic_auth:authentication_attempted': {
|
|
19
|
-
guardName: string;
|
|
20
|
-
};
|
|
21
|
-
/**
|
|
22
|
-
* Authentication was successful
|
|
23
|
-
*/
|
|
24
|
-
'basic_auth:authentication_succeeded': {
|
|
25
|
-
guardName: string;
|
|
26
|
-
user: User;
|
|
27
|
-
};
|
|
28
|
-
/**
|
|
29
|
-
* Authentication failed
|
|
30
|
-
*/
|
|
31
|
-
'basic_auth:authentication_failed': {
|
|
32
|
-
guardName: string;
|
|
33
|
-
error: Exception;
|
|
34
|
-
};
|
|
35
|
-
};
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import type { HttpContext } from '@adonisjs/core/http';
|
|
2
|
-
import type { ConfigProvider } from '@adonisjs/core/types';
|
|
3
|
-
import { SessionGuard } from './guard.js';
|
|
4
|
-
import type { GuardConfigProvider } from '../../auth/types.js';
|
|
5
|
-
import type { SessionGuardConfig, RememberMeProviderContract, SessionUserProviderContract, DatabaseRememberMeProviderOptions } from './types.js';
|
|
6
|
-
/**
|
|
7
|
-
* Helper function to configure the session guard for
|
|
8
|
-
* authentication.
|
|
9
|
-
*
|
|
10
|
-
* This method returns a config builder, which internally
|
|
11
|
-
* returns a factory function to construct a guard
|
|
12
|
-
* during HTTP requests.
|
|
13
|
-
*/
|
|
14
|
-
export declare function sessionGuard<UserProvider extends SessionUserProviderContract<unknown>>(config: SessionGuardConfig & {
|
|
15
|
-
provider: ConfigProvider<UserProvider>;
|
|
16
|
-
tokens?: ConfigProvider<RememberMeProviderContract>;
|
|
17
|
-
}): GuardConfigProvider<(ctx: HttpContext) => SessionGuard<UserProvider>>;
|
|
18
|
-
/**
|
|
19
|
-
* Tokens provider helper to store remember me tokens
|
|
20
|
-
*/
|
|
21
|
-
export declare const tokensProvider: {
|
|
22
|
-
db: (config: DatabaseRememberMeProviderOptions) => ConfigProvider<RememberMeProviderContract>;
|
|
23
|
-
};
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* @adonisjs/auth
|
|
3
|
-
*
|
|
4
|
-
* (c) AdonisJS
|
|
5
|
-
*
|
|
6
|
-
* For the full copyright and license information, please view the LICENSE
|
|
7
|
-
* file that was distributed with this source code.
|
|
8
|
-
*/
|
|
9
|
-
import { configProvider } from '@adonisjs/core';
|
|
10
|
-
import { RuntimeException } from '@poppinss/utils';
|
|
11
|
-
import { SessionGuard } from './guard.js';
|
|
12
|
-
/**
|
|
13
|
-
* Helper function to configure the session guard for
|
|
14
|
-
* authentication.
|
|
15
|
-
*
|
|
16
|
-
* This method returns a config builder, which internally
|
|
17
|
-
* returns a factory function to construct a guard
|
|
18
|
-
* during HTTP requests.
|
|
19
|
-
*/
|
|
20
|
-
export function sessionGuard(config) {
|
|
21
|
-
return {
|
|
22
|
-
async resolver(guardName, app) {
|
|
23
|
-
const provider = await configProvider.resolve(app, config.provider);
|
|
24
|
-
if (!provider) {
|
|
25
|
-
throw new RuntimeException(`Invalid user provider defined on "${guardName}" guard`);
|
|
26
|
-
}
|
|
27
|
-
const emitter = await app.container.make('emitter');
|
|
28
|
-
const tokensProvider = config.tokens
|
|
29
|
-
? await configProvider.resolve(app, config.tokens)
|
|
30
|
-
: undefined;
|
|
31
|
-
/**
|
|
32
|
-
* Factory function needed by Authenticator to switch
|
|
33
|
-
* between guards and perform authentication
|
|
34
|
-
*/
|
|
35
|
-
return (ctx) => {
|
|
36
|
-
const guard = new SessionGuard(guardName, config, ctx, provider);
|
|
37
|
-
if (tokensProvider) {
|
|
38
|
-
guard.withRememberMeTokens(tokensProvider);
|
|
39
|
-
}
|
|
40
|
-
return guard.withEmitter(emitter);
|
|
41
|
-
};
|
|
42
|
-
},
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Tokens provider helper to store remember me tokens
|
|
47
|
-
*/
|
|
48
|
-
export const tokensProvider = {
|
|
49
|
-
db(config) {
|
|
50
|
-
return configProvider.create(async (app) => {
|
|
51
|
-
const db = await app.container.make('lucid.db');
|
|
52
|
-
const { DatabaseRememberTokenProvider } = await import('./token_providers/main.js');
|
|
53
|
-
return new DatabaseRememberTokenProvider(db, config);
|
|
54
|
-
});
|
|
55
|
-
},
|
|
56
|
-
};
|