Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass notifierOptions every were that notifier is called. #155

Merged
merged 4 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 44 additions & 44 deletions src/identity-change.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
const errors = require('@feathersjs/errors');
const makeDebug = require('debug');
const comparePasswords = require('./helpers/compare-passwords');
const ensureObjPropsValid = require('./helpers/ensure-obj-props-valid');
const getLongToken = require('./helpers/get-long-token');
const getShortToken = require('./helpers/get-short-token');
const getUserData = require('./helpers/get-user-data');
const notifier = require('./helpers/notifier');
const debug = makeDebug('authLocalMgnt:identityChange');
module.exports = identityChange;
async function identityChange (options, identifyUser, password, changesIdentifyUser) {
// note this call does not update the authenticated user info in hooks.params.user.
debug('identityChange', password, changesIdentifyUser);
const usersService = options.app.service(options.service);
const usersServiceIdName = usersService.id;
ensureObjPropsValid(identifyUser, options.identifyUserProps);
ensureObjPropsValid(changesIdentifyUser, options.identifyUserProps);
const users = await usersService.find({ query: identifyUser });
const user1 = getUserData(users);
try {
await comparePasswords(password, user1.password, () => {});
} catch (err) {
throw new errors.BadRequest('Password is incorrect.',
{ errors: { password: 'Password is incorrect.', $className: 'badParams' } }
);
}
const user2 = await usersService.patch(user1[usersServiceIdName], {
verifyExpires: Date.now() + options.delay,
verifyToken: await getLongToken(options.longTokenLen),
verifyShortToken: await getShortToken(options.shortTokenLen, options.shortTokenDigits),
verifyChanges: changesIdentifyUser
});
const user3 = await notifier(options.notifier, 'identityChange', user2, null);
return options.sanitizeUserForClient(user3);
}

const errors = require('@feathersjs/errors');
const makeDebug = require('debug');
const comparePasswords = require('./helpers/compare-passwords');
const ensureObjPropsValid = require('./helpers/ensure-obj-props-valid');
const getLongToken = require('./helpers/get-long-token');
const getShortToken = require('./helpers/get-short-token');
const getUserData = require('./helpers/get-user-data');
const notifier = require('./helpers/notifier');

const debug = makeDebug('authLocalMgnt:identityChange');

module.exports = identityChange;

async function identityChange (options, identifyUser, password, changesIdentifyUser, notifierOptions = {}) {
// note this call does not update the authenticated user info in hooks.params.user.
debug('identityChange', password, changesIdentifyUser);
const usersService = options.app.service(options.service);
const usersServiceIdName = usersService.id;

ensureObjPropsValid(identifyUser, options.identifyUserProps);
ensureObjPropsValid(changesIdentifyUser, options.identifyUserProps);

const users = await usersService.find({ query: identifyUser });
const user1 = getUserData(users);

try {
await comparePasswords(password, user1.password, () => {});
} catch (err) {
throw new errors.BadRequest('Password is incorrect.',
{ errors: { password: 'Password is incorrect.', $className: 'badParams' } }
);
}

const user2 = await usersService.patch(user1[usersServiceIdName], {
verifyExpires: Date.now() + options.delay,
verifyToken: await getLongToken(options.longTokenLen),
verifyShortToken: await getShortToken(options.shortTokenLen, options.shortTokenDigits),
verifyChanges: changesIdentifyUser
});

const user3 = await notifier(options.notifier, 'identityChange', user2, notifierOptions);
return options.sanitizeUserForClient(user3);
}
80 changes: 40 additions & 40 deletions src/password-change.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
const errors = require('@feathersjs/errors');
const makeDebug = require('debug');
const comparePasswords = require('./helpers/compare-passwords');
const ensureObjPropsValid = require('./helpers/ensure-obj-props-valid');
const ensureValuesAreStrings = require('./helpers/ensure-values-are-strings');
const getUserData = require('./helpers/get-user-data');
const hashPassword = require('./helpers/hash-password');
const notifier = require('./helpers/notifier');
const debug = makeDebug('authLocalMgnt:passwordChange');
module.exports = passwordChange;
async function passwordChange (options, identifyUser, oldPassword, password, field) {
debug('passwordChange', oldPassword, password);
const usersService = options.app.service(options.service);
const usersServiceIdName = usersService.id;
ensureValuesAreStrings(oldPassword, password);
ensureObjPropsValid(identifyUser, options.identifyUserProps);
const users = await usersService.find({ query: identifyUser });
const user1 = getUserData(users);
try {
await comparePasswords(oldPassword, user1.password, () => { });
} catch (err) {
throw new errors.BadRequest('Current password is incorrect.', {
errors: { oldPassword: 'Current password is incorrect.' }
});
}
const user2 = await usersService.patch(user1[usersServiceIdName], {
password: await hashPassword(options.app, password, field)
});
const user3 = await notifier(options.notifier, 'passwordChange', user2);
return options.sanitizeUserForClient(user3);
}

const errors = require('@feathersjs/errors');
const makeDebug = require('debug');
const comparePasswords = require('./helpers/compare-passwords');
const ensureObjPropsValid = require('./helpers/ensure-obj-props-valid');
const ensureValuesAreStrings = require('./helpers/ensure-values-are-strings');
const getUserData = require('./helpers/get-user-data');
const hashPassword = require('./helpers/hash-password');
const notifier = require('./helpers/notifier');

const debug = makeDebug('authLocalMgnt:passwordChange');

module.exports = passwordChange;

async function passwordChange (options, identifyUser, oldPassword, password, field, notifierOptions = {}) {
debug('passwordChange', oldPassword, password);
const usersService = options.app.service(options.service);
const usersServiceIdName = usersService.id;

ensureValuesAreStrings(oldPassword, password);
ensureObjPropsValid(identifyUser, options.identifyUserProps);

const users = await usersService.find({ query: identifyUser });
const user1 = getUserData(users);

try {
await comparePasswords(oldPassword, user1.password, () => { });
} catch (err) {
throw new errors.BadRequest('Current password is incorrect.', {
errors: { oldPassword: 'Current password is incorrect.' }
});
}

const user2 = await usersService.patch(user1[usersServiceIdName], {
password: await hashPassword(options.app, password, field)
});

const user3 = await notifier(options.notifier, 'passwordChange', user2, notifierOptions);
return options.sanitizeUserForClient(user3);
}
180 changes: 90 additions & 90 deletions src/reset-password.js
Original file line number Diff line number Diff line change
@@ -1,90 +1,90 @@
const errors = require('@feathersjs/errors');
const makeDebug = require('debug');
const comparePasswords = require('./helpers/compare-passwords');
const deconstructId = require('./helpers/deconstruct-id');
const ensureObjPropsValid = require('./helpers/ensure-obj-props-valid');
const ensureValuesAreStrings = require('./helpers/ensure-values-are-strings');
const getUserData = require('./helpers/get-user-data');
const hashPassword = require('./helpers/hash-password');
const notifier = require('./helpers/notifier');
const debug = makeDebug('authLocalMgnt:resetPassword');
module.exports = {
resetPwdWithLongToken,
resetPwdWithShortToken
};
async function resetPwdWithLongToken (options, resetToken, password, field) {
ensureValuesAreStrings(resetToken, password);
const result = await resetPassword(options, { resetToken }, { resetToken }, password, field);
return result;
}
async function resetPwdWithShortToken (options, resetShortToken, identifyUser, password, field) {
ensureValuesAreStrings(resetShortToken, password);
ensureObjPropsValid(identifyUser, options.identifyUserProps);
const result = await resetPassword(options, identifyUser, { resetShortToken }, password, field);
return result;
}
async function resetPassword (options, query, tokens, password, field) {
debug('resetPassword', query, tokens, password);
const usersService = options.app.service(options.service);
const usersServiceIdName = usersService.id;
const promises = [];
let users;
if (tokens.resetToken) {
let id = deconstructId(tokens.resetToken);
users = await usersService.get(id);
} else if (tokens.resetShortToken) {
users = await usersService.find({ query });
} else {
throw new errors.BadRequest('resetToken and resetShortToken are missing. (authLocalMgnt)', {
errors: { $className: 'missingToken' }
});
}
const checkProps = options.skipIsVerifiedCheck ? ['resetNotExpired'] : ['resetNotExpired', 'isVerified'];
const user1 = getUserData(users, checkProps);
Object.keys(tokens).forEach(key => {
promises.push(
comparePasswords(
tokens[key],
user1[key],
() =>
new errors.BadRequest('Reset Token is incorrect. (authLocalMgnt)', {
errors: { $className: 'incorrectToken' }
})
)
);
});
try {
await Promise.all(promises);
} catch (err) {
await usersService.patch(user1[usersServiceIdName], {
resetToken: null,
resetShortToken: null,
resetExpires: null
});
throw new errors.BadRequest('Invalid token. Get for a new one. (authLocalMgnt)', {
errors: { $className: 'invalidToken' }
});
}
const user2 = await usersService.patch(user1[usersServiceIdName], {
password: await hashPassword(options.app, password, field),
resetToken: null,
resetShortToken: null,
resetExpires: null
});
const user3 = await notifier(options.notifier, 'resetPwd', user2);
return options.sanitizeUserForClient(user3);
}
const errors = require('@feathersjs/errors');
const makeDebug = require('debug');
const comparePasswords = require('./helpers/compare-passwords');
const deconstructId = require('./helpers/deconstruct-id');
const ensureObjPropsValid = require('./helpers/ensure-obj-props-valid');
const ensureValuesAreStrings = require('./helpers/ensure-values-are-strings');
const getUserData = require('./helpers/get-user-data');
const hashPassword = require('./helpers/hash-password');
const notifier = require('./helpers/notifier');

