@adonisjs/auth 9.0.0-9 → 9.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/build/chunk-3HZHOWKL.js +8 -0
- package/build/chunk-3HZHOWKL.js.map +1 -0
- package/build/chunk-CZCFTIBB.js +21 -0
- package/build/chunk-CZCFTIBB.js.map +1 -0
- package/build/chunk-OL2Z3AO5.js +246 -0
- package/build/chunk-OL2Z3AO5.js.map +1 -0
- package/build/chunk-UGHJLKDI.js +197 -0
- package/build/chunk-UGHJLKDI.js.map +1 -0
- package/build/index.d.ts +7 -6
- package/build/index.js +149 -15
- package/build/index.js.map +1 -0
- package/build/modules/access_tokens_guard/access_token.d.ts +137 -0
- package/build/modules/access_tokens_guard/crc32.d.ts +16 -0
- package/build/modules/access_tokens_guard/define_config.d.ts +17 -0
- package/build/modules/access_tokens_guard/guard.d.ts +81 -0
- package/build/modules/access_tokens_guard/main.d.ts +5 -0
- package/build/modules/access_tokens_guard/main.js +929 -0
- package/build/modules/access_tokens_guard/main.js.map +1 -0
- package/build/modules/access_tokens_guard/token_providers/db.d.ts +76 -0
- package/build/modules/access_tokens_guard/types.d.ts +208 -0
- package/build/modules/access_tokens_guard/types.js +1 -0
- package/build/modules/access_tokens_guard/types.js.map +1 -0
- package/build/modules/access_tokens_guard/user_providers/lucid.d.ts +53 -0
- package/build/modules/session_guard/define_config.d.ts +17 -0
- package/build/modules/session_guard/guard.d.ts +98 -0
- package/build/modules/session_guard/main.d.ts +5 -0
- package/build/modules/session_guard/main.js +741 -0
- package/build/modules/session_guard/main.js.map +1 -0
- package/build/modules/session_guard/remember_me_token.d.ts +88 -0
- package/build/modules/session_guard/token_providers/db.d.ts +69 -0
- package/build/modules/session_guard/types.d.ts +243 -0
- package/build/modules/session_guard/types.js +1 -0
- package/build/modules/session_guard/types.js.map +1 -0
- package/build/modules/session_guard/user_providers/lucid.d.ts +57 -0
- package/build/providers/auth_provider.d.ts +1 -1
- package/build/providers/auth_provider.js +31 -27
- package/build/providers/auth_provider.js.map +1 -0
- package/build/services/auth.d.ts +1 -1
- package/build/services/auth.js +10 -15
- package/build/services/auth.js.map +1 -0
- package/build/src/{auth/auth_manager.d.ts → auth_manager.d.ts} +8 -3
- package/build/src/authenticator.d.ts +81 -0
- package/build/src/{auth/authenticator_client.d.ts → authenticator_client.d.ts} +4 -4
- package/build/src/{auth/define_config.d.ts → define_config.d.ts} +0 -10
- package/build/src/errors.d.ts +105 -0
- package/build/src/{auth/middleware → middleware}/initialize_auth_middleware.d.ts +3 -2
- package/build/src/middleware/initialize_auth_middleware.js +17 -0
- package/build/src/middleware/initialize_auth_middleware.js.map +1 -0
- package/build/src/mixins/with_auth_finder.d.ts +156 -0
- package/build/src/{auth/plugins → plugins}/japa/api_client.d.ts +8 -8
- package/build/src/plugins/japa/api_client.js +59 -0
- package/build/src/plugins/japa/api_client.js.map +1 -0
- package/build/src/{auth/plugins → plugins}/japa/browser_client.d.ts +10 -7
- package/build/src/plugins/japa/browser_client.js +67 -0
- package/build/src/plugins/japa/browser_client.js.map +1 -0
- package/build/src/{auth/types.d.ts → types.d.ts} +31 -26
- package/build/src/types.js +1 -0
- package/build/src/types.js.map +1 -0
- package/package.json +88 -68
- package/build/configure.js +0 -47
- package/build/factories/basic_auth_guard_factory.d.ts +0 -12
- package/build/factories/basic_auth_guard_factory.js +0 -22
- package/build/factories/database_token_factory.d.ts +0 -36
- package/build/factories/database_token_factory.js +0 -54
- package/build/factories/database_user_provider.d.ts +0 -14
- package/build/factories/database_user_provider.js +0 -27
- package/build/factories/lucid_user_provider.d.ts +0 -28
- package/build/factories/lucid_user_provider.js +0 -68
- package/build/factories/main.d.ts +0 -4
- package/build/factories/main.js +0 -12
- package/build/factories/session_guard_factory.d.ts +0 -13
- package/build/factories/session_guard_factory.js +0 -24
- package/build/src/auth/auth_manager.js +0 -41
- package/build/src/auth/authenticator.d.ts +0 -63
- package/build/src/auth/authenticator.js +0 -129
- package/build/src/auth/authenticator_client.js +0 -59
- package/build/src/auth/debug.js +0 -10
- package/build/src/auth/define_config.js +0 -54
- package/build/src/auth/errors.d.ts +0 -90
- package/build/src/auth/errors.js +0 -201
- package/build/src/auth/middleware/initialize_auth_middleware.js +0 -25
- package/build/src/auth/plugins/japa/api_client.js +0 -63
- package/build/src/auth/plugins/japa/browser_client.js +0 -64
- package/build/src/auth/symbols.js +0 -17
- package/build/src/auth/types.js +0 -9
- package/build/src/auth/user_providers/main.d.ts +0 -15
- package/build/src/auth/user_providers/main.js +0 -22
- package/build/src/core/guard_user.d.ts +0 -26
- package/build/src/core/guard_user.js +0 -29
- package/build/src/core/token.d.ts +0 -89
- package/build/src/core/token.js +0 -114
- package/build/src/core/token_providers/database.d.ts +0 -77
- package/build/src/core/token_providers/database.js +0 -113
- package/build/src/core/types.d.ts +0 -178
- package/build/src/core/types.js +0 -9
- package/build/src/core/user_providers/database.d.ts +0 -78
- package/build/src/core/user_providers/database.js +0 -117
- package/build/src/core/user_providers/lucid.d.ts +0 -61
- package/build/src/core/user_providers/lucid.js +0 -122
- package/build/src/guards/basic_auth/define_config.d.ts +0 -16
- package/build/src/guards/basic_auth/define_config.js +0 -38
- package/build/src/guards/basic_auth/guard.d.ts +0 -70
- package/build/src/guards/basic_auth/guard.js +0 -190
- package/build/src/guards/basic_auth/main.d.ts +0 -2
- package/build/src/guards/basic_auth/main.js +0 -10
- package/build/src/guards/basic_auth/types.d.ts +0 -35
- package/build/src/guards/basic_auth/types.js +0 -9
- package/build/src/guards/session/define_config.d.ts +0 -23
- package/build/src/guards/session/define_config.js +0 -56
- package/build/src/guards/session/guard.d.ts +0 -123
- package/build/src/guards/session/guard.js +0 -510
- package/build/src/guards/session/main.d.ts +0 -3
- package/build/src/guards/session/main.js +0 -11
- package/build/src/guards/session/token.d.ts +0 -57
- package/build/src/guards/session/token.js +0 -58
- package/build/src/guards/session/token_providers/main.d.ts +0 -33
- package/build/src/guards/session/token_providers/main.js +0 -42
- package/build/src/guards/session/types.d.ts +0 -104
- package/build/src/guards/session/types.js +0 -9
- package/build/stubs/main.d.ts +0 -1
- package/build/stubs/main.js +0 -10
- package/build/stubs/middleware/auth_middleware.stub +0 -30
- package/build/stubs/middleware/guest_middleware.stub +0 -36
- /package/build/src/{auth/debug.d.ts → debug.d.ts} +0 -0
- /package/build/src/{auth/symbols.d.ts → symbols.d.ts} +0 -0
package/build/index.js
CHANGED
|
@@ -1,15 +1,149 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
1
|
+
import {
|
|
2
|
+
AuthManager,
|
|
3
|
+
Authenticator,
|
|
4
|
+
AuthenticatorClient
|
|
5
|
+
} from "./chunk-OL2Z3AO5.js";
|
|
6
|
+
import "./chunk-3HZHOWKL.js";
|
|
7
|
+
import {
|
|
8
|
+
E_INVALID_CREDENTIALS,
|
|
9
|
+
errors_exports
|
|
10
|
+
} from "./chunk-UGHJLKDI.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
|
+
if (!uid || !password) {
|
|
114
|
+
throw new E_INVALID_CREDENTIALS("Invalid user credentials");
|
|
115
|
+
}
|
|
116
|
+
const user = await this.findForAuth(options.uids, uid);
|
|
117
|
+
if (!user) {
|
|
118
|
+
await hash.make(password);
|
|
119
|
+
throw new E_INVALID_CREDENTIALS("Invalid user credentials");
|
|
120
|
+
}
|
|
121
|
+
const passwordHash = user[options.passwordColumnName];
|
|
122
|
+
if (!passwordHash) {
|
|
123
|
+
throw new RuntimeException(
|
|
124
|
+
`Cannot verify password during login. The value of column "${options.passwordColumnName}" is undefined or null`
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
if (await hash.verify(passwordHash, password)) {
|
|
128
|
+
return user;
|
|
129
|
+
}
|
|
130
|
+
throw new E_INVALID_CREDENTIALS("Invalid user credentials");
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
__decorateClass([
|
|
134
|
+
beforeSave()
|
|
135
|
+
], UserWithUserFinder, "hashPassword", 1);
|
|
136
|
+
return UserWithUserFinder;
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
export {
|
|
140
|
+
AuthManager,
|
|
141
|
+
Authenticator,
|
|
142
|
+
AuthenticatorClient,
|
|
143
|
+
configure,
|
|
144
|
+
defineConfig,
|
|
145
|
+
errors_exports as errors,
|
|
146
|
+
symbols_exports as symbols,
|
|
147
|
+
withAuthFinder
|
|
148
|
+
};
|
|
149
|
+
//# 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 /**\n * Fail when uid or the password are missing\n */\n if (!uid || !password) {\n throw new E_INVALID_CREDENTIALS('Invalid user credentials')\n }\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)) {\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F;AA7De;AAAA,MADZ,WAAW;AAAA,OALR,oBAMS;AA+Df,WAAO;AAAA,EACT;AACF;","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';
|