-
Notifications
You must be signed in to change notification settings - Fork 21.8k
Allow configurable attribute name on #has_secure_password
#26764
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
Allow configurable attribute name on #has_secure_password
#26764
Conversation
existing `#has_secure_password`. This can be useful when one would like to store some secure field as a digest, just like a password. The method still defaults to `password`. It now also allows using the same `#authenticate` method which now accepts a second argument for specifying the attribute to be authenticated, or will default to 'password`. A new method is also added for generating a new token for an attribute by calling `#regenerate_XXXX` where `XXXX` is the attribute name.
Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @matthewd (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review. Please see the contribution instructions for more information. |
|
||
def password_confirmation=(unencrypted_password) | ||
@password_confirmation = unencrypted_password | ||
def authenticate(unencrypted_password, attribute = :password) |
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.
Let's call this authenticate_#{attribute}
(with an alias when attribute=='password')
# user.password = nil | ||
# user.password_digest # => nil | ||
# user.password = 'mUc3m00RsqyRe' | ||
# user.password_digest # => "$2a$10$4LEA7r4YmNHtvlAvHhsYAeZmk/xeUVtMTYqwIvYY76EW5GUqDiP4." |
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.
We're losing this documentation. Maybe we should push it up to the module on a fake password=
attribute... or otherwise, we should merge it into the has_secure_password
documentation. Either way, it's no good here.
|
||
define_method("regenerate_#{attribute}") do | ||
self.send("#{attribute}=", self.class.generate_unique_secure_token) | ||
end |
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.
We still don't need this.
This change now creates a method `#authenticate_XXX` where XXX is the configured attribute name on `#has_secure_password`. `#authenticate` is now an alias to this method when the attribute name is the default 'password'
#has_secure_password
@matthewd Any further feedback/changes I should look into? |
Any update on this? Would be super nice to be able to have this for other columns on models that require a digest hash of something other than |
@matthewd do you have any direction/feedback in order for us to get this through? @NomNomCameron This has been silent for the past year, I ended up just adding the functionality into the my own projects manually by adding this updated /active_model/secure_password.rb |
👍 for getting this through. I'd love this feature. |
Is there any plans on merging this PR ? People really need it. |
Allow configurable attribute name on `#has_secure_password`
`has_secure_password` allows configuring name of attribute since rails#26764. This commit adds a mention about it in the Active Model Basics Guide.
Summary
This addition will now allow configuring an attribute name for the
existing
#has_secure_password
method. This can be useful when one wouldlike to store some secure field as a digest, just like a password.
This also creates a method
#authenticate_XXX
where XXX isthe configured attribute name on
#has_secure_password
.#authenticate
is now an alias to this method when the attribute name is the default
'password'.