@feathersjs/authentication-local 5.0.0-pre.1 → 5.0.0-pre.15
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/CHANGELOG.md +159 -0
- package/LICENSE +1 -1
- package/README.md +1 -1
- package/lib/hooks/hash-password.d.ts +2 -2
- package/lib/hooks/hash-password.js +27 -39
- package/lib/hooks/hash-password.js.map +1 -1
- package/lib/hooks/protect.d.ts +2 -2
- package/lib/hooks/protect.js +20 -19
- package/lib/hooks/protect.js.map +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/strategy.js +76 -79
- package/lib/strategy.js.map +1 -1
- package/package.json +15 -16
- package/src/hooks/hash-password.ts +28 -33
- package/src/hooks/protect.ts +19 -18
- package/src/strategy.ts +9 -4
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,165 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [5.0.0-pre.15](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.14...v5.0.0-pre.15) (2021-11-27)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Bug Fixes
|
|
10
|
+
|
|
11
|
+
* **typescript:** Overall typing improvements ([#2478](https://github.com/feathersjs/feathers/issues/2478)) ([b8eb804](https://github.com/feathersjs/feathers/commit/b8eb804158556d9651a8607e3c3fda15e0bfd110))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
# [5.0.0-pre.14](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.13...v5.0.0-pre.14) (2021-10-13)
|
|
18
|
+
|
|
19
|
+
**Note:** Version bump only for package @feathersjs/authentication-local
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
# [5.0.0-pre.13](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.12...v5.0.0-pre.13) (2021-10-13)
|
|
26
|
+
|
|
27
|
+
**Note:** Version bump only for package @feathersjs/authentication-local
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
# [5.0.0-pre.12](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.11...v5.0.0-pre.12) (2021-10-12)
|
|
34
|
+
|
|
35
|
+
**Note:** Version bump only for package @feathersjs/authentication-local
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
# [5.0.0-pre.11](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.10...v5.0.0-pre.11) (2021-10-06)
|
|
42
|
+
|
|
43
|
+
**Note:** Version bump only for package @feathersjs/authentication-local
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
# [5.0.0-pre.10](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.9...v5.0.0-pre.10) (2021-09-19)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
### Bug Fixes
|
|
53
|
+
|
|
54
|
+
* **authentication-local:** adds error handling for undefined/null password field ([#2444](https://github.com/feathersjs/feathers/issues/2444)) ([4323f98](https://github.com/feathersjs/feathers/commit/4323f9859a66a7fe3f7f15d81476bd81b735c226))
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
# [5.0.0-pre.9](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.8...v5.0.0-pre.9) (2021-08-09)
|
|
61
|
+
|
|
62
|
+
**Note:** Version bump only for package @feathersjs/authentication-local
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
# [5.0.0-pre.8](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.7...v5.0.0-pre.8) (2021-08-09)
|
|
69
|
+
|
|
70
|
+
**Note:** Version bump only for package @feathersjs/authentication-local
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
# [5.0.0-pre.7](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.6...v5.0.0-pre.7) (2021-08-09)
|
|
77
|
+
|
|
78
|
+
**Note:** Version bump only for package @feathersjs/authentication-local
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
# [5.0.0-pre.6](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.5...v5.0.0-pre.6) (2021-08-08)
|
|
85
|
+
|
|
86
|
+
**Note:** Version bump only for package @feathersjs/authentication-local
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
# [5.0.0-pre.5](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.4...v5.0.0-pre.5) (2021-06-23)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
### Bug Fixes
|
|
96
|
+
|
|
97
|
+
* **hooks:** Migrate built-in hooks and allow backwards compatibility ([#2358](https://github.com/feathersjs/feathers/issues/2358)) ([759c5a1](https://github.com/feathersjs/feathers/commit/759c5a19327a731af965c3604119393b3d09a406))
|
|
98
|
+
* **koa:** Use extended query parser for compatibility ([#2397](https://github.com/feathersjs/feathers/issues/2397)) ([b2944ba](https://github.com/feathersjs/feathers/commit/b2944bac3ec6d5ecc80dc518cd4e58093692db74))
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
### Features
|
|
102
|
+
|
|
103
|
+
* **adapter-commons:** Add support for params.adapter option and move memory adapter to @feathersjs/memory ([#2367](https://github.com/feathersjs/feathers/issues/2367)) ([a43e7da](https://github.com/feathersjs/feathers/commit/a43e7da22b6b981a96d1321736ea9a0cb924fb4f))
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
# [5.0.0-pre.4](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.3...v5.0.0-pre.4) (2021-05-13)
|
|
110
|
+
|
|
111
|
+
**Note:** Version bump only for package @feathersjs/authentication-local
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
# [5.0.0-pre.3](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.2...v5.0.0-pre.3) (2021-04-21)
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
### Bug Fixes
|
|
121
|
+
|
|
122
|
+
* **typescript:** Improve TypeScript backwards compatibility ([#2310](https://github.com/feathersjs/feathers/issues/2310)) ([f33be73](https://github.com/feathersjs/feathers/commit/f33be73fc46a533efb15df9aab0658e3240d3897))
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
### Features
|
|
126
|
+
|
|
127
|
+
* **dependencies:** Remove direct debug dependency ([#2296](https://github.com/feathersjs/feathers/issues/2296)) ([501d416](https://github.com/feathersjs/feathers/commit/501d4164d30c6a126906dc640cdfdc82207ba34a))
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
# [5.0.0-pre.2](https://github.com/feathersjs/feathers/compare/v5.0.0-beta.1...v5.0.0-pre.2) (2021-04-06)
|
|
134
|
+
|
|
135
|
+
**Note:** Version bump only for package @feathersjs/authentication-local
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
# [5.0.0-beta.1](https://github.com/feathersjs/feathers/compare/v5.0.0-beta.0...v5.0.0-beta.1) (2021-04-03)
|
|
142
|
+
|
|
143
|
+
**Note:** Version bump only for package @feathersjs/authentication-local
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
# [5.0.0-beta.0](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.1...v5.0.0-beta.0) (2021-03-28)
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
### Bug Fixes
|
|
153
|
+
|
|
154
|
+
* Update Grant usage and other dependencies ([#2264](https://github.com/feathersjs/feathers/issues/2264)) ([7b0f8fa](https://github.com/feathersjs/feathers/commit/7b0f8fad252419ed0ad0bf259cdf3104d322ab60))
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
### Features
|
|
158
|
+
|
|
159
|
+
* Feathers v5 core refactoring and features ([#2255](https://github.com/feathersjs/feathers/issues/2255)) ([2dafb7c](https://github.com/feathersjs/feathers/commit/2dafb7ce14ba57406aeec13d10ca45b1e709bee9))
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
|
|
6
165
|
# [5.0.0-pre.1](https://github.com/feathersjs/feathers/compare/v4.5.11...v5.0.0-pre.1) (2020-12-17)
|
|
7
166
|
|
|
8
167
|
|
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -18,6 +18,6 @@ Refer to the [Feathers local authentication API documentation](https://docs.feat
|
|
|
18
18
|
|
|
19
19
|
## License
|
|
20
20
|
|
|
21
|
-
Copyright (c)
|
|
21
|
+
Copyright (c) 2021 [Feathers contributors](https://github.com/feathersjs/feathers/graphs/contributors)
|
|
22
22
|
|
|
23
23
|
Licensed under the [MIT license](LICENSE).
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { HookContext } from '@feathersjs/feathers';
|
|
1
|
+
import { HookContext, NextFunction } from '@feathersjs/feathers';
|
|
2
2
|
export interface HashPasswordOptions {
|
|
3
3
|
authentication?: string;
|
|
4
4
|
strategy?: string;
|
|
5
5
|
}
|
|
6
|
-
export default function hashPassword(field: string, options?: HashPasswordOptions): (context: HookContext) => Promise<
|
|
6
|
+
export default function hashPassword(field: string, options?: HashPasswordOptions): (context: HookContext, next?: NextFunction) => Promise<void>;
|
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
@@ -16,43 +7,40 @@ const get_1 = __importDefault(require("lodash/get"));
|
|
|
16
7
|
const set_1 = __importDefault(require("lodash/set"));
|
|
17
8
|
const cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));
|
|
18
9
|
const errors_1 = require("@feathersjs/errors");
|
|
19
|
-
const
|
|
20
|
-
const debug =
|
|
10
|
+
const commons_1 = require("@feathersjs/commons");
|
|
11
|
+
const debug = (0, commons_1.createDebug)('@feathersjs/authentication-local/hooks/hash-password');
|
|
21
12
|
function hashPassword(field, options = {}) {
|
|
22
13
|
if (!field) {
|
|
23
14
|
throw new Error('The hashPassword hook requires a field name option');
|
|
24
15
|
}
|
|
25
|
-
return (context) =>
|
|
26
|
-
if (context.type !== 'before') {
|
|
27
|
-
throw new Error('The \'hashPassword\' hook should only be used as a \'before\' hook');
|
|
28
|
-
}
|
|
16
|
+
return async (context, next) => {
|
|
29
17
|
const { app, data, params } = context;
|
|
30
|
-
if (data
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
18
|
+
if (data !== undefined) {
|
|
19
|
+
const authService = app.defaultAuthentication(options.authentication);
|
|
20
|
+
const { strategy = 'local' } = options;
|
|
21
|
+
if (!authService || typeof authService.getStrategies !== 'function') {
|
|
22
|
+
throw new errors_1.BadRequest('Could not find an authentication service to hash password');
|
|
23
|
+
}
|
|
24
|
+
const [localStrategy] = authService.getStrategies(strategy);
|
|
25
|
+
if (!localStrategy || typeof localStrategy.hashPassword !== 'function') {
|
|
26
|
+
throw new errors_1.BadRequest(`Could not find '${strategy}' strategy to hash password`);
|
|
27
|
+
}
|
|
28
|
+
const addHashedPassword = async (data) => {
|
|
29
|
+
const password = (0, get_1.default)(data, field);
|
|
30
|
+
if (password === undefined) {
|
|
31
|
+
debug(`hook.data.${field} is undefined, not hashing password`);
|
|
32
|
+
return data;
|
|
33
|
+
}
|
|
34
|
+
const hashedPassword = await localStrategy.hashPassword(password, params);
|
|
35
|
+
return (0, set_1.default)((0, cloneDeep_1.default)(data), field, hashedPassword);
|
|
36
|
+
};
|
|
37
|
+
context.data = Array.isArray(data) ? await Promise.all(data.map(addHashedPassword)) :
|
|
38
|
+
await addHashedPassword(data);
|
|
38
39
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
throw new errors_1.BadRequest(`Could not find '${strategy}' strategy to hash password`);
|
|
40
|
+
if (typeof next === 'function') {
|
|
41
|
+
await next();
|
|
42
42
|
}
|
|
43
|
-
|
|
44
|
-
const password = get_1.default(data, field);
|
|
45
|
-
if (password === undefined) {
|
|
46
|
-
debug(`hook.data.${field} is undefined, not hashing password`);
|
|
47
|
-
return data;
|
|
48
|
-
}
|
|
49
|
-
const hashedPassword = yield localStrategy.hashPassword(password, params);
|
|
50
|
-
return set_1.default(cloneDeep_1.default(data), field, hashedPassword);
|
|
51
|
-
});
|
|
52
|
-
context.data = Array.isArray(data) ? yield Promise.all(data.map(addHashedPassword)) :
|
|
53
|
-
yield addHashedPassword(data);
|
|
54
|
-
return context;
|
|
55
|
-
});
|
|
43
|
+
};
|
|
56
44
|
}
|
|
57
45
|
exports.default = hashPassword;
|
|
58
46
|
//# sourceMappingURL=hash-password.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hash-password.js","sourceRoot":"","sources":["../../src/hooks/hash-password.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"hash-password.js","sourceRoot":"","sources":["../../src/hooks/hash-password.ts"],"names":[],"mappings":";;;;;AAAA,qDAA6B;AAC7B,qDAA6B;AAC7B,iEAAyC;AACzC,+CAAgD;AAChD,iDAAkD;AAIlD,MAAM,KAAK,GAAG,IAAA,qBAAW,EAAC,sDAAsD,CAAC,CAAC;AAOlF,SAAwB,YAAY,CAAE,KAAa,EAAE,UAA+B,EAAE;IACpF,IAAI,CAAC,KAAK,EAAE;QACV,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC,CAAC;KACvE;IAED,OAAO,KAAK,EAAE,OAAoB,EAAE,IAAmB,EAAE,EAAE;QACzD,MAAM,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;QAEtC,IAAI,IAAI,KAAK,SAAS,EAAE;YACtB,MAAM,WAAW,GAAG,GAAG,CAAC,qBAAqB,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC;YACtE,MAAM,EAAE,QAAQ,GAAG,OAAO,EAAE,GAAG,OAAO,CAAC;YAEvC,IAAI,CAAC,WAAW,IAAI,OAAO,WAAW,CAAC,aAAa,KAAK,UAAU,EAAE;gBACnE,MAAM,IAAI,mBAAU,CAAC,2DAA2D,CAAC,CAAC;aACnF;YAED,MAAM,CAAE,aAAa,CAAE,GAAG,WAAW,CAAC,aAAa,CAAC,QAAQ,CAAoB,CAAC;YAEjF,IAAI,CAAC,aAAa,IAAI,OAAO,aAAa,CAAC,YAAY,KAAK,UAAU,EAAE;gBACtE,MAAM,IAAI,mBAAU,CAAC,mBAAmB,QAAQ,6BAA6B,CAAC,CAAC;aAChF;YAED,MAAM,iBAAiB,GAAG,KAAK,EAAE,IAAS,EAAE,EAAE;gBAC5C,MAAM,QAAQ,GAAG,IAAA,aAAG,EAAC,IAAI,EAAE,KAAK,CAAC,CAAC;gBAElC,IAAI,QAAQ,KAAK,SAAS,EAAE;oBAC1B,KAAK,CAAC,aAAa,KAAK,qCAAqC,CAAC,CAAC;oBAC/D,OAAO,IAAI,CAAC;iBACb;gBAED,MAAM,cAAc,GAAW,MAAM,aAAa,CAAC,YAAY,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;gBAElF,OAAO,IAAA,aAAG,EAAC,IAAA,mBAAS,EAAC,IAAI,CAAC,EAAE,KAAK,EAAE,cAAc,CAAC,CAAC;YACrD,CAAC,CAAA;YAED,OAAO,CAAC,IAAI,GAAG,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC,CAAC,CAAC;gBACnF,MAAM,iBAAiB,CAAC,IAAI,CAAC,CAAC;SACjC;QAED,IAAI,OAAO,IAAI,KAAK,UAAU,EAAE;YAC9B,MAAM,IAAI,EAAE,CAAC;SACd;IACH,CAAC,CAAC;AACJ,CAAC;AA3CD,+BA2CC"}
|
package/lib/hooks/protect.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { HookContext } from '@feathersjs/feathers';
|
|
2
|
-
declare const _default: (...fields: string[]) => (context: HookContext) =>
|
|
1
|
+
import { HookContext, NextFunction } from '@feathersjs/feathers';
|
|
2
|
+
declare const _default: (...fields: string[]) => (context: HookContext, next?: NextFunction) => Promise<void>;
|
|
3
3
|
export default _default;
|
package/lib/hooks/protect.js
CHANGED
|
@@ -4,33 +4,34 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const omit_1 = __importDefault(require("lodash/omit"));
|
|
7
|
-
exports.default = (...fields) => (context) => {
|
|
8
|
-
const result = context.dispatch || context.result;
|
|
7
|
+
exports.default = (...fields) => async (context, next) => {
|
|
9
8
|
const o = (current) => {
|
|
10
9
|
if (typeof current === 'object' && !Array.isArray(current)) {
|
|
11
10
|
const data = typeof current.toJSON === 'function'
|
|
12
11
|
? current.toJSON() : current;
|
|
13
|
-
return omit_1.default(data, fields);
|
|
12
|
+
return (0, omit_1.default)(data, fields);
|
|
14
13
|
}
|
|
15
14
|
return current;
|
|
16
15
|
};
|
|
17
|
-
if (
|
|
18
|
-
|
|
19
|
-
}
|
|
20
|
-
if (Array.isArray(result)) {
|
|
21
|
-
context.dispatch = result.map(o);
|
|
22
|
-
}
|
|
23
|
-
else if (result.data && context.method === 'find') {
|
|
24
|
-
context.dispatch = Object.assign({}, result, {
|
|
25
|
-
data: result.data.map(o)
|
|
26
|
-
});
|
|
16
|
+
if (typeof next === 'function') {
|
|
17
|
+
await next();
|
|
27
18
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
19
|
+
const result = context.dispatch || context.result;
|
|
20
|
+
if (result) {
|
|
21
|
+
if (Array.isArray(result)) {
|
|
22
|
+
context.dispatch = result.map(o);
|
|
23
|
+
}
|
|
24
|
+
else if (result.data && context.method === 'find') {
|
|
25
|
+
context.dispatch = Object.assign({}, result, {
|
|
26
|
+
data: result.data.map(o)
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
context.dispatch = o(result);
|
|
31
|
+
}
|
|
32
|
+
if (context.params && context.params.provider) {
|
|
33
|
+
context.result = context.dispatch;
|
|
34
|
+
}
|
|
33
35
|
}
|
|
34
|
-
return context;
|
|
35
36
|
};
|
|
36
37
|
//# sourceMappingURL=protect.js.map
|
package/lib/hooks/protect.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"protect.js","sourceRoot":"","sources":["../../src/hooks/protect.ts"],"names":[],"mappings":";;;;;AAAA,uDAA+B;AAG/B,kBAAe,CAAC,GAAG,MAAgB,EAAE,EAAE,CAAC,
|
|
1
|
+
{"version":3,"file":"protect.js","sourceR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,EAAE;YAC7C,OAAO,CAAC,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC;SACnC;KACF;AACH,CAAC,CAAC"}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import hashPassword from './hooks/hash-password';
|
|
2
2
|
export declare const hooks: {
|
|
3
3
|
hashPassword: typeof hashPassword;
|
|
4
|
-
protect: (...fields: string[]) => (context: import("@feathersjs/feathers/lib").HookContext<
|
|
4
|
+
protect: (...fields: string[]) => (context: import("@feathersjs/feathers/lib").HookContext<import("@feathersjs/feathers/lib").Application<any, any>, any>, next?: import("@feathersjs/hooks/lib").NextFunction) => Promise<void>;
|
|
5
5
|
};
|
|
6
6
|
export { LocalStrategy } from './strategy';
|
package/lib/strategy.js
CHANGED
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
@@ -17,10 +8,10 @@ exports.LocalStrategy = void 0;
|
|
|
17
8
|
const bcryptjs_1 = __importDefault(require("bcryptjs"));
|
|
18
9
|
const get_1 = __importDefault(require("lodash/get"));
|
|
19
10
|
const omit_1 = __importDefault(require("lodash/omit"));
|
|
20
|
-
const debug_1 = __importDefault(require("debug"));
|
|
21
11
|
const errors_1 = require("@feathersjs/errors");
|
|
22
12
|
const authentication_1 = require("@feathersjs/authentication");
|
|
23
|
-
const
|
|
13
|
+
const commons_1 = require("@feathersjs/commons");
|
|
14
|
+
const debug = (0, commons_1.createDebug)('@feathersjs/authentication-local/strategy');
|
|
24
15
|
class LocalStrategy extends authentication_1.AuthenticationBaseStrategy {
|
|
25
16
|
verifyConfiguration() {
|
|
26
17
|
const config = this.configuration;
|
|
@@ -33,82 +24,88 @@ class LocalStrategy extends authentication_1.AuthenticationBaseStrategy {
|
|
|
33
24
|
get configuration() {
|
|
34
25
|
const authConfig = this.authentication.configuration;
|
|
35
26
|
const config = super.configuration || {};
|
|
36
|
-
return
|
|
27
|
+
return {
|
|
28
|
+
hashSize: 10,
|
|
29
|
+
service: authConfig.service,
|
|
30
|
+
entity: authConfig.entity,
|
|
31
|
+
entityId: authConfig.entityId,
|
|
32
|
+
errorMessage: 'Invalid login',
|
|
33
|
+
entityPasswordField: config.passwordField,
|
|
34
|
+
entityUsernameField: config.usernameField,
|
|
35
|
+
...config
|
|
36
|
+
};
|
|
37
37
|
}
|
|
38
|
-
getEntityQuery(query, _params) {
|
|
39
|
-
return
|
|
40
|
-
|
|
41
|
-
|
|
38
|
+
async getEntityQuery(query, _params) {
|
|
39
|
+
return {
|
|
40
|
+
$limit: 1,
|
|
41
|
+
...query
|
|
42
|
+
};
|
|
42
43
|
}
|
|
43
|
-
findEntity(username, params) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
return entity;
|
|
63
|
-
});
|
|
44
|
+
async findEntity(username, params) {
|
|
45
|
+
const { entityUsernameField, errorMessage } = this.configuration;
|
|
46
|
+
if (!username) { // don't query for users without any condition set.
|
|
47
|
+
throw new errors_1.NotAuthenticated(errorMessage);
|
|
48
|
+
}
|
|
49
|
+
const query = await this.getEntityQuery({
|
|
50
|
+
[entityUsernameField]: username
|
|
51
|
+
}, params);
|
|
52
|
+
const findParams = Object.assign({}, params, { query });
|
|
53
|
+
const entityService = this.entityService;
|
|
54
|
+
debug('Finding entity with query', params.query);
|
|
55
|
+
const result = await entityService.find(findParams);
|
|
56
|
+
const list = Array.isArray(result) ? result : result.data;
|
|
57
|
+
if (!Array.isArray(list) || list.length === 0) {
|
|
58
|
+
debug('No entity found');
|
|
59
|
+
throw new errors_1.NotAuthenticated(errorMessage);
|
|
60
|
+
}
|
|
61
|
+
const [entity] = list;
|
|
62
|
+
return entity;
|
|
64
63
|
}
|
|
65
|
-
getEntity(result, params) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
64
|
+
async getEntity(result, params) {
|
|
65
|
+
const entityService = this.entityService;
|
|
66
|
+
const { entityId = entityService.id, entity } = this.configuration;
|
|
67
|
+
if (!entityId || result[entityId] === undefined) {
|
|
68
|
+
throw new errors_1.NotAuthenticated('Could not get local entity');
|
|
69
|
+
}
|
|
70
|
+
if (!params.provider) {
|
|
71
|
+
return result;
|
|
72
|
+
}
|
|
73
|
+
return entityService.get(result[entityId], {
|
|
74
|
+
...params,
|
|
75
|
+
[entity]: result
|
|
76
76
|
});
|
|
77
77
|
}
|
|
78
|
-
comparePassword(entity, password) {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
debug(`Record is missing the '${entityPasswordField}' password field`);
|
|
85
|
-
throw new errors_1.NotAuthenticated(errorMessage);
|
|
86
|
-
}
|
|
87
|
-
debug('Verifying password');
|
|
88
|
-
const result = yield bcryptjs_1.default.compare(password, hash);
|
|
89
|
-
if (result) {
|
|
90
|
-
return entity;
|
|
91
|
-
}
|
|
78
|
+
async comparePassword(entity, password) {
|
|
79
|
+
const { entityPasswordField, errorMessage } = this.configuration;
|
|
80
|
+
// find password in entity, this allows for dot notation
|
|
81
|
+
const hash = (0, get_1.default)(entity, entityPasswordField);
|
|
82
|
+
if (!hash) {
|
|
83
|
+
debug(`Record is missing the '${entityPasswordField}' password field`);
|
|
92
84
|
throw new errors_1.NotAuthenticated(errorMessage);
|
|
93
|
-
}
|
|
85
|
+
}
|
|
86
|
+
debug('Verifying password');
|
|
87
|
+
const result = await bcryptjs_1.default.compare(password, hash);
|
|
88
|
+
if (result) {
|
|
89
|
+
return entity;
|
|
90
|
+
}
|
|
91
|
+
throw new errors_1.NotAuthenticated(errorMessage);
|
|
94
92
|
}
|
|
95
|
-
hashPassword(password, _params) {
|
|
96
|
-
return
|
|
97
|
-
return bcryptjs_1.default.hash(password, this.configuration.hashSize);
|
|
98
|
-
});
|
|
93
|
+
async hashPassword(password, _params) {
|
|
94
|
+
return bcryptjs_1.default.hash(password, this.configuration.hashSize);
|
|
99
95
|
}
|
|
100
|
-
authenticate(data, params) {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
|
|
96
|
+
async authenticate(data, params) {
|
|
97
|
+
const { passwordField, usernameField, entity, errorMessage } = this.configuration;
|
|
98
|
+
const username = data[usernameField];
|
|
99
|
+
const password = data[passwordField];
|
|
100
|
+
if (!password) { // exit early if there is no password
|
|
101
|
+
throw new errors_1.NotAuthenticated(errorMessage);
|
|
102
|
+
}
|
|
103
|
+
const result = await this.findEntity(username, (0, omit_1.default)(params, 'provider'));
|
|
104
|
+
await this.comparePassword(result, password);
|
|
105
|
+
return {
|
|
106
|
+
authentication: { strategy: this.name },
|
|
107
|
+
[entity]: await this.getEntity(result, params)
|
|
108
|
+
};
|
|
112
109
|
}
|
|
113
110
|
}
|
|
114
111
|
exports.LocalStrategy = LocalStrategy;
|
package/lib/strategy.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"strategy.js","sourceRoot":"","sources":["../src/strategy.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"strategy.js","sourceRoot":"","sources":["../src/strategy.ts"],"names":[],"mappings":";;;;;;AAAA,sDAAsD;AACtD,wDAA8B;AAC9B,qDAA6B;AAC7B,uDAA+B;AAC/B,+CAAsD;AAEtD,+DAEoC;AACpC,iDAAkD;AAElD,MAAM,KAAK,GAAG,IAAA,qBAAW,EAAC,2CAA2C,CAAC,CAAC;AAEvE,MAAa,aAAc,SAAQ,2CAA0B;IAC3D,mBAAmB;QACjB,MAAM,MAAM,GAAG,IAAI,CAAC,aAAa,CAAC;QAElC,CAAE,eAAe,EAAE,eAAe,CAAE,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE;YAClD,IAAI,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,QAAQ,EAAE;gBACpC,MAAM,IAAI,KAAK,CAAC,IAAI,IAAI,CAAC,IAAI,yCAAyC,IAAI,WAAW,CAAC,CAAC;aACxF;QACH,CAAC,CAAC,CAAC;IACL,CAAC;IAED,IAAI,aAAa;QACf,MAAM,UAAU,GAAG,IAAI,CAAC,cAAc,CAAC,aAAa,CAAC;QACrD,MAAM,MAAM,GAAG,KAAK,CAAC,aAAa,IAAI,EAAE,CAAC;QAEzC,OAAO;YACL,QAAQ,EAAE,EAAE;YACZ,OAAO,EAAE,UAAU,CAAC,OAAO;YAC3B,MAAM,EAAE,UAAU,CAAC,MAAM;YACzB,QAAQ,EAAE,UAAU,CAAC,QAAQ;YAC7B,YAAY,EAAE,eAAe;YAC7B,mBAAmB,EAAE,MAAM,CAAC,aAAa;YACzC,mBAAmB,EAAE,MAAM,CAAC,aAAa;YACzC,GAAG,MAAM;SACV,CAAC;IACJ,CAAC;I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;IAED,KAAK,CAAC,YAAY,CAAE,IAA2B,EAAE,MAAc;QAC7D,MAAM,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,EAAE,YAAY,EAAE,GAAG,IAAI,CAAC,aAAa,CAAC;QAClF,MAAM,QAAQ,GAAG,IAAI,CAAC,aAAa,CAAC,CAAC;QACrC,MAAM,QAAQ,GAAG,IAAI,CAAC,aAAa,CAAC,CAAC;QAErC,IAAI,CAAC,QAAQ,EAAE,EAAE,qCAAqC;YACpD,MAAM,IAAI,yBAAgB,CAAC,YAAY,CAAC,CAAC;SAC1C;QAED,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,QAAQ,EAAE,IAAA,cAAI,EAAC,MAAM,EAAE,UAAU,CAAC,CAAC,CAAC;QAEzE,MAAM,IAAI,CAAC,eAAe,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;QAE7C,OAAO;YACL,cAAc,EAAE,EAAE,QAAQ,EAAE,IAAI,CAAC,IAAI,EAAE;YACvC,CAAC,MAAM,CAAC,EAAE,MAAM,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC;SAC/C,CAAC;IACJ,CAAC;CACF;AA7HD,sCA6HC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@feathersjs/authentication-local",
|
|
3
3
|
"description": "Local authentication strategy for @feathers/authentication",
|
|
4
|
-
"version": "5.0.0-pre.
|
|
4
|
+
"version": "5.0.0-pre.15",
|
|
5
5
|
"homepage": "https://feathersjs.com",
|
|
6
6
|
"main": "lib/",
|
|
7
7
|
"types": "lib/",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"url": "https://github.com/feathersjs/feathers/issues"
|
|
29
29
|
},
|
|
30
30
|
"engines": {
|
|
31
|
-
"node": ">=
|
|
31
|
+
"node": ">= 12"
|
|
32
32
|
},
|
|
33
33
|
"files": [
|
|
34
34
|
"CHANGELOG.md",
|
|
@@ -52,24 +52,23 @@
|
|
|
52
52
|
"access": "public"
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {
|
|
55
|
-
"@feathersjs/authentication": "^5.0.0-pre.
|
|
56
|
-
"@feathersjs/
|
|
57
|
-
"@feathersjs/
|
|
55
|
+
"@feathersjs/authentication": "^5.0.0-pre.15",
|
|
56
|
+
"@feathersjs/commons": "^5.0.0-pre.15",
|
|
57
|
+
"@feathersjs/errors": "^5.0.0-pre.15",
|
|
58
|
+
"@feathersjs/feathers": "^5.0.0-pre.15",
|
|
58
59
|
"bcryptjs": "^2.4.3",
|
|
59
|
-
"
|
|
60
|
-
"lodash": "^4.17.20"
|
|
60
|
+
"lodash": "^4.17.21"
|
|
61
61
|
},
|
|
62
62
|
"devDependencies": {
|
|
63
|
-
"@feathersjs/
|
|
63
|
+
"@feathersjs/memory": "^5.0.0-pre.15",
|
|
64
64
|
"@types/bcryptjs": "^2.4.2",
|
|
65
|
-
"@types/
|
|
66
|
-
"@types/
|
|
67
|
-
"@types/
|
|
68
|
-
"
|
|
69
|
-
"mocha": "^8.2.1",
|
|
65
|
+
"@types/lodash": "^4.14.176",
|
|
66
|
+
"@types/mocha": "^9.0.0",
|
|
67
|
+
"@types/node": "^16.11.6",
|
|
68
|
+
"mocha": "^9.1.3",
|
|
70
69
|
"shx": "^0.3.3",
|
|
71
|
-
"ts-node": "^
|
|
72
|
-
"typescript": "^4.
|
|
70
|
+
"ts-node": "^10.4.0",
|
|
71
|
+
"typescript": "^4.4.4"
|
|
73
72
|
},
|
|
74
|
-
"gitHead": "
|
|
73
|
+
"gitHead": "8008bf4f8529a2a40b6a2f976c1f43ae13675693"
|
|
75
74
|
}
|
|
@@ -2,11 +2,11 @@ import get from 'lodash/get';
|
|
|
2
2
|
import set from 'lodash/set';
|
|
3
3
|
import cloneDeep from 'lodash/cloneDeep';
|
|
4
4
|
import { BadRequest } from '@feathersjs/errors';
|
|
5
|
-
import
|
|
6
|
-
import { HookContext } from '@feathersjs/feathers';
|
|
5
|
+
import { createDebug } from '@feathersjs/commons';
|
|
6
|
+
import { HookContext, NextFunction } from '@feathersjs/feathers';
|
|
7
7
|
import { LocalStrategy } from '../strategy';
|
|
8
8
|
|
|
9
|
-
const debug =
|
|
9
|
+
const debug = createDebug('@feathersjs/authentication-local/hooks/hash-password');
|
|
10
10
|
|
|
11
11
|
export interface HashPasswordOptions {
|
|
12
12
|
authentication?: string;
|
|
@@ -18,47 +18,42 @@ export default function hashPassword (field: string, options: HashPasswordOption
|
|
|
18
18
|
throw new Error('The hashPassword hook requires a field name option');
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
return async (context: HookContext) => {
|
|
22
|
-
if (context.type !== 'before') {
|
|
23
|
-
throw new Error('The \'hashPassword\' hook should only be used as a \'before\' hook');
|
|
24
|
-
}
|
|
25
|
-
|
|
21
|
+
return async (context: HookContext, next?: NextFunction) => {
|
|
26
22
|
const { app, data, params } = context;
|
|
27
23
|
|
|
28
|
-
if (data
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
24
|
+
if (data !== undefined) {
|
|
25
|
+
const authService = app.defaultAuthentication(options.authentication);
|
|
26
|
+
const { strategy = 'local' } = options;
|
|
32
27
|
|
|
33
|
-
|
|
34
|
-
|
|
28
|
+
if (!authService || typeof authService.getStrategies !== 'function') {
|
|
29
|
+
throw new BadRequest('Could not find an authentication service to hash password');
|
|
30
|
+
}
|
|
35
31
|
|
|
36
|
-
|
|
37
|
-
throw new BadRequest('Could not find an authentication service to hash password');
|
|
38
|
-
}
|
|
32
|
+
const [ localStrategy ] = authService.getStrategies(strategy) as LocalStrategy[];
|
|
39
33
|
|
|
40
|
-
|
|
34
|
+
if (!localStrategy || typeof localStrategy.hashPassword !== 'function') {
|
|
35
|
+
throw new BadRequest(`Could not find '${strategy}' strategy to hash password`);
|
|
36
|
+
}
|
|
41
37
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
38
|
+
const addHashedPassword = async (data: any) => {
|
|
39
|
+
const password = get(data, field);
|
|
45
40
|
|
|
46
|
-
|
|
47
|
-
|
|
41
|
+
if (password === undefined) {
|
|
42
|
+
debug(`hook.data.${field} is undefined, not hashing password`);
|
|
43
|
+
return data;
|
|
44
|
+
}
|
|
48
45
|
|
|
49
|
-
|
|
50
|
-
debug(`hook.data.${field} is undefined, not hashing password`);
|
|
51
|
-
return data;
|
|
52
|
-
}
|
|
46
|
+
const hashedPassword: string = await localStrategy.hashPassword(password, params);
|
|
53
47
|
|
|
54
|
-
|
|
48
|
+
return set(cloneDeep(data), field, hashedPassword);
|
|
49
|
+
}
|
|
55
50
|
|
|
56
|
-
|
|
51
|
+
context.data = Array.isArray(data) ? await Promise.all(data.map(addHashedPassword)) :
|
|
52
|
+
await addHashedPassword(data);
|
|
57
53
|
}
|
|
58
54
|
|
|
59
|
-
|
|
60
|
-
await
|
|
61
|
-
|
|
62
|
-
return context;
|
|
55
|
+
if (typeof next === 'function') {
|
|
56
|
+
await next();
|
|
57
|
+
}
|
|
63
58
|
};
|
|
64
59
|
}
|
package/src/hooks/protect.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import omit from 'lodash/omit';
|
|
2
|
-
import { HookContext } from '@feathersjs/feathers';
|
|
2
|
+
import { HookContext, NextFunction } from '@feathersjs/feathers';
|
|
3
3
|
|
|
4
|
-
export default (...fields: string[]) => (context: HookContext) => {
|
|
5
|
-
const result = context.dispatch || context.result;
|
|
4
|
+
export default (...fields: string[]) => async (context: HookContext, next?: NextFunction) => {
|
|
6
5
|
const o = (current: any) => {
|
|
7
6
|
if (typeof current === 'object' && !Array.isArray(current)) {
|
|
8
7
|
const data = typeof current.toJSON === 'function'
|
|
@@ -14,23 +13,25 @@ export default (...fields: string[]) => (context: HookContext) => {
|
|
|
14
13
|
return current;
|
|
15
14
|
};
|
|
16
15
|
|
|
17
|
-
if (
|
|
18
|
-
|
|
16
|
+
if (typeof next === 'function') {
|
|
17
|
+
await next();
|
|
19
18
|
}
|
|
20
19
|
|
|
21
|
-
|
|
22
|
-
context.dispatch = result.map(o);
|
|
23
|
-
} else if (result.data && context.method === 'find') {
|
|
24
|
-
context.dispatch = Object.assign({}, result, {
|
|
25
|
-
data: result.data.map(o)
|
|
26
|
-
});
|
|
27
|
-
} else {
|
|
28
|
-
context.dispatch = o(result);
|
|
29
|
-
}
|
|
20
|
+
const result = context.dispatch || context.result;
|
|
30
21
|
|
|
31
|
-
if (
|
|
32
|
-
|
|
33
|
-
|
|
22
|
+
if (result) {
|
|
23
|
+
if (Array.isArray(result)) {
|
|
24
|
+
context.dispatch = result.map(o);
|
|
25
|
+
} else if (result.data && context.method === 'find') {
|
|
26
|
+
context.dispatch = Object.assign({}, result, {
|
|
27
|
+
data: result.data.map(o)
|
|
28
|
+
});
|
|
29
|
+
} else {
|
|
30
|
+
context.dispatch = o(result);
|
|
31
|
+
}
|
|
34
32
|
|
|
35
|
-
|
|
33
|
+
if (context.params && context.params.provider) {
|
|
34
|
+
context.result = context.dispatch;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
36
37
|
};
|
package/src/strategy.ts
CHANGED
|
@@ -2,14 +2,14 @@
|
|
|
2
2
|
import bcrypt from 'bcryptjs';
|
|
3
3
|
import get from 'lodash/get';
|
|
4
4
|
import omit from 'lodash/omit';
|
|
5
|
-
import Debug from 'debug';
|
|
6
5
|
import { NotAuthenticated } from '@feathersjs/errors';
|
|
7
6
|
import { Query, Params } from '@feathersjs/feathers';
|
|
8
7
|
import {
|
|
9
8
|
AuthenticationRequest, AuthenticationBaseStrategy
|
|
10
9
|
} from '@feathersjs/authentication';
|
|
10
|
+
import { createDebug } from '@feathersjs/commons';
|
|
11
11
|
|
|
12
|
-
const debug =
|
|
12
|
+
const debug = createDebug('@feathersjs/authentication-local/strategy');
|
|
13
13
|
|
|
14
14
|
export class LocalStrategy extends AuthenticationBaseStrategy {
|
|
15
15
|
verifyConfiguration () {
|
|
@@ -76,7 +76,7 @@ export class LocalStrategy extends AuthenticationBaseStrategy {
|
|
|
76
76
|
|
|
77
77
|
async getEntity (result: any, params: Params) {
|
|
78
78
|
const entityService = this.entityService;
|
|
79
|
-
const { entityId = entityService.id, entity } = this.configuration;
|
|
79
|
+
const { entityId = (entityService as any).id, entity } = this.configuration;
|
|
80
80
|
|
|
81
81
|
if (!entityId || result[entityId] === undefined) {
|
|
82
82
|
throw new NotAuthenticated('Could not get local entity');
|
|
@@ -119,9 +119,14 @@ export class LocalStrategy extends AuthenticationBaseStrategy {
|
|
|
119
119
|
}
|
|
120
120
|
|
|
121
121
|
async authenticate (data: AuthenticationRequest, params: Params) {
|
|
122
|
-
const { passwordField, usernameField, entity } = this.configuration;
|
|
122
|
+
const { passwordField, usernameField, entity, errorMessage } = this.configuration;
|
|
123
123
|
const username = data[usernameField];
|
|
124
124
|
const password = data[passwordField];
|
|
125
|
+
|
|
126
|
+
if (!password) { // exit early if there is no password
|
|
127
|
+
throw new NotAuthenticated(errorMessage);
|
|
128
|
+
}
|
|
129
|
+
|
|
125
130
|
const result = await this.findEntity(username, omit(params, 'provider'));
|
|
126
131
|
|
|
127
132
|
await this.comparePassword(result, password);
|