@adonisjs/auth 8.2.3 → 9.0.0-1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +1 -1
- package/README.md +19 -40
- package/build/configure.d.ts +5 -0
- package/build/configure.js +55 -0
- package/build/factories/database_token_factory.d.ts +36 -0
- package/build/factories/database_token_factory.js +54 -0
- package/build/factories/database_user_provider.d.ts +14 -0
- package/build/factories/database_user_provider.js +27 -0
- package/build/factories/lucid_user_provider.d.ts +28 -0
- package/build/factories/lucid_user_provider.js +68 -0
- package/build/factories/main.d.ts +4 -0
- package/build/factories/main.js +12 -0
- package/build/factories/session_guard_factory.d.ts +13 -0
- package/build/factories/session_guard_factory.js +24 -0
- package/build/index.d.ts +7 -0
- package/build/index.js +15 -0
- package/build/providers/auth_provider.d.ts +12 -0
- package/build/providers/auth_provider.js +27 -0
- package/build/services/auth.d.ts +3 -0
- package/build/services/auth.js +17 -0
- package/build/src/auth/auth_manager.d.ts +22 -0
- package/build/src/auth/auth_manager.js +34 -0
- package/build/src/auth/authenticator.d.ts +56 -0
- package/build/src/auth/authenticator.js +122 -0
- package/build/src/auth/debug.d.ts +3 -0
- package/build/{adonis-typings/container.js → src/auth/debug.js} +3 -1
- package/build/src/auth/define_config.d.ts +30 -0
- package/build/src/auth/define_config.js +54 -0
- package/build/src/auth/errors.d.ts +82 -0
- package/build/src/auth/errors.js +181 -0
- package/build/src/auth/middleware/auth_middleware.d.ts +13 -0
- package/build/src/auth/middleware/auth_middleware.js +6 -0
- package/build/src/auth/middleware/initialize_auth_middleware.d.ts +18 -0
- package/build/src/auth/middleware/initialize_auth_middleware.js +25 -0
- package/build/src/auth/symbols.d.ts +9 -0
- package/build/src/auth/symbols.js +17 -0
- package/build/src/auth/types.d.ts +75 -0
- package/build/{adonis-typings/context.js → src/auth/types.js} +2 -1
- package/build/src/auth/user_providers/main.d.ts +15 -0
- package/build/src/auth/user_providers/main.js +22 -0
- package/build/src/core/guard_user.d.ts +26 -0
- package/build/src/core/guard_user.js +29 -0
- package/build/src/core/token.d.ts +89 -0
- package/build/src/core/token.js +114 -0
- package/build/src/core/token_providers/database.d.ts +77 -0
- package/build/src/core/token_providers/database.js +113 -0
- package/build/src/core/types.d.ts +178 -0
- package/build/{adonis-typings/auth.js → src/core/types.js} +2 -1
- package/build/src/core/user_providers/database.d.ts +78 -0
- package/build/src/core/user_providers/database.js +117 -0
- package/build/src/core/user_providers/lucid.d.ts +61 -0
- package/build/src/core/user_providers/lucid.js +122 -0
- package/build/src/guards/session/define_config.d.ts +23 -0
- package/build/src/guards/session/define_config.js +56 -0
- package/build/src/guards/session/guard.d.ts +112 -0
- package/build/src/guards/session/guard.js +454 -0
- package/build/src/guards/session/main.d.ts +3 -0
- package/build/src/guards/session/main.js +11 -0
- package/build/src/guards/session/token.d.ts +57 -0
- package/build/src/guards/session/token.js +58 -0
- package/build/src/guards/session/token_providers/main.d.ts +33 -0
- package/build/src/guards/session/token_providers/main.js +42 -0
- package/build/src/guards/session/types.d.ts +96 -0
- package/build/{adonis-typings/events.js → src/guards/session/types.js} +2 -1
- package/build/stubs/config.stub +35 -0
- package/build/stubs/main.d.ts +1 -0
- package/build/{adonis-typings/tests.js → stubs/main.js} +2 -3
- package/package.json +99 -91
- package/build/adonis-typings/auth.d.ts +0 -635
- package/build/adonis-typings/container.d.ts +0 -6
- package/build/adonis-typings/context.d.ts +0 -6
- package/build/adonis-typings/events.d.ts +0 -10
- package/build/adonis-typings/index.d.ts +0 -5
- package/build/adonis-typings/index.js +0 -13
- package/build/adonis-typings/tests.d.ts +0 -23
- package/build/instructions.js +0 -338
- package/build/providers/AuthProvider.d.ts +0 -30
- package/build/providers/AuthProvider.js +0 -69
- package/build/src/Auth/index.d.ts +0 -97
- package/build/src/Auth/index.js +0 -155
- package/build/src/AuthManager/index.d.ts +0 -117
- package/build/src/AuthManager/index.js +0 -262
- package/build/src/Bindings/Tests.d.ts +0 -6
- package/build/src/Bindings/Tests.js +0 -69
- package/build/src/Clients/Oat/index.d.ts +0 -50
- package/build/src/Clients/Oat/index.js +0 -123
- package/build/src/Clients/Session/index.d.ts +0 -34
- package/build/src/Clients/Session/index.js +0 -72
- package/build/src/Exceptions/AuthenticationException.d.ts +0 -47
- package/build/src/Exceptions/AuthenticationException.js +0 -142
- package/build/src/Exceptions/InvalidCredentialsException.d.ts +0 -34
- package/build/src/Exceptions/InvalidCredentialsException.js +0 -112
- package/build/src/Guards/Base/index.d.ts +0 -75
- package/build/src/Guards/Base/index.js +0 -138
- package/build/src/Guards/BasicAuth/index.d.ts +0 -67
- package/build/src/Guards/BasicAuth/index.js +0 -181
- package/build/src/Guards/Oat/index.d.ts +0 -149
- package/build/src/Guards/Oat/index.js +0 -347
- package/build/src/Guards/Session/index.d.ts +0 -127
- package/build/src/Guards/Session/index.js +0 -338
- package/build/src/TokenProviders/Database/index.d.ts +0 -43
- package/build/src/TokenProviders/Database/index.js +0 -126
- package/build/src/TokenProviders/Redis/index.d.ts +0 -44
- package/build/src/TokenProviders/Redis/index.js +0 -129
- package/build/src/Tokens/OpaqueToken/index.d.ts +0 -46
- package/build/src/Tokens/OpaqueToken/index.js +0 -43
- package/build/src/Tokens/ProviderToken/index.d.ts +0 -23
- package/build/src/Tokens/ProviderToken/index.js +0 -27
- package/build/src/UserProviders/Database/User.d.ts +0 -28
- package/build/src/UserProviders/Database/User.js +0 -74
- package/build/src/UserProviders/Database/index.d.ts +0 -75
- package/build/src/UserProviders/Database/index.js +0 -141
- package/build/src/UserProviders/Lucid/User.d.ts +0 -28
- package/build/src/UserProviders/Lucid/User.js +0 -74
- package/build/src/UserProviders/Lucid/index.d.ts +0 -72
- package/build/src/UserProviders/Lucid/index.js +0 -146
- package/build/standalone.d.ts +0 -1
- package/build/standalone.js +0 -13
- package/build/templates/config/auth.txt +0 -34
- package/build/templates/config/partials/api-guard.txt +0 -22
- package/build/templates/config/partials/basic-guard.txt +0 -19
- package/build/templates/config/partials/tokens-provider-database.txt +0 -19
- package/build/templates/config/partials/tokens-provider-redis.txt +0 -22
- package/build/templates/config/partials/user-provider-database.txt +0 -43
- package/build/templates/config/partials/user-provider-lucid.txt +0 -45
- package/build/templates/config/partials/web-guard.txt +0 -17
- package/build/templates/contract/auth.txt +0 -55
- package/build/templates/contract/partials/api-guard.txt +0 -14
- package/build/templates/contract/partials/basic-guard.txt +0 -14
- package/build/templates/contract/partials/user-provider-database.txt +0 -16
- package/build/templates/contract/partials/user-provider-lucid.txt +0 -16
- package/build/templates/contract/partials/web-guard.txt +0 -14
- package/build/templates/middleware/Auth.txt +0 -76
- package/build/templates/middleware/SilentAuth.txt +0 -21
- package/build/templates/migrations/api_tokens.txt +0 -25
- package/build/templates/migrations/auth.txt +0 -24
- package/build/templates/model.txt +0 -30
package/LICENSE.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# The MIT License
|
|
2
2
|
|
|
3
|
-
Copyright
|
|
3
|
+
Copyright (c) 2023 Harminder Virk
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
6
|
|
package/README.md
CHANGED
|
@@ -1,53 +1,32 @@
|
|
|
1
|
-
|
|
2
|
-
<img src="https://res.cloudinary.com/adonisjs/image/upload/q_100/v1558612869/adonis-readme_zscycu.jpg" width="600px">
|
|
3
|
-
</div>
|
|
1
|
+
# @adonisjs/auth
|
|
4
2
|
|
|
5
3
|
<br />
|
|
6
4
|
|
|
7
|
-
|
|
8
|
-
<h3>Adonis Auth</h3>
|
|
9
|
-
<p>The official user authentication package for AdonisJS. Ships with <strong>sessions</strong>, <strong>api tokens</strong> and <strong>basic auth</strong> guards. </p>
|
|
10
|
-
</div>
|
|
5
|
+
[![gh-workflow-image]][gh-workflow-url] [![npm-image]][npm-url] ![][typescript-image] [![license-image]][license-url]
|
|
11
6
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
<div align="center">
|
|
7
|
+
## Introduction
|
|
15
8
|
|
|
16
|
-
|
|
9
|
+
## Official Documentation
|
|
10
|
+
The documentation is available on the [AdonisJS website](https://docs.adonisjs.com/guides/auth/introduction)
|
|
17
11
|
|
|
18
|
-
|
|
12
|
+
## Contributing
|
|
13
|
+
One of the primary goals of AdonisJS is to have a vibrant community of users and contributors who believes in the principles of the framework.
|
|
19
14
|
|
|
20
|
-
|
|
21
|
-
<h3>
|
|
22
|
-
<a href="https://preview.adonisjs.com">
|
|
23
|
-
Website
|
|
24
|
-
</a>
|
|
25
|
-
<span> | </span>
|
|
26
|
-
<a href="https://preview.adonisjs.com/guides/auth/introduction">
|
|
27
|
-
Guides
|
|
28
|
-
</a>
|
|
29
|
-
<span> | </span>
|
|
30
|
-
<a href="CONTRIBUTING.md">
|
|
31
|
-
Contributing
|
|
32
|
-
</a>
|
|
33
|
-
</h3>
|
|
34
|
-
</div>
|
|
15
|
+
We encourage you to read the [contribution guide](https://github.com/adonisjs/.github/blob/main/docs/CONTRIBUTING.md) before contributing to the framework.
|
|
35
16
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
</div>
|
|
17
|
+
## Code of Conduct
|
|
18
|
+
In order to ensure that the AdonisJS community is welcoming to all, please review and abide by the [Code of Conduct](https://github.com/adonisjs/.github/blob/main/docs/CODE_OF_CONDUCT.md).
|
|
39
19
|
|
|
40
|
-
|
|
41
|
-
[
|
|
20
|
+
## License
|
|
21
|
+
AdonisJS auth is open-sourced software licensed under the [MIT license](LICENSE.md).
|
|
42
22
|
|
|
43
|
-
[
|
|
44
|
-
[
|
|
23
|
+
[gh-workflow-image]: https://img.shields.io/github/actions/workflow/status/adonisjs/auth/checks.yml?style=for-the-badge
|
|
24
|
+
[gh-workflow-url]: https://github.com/adonisjs/auth/actions/workflows/checks.yml "Github action"
|
|
45
25
|
|
|
46
|
-
[npm-image]: https://img.shields.io/npm/v/@adonisjs/auth/
|
|
47
|
-
[npm-url]: https://www.npmjs.com/package/@adonisjs/auth/v/
|
|
26
|
+
[npm-image]: https://img.shields.io/npm/v/@adonisjs/auth/latest.svg?style=for-the-badge&logo=npm
|
|
27
|
+
[npm-url]: https://www.npmjs.com/package/@adonisjs/auth/v/latest "npm"
|
|
48
28
|
|
|
49
|
-
[
|
|
50
|
-
[license-url]: LICENSE.md "license"
|
|
29
|
+
[typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript
|
|
51
30
|
|
|
52
|
-
[
|
|
53
|
-
[
|
|
31
|
+
[license-url]: LICENSE.md
|
|
32
|
+
[license-image]: https://img.shields.io/github/license/adonisjs/auth?style=for-the-badge
|
|
@@ -0,0 +1,55 @@
|
|
|
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
|
+
* Configures the user provider to use for finding
|
|
11
|
+
* users
|
|
12
|
+
*/
|
|
13
|
+
async function configureProvider(command) {
|
|
14
|
+
const provider = await command.prompt.choice('Select the user provider you want to use', [
|
|
15
|
+
{
|
|
16
|
+
name: 'lucid',
|
|
17
|
+
message: 'Lucid models',
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
name: 'db',
|
|
21
|
+
message: 'Database query builder',
|
|
22
|
+
},
|
|
23
|
+
]);
|
|
24
|
+
/**
|
|
25
|
+
* Publish config file
|
|
26
|
+
*/
|
|
27
|
+
await command.publishStub('config.stub', { provider });
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Configures the auth package
|
|
31
|
+
*/
|
|
32
|
+
export async function configure(command) {
|
|
33
|
+
await configureProvider(command);
|
|
34
|
+
const codemods = await command.createCodemods();
|
|
35
|
+
/**
|
|
36
|
+
* Register provider
|
|
37
|
+
*/
|
|
38
|
+
await codemods.updateRcFile((rcFile) => {
|
|
39
|
+
rcFile.addProvider('@adonisjs/auth/auth_provider');
|
|
40
|
+
});
|
|
41
|
+
/**
|
|
42
|
+
* Register middleware
|
|
43
|
+
*/
|
|
44
|
+
await codemods.registerMiddleware('router', [
|
|
45
|
+
{
|
|
46
|
+
path: '@adonisjs/auth/initialize_auth_middleware',
|
|
47
|
+
},
|
|
48
|
+
]);
|
|
49
|
+
await codemods.registerMiddleware('named', [
|
|
50
|
+
{
|
|
51
|
+
name: 'auth',
|
|
52
|
+
path: '@adonisjs/auth/auth_middleware',
|
|
53
|
+
},
|
|
54
|
+
]);
|
|
55
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Database } from '@adonisjs/lucid/database';
|
|
2
|
+
import { Token } from '../src/core/token.js';
|
|
3
|
+
import { DatabaseTokenProvider } from '../src/core/token_providers/database.js';
|
|
4
|
+
/**
|
|
5
|
+
* Representation of token used for testing
|
|
6
|
+
*/
|
|
7
|
+
export declare class TestToken extends Token {
|
|
8
|
+
type: string;
|
|
9
|
+
userId: string | number;
|
|
10
|
+
static create(userId: number | string, expiry: string | number, size?: number): TestToken;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Test implementation of the database token provider
|
|
14
|
+
*/
|
|
15
|
+
export declare class TestDatabaseTokenProvider extends DatabaseTokenProvider<TestToken> {
|
|
16
|
+
protected prepareToken(dbRow: {
|
|
17
|
+
series: string;
|
|
18
|
+
user_id: string | number;
|
|
19
|
+
type: string;
|
|
20
|
+
token: string;
|
|
21
|
+
created_at: Date;
|
|
22
|
+
expires_at: Date | null;
|
|
23
|
+
}): TestToken;
|
|
24
|
+
protected parseToken(token: TestToken): {
|
|
25
|
+
series: string;
|
|
26
|
+
user_id: string | number;
|
|
27
|
+
type: string;
|
|
28
|
+
token: string;
|
|
29
|
+
created_at: Date;
|
|
30
|
+
updated_at: Date;
|
|
31
|
+
expires_at: Date | null;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export declare class DatabaseTokenProviderFactory {
|
|
35
|
+
create(db: Database): TestDatabaseTokenProvider;
|
|
36
|
+
}
|
|
@@ -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
|
+
import { Token } from '../src/core/token.js';
|
|
10
|
+
import { DatabaseTokenProvider } from '../src/core/token_providers/database.js';
|
|
11
|
+
/**
|
|
12
|
+
* Representation of token used for testing
|
|
13
|
+
*/
|
|
14
|
+
export class TestToken extends Token {
|
|
15
|
+
type = 'test_token';
|
|
16
|
+
static create(userId, expiry, size) {
|
|
17
|
+
const { series, value, hash } = this.seed(size);
|
|
18
|
+
const token = new TestToken(series, value, hash);
|
|
19
|
+
token.setExpiry(expiry);
|
|
20
|
+
token.userId = userId;
|
|
21
|
+
return token;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Test implementation of the database token provider
|
|
26
|
+
*/
|
|
27
|
+
export class TestDatabaseTokenProvider extends DatabaseTokenProvider {
|
|
28
|
+
prepareToken(dbRow) {
|
|
29
|
+
const token = new TestToken(dbRow.series, undefined, dbRow.token);
|
|
30
|
+
token.createdAt = dbRow.created_at;
|
|
31
|
+
if (dbRow.expires_at) {
|
|
32
|
+
token.expiresAt = dbRow.expires_at;
|
|
33
|
+
}
|
|
34
|
+
return token;
|
|
35
|
+
}
|
|
36
|
+
parseToken(token) {
|
|
37
|
+
return {
|
|
38
|
+
series: token.series,
|
|
39
|
+
user_id: token.userId,
|
|
40
|
+
type: token.type,
|
|
41
|
+
token: token.hash,
|
|
42
|
+
created_at: token.createdAt,
|
|
43
|
+
updated_at: token.createdAt,
|
|
44
|
+
expires_at: token.expiresAt || null,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
export class DatabaseTokenProviderFactory {
|
|
49
|
+
create(db) {
|
|
50
|
+
return new TestDatabaseTokenProvider(db, {
|
|
51
|
+
table: 'remember_me_tokens',
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Database } from '@adonisjs/lucid/database';
|
|
2
|
+
import { BaseDatabaseUserProvider } from '../src/core/user_providers/database.js';
|
|
3
|
+
export declare class TestDatabaseUserProvider<RealUser extends Record<string, any>> extends BaseDatabaseUserProvider<RealUser> {
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Creates an instance of the DatabaseUserProvider with sane
|
|
7
|
+
* defaults for testing
|
|
8
|
+
*/
|
|
9
|
+
export declare class DatabaseUserProviderFactory {
|
|
10
|
+
create(db: Database): TestDatabaseUserProvider<{
|
|
11
|
+
email: any;
|
|
12
|
+
username: any;
|
|
13
|
+
}>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
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 { Hash } from '@adonisjs/core/hash';
|
|
10
|
+
import { Scrypt } from '@adonisjs/core/hash/drivers/scrypt';
|
|
11
|
+
import { BaseDatabaseUserProvider } from '../src/core/user_providers/database.js';
|
|
12
|
+
export class TestDatabaseUserProvider extends BaseDatabaseUserProvider {
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Creates an instance of the DatabaseUserProvider with sane
|
|
16
|
+
* defaults for testing
|
|
17
|
+
*/
|
|
18
|
+
export class DatabaseUserProviderFactory {
|
|
19
|
+
create(db) {
|
|
20
|
+
return new TestDatabaseUserProvider(db, new Hash(new Scrypt({})), {
|
|
21
|
+
id: 'id',
|
|
22
|
+
table: 'users',
|
|
23
|
+
passwordColumnName: 'password',
|
|
24
|
+
uids: ['email', 'username'],
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { BaseModel } from '@adonisjs/lucid/orm';
|
|
2
|
+
import { PROVIDER_REAL_USER } from '../src/auth/symbols.js';
|
|
3
|
+
import { BaseLucidUserProvider } from '../src/core/user_providers/lucid.js';
|
|
4
|
+
import type { LucidAuthenticatable, LucidUserProviderOptions } from '../src/core/types.js';
|
|
5
|
+
export declare class FactoryUser extends BaseModel {
|
|
6
|
+
static table: string;
|
|
7
|
+
static createWithDefaults(attributes?: {
|
|
8
|
+
email?: string;
|
|
9
|
+
password?: string | null;
|
|
10
|
+
username?: string;
|
|
11
|
+
}): Promise<FactoryUser>;
|
|
12
|
+
id: number;
|
|
13
|
+
username: string;
|
|
14
|
+
email: string;
|
|
15
|
+
password: string | null;
|
|
16
|
+
verifyPasswordForAuth(plainTextPassword: string): Promise<boolean>;
|
|
17
|
+
}
|
|
18
|
+
export declare class TestLucidUserProvider<UserModel extends LucidAuthenticatable> extends BaseLucidUserProvider<UserModel> {
|
|
19
|
+
[PROVIDER_REAL_USER]: InstanceType<UserModel>;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Creates an instance of the LucidUserProvider with sane
|
|
23
|
+
* defaults for testing
|
|
24
|
+
*/
|
|
25
|
+
export declare class LucidUserProviderFactory {
|
|
26
|
+
createForModel<Model extends LucidAuthenticatable>(options: LucidUserProviderOptions<Model>): TestLucidUserProvider<Model>;
|
|
27
|
+
create(): TestLucidUserProvider<typeof FactoryUser>;
|
|
28
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
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
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
10
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
11
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
12
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
13
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
14
|
+
};
|
|
15
|
+
import { Hash } from '@adonisjs/core/hash';
|
|
16
|
+
import { BaseModel, column } from '@adonisjs/lucid/orm';
|
|
17
|
+
import { Scrypt } from '@adonisjs/core/hash/drivers/scrypt';
|
|
18
|
+
import { PROVIDER_REAL_USER } from '../src/auth/symbols.js';
|
|
19
|
+
import { BaseLucidUserProvider } from '../src/core/user_providers/lucid.js';
|
|
20
|
+
export class FactoryUser extends BaseModel {
|
|
21
|
+
static table = 'users';
|
|
22
|
+
static createWithDefaults(attributes) {
|
|
23
|
+
return this.create({
|
|
24
|
+
email: 'foo@bar.com',
|
|
25
|
+
username: 'foo',
|
|
26
|
+
password: 'secret',
|
|
27
|
+
...attributes,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
async verifyPasswordForAuth(plainTextPassword) {
|
|
31
|
+
return new Hash(new Scrypt({})).verify(this.password, plainTextPassword);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
__decorate([
|
|
35
|
+
column()
|
|
36
|
+
], FactoryUser.prototype, "id", void 0);
|
|
37
|
+
__decorate([
|
|
38
|
+
column()
|
|
39
|
+
], FactoryUser.prototype, "username", void 0);
|
|
40
|
+
__decorate([
|
|
41
|
+
column()
|
|
42
|
+
], FactoryUser.prototype, "email", void 0);
|
|
43
|
+
__decorate([
|
|
44
|
+
column()
|
|
45
|
+
], FactoryUser.prototype, "password", void 0);
|
|
46
|
+
export class TestLucidUserProvider extends BaseLucidUserProvider {
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Creates an instance of the LucidUserProvider with sane
|
|
50
|
+
* defaults for testing
|
|
51
|
+
*/
|
|
52
|
+
export class LucidUserProviderFactory {
|
|
53
|
+
createForModel(options) {
|
|
54
|
+
return new TestLucidUserProvider({
|
|
55
|
+
...options,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
create() {
|
|
59
|
+
return this.createForModel({
|
|
60
|
+
model: async () => {
|
|
61
|
+
return {
|
|
62
|
+
default: FactoryUser,
|
|
63
|
+
};
|
|
64
|
+
},
|
|
65
|
+
uids: ['email', 'username'],
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { SessionGuardFactory } from './session_guard_factory.js';
|
|
2
|
+
export { DatabaseUserProviderFactory, TestDatabaseUserProvider } from './database_user_provider.js';
|
|
3
|
+
export { FactoryUser, LucidUserProviderFactory, TestLucidUserProvider, } from './lucid_user_provider.js';
|
|
4
|
+
export { TestToken, TestDatabaseTokenProvider, DatabaseTokenProviderFactory, } from './database_token_factory.js';
|
|
@@ -0,0 +1,12 @@
|
|
|
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 { SessionGuardFactory } from './session_guard_factory.js';
|
|
10
|
+
export { DatabaseUserProviderFactory, TestDatabaseUserProvider } from './database_user_provider.js';
|
|
11
|
+
export { FactoryUser, LucidUserProviderFactory, TestLucidUserProvider, } from './lucid_user_provider.js';
|
|
12
|
+
export { TestToken, TestDatabaseTokenProvider, DatabaseTokenProviderFactory, } from './database_token_factory.js';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { HttpContext } from '@adonisjs/core/http';
|
|
2
|
+
import { SessionGuard } from '../src/guards/session/guard.js';
|
|
3
|
+
import type { SessionGuardConfig, SessionUserProviderContract } from '../src/guards/session/types.js';
|
|
4
|
+
import { FactoryUser, TestLucidUserProvider } from './lucid_user_provider.js';
|
|
5
|
+
/**
|
|
6
|
+
* Exposes the API to create a session guard for testing. Under
|
|
7
|
+
* the hood configures Lucid models for looking up users
|
|
8
|
+
*/
|
|
9
|
+
export declare class SessionGuardFactory {
|
|
10
|
+
#private;
|
|
11
|
+
merge(config: SessionGuardConfig): this;
|
|
12
|
+
create<UserProvider extends SessionUserProviderContract<unknown> = TestLucidUserProvider<typeof FactoryUser>>(ctx: HttpContext, provider?: UserProvider): SessionGuard<TestLucidUserProvider<typeof FactoryUser> | UserProvider>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
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 { SessionGuard } from '../src/guards/session/guard.js';
|
|
10
|
+
import { LucidUserProviderFactory, } from './lucid_user_provider.js';
|
|
11
|
+
/**
|
|
12
|
+
* Exposes the API to create a session guard for testing. Under
|
|
13
|
+
* the hood configures Lucid models for looking up users
|
|
14
|
+
*/
|
|
15
|
+
export class SessionGuardFactory {
|
|
16
|
+
#config = { rememberMeTokenAge: '5y' };
|
|
17
|
+
merge(config) {
|
|
18
|
+
this.#config = config;
|
|
19
|
+
return this;
|
|
20
|
+
}
|
|
21
|
+
create(ctx, provider) {
|
|
22
|
+
return new SessionGuard('web', this.#config, ctx, provider || new LucidUserProviderFactory().create());
|
|
23
|
+
}
|
|
24
|
+
}
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { configure } from './configure.js';
|
|
2
|
+
export { stubsRoot } from './stubs/main.js';
|
|
3
|
+
export * as errors from './src/auth/errors.js';
|
|
4
|
+
export * as symbols from './src/auth/symbols.js';
|
|
5
|
+
export { AuthManager } from './src/auth/auth_manager.js';
|
|
6
|
+
export { Authenticator } from './src/auth/authenticator.js';
|
|
7
|
+
export { defineConfig, providers } from './src/auth/define_config.js';
|
package/build/index.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
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 errors from './src/auth/errors.js';
|
|
12
|
+
export * as symbols from './src/auth/symbols.js';
|
|
13
|
+
export { AuthManager } from './src/auth/auth_manager.js';
|
|
14
|
+
export { Authenticator } from './src/auth/authenticator.js';
|
|
15
|
+
export { defineConfig, providers } from './src/auth/define_config.js';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ApplicationService } from '@adonisjs/core/types';
|
|
2
|
+
import type { AuthService } from '../src/auth/types.js';
|
|
3
|
+
declare module '@adonisjs/core/types' {
|
|
4
|
+
interface ContainerBindings {
|
|
5
|
+
'auth.manager': AuthService;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export default class AuthProvider {
|
|
9
|
+
protected app: ApplicationService;
|
|
10
|
+
constructor(app: ApplicationService);
|
|
11
|
+
register(): void;
|
|
12
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @adonisjs/auth
|
|
3
|
+
*
|
|
4
|
+
* (c) AdonisJS
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
import { configProvider } from '@adonisjs/core';
|
|
10
|
+
import { RuntimeException } from '@poppinss/utils';
|
|
11
|
+
import { AuthManager } from '../src/auth/auth_manager.js';
|
|
12
|
+
export default class AuthProvider {
|
|
13
|
+
app;
|
|
14
|
+
constructor(app) {
|
|
15
|
+
this.app = app;
|
|
16
|
+
}
|
|
17
|
+
register() {
|
|
18
|
+
this.app.container.singleton('auth.manager', async () => {
|
|
19
|
+
const authConfigProvider = this.app.config.get('auth');
|
|
20
|
+
const config = await configProvider.resolve(this.app, authConfigProvider);
|
|
21
|
+
if (!config) {
|
|
22
|
+
throw new RuntimeException('Invalid config exported from "config/auth.ts" file. Make sure to use the defineConfig method');
|
|
23
|
+
}
|
|
24
|
+
return new AuthManager(config);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -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 app from '@adonisjs/core/services/app';
|
|
10
|
+
let auth;
|
|
11
|
+
/**
|
|
12
|
+
* Returns a singleton instance of the Auth manager class
|
|
13
|
+
*/
|
|
14
|
+
await app.booted(async () => {
|
|
15
|
+
auth = await app.container.make('auth.manager');
|
|
16
|
+
});
|
|
17
|
+
export { auth as default };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { HttpContext } from '@adonisjs/core/http';
|
|
2
|
+
import type { GuardFactory } from './types.js';
|
|
3
|
+
import { Authenticator } from './authenticator.js';
|
|
4
|
+
/**
|
|
5
|
+
* Auth manager exposes the API to register and manage authentication
|
|
6
|
+
* guards from the config
|
|
7
|
+
*/
|
|
8
|
+
export declare class AuthManager<KnownGuards extends Record<string, GuardFactory>> {
|
|
9
|
+
#private;
|
|
10
|
+
/**
|
|
11
|
+
* Name of the default guard
|
|
12
|
+
*/
|
|
13
|
+
get defaultGuard(): keyof KnownGuards;
|
|
14
|
+
constructor(config: {
|
|
15
|
+
default: keyof KnownGuards;
|
|
16
|
+
guards: KnownGuards;
|
|
17
|
+
});
|
|
18
|
+
/**
|
|
19
|
+
* Create an authenticator for a given HTTP request
|
|
20
|
+
*/
|
|
21
|
+
createAuthenticator(ctx: HttpContext): Authenticator<KnownGuards>;
|
|
22
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
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 { Authenticator } from './authenticator.js';
|
|
10
|
+
/**
|
|
11
|
+
* Auth manager exposes the API to register and manage authentication
|
|
12
|
+
* guards from the config
|
|
13
|
+
*/
|
|
14
|
+
export class AuthManager {
|
|
15
|
+
/**
|
|
16
|
+
* Registered guards
|
|
17
|
+
*/
|
|
18
|
+
#config;
|
|
19
|
+
/**
|
|
20
|
+
* Name of the default guard
|
|
21
|
+
*/
|
|
22
|
+
get defaultGuard() {
|
|
23
|
+
return this.#config.default;
|
|
24
|
+
}
|
|
25
|
+
constructor(config) {
|
|
26
|
+
this.#config = config;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Create an authenticator for a given HTTP request
|
|
30
|
+
*/
|
|
31
|
+
createAuthenticator(ctx) {
|
|
32
|
+
return new Authenticator(ctx, this.#config);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { HttpContext } from '@adonisjs/core/http';
|
|
2
|
+
import type { GuardFactory } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Authenticator is an HTTP request specific implementation for using
|
|
5
|
+
* guards to login users and authenticate requests.
|
|
6
|
+
*/
|
|
7
|
+
export declare class Authenticator<KnownGuards extends Record<string, GuardFactory>> {
|
|
8
|
+
#private;
|
|
9
|
+
/**
|
|
10
|
+
* Name of the default guard
|
|
11
|
+
*/
|
|
12
|
+
get defaultGuard(): keyof KnownGuards;
|
|
13
|
+
/**
|
|
14
|
+
* Reference to the guard using which the current
|
|
15
|
+
* request has been authenticated.
|
|
16
|
+
*/
|
|
17
|
+
get authenticatedViaGuard(): keyof KnownGuards | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* A boolean to know if the current request has
|
|
20
|
+
* been authenticated
|
|
21
|
+
*/
|
|
22
|
+
get isAuthenticated(): boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Reference to the currently authenticated user
|
|
25
|
+
*/
|
|
26
|
+
get user(): {
|
|
27
|
+
[K in keyof KnownGuards]: ReturnType<KnownGuards[K]>['user'];
|
|
28
|
+
}[keyof KnownGuards];
|
|
29
|
+
/**
|
|
30
|
+
* Whether or not the authentication has been attempted
|
|
31
|
+
* during the current request
|
|
32
|
+
*/
|
|
33
|
+
get authenticationAttempted(): boolean;
|
|
34
|
+
constructor(ctx: HttpContext, config: {
|
|
35
|
+
default: keyof KnownGuards;
|
|
36
|
+
guards: KnownGuards;
|
|
37
|
+
});
|
|
38
|
+
/**
|
|
39
|
+
* Returns an instance of a known guard. Guards instances are
|
|
40
|
+
* cached during the lifecycle of an HTTP request.
|
|
41
|
+
*/
|
|
42
|
+
use<Guard extends keyof KnownGuards>(guard?: Guard): ReturnType<KnownGuards[Guard]>;
|
|
43
|
+
/**
|
|
44
|
+
* Authenticate the request using all of the mentioned
|
|
45
|
+
* guards or the default guard.
|
|
46
|
+
*
|
|
47
|
+
* The authentication process will stop after any of the
|
|
48
|
+
* mentioned guards is able to authenticate the request
|
|
49
|
+
* successfully.
|
|
50
|
+
*
|
|
51
|
+
* Otherwise, "AuthenticationException" will be raised.
|
|
52
|
+
*/
|
|
53
|
+
authenticateUsing(guards?: (keyof KnownGuards)[], options?: {
|
|
54
|
+
redirectTo?: string;
|
|
55
|
+
}): Promise<boolean>;
|
|
56
|
+
}
|