Skip to content

user.valid_password? not work if encrypt provider is sha512 and user.stretches has specified #292

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

Closed
bitberry-dev opened this issue Nov 21, 2021 · 6 comments · Fixed by #293
Assignees
Labels
bug Something isn't working

Comments

@bitberry-dev
Copy link

Configuration

  • Sorcery Version: 0.16.1
  • Ruby Version: 2.7.2
  • Framework: rails-6.0.4.1
  • Platform: macOS 11.6

Expected Behavior

@user.valid_password?(correct_password) # should be true

Actual Behavior

@user.valid_password?(correct_password) # but it false

Steps to Reproduce

change the config/initializers/sorcery.rb as below:

user.stretches = 10
user.encryption_algorithm = :sha512

In rails console, create a user with password, for example 'secret'

Then exit the console;

Start rails console again, user.valid_password?('secret') will return false!

Problem analysis

valid_password?(password) in lib/sorcey/model.rb doesn't set stretches to specified value. The set_encryption_attributes class method will set stretches. authenticate and encrypt call the set_encryption_attributes method, but valid_password? doesn't.

Temporary solution

I temporarily solved this bug by calling method set_encryption_attributes right after the authenticates_with_sorcery! call in User model.

class User < ApplicationRecord
  authenticates_with_sorcery!
  set_encryption_attributes # <- here
end

I ran into this bug when migrating from devise, so it's probably not just my problem.

@bitberry-dev
Copy link
Author

Same bug in sorcery's old repo

NoamB/sorcery#769

@joshbuker joshbuker added the bug Something isn't working label Nov 21, 2021
@joshbuker joshbuker self-assigned this Nov 21, 2021
@joshbuker
Copy link
Member

@bitberry-dev this should be as simple as calling that method from within validate_password?, if you want to open a PR. Otherwise I'll tackle this when I have a moment.

@joshbuker
Copy link
Member

@bitberry-dev Fix should be merged into master. Can you give it a try, and if it works I'll go ahead and release v0.16.2?

@bitberry-dev
Copy link
Author

@athix yep, I will check soon. Thank you

@bitberry-dev
Copy link
Author

@athix just checked, everything works, thank you

@joshbuker
Copy link
Member

@bitberry-dev v0.16.2 has been released with this fix included.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants