Skip to content

refactor(model:user): exclude password info by default #1620

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

Merged
merged 1 commit into from
Feb 9, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
27 changes: 4 additions & 23 deletions app/templates/server/api/user(auth)/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,8 @@ function handleError(res, statusCode) {
* restriction: 'admin'
*/
export function index(req, res) {
<% if (filters.mongooseModels) { %>User.findAsync({}, '-salt -password')<% }
if (filters.sequelizeModels) { %>User.findAll({
attributes: [
'_id',
'name',
'email',
'role',
'provider'
]
})<% } %>
<% if (filters.mongooseModels) { %>User.findAsync()<% }
if (filters.sequelizeModels) { %>User.findAll()<% } %>
.then(users => {
res.status(200).json(users);
})
Expand Down Expand Up @@ -132,19 +124,8 @@ export function changePassword(req, res, next) {
export function me(req, res, next) {
var userId = req.user._id;

<% if (filters.mongooseModels) { %>User.findOneAsync({ _id: userId }, '-salt -password')<% }
if (filters.sequelizeModels) { %>User.find({
where: {
_id: userId
},
attributes: [
'_id',
'name',
'email',
'role',
'provider'
]
})<% } %>
<% if (filters.mongooseModels) { %>User.findOneAsync({ _id: userId })<% }
if (filters.sequelizeModels) { %>User.find({ where: { _id: userId } })<% } %>
.then(user => { // don't ever give out the password or salt
if (!user) {
return res.status(401).end();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ var UserSchema = new Schema({
type: String,
default: 'user'
},
password: String,
password: {type: String, select: false},
provider: String,
salt: String<% if (filters.oauth) { %>,<% if (filters.facebookAuth) { %>
salt: {type: String, select: false}<% if (filters.oauth) { %>,<% if (filters.facebookAuth) { %>
facebook: {},<% } %><% if (filters.twitterAuth) { %>
twitter: {},<% } %><% if (filters.googleAuth) { %>
google: {},<% } %>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

import _ from 'lodash';
import crypto from 'crypto';<% if (filters.oauth) { %>
var authTypes = ['github', 'twitter', 'facebook', 'google'];<% } %>

Expand Down Expand Up @@ -228,6 +229,12 @@ module.exports = function(sequelize, DataTypes) {
} else {
fn(null);
}
},

toJSON: function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var excludedAttributes = ['salt', 'password'];

return _.omit(this.dataValues, excludedAttributes);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ describe('User Model', function() {
return user.saveAsync();
});

it('should exclude salt and hashedPassword by default', function() {
User.find({name: user.name}, function(err, _user) {
_user.should.not.have.property('salt');
_user.should.not.have.property('hashedPassword');
});
});

it('should authenticate user if valid', function() {
<%= expect() %>user.authenticate('password')<%= to() %>.be.true;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ describe('User Model', function() {
return user.save();
});

it('should exclude salt and hashedPassword by default', function() {
User.find({name: user.name}, function(err, _user) {
_user.should.not.have.property('salt');
_user.should.not.have.property('hashedPassword');
});
});

it('should authenticate user if valid', function() {
<%= expect() %>user.authenticate('password')<%= to() %>.be.true;
});
Expand Down
4 changes: 2 additions & 2 deletions app/templates/server/auth(auth)/local/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {Strategy as LocalStrategy} from 'passport-local';
function localAuthenticate(User, email, password, done) {
<% if (filters.mongooseModels) { %>User.findOneAsync({
email: email.toLowerCase()
})<% }
if (filters.sequelizeModels) { %>User.find({
}, '+salt +hashedPassword')<% }
if (filters.sequelizeModels) { %>User.unscoped().find({
where: {
email: email.toLowerCase()
}
Expand Down