@adonisjs/auth 8.2.3 → 9.0.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/LICENSE.md +1 -1
- package/README.md +19 -40
- package/build/configure.d.ts +5 -0
- package/build/configure.js +55 -0
- package/build/factories/database_token_factory.d.ts +36 -0
- package/build/factories/database_token_factory.js +54 -0
- package/build/factories/database_user_provider.d.ts +14 -0
- package/build/factories/database_user_provider.js +27 -0
- package/build/factories/lucid_user_provider.d.ts +28 -0
- package/build/factories/lucid_user_provider.js +68 -0
- package/build/factories/main.d.ts +4 -0
- package/build/factories/main.js +12 -0
- package/build/factories/session_guard_factory.d.ts +13 -0
- package/build/factories/session_guard_factory.js +24 -0
- package/build/index.d.ts +7 -0
- package/build/index.js +15 -0
- package/build/providers/auth_provider.d.ts +12 -0
- package/build/providers/auth_provider.js +27 -0
- package/build/services/auth.d.ts +3 -0
- package/build/services/auth.js +17 -0
- package/build/src/auth/auth_manager.d.ts +22 -0
- package/build/src/auth/auth_manager.js +34 -0
- package/build/src/auth/authenticator.d.ts +56 -0
- package/build/src/auth/authenticator.js +122 -0
- package/build/src/auth/debug.d.ts +3 -0
- package/build/{adonis-typings/container.js → src/auth/debug.js} +3 -1
- package/build/src/auth/define_config.d.ts +30 -0
- package/build/src/auth/define_config.js +54 -0
- package/build/src/auth/errors.d.ts +82 -0
- package/build/src/auth/errors.js +181 -0
- package/build/src/auth/middleware/auth_middleware.d.ts +13 -0
- package/build/src/auth/middleware/auth_middleware.js +6 -0
- package/build/src/auth/middleware/initialize_auth_middleware.d.ts +18 -0
- package/build/src/auth/middleware/initialize_auth_middleware.js +25 -0
- package/build/src/auth/symbols.d.ts +9 -0
- package/build/src/auth/symbols.js +17 -0
- package/build/src/auth/types.d.ts +75 -0
- package/build/{adonis-typings/context.js → src/auth/types.js} +2 -1
- package/build/src/auth/user_providers/main.d.ts +15 -0
- package/build/src/auth/user_providers/main.js +22 -0
- package/build/src/core/guard_user.d.ts +26 -0
- package/build/src/core/guard_user.js +29 -0
- package/build/src/core/token.d.ts +89 -0
- package/build/src/core/token.js +114 -0
- package/build/src/core/token_providers/database.d.ts +77 -0
- package/build/src/core/token_providers/database.js +113 -0
- package/build/src/core/types.d.ts +178 -0
- package/build/{adonis-typings/auth.js → src/core/types.js} +2 -1
- package/build/src/core/user_providers/database.d.ts +78 -0
- package/build/src/core/user_providers/database.js +117 -0
- package/build/src/core/user_providers/lucid.d.ts +61 -0
- package/build/src/core/user_providers/lucid.js +122 -0
- package/build/src/guards/session/define_config.d.ts +23 -0
- package/build/src/guards/session/define_config.js +56 -0
- package/build/src/guards/session/guard.d.ts +112 -0
- package/build/src/guards/session/guard.js +454 -0
- package/build/src/guards/session/main.d.ts +3 -0
- package/build/src/guards/session/main.js +11 -0
- package/build/src/guards/session/token.d.ts +57 -0
- package/build/src/guards/session/token.js +58 -0
- package/build/src/guards/session/token_providers/main.d.ts +33 -0
- package/build/src/guards/session/token_providers/main.js +42 -0
- package/build/src/guards/session/types.d.ts +96 -0
- package/build/{adonis-typings/events.js → src/guards/session/types.js} +2 -1
- package/build/stubs/config.stub +35 -0
- package/build/stubs/main.d.ts +1 -0
- package/build/{adonis-typings/tests.js → stubs/main.js} +2 -3
- package/package.json +99 -91
- package/build/adonis-typings/auth.d.ts +0 -635
- package/build/adonis-typings/container.d.ts +0 -6
- package/build/adonis-typings/context.d.ts +0 -6
- package/build/adonis-typings/events.d.ts +0 -10
- package/build/adonis-typings/index.d.ts +0 -5
- package/build/adonis-typings/index.js +0 -13
- package/build/adonis-typings/tests.d.ts +0 -23
- package/build/instructions.js +0 -338
- package/build/providers/AuthProvider.d.ts +0 -30
- package/build/providers/AuthProvider.js +0 -69
- package/build/src/Auth/index.d.ts +0 -97
- package/build/src/Auth/index.js +0 -155
- package/build/src/AuthManager/index.d.ts +0 -117
- package/build/src/AuthManager/index.js +0 -262
- package/build/src/Bindings/Tests.d.ts +0 -6
- package/build/src/Bindings/Tests.js +0 -69
- package/build/src/Clients/Oat/index.d.ts +0 -50
- package/build/src/Clients/Oat/index.js +0 -123
- package/build/src/Clients/Session/index.d.ts +0 -34
- package/build/src/Clients/Session/index.js +0 -72
- package/build/src/Exceptions/AuthenticationException.d.ts +0 -47
- package/build/src/Exceptions/AuthenticationException.js +0 -142
- package/build/src/Exceptions/InvalidCredentialsException.d.ts +0 -34
- package/build/src/Exceptions/InvalidCredentialsException.js +0 -112
- package/build/src/Guards/Base/index.d.ts +0 -75
- package/build/src/Guards/Base/index.js +0 -138
- package/build/src/Guards/BasicAuth/index.d.ts +0 -67
- package/build/src/Guards/BasicAuth/index.js +0 -181
- package/build/src/Guards/Oat/index.d.ts +0 -149
- package/build/src/Guards/Oat/index.js +0 -347
- package/build/src/Guards/Session/index.d.ts +0 -127
- package/build/src/Guards/Session/index.js +0 -338
- package/build/src/TokenProviders/Database/index.d.ts +0 -43
- package/build/src/TokenProviders/Database/index.js +0 -126
- package/build/src/TokenProviders/Redis/index.d.ts +0 -44
- package/build/src/TokenProviders/Redis/index.js +0 -129
- package/build/src/Tokens/OpaqueToken/index.d.ts +0 -46
- package/build/src/Tokens/OpaqueToken/index.js +0 -43
- package/build/src/Tokens/ProviderToken/index.d.ts +0 -23
- package/build/src/Tokens/ProviderToken/index.js +0 -27
- package/build/src/UserProviders/Database/User.d.ts +0 -28
- package/build/src/UserProviders/Database/User.js +0 -74
- package/build/src/UserProviders/Database/index.d.ts +0 -75
- package/build/src/UserProviders/Database/index.js +0 -141
- package/build/src/UserProviders/Lucid/User.d.ts +0 -28
- package/build/src/UserProviders/Lucid/User.js +0 -74
- package/build/src/UserProviders/Lucid/index.d.ts +0 -72
- package/build/src/UserProviders/Lucid/index.js +0 -146
- package/build/standalone.d.ts +0 -1
- package/build/standalone.js +0 -13
- package/build/templates/config/auth.txt +0 -34
- package/build/templates/config/partials/api-guard.txt +0 -22
- package/build/templates/config/partials/basic-guard.txt +0 -19
- package/build/templates/config/partials/tokens-provider-database.txt +0 -19
- package/build/templates/config/partials/tokens-provider-redis.txt +0 -22
- package/build/templates/config/partials/user-provider-database.txt +0 -43
- package/build/templates/config/partials/user-provider-lucid.txt +0 -45
- package/build/templates/config/partials/web-guard.txt +0 -17
- package/build/templates/contract/auth.txt +0 -55
- package/build/templates/contract/partials/api-guard.txt +0 -14
- package/build/templates/contract/partials/basic-guard.txt +0 -14
- package/build/templates/contract/partials/user-provider-database.txt +0 -16
- package/build/templates/contract/partials/user-provider-lucid.txt +0 -16
- package/build/templates/contract/partials/web-guard.txt +0 -14
- package/build/templates/middleware/Auth.txt +0 -76
- package/build/templates/middleware/SilentAuth.txt +0 -21
- package/build/templates/migrations/api_tokens.txt +0 -25
- package/build/templates/migrations/auth.txt +0 -24
- package/build/templates/model.txt +0 -30
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { Hash } from '@adonisjs/core/hash';
|
|
2
|
+
import type { Database } from '@adonisjs/lucid/database';
|
|
3
|
+
import { GuardUser } from '../guard_user.js';
|
|
4
|
+
import { PROVIDER_REAL_USER } from '../../auth/symbols.js';
|
|
5
|
+
import type { DatabaseUserProviderOptions, UserProviderContract } from '../types.js';
|
|
6
|
+
/**
|
|
7
|
+
* Database user represents a guard user used by authentication guards
|
|
8
|
+
* to perform authentication.
|
|
9
|
+
*/
|
|
10
|
+
declare class DatabaseUser<RealUser extends Record<string, any>> extends GuardUser<RealUser> {
|
|
11
|
+
#private;
|
|
12
|
+
constructor(realUser: RealUser, hasher: Hash, options: {
|
|
13
|
+
id: string;
|
|
14
|
+
passwordColumnName: string;
|
|
15
|
+
});
|
|
16
|
+
/**
|
|
17
|
+
* @inheritdoc
|
|
18
|
+
*/
|
|
19
|
+
getId(): string | number;
|
|
20
|
+
/**
|
|
21
|
+
* @inheritdoc
|
|
22
|
+
*/
|
|
23
|
+
verifyPassword(plainTextPassword: string): Promise<boolean>;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Database user provider is used to lookup user for authentication
|
|
27
|
+
* using the Database query builder.
|
|
28
|
+
*/
|
|
29
|
+
export declare abstract class BaseDatabaseUserProvider<RealUser extends Record<string, any>> implements UserProviderContract<RealUser> {
|
|
30
|
+
/**
|
|
31
|
+
* Reference to the database query builder needed to
|
|
32
|
+
* query the database for users
|
|
33
|
+
*/
|
|
34
|
+
protected db: Database;
|
|
35
|
+
/**
|
|
36
|
+
* Hasher is used to verify plain text passwords
|
|
37
|
+
*/
|
|
38
|
+
protected hasher: Hash;
|
|
39
|
+
/**
|
|
40
|
+
* Options accepted
|
|
41
|
+
*/
|
|
42
|
+
protected options: DatabaseUserProviderOptions<RealUser>;
|
|
43
|
+
[PROVIDER_REAL_USER]: RealUser;
|
|
44
|
+
constructor(
|
|
45
|
+
/**
|
|
46
|
+
* Reference to the database query builder needed to
|
|
47
|
+
* query the database for users
|
|
48
|
+
*/
|
|
49
|
+
db: Database,
|
|
50
|
+
/**
|
|
51
|
+
* Hasher is used to verify plain text passwords
|
|
52
|
+
*/
|
|
53
|
+
hasher: Hash,
|
|
54
|
+
/**
|
|
55
|
+
* Options accepted
|
|
56
|
+
*/
|
|
57
|
+
options: DatabaseUserProviderOptions<RealUser>);
|
|
58
|
+
/**
|
|
59
|
+
* Returns an instance of the query builder
|
|
60
|
+
*/
|
|
61
|
+
protected getQueryBuilder(): import("@adonisjs/lucid/types/querybuilder").DatabaseQueryBuilderContract<any>;
|
|
62
|
+
/**
|
|
63
|
+
* Returns an instance of the "DatabaseUser" that guards
|
|
64
|
+
* can use for authentication
|
|
65
|
+
*/
|
|
66
|
+
createUserForGuard(user: RealUser): Promise<DatabaseUser<RealUser>>;
|
|
67
|
+
/**
|
|
68
|
+
* Finds a user by id by query the configured database
|
|
69
|
+
* table
|
|
70
|
+
*/
|
|
71
|
+
findById(value: string | number): Promise<DatabaseUser<RealUser> | null>;
|
|
72
|
+
/**
|
|
73
|
+
* Finds a user using one of the pre-configured unique
|
|
74
|
+
* ids, via the configured model.
|
|
75
|
+
*/
|
|
76
|
+
findByUid(value: string | number): Promise<DatabaseUser<RealUser> | null>;
|
|
77
|
+
}
|
|
78
|
+
export {};
|
|
@@ -0,0 +1,117 @@
|
|
|
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
|
+
* Database user represents a guard user used by authentication guards
|
|
15
|
+
* to perform authentication.
|
|
16
|
+
*/
|
|
17
|
+
class DatabaseUser extends GuardUser {
|
|
18
|
+
#options;
|
|
19
|
+
#hasher;
|
|
20
|
+
constructor(realUser, hasher, options) {
|
|
21
|
+
super(realUser);
|
|
22
|
+
this.#hasher = hasher;
|
|
23
|
+
this.#options = options;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* @inheritdoc
|
|
27
|
+
*/
|
|
28
|
+
getId() {
|
|
29
|
+
const id = this.realUser[this.#options.id];
|
|
30
|
+
if (!id) {
|
|
31
|
+
throw new RuntimeException(`Invalid user object. The value of column "${this.#options.id}" is undefined or null`);
|
|
32
|
+
}
|
|
33
|
+
return id;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* @inheritdoc
|
|
37
|
+
*/
|
|
38
|
+
async verifyPassword(plainTextPassword) {
|
|
39
|
+
const password = this.realUser[this.#options.passwordColumnName];
|
|
40
|
+
if (!password) {
|
|
41
|
+
throw new RuntimeException(`Cannot verify password during login. The value of column "${this.#options.passwordColumnName}" is undefined or null`);
|
|
42
|
+
}
|
|
43
|
+
return this.#hasher.verify(password, plainTextPassword);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Database user provider is used to lookup user for authentication
|
|
48
|
+
* using the Database query builder.
|
|
49
|
+
*/
|
|
50
|
+
export class BaseDatabaseUserProvider {
|
|
51
|
+
db;
|
|
52
|
+
hasher;
|
|
53
|
+
options;
|
|
54
|
+
constructor(
|
|
55
|
+
/**
|
|
56
|
+
* Reference to the database query builder needed to
|
|
57
|
+
* query the database for users
|
|
58
|
+
*/
|
|
59
|
+
db,
|
|
60
|
+
/**
|
|
61
|
+
* Hasher is used to verify plain text passwords
|
|
62
|
+
*/
|
|
63
|
+
hasher,
|
|
64
|
+
/**
|
|
65
|
+
* Options accepted
|
|
66
|
+
*/
|
|
67
|
+
options) {
|
|
68
|
+
this.db = db;
|
|
69
|
+
this.hasher = hasher;
|
|
70
|
+
this.options = options;
|
|
71
|
+
debug('db_user_provider: options %O', options);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Returns an instance of the query builder
|
|
75
|
+
*/
|
|
76
|
+
getQueryBuilder() {
|
|
77
|
+
return this.db.connection(this.options.connection).query();
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Returns an instance of the "DatabaseUser" that guards
|
|
81
|
+
* can use for authentication
|
|
82
|
+
*/
|
|
83
|
+
async createUserForGuard(user) {
|
|
84
|
+
if (!user || typeof user !== 'object') {
|
|
85
|
+
throw new RuntimeException(`Invalid user object. It must be a database row object from the "${this.options.table}" table`);
|
|
86
|
+
}
|
|
87
|
+
debug('db_user_provider: converting user object to guard user %O', user);
|
|
88
|
+
return new DatabaseUser(user, this.hasher, this.options);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Finds a user by id by query the configured database
|
|
92
|
+
* table
|
|
93
|
+
*/
|
|
94
|
+
async findById(value) {
|
|
95
|
+
const query = this.getQueryBuilder().from(this.options.table);
|
|
96
|
+
debug('db_user_provider: finding user by id %s', value);
|
|
97
|
+
const user = await query.where(this.options.id, value).limit(1).first();
|
|
98
|
+
if (!user) {
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
return this.createUserForGuard(user);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Finds a user using one of the pre-configured unique
|
|
105
|
+
* ids, via the configured model.
|
|
106
|
+
*/
|
|
107
|
+
async findByUid(value) {
|
|
108
|
+
const query = this.getQueryBuilder().from(this.options.table);
|
|
109
|
+
this.options.uids.forEach((uid) => query.orWhere(uid, value));
|
|
110
|
+
debug('db_user_provider: finding user by uids, uids: %O, value: %s', this.options.uids, value);
|
|
111
|
+
const user = await query.limit(1).first();
|
|
112
|
+
if (!user) {
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
return this.createUserForGuard(user);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { GuardUser } from '../guard_user.js';
|
|
2
|
+
import { PROVIDER_REAL_USER } from '../../auth/symbols.js';
|
|
3
|
+
import type { UserProviderContract, LucidAuthenticatable, LucidUserProviderOptions } from '../types.js';
|
|
4
|
+
/**
|
|
5
|
+
* Lucid user represents a guard user, used by authentication guards
|
|
6
|
+
* to perform authentication.
|
|
7
|
+
*/
|
|
8
|
+
declare class LucidUser<RealUser extends InstanceType<LucidAuthenticatable>> extends GuardUser<RealUser> {
|
|
9
|
+
/**
|
|
10
|
+
* @inheritdoc
|
|
11
|
+
*/
|
|
12
|
+
getId(): string | number;
|
|
13
|
+
/**
|
|
14
|
+
* @inheritdoc
|
|
15
|
+
*/
|
|
16
|
+
verifyPassword(plainTextPassword: string): Promise<boolean>;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Lucid user provider is used to lookup user for authentication
|
|
20
|
+
* using a Lucid model.
|
|
21
|
+
*/
|
|
22
|
+
export declare abstract class BaseLucidUserProvider<UserModel extends LucidAuthenticatable> implements UserProviderContract<InstanceType<UserModel>> {
|
|
23
|
+
/**
|
|
24
|
+
* Lucid provider options
|
|
25
|
+
*/
|
|
26
|
+
protected options: LucidUserProviderOptions<UserModel>;
|
|
27
|
+
[PROVIDER_REAL_USER]: InstanceType<UserModel>;
|
|
28
|
+
/**
|
|
29
|
+
* Reference to the lazily imported model
|
|
30
|
+
*/
|
|
31
|
+
protected model?: UserModel;
|
|
32
|
+
constructor(
|
|
33
|
+
/**
|
|
34
|
+
* Lucid provider options
|
|
35
|
+
*/
|
|
36
|
+
options: LucidUserProviderOptions<UserModel>);
|
|
37
|
+
/**
|
|
38
|
+
* Imports the model from the provider, returns and caches it
|
|
39
|
+
* for further operations.
|
|
40
|
+
*/
|
|
41
|
+
protected getModel(): Promise<UserModel>;
|
|
42
|
+
/**
|
|
43
|
+
* Returns an instance of the query builder
|
|
44
|
+
*/
|
|
45
|
+
protected getQueryBuilder(model: UserModel): import("@adonisjs/lucid/types/model").ModelQueryBuilderContract<UserModel, InstanceType<UserModel>>;
|
|
46
|
+
/**
|
|
47
|
+
* Returns an instance of the "LucidUser" that guards
|
|
48
|
+
* can use for authentication
|
|
49
|
+
*/
|
|
50
|
+
createUserForGuard(user: InstanceType<UserModel>): Promise<LucidUser<InstanceType<UserModel>>>;
|
|
51
|
+
/**
|
|
52
|
+
* Finds a user by id using the configured model.
|
|
53
|
+
*/
|
|
54
|
+
findById(value: string | number): Promise<LucidUser<InstanceType<UserModel>> | null>;
|
|
55
|
+
/**
|
|
56
|
+
* Finds a user using one of the pre-configured unique
|
|
57
|
+
* ids, via the configured model.
|
|
58
|
+
*/
|
|
59
|
+
findByUid(value: string | number): Promise<LucidUser<InstanceType<UserModel>> | null>;
|
|
60
|
+
}
|
|
61
|
+
export {};
|
|
@@ -0,0 +1,122 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { Emitter } from '@adonisjs/core/events';
|
|
2
|
+
import type { HttpContext } from '@adonisjs/core/http';
|
|
3
|
+
import type { GuardContract } from '../../auth/types.js';
|
|
4
|
+
import { GUARD_KNOWN_EVENTS, PROVIDER_REAL_USER } from '../../auth/symbols.js';
|
|
5
|
+
import type { SessionGuardEvents, SessionGuardConfig, RememberMeProviderContract, SessionUserProviderContract } from './types.js';
|
|
6
|
+
/**
|
|
7
|
+
* Session guard uses sessions and cookies to login and authenticate
|
|
8
|
+
* users.
|
|
9
|
+
*/
|
|
10
|
+
export declare class SessionGuard<UserProvider extends SessionUserProviderContract<unknown>> implements GuardContract<UserProvider[typeof PROVIDER_REAL_USER]> {
|
|
11
|
+
#private;
|
|
12
|
+
[GUARD_KNOWN_EVENTS]: SessionGuardEvents<UserProvider[typeof PROVIDER_REAL_USER]>;
|
|
13
|
+
/**
|
|
14
|
+
* Driver name of the guard
|
|
15
|
+
*/
|
|
16
|
+
driverName: 'session';
|
|
17
|
+
/**
|
|
18
|
+
* Whether or not the authentication has been attempted
|
|
19
|
+
* during the current request
|
|
20
|
+
*/
|
|
21
|
+
authenticationAttempted: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Find if the user has been logged out during
|
|
24
|
+
* the current request
|
|
25
|
+
*/
|
|
26
|
+
isLoggedOut: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* A boolean to know if the current request has
|
|
29
|
+
* been authenticated
|
|
30
|
+
*/
|
|
31
|
+
isAuthenticated: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* A boolean to know if the current request is authenticated
|
|
34
|
+
* using the "rememember_me" token.
|
|
35
|
+
*/
|
|
36
|
+
viaRemember: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Reference to an instance of the authenticated or logged-in
|
|
39
|
+
* user. The value only exists after calling one of the
|
|
40
|
+
* following methods.
|
|
41
|
+
*
|
|
42
|
+
* - login
|
|
43
|
+
* - loginViaId
|
|
44
|
+
* - attempt
|
|
45
|
+
* - authenticate
|
|
46
|
+
*
|
|
47
|
+
* You can use the "getUserOrFail" method to throw an exception if
|
|
48
|
+
* the request is not authenticated.
|
|
49
|
+
*/
|
|
50
|
+
user?: UserProvider[typeof PROVIDER_REAL_USER];
|
|
51
|
+
/**
|
|
52
|
+
* The key used to store the logged-in user id inside
|
|
53
|
+
* session
|
|
54
|
+
*/
|
|
55
|
+
get sessionKeyName(): string;
|
|
56
|
+
/**
|
|
57
|
+
* The key used to store the remember me token cookie
|
|
58
|
+
*/
|
|
59
|
+
get rememberMeKeyName(): string;
|
|
60
|
+
constructor(name: string, config: SessionGuardConfig, ctx: HttpContext, userProvider: UserProvider);
|
|
61
|
+
/**
|
|
62
|
+
* Register the remember me tokens provider to create
|
|
63
|
+
* remember me tokens during user login.
|
|
64
|
+
*
|
|
65
|
+
* Note: This method only registers the remember me tokens provider
|
|
66
|
+
* and does not enable them. You must pass "rememberMe = true" during
|
|
67
|
+
* the "login" method call.
|
|
68
|
+
*/
|
|
69
|
+
withRememberMeTokens(tokensProvider: RememberMeProviderContract): this;
|
|
70
|
+
/**
|
|
71
|
+
* Register an event emitter to listen for global events for
|
|
72
|
+
* authentication lifecycle.
|
|
73
|
+
*/
|
|
74
|
+
withEmitter(emitter: Emitter<any>): this;
|
|
75
|
+
/**
|
|
76
|
+
* Returns an instance of the authenticated user. Or throws
|
|
77
|
+
* an exception if the request is not authenticated.
|
|
78
|
+
*/
|
|
79
|
+
getUserOrFail(): UserProvider[typeof PROVIDER_REAL_USER];
|
|
80
|
+
/**
|
|
81
|
+
* Verifies user credentials and returns an instance of
|
|
82
|
+
* the user or throws "E_INVALID_CREDENTIALS" exception.
|
|
83
|
+
*/
|
|
84
|
+
verifyCredentials(uid: string, password: string): Promise<UserProvider[typeof PROVIDER_REAL_USER]>;
|
|
85
|
+
/**
|
|
86
|
+
* Attempt to login a user after verifying their
|
|
87
|
+
* credentials.
|
|
88
|
+
*/
|
|
89
|
+
attempt(uid: string, password: string): Promise<UserProvider[typeof PROVIDER_REAL_USER]>;
|
|
90
|
+
/**
|
|
91
|
+
* Attempt to login a user using the user id. The
|
|
92
|
+
* user will be first fetched from the db before
|
|
93
|
+
* marking them as logged-in
|
|
94
|
+
*/
|
|
95
|
+
loginViaId(id: string | number): Promise<UserProvider[typeof PROVIDER_REAL_USER]>;
|
|
96
|
+
/**
|
|
97
|
+
* Login a user using the user object.
|
|
98
|
+
*/
|
|
99
|
+
login(user: UserProvider[typeof PROVIDER_REAL_USER], remember?: boolean): Promise<UserProvider[typeof PROVIDER_REAL_USER]>;
|
|
100
|
+
/**
|
|
101
|
+
* Authenticates the HTTP request to ensure the
|
|
102
|
+
* user is logged-in
|
|
103
|
+
*/
|
|
104
|
+
authenticate(): Promise<UserProvider[typeof PROVIDER_REAL_USER]>;
|
|
105
|
+
/**
|
|
106
|
+
* Silently attempt to authenticate the user.
|
|
107
|
+
*
|
|
108
|
+
* The method returns a boolean indicating if the authentication
|
|
109
|
+
* succeeded or failed.
|
|
110
|
+
*/
|
|
111
|
+
check(): Promise<boolean>;
|
|
112
|
+
}
|