@adonisjs/auth 8.2.2 → 9.0.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.
Files changed (134) hide show
  1. package/LICENSE.md +1 -1
  2. package/README.md +19 -40
  3. package/build/configure.d.ts +5 -0
  4. package/build/configure.js +41 -0
  5. package/build/factories/database_token_factory.d.ts +36 -0
  6. package/build/factories/database_token_factory.js +54 -0
  7. package/build/factories/database_user_provider.d.ts +14 -0
  8. package/build/factories/database_user_provider.js +27 -0
  9. package/build/factories/lucid_user_provider.d.ts +28 -0
  10. package/build/factories/lucid_user_provider.js +68 -0
  11. package/build/factories/main.d.ts +4 -0
  12. package/build/factories/main.js +12 -0
  13. package/build/factories/session_guard_factory.d.ts +13 -0
  14. package/build/factories/session_guard_factory.js +24 -0
  15. package/build/index.d.ts +7 -0
  16. package/build/index.js +15 -0
  17. package/build/providers/auth_provider.d.ts +12 -0
  18. package/build/providers/auth_provider.js +27 -0
  19. package/build/services/auth.d.ts +3 -0
  20. package/build/services/auth.js +17 -0
  21. package/build/src/auth/auth_manager.d.ts +18 -0
  22. package/build/src/auth/auth_manager.js +28 -0
  23. package/build/src/auth/authenticator.d.ts +18 -0
  24. package/build/src/auth/authenticator.js +55 -0
  25. package/build/src/auth/debug.d.ts +3 -0
  26. package/build/{adonis-typings/container.js → src/auth/debug.js} +3 -1
  27. package/build/src/auth/define_config.d.ts +30 -0
  28. package/build/src/auth/define_config.js +54 -0
  29. package/build/src/auth/errors.d.ts +8 -0
  30. package/build/src/auth/errors.js +17 -0
  31. package/build/src/auth/symbols.d.ts +9 -0
  32. package/build/src/auth/symbols.js +17 -0
  33. package/build/src/auth/types.d.ts +52 -0
  34. package/build/{adonis-typings/context.js → src/auth/types.js} +2 -1
  35. package/build/src/auth/user_providers/main.d.ts +15 -0
  36. package/build/src/auth/user_providers/main.js +22 -0
  37. package/build/src/core/guard_user.d.ts +26 -0
  38. package/build/src/core/guard_user.js +29 -0
  39. package/build/src/core/token.d.ts +86 -0
  40. package/build/src/core/token.js +112 -0
  41. package/build/src/core/token_providers/database.d.ts +77 -0
  42. package/build/src/core/token_providers/database.js +113 -0
  43. package/build/src/core/types.d.ts +178 -0
  44. package/build/{adonis-typings/auth.js → src/core/types.js} +2 -1
  45. package/build/src/core/user_providers/database.d.ts +78 -0
  46. package/build/src/core/user_providers/database.js +117 -0
  47. package/build/src/core/user_providers/lucid.d.ts +61 -0
  48. package/build/src/core/user_providers/lucid.js +122 -0
  49. package/build/src/guards/session/define_config.d.ts +23 -0
  50. package/build/src/guards/session/define_config.js +56 -0
  51. package/build/src/guards/session/guard.d.ts +92 -0
  52. package/build/src/guards/session/guard.js +380 -0
  53. package/build/src/guards/session/main.d.ts +3 -0
  54. package/build/src/guards/session/main.js +11 -0
  55. package/build/src/guards/session/token.d.ts +57 -0
  56. package/build/src/guards/session/token.js +58 -0
  57. package/build/src/guards/session/token_providers/main.d.ts +33 -0
  58. package/build/src/guards/session/token_providers/main.js +42 -0
  59. package/build/src/guards/session/types.d.ts +97 -0
  60. package/build/{adonis-typings/events.js → src/guards/session/types.js} +2 -1
  61. package/build/stubs/config/auth_middleware.stub +12 -0
  62. package/build/stubs/config.stub +35 -0
  63. package/build/stubs/main.d.ts +1 -0
  64. package/build/{adonis-typings/tests.js → stubs/main.js} +2 -3
  65. package/package.json +96 -91
  66. package/build/adonis-typings/auth.d.ts +0 -635
  67. package/build/adonis-typings/container.d.ts +0 -6
  68. package/build/adonis-typings/context.d.ts +0 -6
  69. package/build/adonis-typings/events.d.ts +0 -10
  70. package/build/adonis-typings/index.d.ts +0 -5
  71. package/build/adonis-typings/index.js +0 -13
  72. package/build/adonis-typings/tests.d.ts +0 -23
  73. package/build/instructions.js +0 -338
  74. package/build/providers/AuthProvider.d.ts +0 -30
  75. package/build/providers/AuthProvider.js +0 -69
  76. package/build/src/Auth/index.d.ts +0 -97
  77. package/build/src/Auth/index.js +0 -155
  78. package/build/src/AuthManager/index.d.ts +0 -117
  79. package/build/src/AuthManager/index.js +0 -262
  80. package/build/src/Bindings/Tests.d.ts +0 -6
  81. package/build/src/Bindings/Tests.js +0 -69
  82. package/build/src/Clients/Oat/index.d.ts +0 -50
  83. package/build/src/Clients/Oat/index.js +0 -123
  84. package/build/src/Clients/Session/index.d.ts +0 -34
  85. package/build/src/Clients/Session/index.js +0 -72
  86. package/build/src/Exceptions/AuthenticationException.d.ts +0 -47
  87. package/build/src/Exceptions/AuthenticationException.js +0 -142
  88. package/build/src/Exceptions/InvalidCredentialsException.d.ts +0 -34
  89. package/build/src/Exceptions/InvalidCredentialsException.js +0 -112
  90. package/build/src/Guards/Base/index.d.ts +0 -75
  91. package/build/src/Guards/Base/index.js +0 -138
  92. package/build/src/Guards/BasicAuth/index.d.ts +0 -67
  93. package/build/src/Guards/BasicAuth/index.js +0 -181
  94. package/build/src/Guards/Oat/index.d.ts +0 -149
  95. package/build/src/Guards/Oat/index.js +0 -347
  96. package/build/src/Guards/Session/index.d.ts +0 -127
  97. package/build/src/Guards/Session/index.js +0 -338
  98. package/build/src/TokenProviders/Database/index.d.ts +0 -43
  99. package/build/src/TokenProviders/Database/index.js +0 -126
  100. package/build/src/TokenProviders/Redis/index.d.ts +0 -44
  101. package/build/src/TokenProviders/Redis/index.js +0 -129
  102. package/build/src/Tokens/OpaqueToken/index.d.ts +0 -46
  103. package/build/src/Tokens/OpaqueToken/index.js +0 -43
  104. package/build/src/Tokens/ProviderToken/index.d.ts +0 -23
  105. package/build/src/Tokens/ProviderToken/index.js +0 -27
  106. package/build/src/UserProviders/Database/User.d.ts +0 -28
  107. package/build/src/UserProviders/Database/User.js +0 -74
  108. package/build/src/UserProviders/Database/index.d.ts +0 -75
  109. package/build/src/UserProviders/Database/index.js +0 -141
  110. package/build/src/UserProviders/Lucid/User.d.ts +0 -28
  111. package/build/src/UserProviders/Lucid/User.js +0 -74
  112. package/build/src/UserProviders/Lucid/index.d.ts +0 -72
  113. package/build/src/UserProviders/Lucid/index.js +0 -146
  114. package/build/standalone.d.ts +0 -1
  115. package/build/standalone.js +0 -13
  116. package/build/templates/config/auth.txt +0 -34
  117. package/build/templates/config/partials/api-guard.txt +0 -22
  118. package/build/templates/config/partials/basic-guard.txt +0 -19
  119. package/build/templates/config/partials/tokens-provider-database.txt +0 -19
  120. package/build/templates/config/partials/tokens-provider-redis.txt +0 -22
  121. package/build/templates/config/partials/user-provider-database.txt +0 -43
  122. package/build/templates/config/partials/user-provider-lucid.txt +0 -45
  123. package/build/templates/config/partials/web-guard.txt +0 -17
  124. package/build/templates/contract/auth.txt +0 -55
  125. package/build/templates/contract/partials/api-guard.txt +0 -14
  126. package/build/templates/contract/partials/basic-guard.txt +0 -14
  127. package/build/templates/contract/partials/user-provider-database.txt +0 -16
  128. package/build/templates/contract/partials/user-provider-lucid.txt +0 -16
  129. package/build/templates/contract/partials/web-guard.txt +0 -14
  130. package/build/templates/middleware/Auth.txt +0 -76
  131. package/build/templates/middleware/SilentAuth.txt +0 -21
  132. package/build/templates/migrations/api_tokens.txt +0 -25
  133. package/build/templates/migrations/auth.txt +0 -24
  134. package/build/templates/model.txt +0 -30
