@adonisjs/auth 9.0.0-8 → 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.
Files changed (125) hide show
  1. package/build/chunk-3HZHOWKL.js +8 -0
  2. package/build/chunk-3HZHOWKL.js.map +1 -0
  3. package/build/chunk-52DVKHJR.js +246 -0
  4. package/build/chunk-52DVKHJR.js.map +1 -0
  5. package/build/chunk-BHB55PBJ.js +197 -0
  6. package/build/chunk-BHB55PBJ.js.map +1 -0
  7. package/build/chunk-CZCFTIBB.js +21 -0
  8. package/build/chunk-CZCFTIBB.js.map +1 -0
  9. package/build/index.d.ts +7 -6
  10. package/build/index.js +146 -15
  11. package/build/index.js.map +1 -0
  12. package/build/modules/access_tokens_guard/access_token.d.ts +137 -0
  13. package/build/modules/access_tokens_guard/crc32.d.ts +16 -0
  14. package/build/modules/access_tokens_guard/define_config.d.ts +17 -0
  15. package/build/modules/access_tokens_guard/guard.d.ts +81 -0
  16. package/build/modules/access_tokens_guard/main.d.ts +5 -0
  17. package/build/modules/access_tokens_guard/main.js +929 -0
  18. package/build/modules/access_tokens_guard/main.js.map +1 -0
  19. package/build/modules/access_tokens_guard/token_providers/db.d.ts +76 -0
  20. package/build/modules/access_tokens_guard/types.d.ts +208 -0
  21. package/build/modules/access_tokens_guard/types.js +1 -0
  22. package/build/modules/access_tokens_guard/types.js.map +1 -0
  23. package/build/modules/access_tokens_guard/user_providers/lucid.d.ts +53 -0
  24. package/build/modules/session_guard/define_config.d.ts +17 -0
  25. package/build/modules/session_guard/guard.d.ts +98 -0
  26. package/build/modules/session_guard/main.d.ts +5 -0
  27. package/build/modules/session_guard/main.js +741 -0
  28. package/build/modules/session_guard/main.js.map +1 -0
  29. package/build/modules/session_guard/remember_me_token.d.ts +88 -0
  30. package/build/modules/session_guard/token_providers/db.d.ts +69 -0
  31. package/build/modules/session_guard/types.d.ts +243 -0
  32. package/build/modules/session_guard/types.js +1 -0
  33. package/build/modules/session_guard/types.js.map +1 -0
  34. package/build/modules/session_guard/user_providers/lucid.d.ts +57 -0
  35. package/build/providers/auth_provider.d.ts +1 -1
  36. package/build/providers/auth_provider.js +31 -27
  37. package/build/providers/auth_provider.js.map +1 -0
  38. package/build/services/auth.d.ts +1 -1
  39. package/build/services/auth.js +10 -15
  40. package/build/services/auth.js.map +1 -0
  41. package/build/src/{auth/auth_manager.d.ts → auth_manager.d.ts} +8 -3
  42. package/build/src/authenticator.d.ts +81 -0
  43. package/build/src/{auth/authenticator_client.d.ts → authenticator_client.d.ts} +4 -4
  44. package/build/src/{auth/define_config.d.ts → define_config.d.ts} +0 -10
  45. package/build/src/errors.d.ts +105 -0
  46. package/build/src/{auth/middleware → middleware}/initialize_auth_middleware.d.ts +3 -2
  47. package/build/src/middleware/initialize_auth_middleware.js +17 -0
  48. package/build/src/middleware/initialize_auth_middleware.js.map +1 -0
  49. package/build/src/mixins/with_auth_finder.d.ts +156 -0
  50. package/build/src/{auth/plugins → plugins}/japa/api_client.d.ts +8 -8
  51. package/build/src/plugins/japa/api_client.js +59 -0
  52. package/build/src/plugins/japa/api_client.js.map +1 -0
  53. package/build/src/{auth/plugins → plugins}/japa/browser_client.d.ts +10 -7
  54. package/build/src/plugins/japa/browser_client.js +67 -0
  55. package/build/src/plugins/japa/browser_client.js.map +1 -0
  56. package/build/src/{auth/types.d.ts → types.d.ts} +31 -26
  57. package/build/src/types.js +1 -0
  58. package/build/src/types.js.map +1 -0
  59. package/package.json +90 -70
  60. package/build/configure.js +0 -47
  61. package/build/factories/basic_auth_guard_factory.d.ts +0 -12
  62. package/build/factories/basic_auth_guard_factory.js +0 -22
  63. package/build/factories/database_token_factory.d.ts +0 -36
  64. package/build/factories/database_token_factory.js +0 -54
  65. package/build/factories/database_user_provider.d.ts +0 -14
  66. package/build/factories/database_user_provider.js +0 -27
  67. package/build/factories/lucid_user_provider.d.ts +0 -28
  68. package/build/factories/lucid_user_provider.js +0 -68
  69. package/build/factories/main.d.ts +0 -4
  70. package/build/factories/main.js +0 -12
  71. package/build/factories/session_guard_factory.d.ts +0 -13
  72. package/build/factories/session_guard_factory.js +0 -24
  73. package/build/src/auth/auth_manager.js +0 -41
  74. package/build/src/auth/authenticator.d.ts +0 -63
  75. package/build/src/auth/authenticator.js +0 -129
  76. package/build/src/auth/authenticator_client.js +0 -59
  77. package/build/src/auth/debug.js +0 -10
  78. package/build/src/auth/define_config.js +0 -54
  79. package/build/src/auth/errors.d.ts +0 -90
  80. package/build/src/auth/errors.js +0 -201
  81. package/build/src/auth/middleware/initialize_auth_middleware.js +0 -25
  82. package/build/src/auth/plugins/japa/api_client.js +0 -63
  83. package/build/src/auth/plugins/japa/browser_client.js +0 -42
  84. package/build/src/auth/symbols.js +0 -17
  85. package/build/src/auth/types.js +0 -9
  86. package/build/src/auth/user_providers/main.d.ts +0 -15
  87. package/build/src/auth/user_providers/main.js +0 -22
  88. package/build/src/core/guard_user.d.ts +0 -26
  89. package/build/src/core/guard_user.js +0 -29
  90. package/build/src/core/token.d.ts +0 -89
  91. package/build/src/core/token.js +0 -114
  92. package/build/src/core/token_providers/database.d.ts +0 -77
  93. package/build/src/core/token_providers/database.js +0 -113
  94. package/build/src/core/types.d.ts +0 -178
  95. package/build/src/core/types.js +0 -9
  96. package/build/src/core/user_providers/database.d.ts +0 -78
  97. package/build/src/core/user_providers/database.js +0 -117
  98. package/build/src/core/user_providers/lucid.d.ts +0 -61
  99. package/build/src/core/user_providers/lucid.js +0 -122
  100. package/build/src/guards/basic_auth/define_config.d.ts +0 -16
  101. package/build/src/guards/basic_auth/define_config.js +0 -38
  102. package/build/src/guards/basic_auth/guard.d.ts +0 -70
  103. package/build/src/guards/basic_auth/guard.js +0 -190
  104. package/build/src/guards/basic_auth/main.d.ts +0 -2
  105. package/build/src/guards/basic_auth/main.js +0 -10
  106. package/build/src/guards/basic_auth/types.d.ts +0 -35
  107. package/build/src/guards/basic_auth/types.js +0 -9
  108. package/build/src/guards/session/define_config.d.ts +0 -23
  109. package/build/src/guards/session/define_config.js +0 -56
  110. package/build/src/guards/session/guard.d.ts +0 -123
  111. package/build/src/guards/session/guard.js +0 -510
  112. package/build/src/guards/session/main.d.ts +0 -3
  113. package/build/src/guards/session/main.js +0 -11
  114. package/build/src/guards/session/token.d.ts +0 -57
  115. package/build/src/guards/session/token.js +0 -58
  116. package/build/src/guards/session/token_providers/main.d.ts +0 -33
  117. package/build/src/guards/session/token_providers/main.js +0 -42
  118. package/build/src/guards/session/types.d.ts +0 -104
  119. package/build/src/guards/session/types.js +0 -9
  120. package/build/stubs/main.d.ts +0 -1
  121. package/build/stubs/main.js +0 -10
  122. package/build/stubs/middleware/auth_middleware.stub +0 -30
  123. package/build/stubs/middleware/guest_middleware.stub +0 -36
  124. /package/build/src/{auth/debug.d.ts → debug.d.ts} +0 -0
  125. /package/build/src/{auth/symbols.d.ts → symbols.d.ts} +0 -0
