Skip to content

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

Merged
merged 2 commits into from
Jun 28, 2018
Merged

Allow configurable attribute name on #has_secure_password #26764

merged 2 commits into from
Jun 28, 2018

Conversation

choncou
Copy link

@choncou choncou commented Oct 11, 2016

Summary

This addition will now allow configuring an attribute name for the
existing #has_secure_password method. This can be useful when one would
like to store some secure field as a digest, just like a password.

This also 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'.

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.
@rails-bot
Copy link

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)
Copy link
Member

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."
Copy link
Member

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
Copy link
Member

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'
@choncou choncou changed the title Allow configurable attribute name on `#has_secure_passwors Allow configurable attribute name on #has_secure_password Oct 12, 2016
@choncou
Copy link
Author

choncou commented Oct 29, 2016

@matthewd Any further feedback/changes I should look into?

@NomNomCameron
Copy link

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 password

@choncou
Copy link
Author

choncou commented Oct 18, 2017

@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

@alexstophel
Copy link

👍 for getting this through. I'd love this feature.

@shideneyu
Copy link

Is there any plans on merging this PR ? People really need it.

@rafaelfranca rafaelfranca merged commit 9b63bf1 into rails:master Jun 28, 2018
rafaelfranca added a commit that referenced this pull request Jun 28, 2018
Allow configurable attribute name on `#has_secure_password`
bogdanvlviv added a commit to bogdanvlviv/rails that referenced this pull request Jul 6, 2018
`has_secure_password` allows configuring name of attribute since rails#26764.
This commit adds a mention about it in the Active Model Basics Guide.
kamipo added a commit that referenced this pull request Apr 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants