@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
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*
|
|
3
|
-
* @adonisjs/auth
|
|
4
|
-
*
|
|
5
|
-
* (c) AdonisJS
|
|
6
|
-
*
|
|
7
|
-
* For the full copyright and license information, please view the LICENSE
|
|
8
|
-
* file that was distributed with this source code.
|
|
9
|
-
*/
|
|
10
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
-
exports.OATClient = void 0;
|
|
12
|
-
const luxon_1 = require("luxon");
|
|
13
|
-
const crypto_1 = require("crypto");
|
|
14
|
-
const utils_1 = require("@poppinss/utils");
|
|
15
|
-
const helpers_1 = require("@poppinss/utils/build/helpers");
|
|
16
|
-
const ProviderToken_1 = require("../../Tokens/ProviderToken");
|
|
17
|
-
/**
|
|
18
|
-
* OAT client to login a user during tests using the
|
|
19
|
-
* opaque tokens guard
|
|
20
|
-
*/
|
|
21
|
-
class OATClient {
|
|
22
|
-
constructor(name, config, provider, tokenProvider) {
|
|
23
|
-
this.name = name;
|
|
24
|
-
this.config = config;
|
|
25
|
-
this.provider = provider;
|
|
26
|
-
this.tokenProvider = tokenProvider;
|
|
27
|
-
/**
|
|
28
|
-
* Length of the raw token. The hash length will vary
|
|
29
|
-
*/
|
|
30
|
-
this.tokenLength = 60;
|
|
31
|
-
/**
|
|
32
|
-
* Token type for the persistance store
|
|
33
|
-
*/
|
|
34
|
-
this.tokenType = this.config.tokenProvider.type || 'opaque_token';
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Returns the provider user instance from the regular user details. Raises
|
|
38
|
-
* exception when id is missing
|
|
39
|
-
*/
|
|
40
|
-
async getUserForLogin(user, identifierKey) {
|
|
41
|
-
const providerUser = await this.provider.getUserFor(user);
|
|
42
|
-
/**
|
|
43
|
-
* Ensure id exists on the user
|
|
44
|
-
*/
|
|
45
|
-
const id = providerUser.getId();
|
|
46
|
-
if (!id) {
|
|
47
|
-
throw new utils_1.Exception(`Cannot login user. Value of "${identifierKey}" is not defined`);
|
|
48
|
-
}
|
|
49
|
-
return providerUser;
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Converts value to a sha256 hash
|
|
53
|
-
*/
|
|
54
|
-
generateHash(token) {
|
|
55
|
-
return (0, crypto_1.createHash)('sha256').update(token).digest('hex');
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Converts expiry duration to an absolute date/time value
|
|
59
|
-
*/
|
|
60
|
-
getExpiresAtDate(expiresIn) {
|
|
61
|
-
if (!expiresIn) {
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
const milliseconds = typeof expiresIn === 'string' ? helpers_1.string.toMs(expiresIn) : expiresIn;
|
|
65
|
-
return luxon_1.DateTime.local().plus({ milliseconds });
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Generates a new token + hash for the persistance
|
|
69
|
-
*/
|
|
70
|
-
generateTokenForPersistance(expiresIn) {
|
|
71
|
-
const token = helpers_1.string.generateRandom(this.tokenLength);
|
|
72
|
-
return {
|
|
73
|
-
token,
|
|
74
|
-
hash: this.generateHash(token),
|
|
75
|
-
expiresAt: this.getExpiresAtDate(expiresIn),
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* Returns the request data to mark user as logged in
|
|
80
|
-
*/
|
|
81
|
-
async login(user, options) {
|
|
82
|
-
/**
|
|
83
|
-
* Normalize options with defaults
|
|
84
|
-
*/
|
|
85
|
-
const { expiresIn, name, ...meta } = Object.assign({
|
|
86
|
-
name: 'Opaque Access Token',
|
|
87
|
-
}, options);
|
|
88
|
-
/**
|
|
89
|
-
* Since the login method is not exposed to the end user, we cannot expect
|
|
90
|
-
* them to instantiate and pass an instance of provider user, so we
|
|
91
|
-
* create one manually.
|
|
92
|
-
*/
|
|
93
|
-
const providerUser = await this.getUserForLogin(user, this.config.provider.identifierKey);
|
|
94
|
-
/**
|
|
95
|
-
* "getUserForLogin" raises exception when id is missing, so we can
|
|
96
|
-
* safely assume it is defined
|
|
97
|
-
*/
|
|
98
|
-
const id = providerUser.getId();
|
|
99
|
-
const token = this.generateTokenForPersistance(expiresIn);
|
|
100
|
-
/**
|
|
101
|
-
* Persist token to the database. Make sure that we are always
|
|
102
|
-
* passing the hash to the storage driver
|
|
103
|
-
*/
|
|
104
|
-
const providerToken = new ProviderToken_1.ProviderToken(name, token.hash, id, this.tokenType);
|
|
105
|
-
providerToken.expiresAt = token.expiresAt;
|
|
106
|
-
providerToken.meta = meta;
|
|
107
|
-
this.tokenId = await this.tokenProvider.write(providerToken);
|
|
108
|
-
return {
|
|
109
|
-
headers: {
|
|
110
|
-
Authorization: `Bearer ${helpers_1.base64.urlEncode(this.tokenId)}.${token.token}`,
|
|
111
|
-
},
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* Logout user
|
|
116
|
-
*/
|
|
117
|
-
async logout() {
|
|
118
|
-
if (this.tokenId) {
|
|
119
|
-
await this.tokenProvider.destroy(this.tokenId, this.tokenType);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
exports.OATClient = OATClient;
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { SessionGuardConfig, GuardClientContract, UserProviderContract, ProviderUserContract } from '@ioc:Adonis/Addons/Auth';
|
|
2
|
-
/**
|
|
3
|
-
* Session client to login a user during tests using the
|
|
4
|
-
* sessions guard
|
|
5
|
-
*/
|
|
6
|
-
export declare class SessionClient implements GuardClientContract<any> {
|
|
7
|
-
name: string;
|
|
8
|
-
private config;
|
|
9
|
-
private provider;
|
|
10
|
-
constructor(name: string, config: SessionGuardConfig<any>, provider: UserProviderContract<any>);
|
|
11
|
-
/**
|
|
12
|
-
* The name of the session key name
|
|
13
|
-
*/
|
|
14
|
-
get sessionKeyName(): string;
|
|
15
|
-
/**
|
|
16
|
-
* Returns the provider user instance from the regular user details. Raises
|
|
17
|
-
* exception when id is missing
|
|
18
|
-
*/
|
|
19
|
-
protected getUserForLogin(user: any, identifierKey: string): Promise<ProviderUserContract<any>>;
|
|
20
|
-
/**
|
|
21
|
-
* Returns the request data to mark user as logged in
|
|
22
|
-
*/
|
|
23
|
-
login(user: any): Promise<{
|
|
24
|
-
session: {
|
|
25
|
-
[x: string]: string | number;
|
|
26
|
-
};
|
|
27
|
-
}>;
|
|
28
|
-
/**
|
|
29
|
-
* No need to logout when using session client.
|
|
30
|
-
* Session data is persisted within memory and will
|
|
31
|
-
* be cleared after each test
|
|
32
|
-
*/
|
|
33
|
-
logout(): Promise<void>;
|
|
34
|
-
}
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*
|
|
3
|
-
* @adonisjs/auth
|
|
4
|
-
*
|
|
5
|
-
* (c) AdonisJS
|
|
6
|
-
*
|
|
7
|
-
* For the full copyright and license information, please view the LICENSE
|
|
8
|
-
* file that was distributed with this source code.
|
|
9
|
-
*/
|
|
10
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
-
exports.SessionClient = void 0;
|
|
12
|
-
const utils_1 = require("@poppinss/utils");
|
|
13
|
-
/**
|
|
14
|
-
* Session client to login a user during tests using the
|
|
15
|
-
* sessions guard
|
|
16
|
-
*/
|
|
17
|
-
class SessionClient {
|
|
18
|
-
constructor(name, config, provider) {
|
|
19
|
-
this.name = name;
|
|
20
|
-
this.config = config;
|
|
21
|
-
this.provider = provider;
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* The name of the session key name
|
|
25
|
-
*/
|
|
26
|
-
get sessionKeyName() {
|
|
27
|
-
return `auth_${this.name}`;
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Returns the provider user instance from the regular user details. Raises
|
|
31
|
-
* exception when id is missing
|
|
32
|
-
*/
|
|
33
|
-
async getUserForLogin(user, identifierKey) {
|
|
34
|
-
const providerUser = await this.provider.getUserFor(user);
|
|
35
|
-
/**
|
|
36
|
-
* Ensure id exists on the user
|
|
37
|
-
*/
|
|
38
|
-
const id = providerUser.getId();
|
|
39
|
-
if (!id) {
|
|
40
|
-
throw new utils_1.Exception(`Cannot login user. Value of "${identifierKey}" is not defined`);
|
|
41
|
-
}
|
|
42
|
-
return providerUser;
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Returns the request data to mark user as logged in
|
|
46
|
-
*/
|
|
47
|
-
async login(user) {
|
|
48
|
-
/**
|
|
49
|
-
* Since the login method is exposed to the end user, we cannot expect
|
|
50
|
-
* them to instantiate and return an instance of authenticatable, so
|
|
51
|
-
* we create one manually.
|
|
52
|
-
*/
|
|
53
|
-
const providerUser = await this.getUserForLogin(user, this.config.provider.identifierKey);
|
|
54
|
-
/**
|
|
55
|
-
* getUserForLogin raises exception when id is missing, so we can
|
|
56
|
-
* safely assume it is defined
|
|
57
|
-
*/
|
|
58
|
-
const id = providerUser.getId();
|
|
59
|
-
return {
|
|
60
|
-
session: {
|
|
61
|
-
[this.sessionKeyName]: id,
|
|
62
|
-
},
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* No need to logout when using session client.
|
|
67
|
-
* Session data is persisted within memory and will
|
|
68
|
-
* be cleared after each test
|
|
69
|
-
*/
|
|
70
|
-
async logout() { }
|
|
71
|
-
}
|
|
72
|
-
exports.SessionClient = SessionClient;
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { Exception } from '@poppinss/utils';
|
|
2
|
-
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext';
|
|
3
|
-
/**
|
|
4
|
-
* Exception raised when unable to authenticate user session
|
|
5
|
-
*/
|
|
6
|
-
export declare class AuthenticationException extends Exception {
|
|
7
|
-
guard: string;
|
|
8
|
-
redirectTo: string;
|
|
9
|
-
responseText: string;
|
|
10
|
-
/**
|
|
11
|
-
* Raise exception with message and redirect url
|
|
12
|
-
*/
|
|
13
|
-
constructor(message: string, code: string, guard?: string, redirectTo?: string);
|
|
14
|
-
/**
|
|
15
|
-
* Prompts user to enter credentials
|
|
16
|
-
*/
|
|
17
|
-
protected respondWithBasicAuthPrompt(ctx: HttpContextContract, realm?: string): void;
|
|
18
|
-
/**
|
|
19
|
-
* Send response as an array of errors
|
|
20
|
-
*/
|
|
21
|
-
protected respondWithJson(ctx: HttpContextContract): void;
|
|
22
|
-
/**
|
|
23
|
-
* Flash error message and redirect the user back
|
|
24
|
-
*/
|
|
25
|
-
protected respondWithRedirect(ctx: HttpContextContract): void;
|
|
26
|
-
/**
|
|
27
|
-
* Send response as an array of errors formatted as per JSONAPI spec
|
|
28
|
-
*/
|
|
29
|
-
protected respondWithJsonAPI(ctx: HttpContextContract): void;
|
|
30
|
-
/**
|
|
31
|
-
* Missing session or unable to lookup user from session
|
|
32
|
-
*/
|
|
33
|
-
static invalidSession(guard: string): AuthenticationException;
|
|
34
|
-
/**
|
|
35
|
-
* Missing/Invalid token or unable to lookup user from the token
|
|
36
|
-
*/
|
|
37
|
-
static invalidToken(guard: string): AuthenticationException;
|
|
38
|
-
/**
|
|
39
|
-
* Missing or invalid basic auth credentials
|
|
40
|
-
*/
|
|
41
|
-
static invalidBasicCredentials(guard: string): AuthenticationException;
|
|
42
|
-
/**
|
|
43
|
-
* Self handle exception and attempt to make the best response based
|
|
44
|
-
* upon the type of request
|
|
45
|
-
*/
|
|
46
|
-
handle(_: AuthenticationException, ctx: HttpContextContract): Promise<void>;
|
|
47
|
-
}
|
|
@@ -1,142 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*
|
|
3
|
-
* @adonisjs/auth
|
|
4
|
-
*
|
|
5
|
-
* (c) Harminder Virk <virk@adonisjs.com>
|
|
6
|
-
*
|
|
7
|
-
* For the full copyright and license information, please view the LICENSE
|
|
8
|
-
* file that was distributed with this source code.
|
|
9
|
-
*/
|
|
10
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
-
exports.AuthenticationException = void 0;
|
|
12
|
-
const utils_1 = require("@poppinss/utils");
|
|
13
|
-
/**
|
|
14
|
-
* Exception raised when unable to authenticate user session
|
|
15
|
-
*/
|
|
16
|
-
class AuthenticationException extends utils_1.Exception {
|
|
17
|
-
/**
|
|
18
|
-
* Raise exception with message and redirect url
|
|
19
|
-
*/
|
|
20
|
-
constructor(message, code, guard, redirectTo) {
|
|
21
|
-
super(message, 401, code);
|
|
22
|
-
this.redirectTo = '/login';
|
|
23
|
-
this.responseText = this.message;
|
|
24
|
-
if (redirectTo) {
|
|
25
|
-
this.redirectTo = redirectTo;
|
|
26
|
-
}
|
|
27
|
-
if (guard) {
|
|
28
|
-
this.guard = guard;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Prompts user to enter credentials
|
|
33
|
-
*/
|
|
34
|
-
respondWithBasicAuthPrompt(ctx, realm) {
|
|
35
|
-
realm = realm || 'Authenticate';
|
|
36
|
-
ctx.response
|
|
37
|
-
.status(this.status)
|
|
38
|
-
.header('WWW-Authenticate', `Basic realm="${realm}", charset="UTF-8"`)
|
|
39
|
-
.send(this.responseText);
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Send response as an array of errors
|
|
43
|
-
*/
|
|
44
|
-
respondWithJson(ctx) {
|
|
45
|
-
ctx.response.status(this.status).send({
|
|
46
|
-
errors: [
|
|
47
|
-
{
|
|
48
|
-
message: this.responseText,
|
|
49
|
-
},
|
|
50
|
-
],
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* Flash error message and redirect the user back
|
|
55
|
-
*/
|
|
56
|
-
respondWithRedirect(ctx) {
|
|
57
|
-
if (!ctx.session) {
|
|
58
|
-
return ctx.response.status(this.status).send(this.responseText);
|
|
59
|
-
}
|
|
60
|
-
ctx.session.flashExcept(['_csrf']);
|
|
61
|
-
ctx.session.flash('auth', { error: this.responseText });
|
|
62
|
-
ctx.response.redirect(this.redirectTo, true);
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Send response as an array of errors formatted as per JSONAPI spec
|
|
66
|
-
*/
|
|
67
|
-
respondWithJsonAPI(ctx) {
|
|
68
|
-
ctx.response.status(this.status).send({
|
|
69
|
-
errors: [
|
|
70
|
-
{
|
|
71
|
-
code: this.code,
|
|
72
|
-
title: this.responseText,
|
|
73
|
-
source: null,
|
|
74
|
-
},
|
|
75
|
-
],
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* Missing session or unable to lookup user from session
|
|
80
|
-
*/
|
|
81
|
-
static invalidSession(guard) {
|
|
82
|
-
return new this('Invalid session', 'E_INVALID_AUTH_SESSION', guard);
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Missing/Invalid token or unable to lookup user from the token
|
|
86
|
-
*/
|
|
87
|
-
static invalidToken(guard) {
|
|
88
|
-
return new this('Invalid API token', 'E_INVALID_API_TOKEN', guard);
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* Missing or invalid basic auth credentials
|
|
92
|
-
*/
|
|
93
|
-
static invalidBasicCredentials(guard) {
|
|
94
|
-
return new this('Invalid basic auth credentials', 'E_INVALID_BASIC_CREDENTIALS', guard);
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Self handle exception and attempt to make the best response based
|
|
98
|
-
* upon the type of request
|
|
99
|
-
*/
|
|
100
|
-
async handle(_, ctx) {
|
|
101
|
-
/**
|
|
102
|
-
* We need access to the guard config and driver to make appropriate response
|
|
103
|
-
*/
|
|
104
|
-
const config = this.guard ? ctx.auth.use(this.guard).config : null;
|
|
105
|
-
/**
|
|
106
|
-
* Use translation when using i18n
|
|
107
|
-
*/
|
|
108
|
-
if ('i18n' in ctx) {
|
|
109
|
-
this.responseText = ctx.i18n.formatMessage(`auth.${this.code}`, {}, this.message);
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* Show username, password prompt when using basic auth driver
|
|
113
|
-
*/
|
|
114
|
-
if (config && config.driver === 'basic') {
|
|
115
|
-
this.respondWithBasicAuthPrompt(ctx, config.realm);
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Respond with json for ajax requests
|
|
120
|
-
*/
|
|
121
|
-
if (ctx.request.ajax()) {
|
|
122
|
-
this.respondWithJson(ctx);
|
|
123
|
-
return;
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Uses content negotiation to make the response
|
|
127
|
-
*/
|
|
128
|
-
switch (ctx.request.accepts(['html', 'application/vnd.api+json', 'json'])) {
|
|
129
|
-
case 'html':
|
|
130
|
-
case null:
|
|
131
|
-
this.respondWithRedirect(ctx);
|
|
132
|
-
break;
|
|
133
|
-
case 'json':
|
|
134
|
-
this.respondWithJson(ctx);
|
|
135
|
-
break;
|
|
136
|
-
case 'application/vnd.api+json':
|
|
137
|
-
this.respondWithJsonAPI(ctx);
|
|
138
|
-
break;
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
exports.AuthenticationException = AuthenticationException;
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { Exception } from '@poppinss/utils';
|
|
2
|
-
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext';
|
|
3
|
-
/**
|
|
4
|
-
* Exception raised when unable to verify user credentials
|
|
5
|
-
*/
|
|
6
|
-
export declare class InvalidCredentialsException extends Exception {
|
|
7
|
-
guard: string;
|
|
8
|
-
responseText: string;
|
|
9
|
-
/**
|
|
10
|
-
* Unable to find user
|
|
11
|
-
*/
|
|
12
|
-
static invalidUid(guard: string): InvalidCredentialsException;
|
|
13
|
-
/**
|
|
14
|
-
* Invalid user password
|
|
15
|
-
*/
|
|
16
|
-
static invalidPassword(guard: string): InvalidCredentialsException;
|
|
17
|
-
/**
|
|
18
|
-
* Send response as an array of errors
|
|
19
|
-
*/
|
|
20
|
-
protected respondWithJson(ctx: HttpContextContract): void;
|
|
21
|
-
/**
|
|
22
|
-
* Flash error message and redirect the user back
|
|
23
|
-
*/
|
|
24
|
-
protected respondWithRedirect(ctx: HttpContextContract): void;
|
|
25
|
-
/**
|
|
26
|
-
* Send response as an array of errors formatted as per JSONAPI spec
|
|
27
|
-
*/
|
|
28
|
-
protected respondWithJsonAPI(ctx: HttpContextContract): void;
|
|
29
|
-
/**
|
|
30
|
-
* Self handle exception and attempt to make the best response based
|
|
31
|
-
* upon the type of request
|
|
32
|
-
*/
|
|
33
|
-
handle(_: InvalidCredentialsException, ctx: HttpContextContract): Promise<void>;
|
|
34
|
-
}
|
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*
|
|
3
|
-
* @adonisjs/auth
|
|
4
|
-
*
|
|
5
|
-
* (c) Harminder Virk <virk@adonisjs.com>
|
|
6
|
-
*
|
|
7
|
-
* For the full copyright and license information, please view the LICENSE
|
|
8
|
-
* file that was distributed with this source code.
|
|
9
|
-
*/
|
|
10
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
-
exports.InvalidCredentialsException = void 0;
|
|
12
|
-
const utils_1 = require("@poppinss/utils");
|
|
13
|
-
/**
|
|
14
|
-
* Exception raised when unable to verify user credentials
|
|
15
|
-
*/
|
|
16
|
-
class InvalidCredentialsException extends utils_1.Exception {
|
|
17
|
-
constructor() {
|
|
18
|
-
super(...arguments);
|
|
19
|
-
this.responseText = this.message;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Unable to find user
|
|
23
|
-
*/
|
|
24
|
-
static invalidUid(guard) {
|
|
25
|
-
const error = new this('User not found', 400, 'E_INVALID_AUTH_UID');
|
|
26
|
-
error.guard = guard;
|
|
27
|
-
return error;
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Invalid user password
|
|
31
|
-
*/
|
|
32
|
-
static invalidPassword(guard) {
|
|
33
|
-
const error = new this('Password mis-match', 400, 'E_INVALID_AUTH_PASSWORD');
|
|
34
|
-
error.guard = guard;
|
|
35
|
-
return error;
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Send response as an array of errors
|
|
39
|
-
*/
|
|
40
|
-
respondWithJson(ctx) {
|
|
41
|
-
ctx.response.status(this.status).send({
|
|
42
|
-
errors: [
|
|
43
|
-
{
|
|
44
|
-
message: this.responseText,
|
|
45
|
-
},
|
|
46
|
-
],
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Flash error message and redirect the user back
|
|
51
|
-
*/
|
|
52
|
-
respondWithRedirect(ctx) {
|
|
53
|
-
if (!ctx.session) {
|
|
54
|
-
return ctx.response.status(this.status).send(this.responseText);
|
|
55
|
-
}
|
|
56
|
-
ctx.session.flashExcept(['_csrf']);
|
|
57
|
-
ctx.session.flash('auth', {
|
|
58
|
-
error: this.responseText,
|
|
59
|
-
/**
|
|
60
|
-
* Will be removed in the future
|
|
61
|
-
*/
|
|
62
|
-
errors: {
|
|
63
|
-
uid: this.code === 'E_INVALID_AUTH_UID' ? ['Invalid login id'] : null,
|
|
64
|
-
password: this.code === 'E_INVALID_AUTH_PASSWORD' ? ['Invalid password'] : null,
|
|
65
|
-
},
|
|
66
|
-
});
|
|
67
|
-
ctx.response.redirect('back', true);
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Send response as an array of errors formatted as per JSONAPI spec
|
|
71
|
-
*/
|
|
72
|
-
respondWithJsonAPI(ctx) {
|
|
73
|
-
ctx.response.status(this.status).send({
|
|
74
|
-
errors: [
|
|
75
|
-
{
|
|
76
|
-
code: this.code,
|
|
77
|
-
title: this.responseText,
|
|
78
|
-
source: null,
|
|
79
|
-
},
|
|
80
|
-
],
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Self handle exception and attempt to make the best response based
|
|
85
|
-
* upon the type of request
|
|
86
|
-
*/
|
|
87
|
-
async handle(_, ctx) {
|
|
88
|
-
/**
|
|
89
|
-
* Use translation when using i18n
|
|
90
|
-
*/
|
|
91
|
-
if ('i18n' in ctx) {
|
|
92
|
-
this.responseText = ctx.i18n.formatMessage(`auth.${this.code}`, {}, this.message);
|
|
93
|
-
}
|
|
94
|
-
if (ctx.request.ajax()) {
|
|
95
|
-
this.respondWithJson(ctx);
|
|
96
|
-
return;
|
|
97
|
-
}
|
|
98
|
-
switch (ctx.request.accepts(['html', 'application/vnd.api+json', 'json'])) {
|
|
99
|
-
case 'html':
|
|
100
|
-
case null:
|
|
101
|
-
this.respondWithRedirect(ctx);
|
|
102
|
-
break;
|
|
103
|
-
case 'json':
|
|
104
|
-
this.respondWithJson(ctx);
|
|
105
|
-
break;
|
|
106
|
-
case 'application/vnd.api+json':
|
|
107
|
-
this.respondWithJsonAPI(ctx);
|
|
108
|
-
break;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
exports.InvalidCredentialsException = InvalidCredentialsException;
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import { UserProviderContract, ProviderUserContract, GuardsList } from '@ioc:Adonis/Addons/Auth';
|
|
2
|
-
/**
|
|
3
|
-
* Base guard with shared abilities
|
|
4
|
-
*/
|
|
5
|
-
export declare abstract class BaseGuard<Guard extends keyof GuardsList> {
|
|
6
|
-
name: Guard;
|
|
7
|
-
config: GuardsList[Guard]['config'];
|
|
8
|
-
provider: UserProviderContract<any>;
|
|
9
|
-
constructor(name: Guard, config: GuardsList[Guard]['config'], provider: UserProviderContract<any>);
|
|
10
|
-
/**
|
|
11
|
-
* Reference to the name of the guard driver
|
|
12
|
-
*/
|
|
13
|
-
get driver(): "basic" | "session" | "oat";
|
|
14
|
-
/**
|
|
15
|
-
* Whether or not the authentication has been attempted
|
|
16
|
-
* for the current request
|
|
17
|
-
*/
|
|
18
|
-
authenticationAttempted: boolean;
|
|
19
|
-
/**
|
|
20
|
-
* Find if the user has been logged out in the current request
|
|
21
|
-
*/
|
|
22
|
-
isLoggedOut: boolean;
|
|
23
|
-
/**
|
|
24
|
-
* A boolean to know if user is retrieved by authenticating
|
|
25
|
-
* the current request or not
|
|
26
|
-
*/
|
|
27
|
-
isAuthenticated: boolean;
|
|
28
|
-
/**
|
|
29
|
-
* A boolean to know if user is loggedin via remember me token
|
|
30
|
-
* or not.
|
|
31
|
-
*/
|
|
32
|
-
viaRemember: boolean;
|
|
33
|
-
/**
|
|
34
|
-
* Logged in or authenticated user
|
|
35
|
-
*/
|
|
36
|
-
user?: any;
|
|
37
|
-
/**
|
|
38
|
-
* Accessor to know if user is logged in
|
|
39
|
-
*/
|
|
40
|
-
get isLoggedIn(): boolean;
|
|
41
|
-
/**
|
|
42
|
-
* Accessor to know if user is a guest. It is always opposite
|
|
43
|
-
* of [[isLoggedIn]]
|
|
44
|
-
*/
|
|
45
|
-
get isGuest(): boolean;
|
|
46
|
-
/**
|
|
47
|
-
* Lookup user using UID
|
|
48
|
-
*/
|
|
49
|
-
private lookupUsingUid;
|
|
50
|
-
/**
|
|
51
|
-
* Verify user password
|
|
52
|
-
*/
|
|
53
|
-
private verifyPassword;
|
|
54
|
-
/**
|
|
55
|
-
* Finds user by their id and returns the provider user instance
|
|
56
|
-
*/
|
|
57
|
-
protected findById(id: string | number): Promise<ProviderUserContract<any>>;
|
|
58
|
-
/**
|
|
59
|
-
* Returns the provider user instance from the regular user details. Raises
|
|
60
|
-
* exception when id is missing
|
|
61
|
-
*/
|
|
62
|
-
protected getUserForLogin(user: any, identifierKey: string): Promise<ProviderUserContract<any>>;
|
|
63
|
-
/**
|
|
64
|
-
* Marks user as logged-in
|
|
65
|
-
*/
|
|
66
|
-
protected markUserAsLoggedIn(user: any, authenticated?: boolean, viaRemember?: boolean): void;
|
|
67
|
-
/**
|
|
68
|
-
* Marks the user as logged out
|
|
69
|
-
*/
|
|
70
|
-
protected markUserAsLoggedOut(): void;
|
|
71
|
-
/**
|
|
72
|
-
* Verifies user credentials
|
|
73
|
-
*/
|
|
74
|
-
verifyCredentials(uid: string, password: string): Promise<any>;
|
|
75
|
-
}
|