-
Notifications
You must be signed in to change notification settings - Fork 970
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
feat(hash): Add PBKDF2 as password hashing algorithm #1774
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks pretty good already. I am not sure though if this is the intended way discussed in the issue. This does not allow to update imported hashes but just adds PBKDF2 as an additional hasher. The password upgrade process might actually be implemented on top, so we might actually need it as is.
We could have something along the lines of
hashers:
bcrypt:
cost: 10
pbkdf2:
upgrade_to: bcrypt
in the config, which would mean that all pbkdf2 hashes will be upgraded once checked.
@zepatrik Thank you for your super fast review! And I remove configuration for PBKDF2 because we can upgrade verified and not primary hashing algorithm automatically. |
Codecov Report
@@ Coverage Diff @@
## master #1774 +/- ##
==========================================
- Coverage 74.85% 74.16% -0.70%
==========================================
Files 291 267 -24
Lines 14963 13242 -1721
==========================================
- Hits 11201 9821 -1380
+ Misses 2960 2757 -203
+ Partials 802 664 -138
Continue to review full report at Codecov.
|
cd3518a
to
c7beb57
Compare
Added tests. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking pretty good now 👍
One big thing missing now is documentation. I am also not 💯 sure how that should look like, maybe you can start and we discuss it then?
@@ -68,6 +71,12 @@ func (s *Strategy) Login(w http.ResponseWriter, r *http.Request, f *login.Flow) | |||
return nil, s.handleLoginError(w, r, f, &p, errors.WithStack(schema.NewInvalidCredentialsError())) | |||
} | |||
|
|||
if !s.d.Hasher().IsSameAlgorithm([]byte(o.HashedPassword)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check does not make sense here. The clear-text password does not contain the algorithm. - you fixed that already 😉
In the long term, we have to make it configurable which hash alg should be upgraded to which one. And some, like PBKDF2, will just be upgraded by default, as they are only intended for import.
For this PR it would be sufficient to just auto-upgrade PBKDF2 without a config option. So adding a check here that returns whether a hash is auto-upgrade (currently only PBKDF2), should be the way to go IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made it configurable!
One big thing missing now is documentation
Hmm... currently, Kratos doesn't have ability to import user credentials now.
So we have 2 choices to document currently.
- Document "Wait to resolve Add ability to import user credentials #605 " 323fdc3
- Implement for Add ability to import user credentials #605 in thi PR
Which do you think better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both are nice :) It would be awesome to be able to import users - this has been something the community has asked for for a long time. However, I think it's fine with documenting that this is coming in the future for now in this PR
What's the point of the |
Sounds reasonable to me, only thing would be that you might want to specify a per-alg migrate flag. hashers:
algorithm: bcrypt
bcrypt:
cost: 10
pbkdf2:
migrate: true
argon2:
migrate: false which would keep argon2 hashes, but migrate pbkdf2. Not sure if this is actually a use case, but I can imagine you could safe some resources with that strategy. But a general migrate flag is probably good for now. |
I would enforce migration. We don't want to stick with old PKBDF2 hashes in the DB forever but slowly migrate everyone over :) |
I also think we have to force passwords migration, maybe even do not provide an option not to migrate. As I separate topic, I wanted to discuss a format of hash. Because we already use the PHC standard for password hashes, we have to stick with it https://github.com/P-H-C/phc-string-format/blob/master/phc-sf-spec.md. So hash for this algorithm should have a format:
Example here https://www.npmjs.com/package/@phc/pbkdf2#phc-string-format |
Ok, that makes a ton of sense @seremenko-wish - thank you for the input |
For now, I want to clarify what is configurable to enforce migration. hashers:
algorithm: bcrypt
bcrypt:
cost: 10
# Cannot configure for pbkdf2. Auto-upgrade to primary algorithm.
#pbkdf2:
# migrate: true
argon2:
migrate: false # Is this configurable like this, leave it or auto-upgrade to primary algorithm? |
I would remove the migrate option. We always migrate to the globally configured hasher (
|
bc887f9
to
713b1c3
Compare
I fixed to always migrate to the globally configured hasher. |
b2555e3
to
2b97863
Compare
Resolved conflict. |
Awesome, I will take a look at this now! |
@sawadashota what do you think about this comment from @seremenko-wish ?
I think it would make sense to follow the PHC format here. |
I think it makes sense but I have a small question about this format Referring to RFC2898, I expect |
@seremenko-wish maybe you can chime in here? |
@sawadashota I see that number of iterations is defined as
https://passlib.readthedocs.io/en/stable/lib/passlib.hash.pbkdf2_digest.html#passlib.hash.pbkdf2_sha1 does not use named parameters at all :/ As there is no defined standard for this specific algorithm, it would be up to you to decide what named parameter to use for iterations. |
As far as I understand, the RFC itself does not touch on the PHC format. Might make sense though to use I also did some research and found that Auth0 uses the format from https://github.com/simonepri/phc-pbkdf2#phc-string-format . I assume Auth0 probably uses that library internally. I also found a few more docs but unfortunately no concrete guidance:
So yeah basically the question is:
|
Signed-off-by: sawadashota <[email protected]>
Signed-off-by: sawadashota <[email protected]>
Signed-off-by: sawadashota <[email protected]>
Signed-off-by: sawadashota <[email protected]>
Signed-off-by: sawadashota <[email protected]>
Signed-off-by: sawadashota <[email protected]>
Signed-off-by: sawadashota <[email protected]>
18891e3
to
30b1b5c
Compare
@aeneasr Could you review PR? |
It's part of 0.8.0 now so due to be released before the ory summit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks ready to be merged from a code level! Just one thing :)
- Could you please add docs to e.g. https://www.ory.sh/kratos/docs/next/concepts/credentials/username-email-password and explain how we expect PKBDF2 hashes to look like and what the parameters mean?
@@ -68,6 +71,12 @@ func (s *Strategy) Login(w http.ResponseWriter, r *http.Request, f *login.Flow) | |||
return nil, s.handleLoginError(w, r, f, &p, errors.WithStack(schema.NewInvalidCredentialsError())) | |||
} | |||
|
|||
if !s.d.Hasher().IsSameAlgorithm([]byte(o.HashedPassword)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both are nice :) It would be awesome to be able to import users - this has been something the community has asked for for a long time. However, I think it's fine with documenting that this is coming in the future for now in this PR
Failing e2e tests are probably my fault |
Signed-off-by: sawadashota <[email protected]>
Signed-off-by: sawadashota <[email protected]>
Signed-off-by: sawadashota <[email protected]>
Added Hashed Password Format section to https://www.ory.sh/kratos/docs/next/concepts/credentials/username-email-password ! |
Signed-off-by: sawadashota <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job! Thank you! :)
Related issue(s)
#1659
Checklist
contributing code guidelines.
vulnerability. If this pull request addresses a security. vulnerability, I
confirm that I got green light (please contact
[email protected]) from the maintainers to push
the changes.
works.
Further Comments