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

Merged
merged 2 commits into from Jun 28, 2018

Conversation

Projects
None yet
8 participants
@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'.

Unathi Chonco Unathi Chonco
This addition will now allow configuring an attribute name for the
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

This comment has been minimized.

rails-bot commented Oct 11, 2016

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)

This comment has been minimized.

@matthewd

matthewd Oct 11, 2016

Member

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."

This comment has been minimized.

@matthewd

matthewd Oct 11, 2016

Member

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

This comment has been minimized.

@matthewd

matthewd Oct 11, 2016

Member

We still don't need this.

Unathi Chonco Unathi Chonco
Remove method for regenerating a token, and update `#authenticate`.
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 from Allow configurable attribute name on `#has_secure_passwors to Allow configurable attribute name on `#has_secure_password` Oct 12, 2016

@choncou

This comment has been minimized.

choncou commented Oct 29, 2016

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

@NomNomCameron

This comment has been minimized.

NomNomCameron commented Oct 18, 2017

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

This comment has been minimized.

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

This comment has been minimized.

alexstophel commented Nov 16, 2017

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

@shideneyu

This comment has been minimized.

shideneyu commented Jun 27, 2018

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

@rafaelfranca rafaelfranca merged commit 9b63bf1 into rails:master Jun 28, 2018

1 of 2 checks passed

codeclimate Code Climate is analyzing this code.
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

rafaelfranca added a commit that referenced this pull request Jun 28, 2018

Merge pull request #26764 from choncou/improve_has_secure_password
Allow configurable attribute name on `#has_secure_password`

bogdanvlviv added a commit to bogdanvlviv/rails that referenced this pull request Jul 6, 2018

Update `has_secure_password` info in the guide
`has_secure_password` allows configuring name of attribute since rails#26764.
This commit adds a mention about it in the Active Model Basics Guide.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment