Skip to content

Commit 467c071

Browse files
authored
Merge pull request #2 from feathersjs-ecosystem/master
Master sync
2 parents 4650962 + 3700ad0 commit 467c071

17 files changed

+2782
-2755
lines changed

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "feathers-authentication-management",
33
"description": "Adds sign up verification, forgotten password reset, and other capabilities to local feathers-authentication ",
4-
"version": "3.0.0",
4+
"version": "3.0.1",
55
"repository": {
66
"type": "git",
77
"url": "git://github.com/feathers-plus/feathers-authentication-management.git"

src/identity-change.js

+44-44
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
1-
2-
const errors = require('@feathersjs/errors');
3-
const makeDebug = require('debug');
4-
const comparePasswords = require('./helpers/compare-passwords');
5-
const ensureObjPropsValid = require('./helpers/ensure-obj-props-valid');
6-
const getLongToken = require('./helpers/get-long-token');
7-
const getShortToken = require('./helpers/get-short-token');
8-
const getUserData = require('./helpers/get-user-data');
9-
const notifier = require('./helpers/notifier');
10-
11-
const debug = makeDebug('authLocalMgnt:identityChange');
12-
13-
module.exports = identityChange;
14-
15-
async function identityChange (options, identifyUser, password, changesIdentifyUser) {
16-
// note this call does not update the authenticated user info in hooks.params.user.
17-
debug('identityChange', password, changesIdentifyUser);
18-
const usersService = options.app.service(options.service);
19-
const usersServiceIdName = usersService.id;
20-
21-
ensureObjPropsValid(identifyUser, options.identifyUserProps);
22-
ensureObjPropsValid(changesIdentifyUser, options.identifyUserProps);
23-
24-
const users = await usersService.find({ query: identifyUser });
25-
const user1 = getUserData(users);
26-
27-
try {
28-
await comparePasswords(password, user1.password, () => {});
29-
} catch (err) {
30-
throw new errors.BadRequest('Password is incorrect.',
31-
{ errors: { password: 'Password is incorrect.', $className: 'badParams' } }
32-
);
33-
}
34-
35-
const user2 = await usersService.patch(user1[usersServiceIdName], {
36-
verifyExpires: Date.now() + options.delay,
37-
verifyToken: await getLongToken(options.longTokenLen),
38-
verifyShortToken: await getShortToken(options.shortTokenLen, options.shortTokenDigits),
39-
verifyChanges: changesIdentifyUser
40-
});
41-
42-
const user3 = await notifier(options.notifier, 'identityChange', user2, null);
43-
return options.sanitizeUserForClient(user3);
44-
}
1+
2+
const errors = require('@feathersjs/errors');
3+
const makeDebug = require('debug');
4+
const comparePasswords = require('./helpers/compare-passwords');
5+
const ensureObjPropsValid = require('./helpers/ensure-obj-props-valid');
6+
const getLongToken = require('./helpers/get-long-token');
7+
const getShortToken = require('./helpers/get-short-token');
8+
const getUserData = require('./helpers/get-user-data');
9+
const notifier = require('./helpers/notifier');
10+
11+
const debug = makeDebug('authLocalMgnt:identityChange');
12+
13+
module.exports = identityChange;
14+
15+
async function identityChange (options, identifyUser, password, changesIdentifyUser, notifierOptions = {}) {
16+
// note this call does not update the authenticated user info in hooks.params.user.
17+
debug('identityChange', password, changesIdentifyUser);
18+
const usersService = options.app.service(options.service);
19+
const usersServiceIdName = usersService.id;
20+
21+
ensureObjPropsValid(identifyUser, options.identifyUserProps);
22+
ensureObjPropsValid(changesIdentifyUser, options.identifyUserProps);
23+
24+
const users = await usersService.find({ query: identifyUser });
25+
const user1 = getUserData(users);
26+
27+
try {
28+
await comparePasswords(password, user1.password, () => {});
29+
} catch (err) {
30+
throw new errors.BadRequest('Password is incorrect.',
31+
{ errors: { password: 'Password is incorrect.', $className: 'badParams' } }
32+
);
33+
}
34+
35+
const user2 = await usersService.patch(user1[usersServiceIdName], {
36+
verifyExpires: Date.now() + options.delay,
37+
verifyToken: await getLongToken(options.longTokenLen),
38+
verifyShortToken: await getShortToken(options.shortTokenLen, options.shortTokenDigits),
39+
verifyChanges: changesIdentifyUser
40+
});
41+
42+
const user3 = await notifier(options.notifier, 'identityChange', user2, notifierOptions);
43+
return options.sanitizeUserForClient(user3);
44+
}

src/password-change.js

+40-40
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
1-
2-
const errors = require('@feathersjs/errors');
3-
const makeDebug = require('debug');
4-
const comparePasswords = require('./helpers/compare-passwords');
5-
const ensureObjPropsValid = require('./helpers/ensure-obj-props-valid');
6-
const ensureValuesAreStrings = require('./helpers/ensure-values-are-strings');
7-
const getUserData = require('./helpers/get-user-data');
8-
const hashPassword = require('./helpers/hash-password');
9-
const notifier = require('./helpers/notifier');
10-
11-
const debug = makeDebug('authLocalMgnt:passwordChange');
12-
13-
module.exports = passwordChange;
14-
15-
async function passwordChange (options, identifyUser, oldPassword, password, field) {
16-
debug('passwordChange', oldPassword, password);
17-
const usersService = options.app.service(options.service);
18-
const usersServiceIdName = usersService.id;
19-
20-
ensureValuesAreStrings(oldPassword, password);
21-
ensureObjPropsValid(identifyUser, options.identifyUserProps);
22-
23-
const users = await usersService.find({ query: identifyUser });
24-
const user1 = getUserData(users);
25-
26-
try {
27-
await comparePasswords(oldPassword, user1.password, () => { });
28-
} catch (err) {
29-
throw new errors.BadRequest('Current password is incorrect.', {
30-
errors: { oldPassword: 'Current password is incorrect.' }
31-
});
32-
}
33-
34-
const user2 = await usersService.patch(user1[usersServiceIdName], {
35-
password: await hashPassword(options.app, password, field)
36-
});
37-
38-
const user3 = await notifier(options.notifier, 'passwordChange', user2);
39-
return options.sanitizeUserForClient(user3);
40-
}
1+
2+
const errors = require('@feathersjs/errors');
3+
const makeDebug = require('debug');
4+
const comparePasswords = require('./helpers/compare-passwords');
5+
const ensureObjPropsValid = require('./helpers/ensure-obj-props-valid');
6+
const ensureValuesAreStrings = require('./helpers/ensure-values-are-strings');
7+
const getUserData = require('./helpers/get-user-data');
8+
const hashPassword = require('./helpers/hash-password');
9+
const notifier = require('./helpers/notifier');
10+
11+
const debug = makeDebug('authLocalMgnt:passwordChange');
12+
13+
module.exports = passwordChange;
14+
15+
async function passwordChange (options, identifyUser, oldPassword, password, field, notifierOptions = {}) {
16+
debug('passwordChange', oldPassword, password);
17+
const usersService = options.app.service(options.service);
18+
const usersServiceIdName = usersService.id;
19+
20+
ensureValuesAreStrings(oldPassword, password);
21+
ensureObjPropsValid(identifyUser, options.identifyUserProps);
22+
23+
const users = await usersService.find({ query: identifyUser });
24+
const user1 = getUserData(users);
25+
26+
try {
27+
await comparePasswords(oldPassword, user1.password, () => { });
28+
} catch (err) {
29+
throw new errors.BadRequest('Current password is incorrect.', {
30+
errors: { oldPassword: 'Current password is incorrect.' }
31+
});
32+
}
33+
34+
const user2 = await usersService.patch(user1[usersServiceIdName], {
35+
password: await hashPassword(options.app, password, field)
36+
});
37+
38+
const user3 = await notifier(options.notifier, 'passwordChange', user2, notifierOptions);
39+
return options.sanitizeUserForClient(user3);
40+
}

src/reset-password.js

+90-90
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,90 @@
1-
const errors = require('@feathersjs/errors');
2-
const makeDebug = require('debug');
3-
const comparePasswords = require('./helpers/compare-passwords');
4-
const deconstructId = require('./helpers/deconstruct-id');
5-
const ensureObjPropsValid = require('./helpers/ensure-obj-props-valid');
6-
const ensureValuesAreStrings = require('./helpers/ensure-values-are-strings');
7-
const getUserData = require('./helpers/get-user-data');
8-
const hashPassword = require('./helpers/hash-password');
9-
const notifier = require('./helpers/notifier');
10-
11-
const debug = makeDebug('authLocalMgnt:resetPassword');
12-
13-
module.exports = {
14-
resetPwdWithLongToken,
15-
resetPwdWithShortToken
16-
};
17-
18-
async function resetPwdWithLongToken (options, resetToken, password, field) {
19-
ensureValuesAreStrings(resetToken, password);
20-
21-
const result = await resetPassword(options, { resetToken }, { resetToken }, password, field);
22-
return result;
23-
}
24-
25-
async function resetPwdWithShortToken (options, resetShortToken, identifyUser, password, field) {
26-
ensureValuesAreStrings(resetShortToken, password);
27-
ensureObjPropsValid(identifyUser, options.identifyUserProps);
28-
29-
const result = await resetPassword(options, identifyUser, { resetShortToken }, password, field);
30-
return result;
31-
}
32-
33-
async function resetPassword (options, query, tokens, password, field) {
34-
debug('resetPassword', query, tokens, password);
35-
const usersService = options.app.service(options.service);
36-
const usersServiceIdName = usersService.id;
37-
const promises = [];
38-
let users;
39-
40-
if (tokens.resetToken) {
41-
let id = deconstructId(tokens.resetToken);
42-
users = await usersService.get(id);
43-
} else if (tokens.resetShortToken) {
44-
users = await usersService.find({ query });
45-
} else {
46-
throw new errors.BadRequest('resetToken and resetShortToken are missing. (authLocalMgnt)', {
47-
errors: { $className: 'missingToken' }
48-
});
49-
}
50-
51-
const checkProps = options.skipIsVerifiedCheck ? ['resetNotExpired'] : ['resetNotExpired', 'isVerified'];
52-
const user1 = getUserData(users, checkProps);
53-
54-
Object.keys(tokens).forEach(key => {
55-
promises.push(
56-
comparePasswords(
57-
tokens[key],
58-
user1[key],
59-
() =>
60-
new errors.BadRequest('Reset Token is incorrect. (authLocalMgnt)', {
61-
errors: { $className: 'incorrectToken' }
62-
})
63-
)
64-
);
65-
});
66-
67-
try {
68-
await Promise.all(promises);
69-
} catch (err) {
70-
await usersService.patch(user1[usersServiceIdName], {
71-
resetToken: null,
72-
resetShortToken: null,
73-
resetExpires: null
74-
});
75-
76-
throw new errors.BadRequest('Invalid token. Get for a new one. (authLocalMgnt)', {
77-
errors: { $className: 'invalidToken' }
78-
});
79-
}
80-
81-
const user2 = await usersService.patch(user1[usersServiceIdName], {
82-
password: await hashPassword(options.app, password, field),
83-
resetToken: null,
84-
resetShortToken: null,
85-
resetExpires: null
86-
});
87-
88-
const user3 = await notifier(options.notifier, 'resetPwd', user2);
89-
return options.sanitizeUserForClient(user3);
90-
}
1+
const errors = require('@feathersjs/errors');
2+
const makeDebug = require('debug');
3+
const comparePasswords = require('./helpers/compare-passwords');
4+
const deconstructId = require('./helpers/deconstruct-id');
5+
const ensureObjPropsValid = require('./helpers/ensure-obj-props-valid');
6+
const ensureValuesAreStrings = require('./helpers/ensure-values-are-strings');
7+
const getUserData = require('./helpers/get-user-data');
8+
const hashPassword = require('./helpers/hash-password');
9+
const notifier = require('./helpers/notifier');
10+
11+
const debug = makeDebug('authLocalMgnt:resetPassword');
12+
13+
module.exports = {
14+
resetPwdWithLongToken,
15+
resetPwdWithShortToken
16+
};
17+
18+
async function resetPwdWithLongToken (options, resetToken, password, field, notifierOptions = {}) {
19+
ensureValuesAreStrings(resetToken, password);
20+
21+
const result = await resetPassword(options, { resetToken }, { resetToken }, password, field, notifierOptions);
22+
return result;
23+
}
24+
25+
async function resetPwdWithShortToken (options, resetShortToken, identifyUser, password, field, notifierOptions = {}) {
26+
ensureValuesAreStrings(resetShortToken, password);
27+
ensureObjPropsValid(identifyUser, options.identifyUserProps);
28+
29+
const result = await resetPassword(options, identifyUser, { resetShortToken }, password, field, notifierOptions);
30+
return result;
31+
}
32+
33+
async function resetPassword (options, query, tokens, password, field, notifierOptions = {}) {
34+
debug('resetPassword', query, tokens, password);
35+
const usersService = options.app.service(options.service);
36+
const usersServiceIdName = usersService.id;
37+
const promises = [];
38+
let users;
39+
40+
if (tokens.resetToken) {
41+
let id = deconstructId(tokens.resetToken);
42+
users = await usersService.get(id);
43+
} else if (tokens.resetShortToken) {
44+
users = await usersService.find({ query });
45+
} else {
46+
throw new errors.BadRequest('resetToken and resetShortToken are missing. (authLocalMgnt)', {
47+
errors: { $className: 'missingToken' }
48+
});
49+
}
50+
51+
const checkProps = options.skipIsVerifiedCheck ? ['resetNotExpired'] : ['resetNotExpired', 'isVerified'];
52+
const user1 = getUserData(users, checkProps);
53+
54+
Object.keys(tokens).forEach(key => {
55+
promises.push(
56+
comparePasswords(
57+
tokens[key],
58+
user1[key],
59+
() =>
60+
new errors.BadRequest('Reset Token is incorrect. (authLocalMgnt)', {
61+
errors: { $className: 'incorrectToken' }
62+
})
63+
)
64+
);
65+
});
66+
67+
try {
68+
await Promise.all(promises);
69+
} catch (err) {
70+
await usersService.patch(user1[usersServiceIdName], {
71+
resetToken: null,
72+
resetShortToken: null,
73+
resetExpires: null
74+
});
75+
76+
throw new errors.BadRequest('Invalid token. Get for a new one. (authLocalMgnt)', {
77+
errors: { $className: 'invalidToken' }
78+
});
79+
}
80+
81+
const user2 = await usersService.patch(user1[usersServiceIdName], {
82+
password: await hashPassword(options.app, password, field),
83+
resetToken: null,
84+
resetShortToken: null,
85+
resetExpires: null
86+
});
87+
88+
const user3 = await notifier(options.notifier, 'resetPwd', user2, notifierOptions);
89+
return options.sanitizeUserForClient(user3);
90+
}

0 commit comments

Comments
 (0)