package/build/index.js CHANGED
@@ -1,15 +1,146 @@
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 { configure } from './configure.js';
10
- export { stubsRoot } from './stubs/main.js';
11
- export * as symbols from './src/auth/symbols.js';
12
- export { AuthManager } from './src/auth/auth_manager.js';
13
- export { Authenticator } from './src/auth/authenticator.js';
14
- export { defineConfig, providers } from './src/auth/define_config.js';
15
- export { AuthenticationException, InvalidCredentialsException } from './src/auth/errors.js';
1
+ import {
2
+ AuthManager,
3
+ Authenticator,
4
+ AuthenticatorClient
5
+ } from "./chunk-52DVKHJR.js";
6
+ import "./chunk-3HZHOWKL.js";
7
+ import {
8
+ E_INVALID_CREDENTIALS,
9
+ errors_exports
10
+ } from "./chunk-BHB55PBJ.js";
11
+ import {
12
+ __decorateClass,
13
+ __export
14
+ } from "./chunk-CZCFTIBB.js";
15
+
16
+ // configure.ts
17
+ import { presetAuth } from "@adonisjs/presets/auth";
18
+ async function configure(command) {
19
+ const codemods = await command.createCodemods();
20
+ let guard = command.parsedFlags.guard;
21
+ if (guard === void 0) {
22
+ guard = await command.prompt.choice(
23
+ "Select the auth guard you want to use",
24
+ [
25
+ {
26
+ name: "session",
27
+ message: "Session"
28
+ },
29
+ {
30
+ name: "access_tokens",
31
+ message: "Opaque access tokens"
32
+ }
33
+ ],
34
+ {
35
+ validate(value) {
36
+ return !!value;
37
+ }
38
+ }
39
+ );
40
+ }
41
+ if (!["session", "access_tokens"].includes(guard)) {
42
+ command.logger.error(
43
+ `The selected guard "${guard}" is invalid. Select one from: session, access_tokens`
44
+ );
45
+ command.exitCode = 1;
46
+ return;
47
+ }
48
+ await presetAuth(codemods, command.app, {
49
+ guard,
50
+ userProvider: "lucid"
51
+ });
52
+ }
53
+
54
+ // src/symbols.ts
55
+ var symbols_exports = {};
56
+ __export(symbols_exports, {
57
+ GUARD_KNOWN_EVENTS: () => GUARD_KNOWN_EVENTS,
58
+ PROVIDER_REAL_USER: () => PROVIDER_REAL_USER
59
+ });
60
+ var PROVIDER_REAL_USER = Symbol.for("PROVIDER_REAL_USER");
61
+ var GUARD_KNOWN_EVENTS = Symbol.for("GUARD_KNOWN_EVENTS");
62
+
63
+ // src/define_config.ts
64
+ import { configProvider } from "@adonisjs/core";
65
+ function defineConfig(config) {
66
+ return configProvider.create(async (app) => {
67
+ const guardsList = Object.keys(config.guards);
68
+ const guards = {};
69
+ for (let guardName of guardsList) {
70
+ const guard = config.guards[guardName];
71
+ if (typeof guard === "function") {
72
+ guards[guardName] = guard;
73
+ } else {
74
+ guards[guardName] = await guard.resolver(guardName, app);
75
+ }
76
+ }
77
+ return {
78
+ default: config.default,
79
+ guards
80
+ };
81
+ });
82
+ }
83
+
84
+ // src/mixins/with_auth_finder.ts
85
+ import { RuntimeException } from "@adonisjs/core/exceptions";
86
+ import { beforeSave } from "@adonisjs/lucid/orm";
87
+ function withAuthFinder(hash, options) {
88
+ return (superclass) => {
89
+ class UserWithUserFinder extends superclass {
90
+ static async hashPassword(user) {
91
+ if (user.$dirty[options.passwordColumnName]) {
92
+ ;
93
+ user[options.passwordColumnName] = await hash.make(
94
+ user[options.passwordColumnName]
95
+ );
96
+ }
97
+ }
98
+ /**
99
+ * Finds the user for authentication via "verifyCredentials".
100
+ * Feel free to override this method customize the user
101
+ * lookup behavior.
102
+ */
103
+ static findForAuth(uids, value) {
104
+ const query = this.query();
105
+ uids.forEach((uid) => query.orWhere(uid, value));
106
+ return query.limit(1).first();
107
+ }
108
+ /**
109
+ * Find a user by uid and verify their password. This method is
110
+ * safe from timing attacks.
111
+ */
112
+ static async verifyCredentials(uid, password) {
113
+ const user = await this.findForAuth(options.uids, uid);
114
+ if (!user) {
115
+ await hash.make(password);
116
+ throw new E_INVALID_CREDENTIALS("Invalid user credentials");
117
+ }
118
+ const passwordHash = user[options.passwordColumnName];
119
+ if (!passwordHash) {
120
+ throw new RuntimeException(
121
+ `Cannot verify password during login. The value of column "${options.passwordColumnName}" is undefined or null`
122
+ );
123
+ }
124
+ if (await hash.verify(passwordHash, password)) {
125
+ return user;
126
+ }
127
+ throw new E_INVALID_CREDENTIALS("Invalid user credentials");
128
+ }
129
+ }
130
+ __decorateClass([
131
+ beforeSave()
132
+ ], UserWithUserFinder, "hashPassword", 1);
133
+ return UserWithUserFinder;
134
+ };
135
+ }
136
+ export {
137
+ AuthManager,
138
+ Authenticator,
139
+ AuthenticatorClient,
140
+ configure,
141
+ defineConfig,
142
+ errors_exports as errors,
143
+ symbols_exports as symbols,
144
+ withAuthFinder
145
+ };
146
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../configure.ts","../src/symbols.ts","../src/define_config.ts","../src/mixins/with_auth_finder.ts"],"sourcesContent":["/*\n * @adonisjs/auth\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport { presetAuth } from '@adonisjs/presets/auth'\nimport type Configure from '@adonisjs/core/commands/configure'\n\n/**\n * Configures the auth package\n */\nexport async function configure(command: Configure) {\n const codemods = await command.createCodemods()\n let guard: string | undefined = command.parsedFlags.guard\n\n /**\n * Prompts user to select a guard when not mentioned via\n * the CLI\n */\n if (guard === undefined) {\n guard = await command.prompt.choice(\n 'Select the auth guard you want to use',\n [\n {\n name: 'session',\n message: 'Session',\n },\n {\n name: 'access_tokens',\n message: 'Opaque access tokens',\n },\n ],\n {\n validate(value) {\n return !!value\n },\n }\n )\n }\n\n /**\n * Ensure selected or guard defined via the CLI flag is\n * valid\n */\n if (!['session', 'access_tokens'].includes(guard!)) {\n command.logger.error(\n `The selected guard \"${guard}\" is invalid. Select one from: session, access_tokens`\n )\n command.exitCode = 1\n return\n }\n\n await presetAuth(codemods, command.app, {\n guard: guard as 'session' | 'access_tokens',\n userProvider: 'lucid',\n })\n}\n","/*\n * @adonisjs/lucid\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\n/**\n * A symbol to identify the type of the real user for a given\n * user provider\n */\nexport const PROVIDER_REAL_USER = Symbol.for('PROVIDER_REAL_USER')\n\n/**\n * A symbol to identify the type for the events emitted by a guard\n */\nexport const GUARD_KNOWN_EVENTS = Symbol.for('GUARD_KNOWN_EVENTS')\n","/*\n * @adonisjs/auth\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport { configProvider } from '@adonisjs/core'\nimport type { ConfigProvider } from '@adonisjs/core/types'\nimport type { GuardConfigProvider, GuardFactory } from './types.js'\n\n/**\n * Config resolved by the \"defineConfig\" method\n */\nexport type ResolvedAuthConfig<\n KnownGuards extends Record<string, GuardFactory | GuardConfigProvider<GuardFactory>>,\n> = {\n default: keyof KnownGuards\n guards: {\n [K in keyof KnownGuards]: KnownGuards[K] extends GuardConfigProvider<infer A>\n ? A\n : KnownGuards[K]\n }\n}\n\n/**\n * Define configuration for the auth package. The function returns\n * a config provider that is invoked inside the auth service\n * provider\n */\nexport function defineConfig<\n KnownGuards extends Record<string, GuardFactory | GuardConfigProvider<GuardFactory>>,\n>(config: {\n default: keyof KnownGuards\n guards: KnownGuards\n}): ConfigProvider<ResolvedAuthConfig<KnownGuards>> {\n return configProvider.create(async (app) => {\n const guardsList = Object.keys(config.guards)\n const guards = {} as Record<string, GuardFactory>\n\n for (let guardName of guardsList) {\n const guard = config.guards[guardName]\n if (typeof guard === 'function') {\n guards[guardName] = guard\n } else {\n guards[guardName] = await guard.resolver(guardName, app)\n }\n }\n\n return {\n default: config.default,\n guards: guards,\n } as ResolvedAuthConfig<KnownGuards>\n })\n}\n","/*\n * @adonisjs/auth\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport type { Hash } from '@adonisjs/core/hash'\nimport { RuntimeException } from '@adonisjs/core/exceptions'\nimport { beforeSave, type BaseModel } from '@adonisjs/lucid/orm'\nimport type { NormalizeConstructor } from '@adonisjs/core/types/helpers'\nimport { E_INVALID_CREDENTIALS } from '../errors.js'\n\n/**\n * Mixing to add user lookup and password verification methods\n * on a model.\n *\n * Under the hood, this mixin defines following methods and hooks\n *\n * - beforeSave hook to hash user password\n * - findForAuth method to find a user during authentication\n * - verifyCredentials method to verify user credentials and prevent\n * timing attacks.\n */\nexport function withAuthFinder(\n hash: Hash,\n options: {\n uids: string[]\n passwordColumnName: string\n }\n) {\n return <Model extends NormalizeConstructor<typeof BaseModel>>(superclass: Model) => {\n class UserWithUserFinder extends superclass {\n /**\n * Hook to verify user password when creating or updating\n * the user model.\n */\n @beforeSave()\n static async hashPassword<T extends typeof UserWithUserFinder>(\n this: T,\n user: InstanceType<T>\n ) {\n if (user.$dirty[options.passwordColumnName]) {\n ;(user as any)[options.passwordColumnName] = await hash.make(\n (user as any)[options.passwordColumnName]\n )\n }\n }\n\n /**\n * Finds the user for authentication via \"verifyCredentials\".\n * Feel free to override this method customize the user\n * lookup behavior.\n */\n static findForAuth<T extends typeof UserWithUserFinder>(\n this: T,\n uids: string[],\n value: string\n ): Promise<InstanceType<T> | null> {\n const query = this.query()\n uids.forEach((uid) => query.orWhere(uid, value))\n return query.limit(1).first()\n }\n\n /**\n * Find a user by uid and verify their password. This method is\n * safe from timing attacks.\n */\n static async verifyCredentials<T extends typeof UserWithUserFinder>(\n this: T,\n uid: string,\n password: string\n ) {\n const user = await this.findForAuth(options.uids, uid)\n if (!user) {\n await hash.make(password)\n throw new E_INVALID_CREDENTIALS('Invalid user credentials')\n }\n\n const passwordHash = (user as any)[options.passwordColumnName]\n if (!passwordHash) {\n throw new RuntimeException(\n `Cannot verify password during login. The value of column \"${options.passwordColumnName}\" is undefined or null`\n )\n }\n\n if (await hash.verify(passwordHash, password)) {\n return user\n }\n\n throw new E_INVALID_CREDENTIALS('Invalid user credentials')\n }\n }\n\n return UserWithUserFinder\n }\n}\n"],"mappings":"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","names":[]}
@@ -0,0 +1,137 @@
1
+ import { Secret } from '@adonisjs/core/helpers';
2
+ /**
3
+ * Access token represents a token created for a user to authenticate
4
+ * using the auth module.
5
+ *
6
+ * It encapsulates the logic of creating an opaque token, generating
7
+ * its hash and verifying its hash.
8
+ */
9
+ export declare class AccessToken {
10
+ /**
11
+ * Decodes a publicly shared token and return the series
12
+ * and the token value from it.
13
+ *
14
+ * Returns null when unable to decode the token because of
15
+ * invalid format or encoding.
16
+ */
17
+ static decode(prefix: string, value: string): null | {
18
+ identifier: string;
19
+ secret: Secret<string>;
20
+ };
21
+ /**
22
+ * Creates a transient token that can be shared with the persistence
23
+ * layer.
24
+ */
25
+ static createTransientToken(userId: string | number | BigInt, size: number, expiresIn?: string | number): {
26
+ secret: Secret<string>;
27
+ hash: string;
28
+ userId: string | number | BigInt;
29
+ expiresAt: Date | undefined;
30
+ };
31
+ /**
32
+ * Creates a secret opaque token and its hash. The secret is
33
+ * suffixed with a crc32 checksum for secret scanning tools
34
+ * to easily identify the token.
35
+ */
36
+ static seed(size: number): {
37
+ secret: Secret<string>;
38
+ hash: string;
39
+ };
40
+ /**
41
+ * Identifer is a unique sequence to identify the
42
+ * token within database. It should be the
43
+ * primary/unique key
44
+ */
45
+ identifier: string | number | BigInt;
46
+ /**
47
+ * Reference to the user id for whom the token
48
+ * is generated.
49
+ */
50
+ tokenableId: string | number | BigInt;
51
+ /**
52
+ * The value is a public representation of a token. It is created
53
+ * by combining the "identifier"."secret"
54
+ */
55
+ value?: Secret<string>;
56
+ /**
57
+ * Recognizable name for the token
58
+ */
59
+ name: string | null;
60
+ /**
61
+ * A unique type to identify a bucket of tokens inside the
62
+ * storage layer.
63
+ */
64
+ type: string;
65
+ /**
66
+ * Hash is computed from the seed to later verify the validity
67
+ * of seed
68
+ */
69
+ hash: string;
70
+ /**
71
+ * Date/time when the token instance was created
72
+ */
73
+ createdAt: Date;
74
+ /**
75
+ * Date/time when the token was updated
76
+ */
77
+ updatedAt: Date;
78
+ /**
79
+ * Timestamp at which the token was used for authentication
80
+ */
81
+ lastUsedAt: Date | null;
82
+ /**
83
+ * Timestamp at which the token will expire
84
+ */
85
+ expiresAt: Date | null;
86
+ /**
87
+ * An array of abilities the token can perform. The abilities
88
+ * is an array of abritary string values
89
+ */
90
+ abilities: string[];
91
+ constructor(attributes: {
92
+ identifier: string | number | BigInt;
93
+ tokenableId: string | number | BigInt;
94
+ type: string;
95
+ hash: string;
96
+ createdAt: Date;
97
+ updatedAt: Date;
98
+ lastUsedAt: Date | null;
99
+ expiresAt: Date | null;
100
+ name: string | null;
101
+ prefix?: string;
102
+ secret?: Secret<string>;
103
+ abilities?: string[];
104
+ });
105
+ /**
106
+ * Check if the token allows the given ability.
107
+ */
108
+ allows(ability: string): boolean;
109
+ /**
110
+ * Check if the token denies the ability.
111
+ */
112
+ denies(ability: string): boolean;
113
+ /**
114
+ * Authorize ability access using the current access token
115
+ */
116
+ authorize(ability: string): void;
117
+ /**
118
+ * Check if the token has been expired. Verifies
119
+ * the "expiresAt" timestamp with the current
120
+ * date.
121
+ *
122
+ * Tokens with no expiry never expire
123
+ */
124
+ isExpired(): boolean;
125
+ /**
126
+ * Verifies the value of a token against the pre-defined hash
127
+ */
128
+ verify(secret: Secret<string>): boolean;
129
+ toJSON(): {
130
+ type: string;
131
+ name: string | null;
132
+ token: string | undefined;
133
+ abilities: string[];
134
+ lastUsedAt: Date | null;
135
+ expiresAt: Date | null;
136
+ };
137
+ }
@@ -0,0 +1,16 @@
1
+ /**
2
+ * We use CRC32 just to add a recognizable checksum to tokens. This helps
3
+ * secret scanning tools like https://docs.github.com/en/github/administering-a-repository/about-secret-scanning easily detect tokens generated by a given program.
4
+ *
5
+ * You can learn more about appending checksum to a hash here in this Github
6
+ * article. https://github.blog/2021-04-05-behind-githubs-new-authentication-token-formats/
7
+ *
8
+ * Code taken from:
9
+ * https://github.com/tsxper/crc32/blob/main/src/CRC32.ts
10
+ */
11
+ export declare class CRC32 {
12
+ #private;
13
+ calculate(input: string): number;
14
+ forString(input: string): number;
15
+ forBytes(bytes: Uint8Array, accumulator?: number): number;
16
+ }
@@ -0,0 +1,17 @@
1
+ import type { HttpContext } from '@adonisjs/core/http';
2
+ import type { ConfigProvider } from '@adonisjs/core/types';
3
+ import { AccessTokensGuard } from './guard.js';
4
+ import type { GuardConfigProvider } from '../../src/types.js';
5
+ import { AccessTokensLucidUserProvider } from './user_providers/lucid.js';
6
+ import type { LucidTokenable, AccessTokensUserProviderContract, AccessTokensLucidUserProviderOptions } from './types.js';
7
+ /**
8
+ * Configures access tokens guard for authentication
9
+ */
10
+ export declare function tokensGuard<UserProvider extends AccessTokensUserProviderContract<unknown>>(config: {
11
+ provider: UserProvider | ConfigProvider<UserProvider>;
12
+ }): GuardConfigProvider<(ctx: HttpContext) => AccessTokensGuard<UserProvider>>;
13
+ /**
14
+ * Configures user provider that uses Lucid models to verify
15
+ * access tokens and find users during authentication.
16
+ */
17
+ export declare function tokensUserProvider<TokenableProperty extends string, Model extends LucidTokenable<TokenableProperty>>(config: AccessTokensLucidUserProviderOptions<TokenableProperty, Model>): AccessTokensLucidUserProvider<TokenableProperty, Model>;
@@ -0,0 +1,81 @@
1
+ import type { HttpContext } from '@adonisjs/core/http';
2
+ import type { EmitterLike } from '@adonisjs/core/types/events';
3
+ import type { AccessToken } from './access_token.js';
4
+ import type { AuthClientResponse, GuardContract } from '../../src/types.js';
5
+ import { GUARD_KNOWN_EVENTS, PROVIDER_REAL_USER } from '../../src/symbols.js';
6
+ import type { AccessTokensGuardEvents, AccessTokensUserProviderContract } from './types.js';
7
+ /**
8
+ * Implementation of access tokens guard for the Auth layer. The heavy lifting
9
+ * of verifying tokens is done by the user provider. However, the guard is
10
+ * used to seamlessly integrate with the auth layer of the package.
11
+ */
12
+ export declare class AccessTokensGuard<UserProvider extends AccessTokensUserProviderContract<unknown>> implements GuardContract<UserProvider[typeof PROVIDER_REAL_USER] & {
13
+ currentAccessToken: AccessToken;
14
+ }> {
15
+ #private;
16
+ /**
17
+ * Events emitted by the guard
18
+ */
19
+ [GUARD_KNOWN_EVENTS]: AccessTokensGuardEvents<UserProvider[typeof PROVIDER_REAL_USER] & {
20
+ currentAccessToken: AccessToken;
21
+ }>;
22
+ /**
23
+ * Driver name of the guard
24
+ */
25
+ driverName: 'access_tokens';
26
+ /**
27
+ * Whether or not the authentication has been attempted
28
+ * during the current request.
29
+ */
30
+ authenticationAttempted: boolean;
31
+ /**
32
+ * A boolean to know if the current request has
33
+ * been authenticated
34
+ */
35
+ isAuthenticated: boolean;
36
+ /**
37
+ * Reference to an instance of the authenticated user.
38
+ * The value only exists after calling one of the
39
+ * following methods.
40
+ *
41
+ * - authenticate
42
+ * - check
43
+ *
44
+ * You can use the "getUserOrFail" method to throw an exception if
45
+ * the request is not authenticated.
46
+ */
47
+ user?: UserProvider[typeof PROVIDER_REAL_USER] & {
48
+ currentAccessToken: AccessToken;
49
+ };
50
+ constructor(name: string, ctx: HttpContext, emitter: EmitterLike<AccessTokensGuardEvents<UserProvider[typeof PROVIDER_REAL_USER] & {
51
+ currentAccessToken: AccessToken;
52
+ }>>, userProvider: UserProvider);
53
+ /**
54
+ * Returns an instance of the authenticated user. Or throws
55
+ * an exception if the request is not authenticated.
56
+ */
57
+ getUserOrFail(): UserProvider[typeof PROVIDER_REAL_USER] & {
58
+ currentAccessToken: AccessToken;
59
+ };
60
+ /**
61
+ * Authenticate the current HTTP request by verifying the bearer
62
+ * token or fails with an exception
63
+ */
64
+ authenticate(): Promise<UserProvider[typeof PROVIDER_REAL_USER] & {
65
+ currentAccessToken: AccessToken;
66
+ }>;
67
+ /**
68
+ * Returns the Authorization header clients can use to authenticate
69
+ * the request.
70
+ */
71
+ authenticateAsClient(user: UserProvider[typeof PROVIDER_REAL_USER], abilities?: string[], options?: {
72
+ expiresIn?: string | number;
73
+ name?: string;
74
+ }): Promise<AuthClientResponse>;
75
+ /**
76
+ * Silently check if the user is authenticated or not. The
77
+ * method is same the "authenticate" method but does not
78
+ * throw any exceptions.
79
+ */
80
+ check(): Promise<boolean>;
81
+ }
@@ -0,0 +1,5 @@
1
+ export { AccessToken } from './access_token.js';
2
+ export { AccessTokensGuard } from './guard.js';
3
+ export { DbAccessTokensProvider } from './token_providers/db.js';
4
+ export { tokensGuard, tokensUserProvider } from './define_config.js';
5
+ export { AccessTokensLucidUserProvider } from './user_providers/lucid.js';