Skip to content

Commit 6a3db1b

Browse files
committed
Merge pull request #32 from seanjohnite/sanitize3
add sanitize method
2 parents 90f768e + 30b778c commit 6a3db1b

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

generated/server/app/configure/authentication/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ module.exports = function (app) {
4747
// logged in already.
4848
app.get('/session', function (req, res) {
4949
if (req.user) {
50-
res.send({ user: _.omit(req.user.toJSON(), ['salt', 'password']) });
50+
res.send({ user: req.user.sanitize() });
5151
} else {
5252
res.status(401).send('No authenticated user.');
5353
}

generated/server/app/configure/authentication/local.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ module.exports = function (app) {
4444
if (loginErr) return next(loginErr);
4545
// We respond with a response object that has user with _id and email.
4646
res.status(200).send({
47-
user: _.omit(user.toJSON(), ['password', 'salt'])
47+
user: user.sanitize()
4848
});
4949
});
5050

generated/server/db/models/user.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ var schema = new mongoose.Schema({
2626
}
2727
});
2828

29+
// method to remove sensitive information from user objects before sending them out
30+
schema.methods.sanitize = function () {
31+
return _.omit(this.toJSON(), ['password', 'salt']);
32+
};
33+
2934
// generateSalt, encryptPassword and the pre 'save' and 'correctPassword' operations
3035
// are all used for local authentication security.
3136
var generateSalt = function () {

generated/tests/server/models/user-test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,23 @@ describe('User model', function () {
142142

143143
});
144144

145+
describe('sanitize method', function () {
146+
147+
var createUser = function () {
148+
return User.create({ email: '[email protected]', password: 'potus' });
149+
};
150+
151+
it('should remove sensitive information from a user object', function () {
152+
createUser().then(function (user) {
153+
var sanitizedUser = user.sanitize();
154+
expect(user.password).to.be.ok;
155+
expect(user.salt).to.be.ok;
156+
expect(sanitizedUser.password).to.be.undefined;
157+
expect(sanitizedUser.salt).to.be.undefined;
158+
});
159+
});
160+
});
161+
145162
});
146163

147164
});

0 commit comments

Comments
 (0)