Skip to content
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

Devise Password generator #14

Open
byhbt opened this issue Aug 18, 2020 · 0 comments
Open

Devise Password generator #14

byhbt opened this issue Aug 18, 2020 · 0 comments
Labels

Comments

@byhbt
Copy link
Owner

byhbt commented Aug 18, 2020

Problem:

When use Devise with Devise security gem for ensuring the password is always strong enough.

If the user login via OAuth without password, we have to generate a random password for this user in order to bypass the NOT NULL of the encrypted_password in users table as default.

password = SecureRandom.urlsafe_base64

or

password = Devise.friendly_token[0,20]

Sometimes the result of urlsafe_base64 returns a string without number, then it causes password insecurity according to the security extension devise_security_extension

You can check by comparing the generated value of urlsafe_base64 to the pattern of strong password in the security gem config

[16] pry(main)> password_regex.match?(SecureRandom.urlsafe_base64)
=> true
[17] pry(main)> password_regex.match?(SecureRandom.urlsafe_base64)
=> true
[18] pry(main)> password_regex.match?(SecureRandom.urlsafe_base64)
=> true
[19] pry(main)> password_regex.match?(SecureRandom.urlsafe_base64)
=> false
[20] pry(main)> password_regex.match?(SecureRandom.urlsafe_base64)
=> true

The Devise has provided a method but still uses the same urlsafe_base64 method, therefore this case is still possible to happen.

  # Generate a friendly string randomly to be used as token.
  # By default, the length is 20 characters.
  def self.friendly_token(length = 20)
    # To calculate real characters, we must perform this operation.
    # See SecureRandom.urlsafe_base64
    rlength = (length * 3) / 4
    SecureRandom.urlsafe_base64(rlength).tr('lIO0', 'sxyz')
  end

Solution:

Override the default generator of Devise password.

Lesson:

  1. Dig into the gem source to see how the data is generated.

  2. By the urlsafe_base64 sometimes return a string without number?

Reference:

https://stackoverflow.com/questions/3681827/how-to-auto-generate-passwords-in-rails-devise

@byhbt byhbt added the Rails label Aug 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant