Skip to content

Commit

Permalink
Merge pull request #741 from lwhiteley/dont-leak-password
Browse files Browse the repository at this point in the history
chore: dont leak password from server
  • Loading branch information
JamieSlome authored Oct 14, 2024
2 parents ce03d71 + eecd5ce commit a1d8f42
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/db/mongo/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ exports.findUser = async function (username) {
exports.getUsers = async function (query) {
console.log(`Getting users for query= ${JSON.stringify(query)}`);
const collection = await connect(usersCollection);
return collection.find(query).toArray();
return collection.find(query, { password: 0 }).toArray();
};

exports.deleteUser = async function (username) {
Expand Down
15 changes: 9 additions & 6 deletions src/service/routes/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,22 @@ router.get('/', (req, res) => {

router.post('/login', passport.authenticate(passportType), async (req, res) => {
try {
const currentUser = { ...req.user };
delete currentUser.password;
console.log(
`serivce.routes.auth.login: user logged in, username=${
req.user.username
} profile=${JSON.stringify(req.user)}`,
currentUser.username
} profile=${JSON.stringify(currentUser)}`,
);
res.send({
message: 'success',
user: currentUser,
});
} catch (e) {
console.log(`service.routes.auth.login: Error logging user in ${JSON.stringify(e)}`);
res.status(500).send('Failed to login').end();
return;
}
res.send({
message: 'success',
user: req.user,
});
});

// when login is successful, retrieve user info
Expand Down Expand Up @@ -115,6 +117,7 @@ router.get('/userLoggedIn', async (req, res) => {
delete user.password;
const login = user.username;
const userVal = await db.findUser(login);
delete userVal.password;
res.send(userVal);
} else {
res.status(401).end();
Expand Down
5 changes: 5 additions & 0 deletions test/testLogin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ describe('auth', async () => {
});
});

it('should now be able to access the user login metadata', async function () {
const res = await chai.request(app).get('/api/auth/userLoggedIn').set('Cookie', `${cookie}`);
res.should.have.status(200);
});

it('should now be able to access the profile', async function () {
const res = await chai.request(app).get('/api/auth/profile').set('Cookie', `${cookie}`);
res.should.have.status(200);
Expand Down

0 comments on commit a1d8f42

Please sign in to comment.