@@ -0,0 +1,113 @@
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 debug from '../../auth/debug.js';
10
+ /**
11
+ * A generic implementation to read tokens from the database
12
+ */
13
+ export class DatabaseTokenProvider {
14
+ db;
15
+ options;
16
+ constructor(
17
+ /**
18
+ * Reference to the database query builder needed to
19
+ * query the database for tokens
20
+ */
21
+ db,
22
+ /**
23
+ * Options accepted
24
+ */
25
+ options) {
26
+ this.db = db;
27
+ this.options = options;
28
+ debug('db_token_provider: options %O', options);
29
+ }
30
+ /**
31
+ * Returns an instance of the query builder
32
+ */
33
+ getQueryBuilder() {
34
+ return this.db.connection(this.options.connection).query();
35
+ }
36
+ /**
37
+ * Returns an instance of the query builder for insert
38
+ * queries
39
+ */
40
+ getInsertQueryBuilder() {
41
+ return this.db.connection(this.options.connection).insertQuery();
42
+ }
43
+ /**
44
+ * Persists token inside the database
45
+ */
46
+ async createToken(token) {
47
+ const parsedToken = this.parseToken(token);
48
+ debug('db_token_provider: creating token %O', parsedToken);
49
+ await this.getInsertQueryBuilder()
50
+ .table(this.options.table)
51
+ .insert({
52
+ ...parsedToken,
53
+ });
54
+ }
55
+ /**
56
+ * Finds a token by series inside the database and returns an
57
+ * instance of it.
58
+ *
59
+ * Returns null if the token is missing or expired
60
+ */
61
+ async getTokenBySeries(series) {
62
+ debug('db_token_provider: reading token by series %s', series);
63
+ const token = await this.getQueryBuilder()
64
+ .from(this.options.table)
65
+ .where('series', series)
66
+ .limit(1)
67
+ .first();
68
+ if (!token) {
69
+ debug('db_token_provider:: token %O', token);
70
+ return null;
71
+ }
72
+ if (typeof token.expires_at === 'number') {
73
+ token.expires_at = new Date(token.expires_at);
74
+ }
75
+ if (typeof token.created_at === 'number') {
76
+ token.created_at = new Date(token.created_at);
77
+ }
78
+ if (typeof token.updated_at === 'number') {
79
+ token.updated_at = new Date(token.updated_at);
80
+ }
81
+ debug('db_token_provider:: token %O', token);
82
+ /**
83
+ * Return null when token has been expired
84
+ */
85
+ if (token.expires_at && token.expires_at instanceof Date && token.expires_at < new Date()) {
86
+ return null;
87
+ }
88
+ return this.prepareToken(token);
89
+ }
90
+ /**
91
+ * Removes a token from the database by the
92
+ * series number
93
+ */
94
+ async deleteTokenBySeries(series) {
95
+ debug('db_token_provider: deleting token by series %s', series);
96
+ await this.getQueryBuilder().from(this.options.table).where('series', series).del();
97
+ }
98
+ /**
99
+ * Updates token hash and expiry
100
+ */
101
+ async updateTokenBySeries(series, hash, expiresAt) {
102
+ const updatePayload = {
103
+ token: hash,
104
+ updated_at: new Date(),
105
+ expires_at: expiresAt,
106
+ };
107
+ debug('db_token_provider: updating token by series %s: %O', series, updatePayload);
108
+ await this.getQueryBuilder()
109
+ .from(this.options.table)
110
+ .where('series', series)
111
+ .update(updatePayload);
112
+ }
113
+ }
@@ -0,0 +1,178 @@
1
+ import type { QueryClientContract } from '@adonisjs/lucid/types/database';
2
+ import type { GuardUser } from './guard_user.js';
3
+ import type { PROVIDER_REAL_USER } from '../auth/symbols.js';
4
+ import type { LucidModel, LucidRow } from '@adonisjs/lucid/types/model';
5
+ /**
6
+ * A token represents an opaque token issued to a client
7
+ * to perform a specific task.
8
+ *
9
+ * The raw value of a token is only visible at the time of
10
+ * issuing it and one must persist hash to the database.
11
+ */
12
+ export interface TokenContract {
13
+ /**
14
+ * Token type to uniquely identify a bucket of tokens
15
+ */
16
+ readonly type: string;
17
+ /**
18
+ * The plain text value. Only exists when the token is first
19
+ * created
20
+ */
21
+ value?: string;
22
+ /**
23
+ * Additional metadata associated with the token.
24
+ */
25
+ metaData?: Record<string, any>;
26
+ /**
27
+ * The token hash for persisting the token in a database
28
+ */
29
+ hash: string;
30
+ /**
31
+ * A unique readable series counter to find the token inside the
32
+ * database.
33
+ */
34
+ series: string;
35
+ /**
36
+ * Timestamp when the token was first persisted
37
+ */
38
+ createdAt: Date;
39
+ /**
40
+ * Timestamp when the token was updated
41
+ */
42
+ updatedAt: Date;
43
+ /**
44
+ * Timestamp when the token will expire
45
+ */
46
+ expiresAt?: Date;
47
+ /**
48
+ * Verifies the raw text value against the hash
49
+ */
50
+ verify(value: string): boolean;
51
+ }
52
+ /**
53
+ * The UserProvider is used to lookup a user for authentication
54
+ */
55
+ export interface UserProviderContract<RealUser> {
56
+ [PROVIDER_REAL_USER]: RealUser;
57
+ /**
58
+ * Creates a user object that guards can use for
59
+ * authentication.
60
+ */
61
+ createUserForGuard(user: RealUser): Promise<GuardUser<RealUser>>;
62
+ /**
63
+ * Find a user by uid. The uid could be one or multiple fields
64
+ * to unique identify a user.
65
+ *
66
+ * This method is called when finding a user for login
67
+ */
68
+ findByUid(value: string | number): Promise<GuardUser<RealUser> | null>;
69
+ /**
70
+ * Find a user by unique primary id. This method is called when
71
+ * authenticating user from their session.
72
+ */
73
+ findById(value: string | number): Promise<GuardUser<RealUser> | null>;
74
+ }
75
+ /**
76
+ * The TokenProvider is used to lookup/persist tokens during authentication
77
+ */
78
+ export interface TokenProviderContract<Token> {
79
+ /**
80
+ * Returns a token by the series counter, or null when token is
81
+ * missing
82
+ */
83
+ getTokenBySeries(series: string): Promise<Token | null>;
84
+ /**
85
+ * Deletes a token by the series counter
86
+ */
87
+ deleteTokenBySeries(series: string): Promise<void>;
88
+ /**
89
+ * Updates a token by the series counter
90
+ */
91
+ updateTokenBySeries(series: string, hash: string, expiresAt: Date): Promise<void>;
92
+ /**
93
+ * Creates a new token and persists it to the database
94
+ */
95
+ createToken(token: Token): Promise<void>;
96
+ }
97
+ /**
98
+ * A lucid model that can be used during authentication
99
+ */
100
+ export type LucidAuthenticatable = LucidModel & {
101
+ new (): LucidRow & {
102
+ /**
103
+ * Verify the plain text password against the user password
104
+ * hash
105
+ */
106
+ verifyPasswordForAuth(plainTextPassword: string): Promise<boolean>;
107
+ };
108
+ };
109
+ /**
110
+ * Options accepted by the Lucid user provider
111
+ */
112
+ export type LucidUserProviderOptions<Model extends LucidAuthenticatable> = {
113
+ /**
114
+ * Optionally define the connection to use when making database
115
+ * queries
116
+ */
117
+ connection?: string;
118
+ /**
119
+ * Optionally define the query client instance to use for making
120
+ * database queries.
121
+ *
122
+ * When both "connection" and "client" are defined, the client will
123
+ * be given the preference.
124
+ */
125
+ client?: QueryClientContract;
126
+ /**
127
+ * Model to use for authentication
128
+ */
129
+ model: () => Promise<{
130
+ default: Model;
131
+ }>;
132
+ /**
133
+ * An array of uids to use when finding a user for login. Make
134
+ * sure all fields can be used to uniquely lookup a user.
135
+ */
136
+ uids: Extract<keyof InstanceType<Model>, string>[];
137
+ };
138
+ /**
139
+ * Options accepted by the Database user provider
140
+ */
141
+ export type DatabaseUserProviderOptions<RealUser extends Record<string, any>> = {
142
+ /**
143
+ * Optionally define the connection to use when making database
144
+ * queries
145
+ */
146
+ connection?: string;
147
+ /**
148
+ * Database table to query to find the user
149
+ */
150
+ table: string;
151
+ /**
152
+ * Column name to read the hashed password
153
+ */
154
+ passwordColumnName: string;
155
+ /**
156
+ * An array of uids to use when finding a user for login. Make
157
+ * sure all fields can be used to uniquely lookup a user.
158
+ */
159
+ uids: Extract<keyof RealUser, string>[];
160
+ /**
161
+ * The name of the id column to unique identify the user.
162
+ */
163
+ id: string;
164
+ };
165
+ /**
166
+ * Options accepted by the Database token provider
167
+ */
168
+ export type DatabaseTokenProviderOptions = {
169
+ /**
170
+ * Optionally define the connection to use when making database
171
+ * queries
172
+ */
173
+ connection?: string;
174
+ /**
175
+ * Database table to query to find the user
176
+ */
177
+ table: string;
178
+ };
@@ -1,8 +1,9 @@
1
1
  /*
2
2
  * @adonisjs/auth
3
3
  *
4
- * (c) Harminder Virk <virk@adonisjs.com>
4
+ * (c) AdonisJS
5
5
  *
6
6
  * For the full copyright and license information, please view the LICENSE
7
7
  * file that was distributed with this source code.
8
8
  */
9
+ export {};
@@ -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
+ }