const debug = makeDebug('authLocalMgnt:resetPassword');

module.exports = {
resetPwdWithLongToken,
resetPwdWithShortToken
};

async function resetPwdWithLongToken (options, resetToken, password, field, notifierOptions = {}) {
ensureValuesAreStrings(resetToken, password);

const result = await resetPassword(options, { resetToken }, { resetToken }, password, field, notifierOptions);
return result;
}

async function resetPwdWithShortToken (options, resetShortToken, identifyUser, password, field, notifierOptions = {}) {
ensureValuesAreStrings(resetShortToken, password);
ensureObjPropsValid(identifyUser, options.identifyUserProps);

const result = await resetPassword(options, identifyUser, { resetShortToken }, password, field, notifierOptions);
return result;
}

async function resetPassword (options, query, tokens, password, field, notifierOptions = {}) {
debug('resetPassword', query, tokens, password);
const usersService = options.app.service(options.service);
const usersServiceIdName = usersService.id;
const promises = [];
let users;

if (tokens.resetToken) {
let id = deconstructId(tokens.resetToken);
users = await usersService.get(id);
} else if (tokens.resetShortToken) {
users = await usersService.find({ query });
} else {
throw new errors.BadRequest('resetToken and resetShortToken are missing. (authLocalMgnt)', {
errors: { $className: 'missingToken' }
});
}

const checkProps = options.skipIsVerifiedCheck ? ['resetNotExpired'] : ['resetNotExpired', 'isVerified'];
const user1 = getUserData(users, checkProps);

Object.keys(tokens).forEach(key => {
promises.push(
comparePasswords(
tokens[key],
user1[key],
() =>
new errors.BadRequest('Reset Token is incorrect. (authLocalMgnt)', {
errors: { $className: 'incorrectToken' }
})
)
);
});

try {
await Promise.all(promises);
} catch (err) {
await usersService.patch(user1[usersServiceIdName], {
resetToken: null,
resetShortToken: null,
resetExpires: null
});

throw new errors.BadRequest('Invalid token. Get for a new one. (authLocalMgnt)', {
errors: { $className: 'invalidToken' }
});
}

const user2 = await usersService.patch(user1[usersServiceIdName], {
password: await hashPassword(options.app, password, field),
resetToken: null,
resetShortToken: null,
resetExpires: null
});

const user3 = await notifier(options.notifier, 'resetPwd', user2, notifierOptions);
return options.sanitizeUserForClient(user3);
}
Loading