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

chore: dont leak password from server #741

Merged
merged 4 commits into from
Oct 14, 2024
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
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();
JamieSlome marked this conversation as resolved.
Show resolved Hide resolved
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
Loading