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

fix: hash the password #37

Merged
merged 1 commit into from
Jan 31, 2019
Merged

fix: hash the password #37

merged 1 commit into from
Jan 31, 2019

Conversation

jannyHou
Copy link
Contributor

@jannyHou jannyHou commented Jan 30, 2019

Description

Fix the accesstoken generation function by checking the password instead of sending the plain password.

A follow up PR for #26
And connect to the first acceptance criteria in #35

  • Local test passes

@jannyHou jannyHou force-pushed the fix/hashpass branch 2 times, most recently from e84f4f2 to 6d993b0 Compare January 30, 2019 21:22
@@ -18,11 +20,21 @@ export async function getAccessTokenForUser(
userRepository: UserRepository,
credentials: Credentials,
): Promise<string> {
console.log('getAccessTokenForUser', credentials.password);
Copy link
Contributor

Choose a reason for hiding this comment

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

It should be removed.

before('create new user', async () => {
plainPassword = user.password;
// Salt + Hash Password
user.password = await hashAsync(user.password, 10);
Copy link
Contributor

Choose a reason for hiding this comment

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

Should it be the responsibility of the controller/repository to hash passwords?

Copy link
Contributor

Choose a reason for hiding this comment

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

@jannyHou and I were talking about a hash utility function which can be called from the user repo or controller. I think it makes sense for the repository to call it when storing user passwords in the backend, but I see how that can be delegated to a controller too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@raymondfeng @b-admike My opinion is unless we define a specific repository interface for model User and add a function for the hashing purpose, I tend to put the logic in controller. And as @b-admike said, we can extract it into utility/service that can be called from the controller.
Like what we have in authentication utils.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And I plan to refactor them into the util in the next PR, this one focus on fixing the plain password.

Explained in the PR description :)

A follow up PR for #26
And connect to the first acceptance criteria in #35 (there will be another PR after it to refactor the hash utils into a proper place.)

Copy link
Member

Choose a reason for hiding this comment

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

@jannyHou Please do extract this code into utility/service that can be called from multiple places. For example tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bajtos See the refactor PR #41

});
if (!foundUser) {
throw new HttpErrors.Unauthorized('Wrong credentials!');
throw new HttpErrors.Unauthorized(
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we throw a 404 here or would that be a security risk?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@b-admike Good point, I think 404 makes more sense.

foundUser.password,
);
if (!passwordMatched) {
throw new HttpErrors.Unauthorized('The credential is not correct!');
Copy link
Contributor

Choose a reason for hiding this comment

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

nitpick: I would rephrase this to The credentials are not correct.

Copy link
Contributor

@b-admike b-admike left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@@ -20,21 +20,21 @@ export async function getAccessTokenForUser(
userRepository: UserRepository,
credentials: Credentials,
): Promise<string> {
console.log('getAccessTokenForUser', credentials.password);
console.log(credentials.email);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we remove the console.log altogether here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good catch removed and squashed into one commit.

Signed-off-by: jannyHou <[email protected]>
});

async function createUser() {
user.password = await hashAsync(user.password, 10);
Copy link
Member

Choose a reason for hiding this comment

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

@jannyHou
In the test suite, please use the smallest number that's still supported by bcrypt - we use 4 in LoopBack 3.x

The higher the number is, the longer it takes to compute the hash. Slow hash computation is good in production as it makes brute-force attacks difficult, but also impractical in tests because it can significantly slow down the test suite. We have been through this in LoopBack 3.x, let's not repeat that mistake again.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bajtos Thanks for sharing the experience and explain the reason behind using the smallest number!

In the test suite, please use the smallest number that's still supported by bcrypt

Will do it in the next PR that extracts hash functions into utils.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bajtos bajtos deleted the fix/hashpass branch February 1, 2019 06:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants