Created
March 15, 2021 14:31
-
-
Save scicco/df5d7811a6c662f8516c4fd5e757c299 to your computer and use it in GitHub Desktop.
Devise - Check if existing user has weak passwords and force password change
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Warden::Manager.after_set_user except: :fetch do |user, auth, opts| | |
if user.class.respond_to?(:pwned_password_check_on_sign_in) && user.class.pwned_password_check_on_sign_in | |
password = auth.request.params.fetch(opts[:scope], {}).fetch(:password, nil) | |
is_pwned = password && auth.authenticated?(opts[:scope]) && user.respond_to?(:password_pwned?) && user.password_pwned?(password) | |
if is_pwned | |
Devise.sign_out_all_scopes | |
if defined?(::Devise::Models::Recoverable) && user.respond_to?(:send_reset_password_instructions) | |
user.send_reset_password_instructions | |
message = :pwned_recoverable | |
else | |
message = :pwned | |
end | |
scope = opts[:scope] | |
auth.logout(scope) | |
throw(:warden, :scope => scope, :message => message) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Inside devise.en.yml put these two keys: