@adonisjs/auth 8.2.3 → 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,30 @@
1
+ import type { ConfigProvider } from '@adonisjs/core/types';
2
+ import type { GuardConfigProvider, GuardFactory } from './types.js';
3
+ import type { LucidUserProvider, DatabaseUserProvider } from './user_providers/main.js';
4
+ import type { LucidAuthenticatable, LucidUserProviderOptions, DatabaseUserProviderOptions } from '../core/types.js';
5
+ /**
6
+ * Config resolved by the "defineConfig" method
7
+ */
8
+ export type ResolvedAuthConfig<KnownGuards extends Record<string, GuardFactory | GuardConfigProvider<GuardFactory>>> = {
9
+ default: keyof KnownGuards;
10
+ guards: {
11
+ [K in keyof KnownGuards]: KnownGuards[K] extends GuardConfigProvider<infer A> ? A : KnownGuards[K];
12
+ };
13
+ };
14
+ /**
15
+ * Define configuration for the auth package. The function returns
16
+ * a config provider that is invoked inside the auth service
17
+ * provider
18
+ */
19
+ export declare function defineConfig<KnownGuards extends Record<string, GuardFactory | GuardConfigProvider<GuardFactory>>>(config: {
20
+ default: keyof KnownGuards;
21
+ guards: KnownGuards;
22
+ }): ConfigProvider<ResolvedAuthConfig<KnownGuards>>;
23
+ /**
24
+ * Providers helper to configure user providers for
25
+ * finding users for authentication
26
+ */
27
+ export declare const providers: {
28
+ db: <RealUser extends Record<string, any>>(config: DatabaseUserProviderOptions<RealUser>) => ConfigProvider<DatabaseUserProvider<RealUser>>;
29
+ lucid: <RealUser extends LucidAuthenticatable>(config: LucidUserProviderOptions<RealUser>) => ConfigProvider<LucidUserProvider<RealUser>>;
30
+ };
@@ -0,0 +1,54 @@
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
+ /// <reference types="@adonisjs/lucid/database_provider" />
10
+ import { configProvider } from '@adonisjs/core';
11
+ /**
12
+ * Define configuration for the auth package. The function returns
13
+ * a config provider that is invoked inside the auth service
14
+ * provider
15
+ */
16
+ export function defineConfig(config) {
17
+ return configProvider.create(async (app) => {
18
+ const guardsList = Object.keys(config.guards);
19
+ const guards = {};
20
+ for (let guardName of guardsList) {
21
+ const guard = config.guards[guardName];
22
+ if (typeof guard === 'function') {
23
+ guards[guardName] = guard;
24
+ }
25
+ else {
26
+ guards[guardName] = await guard.resolver(guardName, app);
27
+ }
28
+ }
29
+ return {
30
+ default: config.default,
31
+ guards: guards,
32
+ };
33
+ });
34
+ }
35
+ /**
36
+ * Providers helper to configure user providers for
37
+ * finding users for authentication
38
+ */
39
+ export const providers = {
40
+ db(config) {
41
+ return configProvider.create(async (app) => {
42
+ const db = await app.container.make('lucid.db');
43
+ const hasher = await app.container.make('hash');
44
+ const { DatabaseUserProvider } = await import('./user_providers/main.js');
45
+ return new DatabaseUserProvider(db, hasher.use(), config);
46
+ });
47
+ },
48
+ lucid(config) {
49
+ return configProvider.create(async () => {
50
+ const { LucidUserProvider } = await import('./user_providers/main.js');
51
+ return new LucidUserProvider(config);
52
+ });
53
+ },
54
+ };
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Invalid token provided
3
+ */
4
+ export declare const E_INVALID_AUTH_TOKEN: new (args?: any, options?: ErrorOptions | undefined) => import("@poppinss/utils").Exception;
5
+ /**
6
+ * The user session is invalid
7
+ */
8
+ export declare const E_INVALID_AUTH_SESSION: new (args?: any, options?: ErrorOptions | undefined) => import("@poppinss/utils").Exception;
@@ -0,0 +1,17 @@
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 { createError } from '@poppinss/utils';
10
+ /**
11
+ * Invalid token provided
12
+ */
13
+ export const E_INVALID_AUTH_TOKEN = createError('Invalid or expired token value', 'E_INVALID_AUTH_TOKEN', 401);
14
+ /**
15
+ * The user session is invalid
16
+ */
17
+ export const E_INVALID_AUTH_SESSION = createError('Invalid or expired authentication session', 'E_INVALID_AUTH_SESSION', 401);
@@ -0,0 +1,9 @@
1
+ /**
2
+ * A symbol to identify the type of the real user for a given
3
+ * user provider
4
+ */
5
+ export declare const PROVIDER_REAL_USER: unique symbol;
6
+ /**
7
+ * A symbol to identify the type for the events emitted by a guard
8
+ */
9
+ export declare const GUARD_KNOWN_EVENTS: unique symbol;
@@ -0,0 +1,17 @@
1
+ /*
2
+ * @adonisjs/lucid
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
+ /**
10
+ * A symbol to identify the type of the real user for a given
11
+ * user provider
12
+ */
13
+ export const PROVIDER_REAL_USER = Symbol.for('PROVIDER_REAL_USER');
14
+ /**
15
+ * A symbol to identify the type for the events emitted by a guard
16
+ */
17
+ export const GUARD_KNOWN_EVENTS = Symbol.for('GUARD_KNOWN_EVENTS');
@@ -0,0 +1,52 @@
1
+ import type { Emitter } from '@adonisjs/core/events';
2
+ import type { HttpContext } from '@adonisjs/core/http';
3
+ import type { ApplicationService, ConfigProvider } from '@adonisjs/core/types';
4
+ import type { AuthManager } from './auth_manager.js';
5
+ import type { GUARD_KNOWN_EVENTS } from './symbols.js';
6
+ /**
7
+ * A set of properties a guard must implement.
8
+ */
9
+ export interface GuardContract<User> {
10
+ /**
11
+ * Reference to the user type
12
+ */
13
+ user?: User;
14
+ /**
15
+ * Aymbol for infer the events emitted by a specific
16
+ * guard
17
+ */
18
+ [GUARD_KNOWN_EVENTS]: unknown;
19
+ /**
20
+ * Accept an instance of the emitter to emit events
21
+ */
22
+ withEmitter(emitter: Emitter<any>): this;
23
+ }
24
+ /**
25
+ * The authenticator guard factory method is called by the
26
+ * Authenticator class to create an instance of a specific
27
+ * guard during an HTTP request
28
+ */
29
+ export type GuardFactory = (ctx: HttpContext) => GuardContract<unknown>;
30
+ /**
31
+ * Authenticators are inferred inside the user application
32
+ * from the config file
33
+ */
34
+ export interface Authenticators {
35
+ }
36
+ /**
37
+ * Infer authenticators from the auth config
38
+ */
39
+ export type InferAuthenticators<Config extends ConfigProvider<unknown>> = Awaited<ReturnType<Config['resolver']>>;
40
+ /**
41
+ * Auth service is a singleton instance of the AuthManager
42
+ * configured using the config stored within the user
43
+ * app.
44
+ */
45
+ export interface AuthService extends AuthManager<Authenticators extends GuardFactory ? Authenticators : never> {
46
+ }
47
+ /**
48
+ * Config provider for exporting guard
49
+ */
50
+ export type GuardConfigProvider<Guard extends GuardFactory> = {
51
+ resolver: (name: string, app: ApplicationService) => Promise<Guard>;
52
+ };
@@ -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,15 @@
1
+ import { BaseLucidUserProvider } from '../../core/user_providers/lucid.js';
2
+ import { BaseDatabaseUserProvider } from '../../core/user_providers/database.js';
3
+ import type { LucidAuthenticatable, UserProviderContract } from '../../core/types.js';
4
+ /**
5
+ * Using lucid models to find users for session
6
+ * auth
7
+ */
8
+ export declare class LucidUserProvider<UserModel extends LucidAuthenticatable> extends BaseLucidUserProvider<UserModel> implements UserProviderContract<InstanceType<UserModel>> {
9
+ }
10
+ /**
11
+ * Using database query builder to find users for
12
+ * session auth
13
+ */
14
+ export declare class DatabaseUserProvider<User extends Record<string, any>> extends BaseDatabaseUserProvider<User> implements UserProviderContract<User> {
15
+ }
@@ -0,0 +1,22 @@
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 { BaseLucidUserProvider } from '../../core/user_providers/lucid.js';
10
+ import { BaseDatabaseUserProvider } from '../../core/user_providers/database.js';
11
+ /**
12
+ * Using lucid models to find users for session
13
+ * auth
14
+ */
15
+ export class LucidUserProvider extends BaseLucidUserProvider {
16
+ }
17
+ /**
18
+ * Using database query builder to find users for
19
+ * session auth
20
+ */
21
+ export class DatabaseUserProvider extends BaseDatabaseUserProvider {
22
+ }
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Guard user represents a user independent of the storage
3
+ * provider. It contains a standard set of properties
4
+ * used by authentication guards to interact with
5
+ * a user.
6
+ *
7
+ * Think of it as a bridge between a user and the authentication
8
+ * guard.
9
+ */
10
+ export declare abstract class GuardUser<RealUser> {
11
+ protected realUser: RealUser;
12
+ constructor(realUser: RealUser);
13
+ /**
14
+ * Verifies the plain text password against the user password
15
+ * hash
16
+ */
17
+ abstract verifyPassword(plainTextPassword: string): Promise<boolean>;
18
+ /**
19
+ * Returns a value to uniquely identify the user.
20
+ */
21
+ abstract getId(): number | string;
22
+ /**
23
+ * Returns the original provider specific user object.
24
+ */
25
+ getOriginal(): RealUser;
26
+ }
@@ -0,0 +1,29 @@
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
+ /**
10
+ * Guard user represents a user independent of the storage
11
+ * provider. It contains a standard set of properties
12
+ * used by authentication guards to interact with
13
+ * a user.
14
+ *
15
+ * Think of it as a bridge between a user and the authentication
16
+ * guard.
17
+ */
18
+ export class GuardUser {
19
+ realUser;
20
+ constructor(realUser) {
21
+ this.realUser = realUser;
22
+ }
23
+ /**
24
+ * Returns the original provider specific user object.
25
+ */
26
+ getOriginal() {
27
+ return this.realUser;
28
+ }
29
+ }
@@ -0,0 +1,86 @@
1
+ import type { TokenContract } from './types.js';
2
+ /**
3
+ * A token represents an opaque token issued to a client
4
+ * to perform a specific task.
5
+ *
6
+ * The raw value of a token is only visible at the time of
7
+ * issuing it and one must persist hash to the database.
8
+ */
9
+ export declare abstract class Token implements TokenContract {
10
+ /**
11
+ * Series is a random number stored inside the database as it is
12
+ */
13
+ series: string;
14
+ /**
15
+ * Value is a random number only available at the time of issuing
16
+ * the token. Afterwards, the value is undefined.
17
+ */
18
+ value: string | undefined;
19
+ /**
20
+ * Hash reference to the token hash
21
+ */
22
+ hash: string;
23
+ /**
24
+ * Token type to uniquely identify a bucket of tokens
25
+ */
26
+ abstract readonly type: string;
27
+ /**
28
+ * Arbitary meta-data associated with the token
29
+ */
30
+ metaData?: Record<string, any>;
31
+ /**
32
+ * Timestamp when the token will expire
33
+ */
34
+ expiresAt?: Date;
35
+ /**
36
+ * Date/time when the token instance was created
37
+ */
38
+ createdAt: Date;
39
+ /**
40
+ * Date/time when the token was updated
41
+ */
42
+ updatedAt: Date;
43
+ constructor(
44
+ /**
45
+ * Series is a random number stored inside the database as it is
46
+ */
47
+ series: string,
48
+ /**
49
+ * Value is a random number only available at the time of issuing
50
+ * the token. Afterwards, the value is undefined.
51
+ */
52
+ value: string | undefined,
53
+ /**
54
+ * Hash reference to the token hash
55
+ */
56
+ hash: string);
57
+ /**
58
+ * Define metadata for the token
59
+ */
60
+ setMetaData(metaData: Record<string, any>): this;
61
+ /**
62
+ * Verifies the value of a token against the pre-defined hash
63
+ */
64
+ verify(value: string): boolean;
65
+ /**
66
+ * Define the token expiresAt timestamp from a duration. The value
67
+ * value must be a number in seconds or a string expression.
68
+ */
69
+ setExpiry(duration: string | number): void;
70
+ /**
71
+ * Creates token value, series, and hash
72
+ */
73
+ static seed(size?: number): {
74
+ series: string;
75
+ value: string;
76
+ hash: string;
77
+ };
78
+ /**
79
+ * Decodes a publicly shared token and return the series
80
+ * and the token value from it.
81
+ */
82
+ static decode(value: string): {
83
+ series: string;
84
+ value: string;
85
+ };
86
+ }
@@ -0,0 +1,112 @@
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 { createHash } from 'node:crypto';
10
+ import string from '@adonisjs/core/helpers/string';
11
+ import { base64, safeEqual } from '@adonisjs/core/helpers';
12
+ import * as errors from '../auth/errors.js';
13
+ /**
14
+ * A token represents an opaque token issued to a client
15
+ * to perform a specific task.
16
+ *
17
+ * The raw value of a token is only visible at the time of
18
+ * issuing it and one must persist hash to the database.
19
+ */
20
+ export class Token {
21
+ series;
22
+ value;
23
+ hash;
24
+ /**
25
+ * Arbitary meta-data associated with the token
26
+ */
27
+ metaData;
28
+ /**
29
+ * Timestamp when the token will expire
30
+ */
31
+ expiresAt;
32
+ /**
33
+ * Date/time when the token instance was created
34
+ */
35
+ createdAt = new Date();
36
+ /**
37
+ * Date/time when the token was updated
38
+ */
39
+ updatedAt = new Date();
40
+ constructor(
41
+ /**
42
+ * Series is a random number stored inside the database as it is
43
+ */
44
+ series,
45
+ /**
46
+ * Value is a random number only available at the time of issuing
47
+ * the token. Afterwards, the value is undefined.
48
+ */
49
+ value,
50
+ /**
51
+ * Hash reference to the token hash
52
+ */
53
+ hash) {
54
+ this.series = series;
55
+ this.value = value;
56
+ this.hash = hash;
57
+ }
58
+ /**
59
+ * Define metadata for the token
60
+ */
61
+ setMetaData(metaData) {
62
+ this.metaData = metaData;
63
+ return this;
64
+ }
65
+ /**
66
+ * Verifies the value of a token against the pre-defined hash
67
+ */
68
+ verify(value) {
69
+ const newHash = createHash('sha256').update(value).digest('hex');
70
+ return safeEqual(this.hash, newHash);
71
+ }
72
+ /**
73
+ * Define the token expiresAt timestamp from a duration. The value
74
+ * value must be a number in seconds or a string expression.
75
+ */
76
+ setExpiry(duration) {
77
+ /**
78
+ * Defining a date object and adding seconds since the
79
+ * creation of the token
80
+ */
81
+ this.expiresAt = new Date();
82
+ this.expiresAt.setSeconds(this.createdAt.getSeconds() + string.seconds.parse(duration));
83
+ }
84
+ /**
85
+ * Creates token value, series, and hash
86
+ */
87
+ static seed(size = 30) {
88
+ const series = string.random(15);
89
+ const value = string.random(size);
90
+ const hash = createHash('sha256').update(value).digest('hex');
91
+ return { series, value: `${base64.urlEncode(series)}.${base64.urlEncode(value)}`, hash };
92
+ }
93
+ /**
94
+ * Decodes a publicly shared token and return the series
95
+ * and the token value from it.
96
+ */
97
+ static decode(value) {
98
+ const [series, ...tokenValue] = value.split('.');
99
+ if (!series || tokenValue.length === 0) {
100
+ throw new errors.E_INVALID_AUTH_TOKEN();
101
+ }
102
+ const decodedSeries = base64.urlDecode(series);
103
+ const decodedValue = base64.urlDecode(tokenValue.join('.'));
104
+ if (!decodedSeries || !decodedValue) {
105
+ throw new errors.E_INVALID_AUTH_TOKEN();
106
+ }
107
+ return {
108
+ series: decodedSeries,
109
+ value: decodedValue,
110
+ };
111
+ }
112
+ }
@@ -0,0 +1,77 @@
1
+ import type { Database } from '@adonisjs/lucid/database';
2
+ import type { DatabaseTokenProviderOptions, TokenProviderContract } from '../types.js';
3
+ /**
4
+ * The representation of a token inside the database
5
+ */
6
+ type DatabaseTokenRow = {
7
+ series: string;
8
+ user_id: string | number;
9
+ type: string;
10
+ token: string;
11
+ created_at: Date;
12
+ updated_at: Date;
13
+ expires_at: Date | null;
14
+ };
15
+ /**
16
+ * A generic implementation to read tokens from the database
17
+ */
18
+ export declare abstract class DatabaseTokenProvider<Token> implements TokenProviderContract<Token> {
19
+ /**
20
+ * Reference to the database query builder needed to
21
+ * query the database for tokens
22
+ */
23
+ protected db: Database;
24
+ /**
25
+ * Options accepted
26
+ */
27
+ protected options: DatabaseTokenProviderOptions;
28
+ constructor(
29
+ /**
30
+ * Reference to the database query builder needed to
31
+ * query the database for tokens
32
+ */
33
+ db: Database,
34
+ /**
35
+ * Options accepted
36
+ */
37
+ options: DatabaseTokenProviderOptions);
38
+ /**
39
+ * Should parse token to a database token row
40
+ */
41
+ protected abstract parseToken(token: Token): DatabaseTokenRow;
42
+ /**
43
+ * Abstract method to prepare a token from the database
44
+ * row
45
+ */
46
+ protected abstract prepareToken(dbRow: DatabaseTokenRow): Token;
47
+ /**
48
+ * Returns an instance of the query builder
49
+ */
50
+ protected getQueryBuilder(): import("@adonisjs/lucid/types/querybuilder").DatabaseQueryBuilderContract<DatabaseTokenRow>;
51
+ /**
52
+ * Returns an instance of the query builder for insert
53
+ * queries
54
+ */
55
+ protected getInsertQueryBuilder(): import("@adonisjs/lucid/types/querybuilder").InsertQueryBuilderContract<any[]>;
56
+ /**
57
+ * Persists token inside the database
58
+ */
59
+ createToken(token: Token): Promise<void>;
60
+ /**
61
+ * Finds a token by series inside the database and returns an
62
+ * instance of it.
63
+ *
64
+ * Returns null if the token is missing or expired
65
+ */
66
+ getTokenBySeries(series: string): Promise<Token | null>;
67
+ /**
68
+ * Removes a token from the database by the
69
+ * series number
70
+ */
71
+ deleteTokenBySeries(series: string): Promise<void>;
72
+ /**
73
+ * Updates token hash and expiry
74
+ */
75
+ updateTokenBySeries(series: string, hash: string, expiresAt: Date): Promise<void>;
76
+ }
77
+ export {};