Open scicco opened 3 years ago
I found a way:
#config/initializers/devise_pwned_passwords_hooks.rb
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
#config/locales/devise.en.yml
en:
devise:
failure:
#...
pwned: "Your password has previously appeared in a data breach and should never be used. Please contact Support Team to get assistance"
pwned_recoverable: "Your password has previously appeared in a data breach and should never be used. Check your Email to change your password"
#...
Hope this could be useful for others.
Hello,
first of all thank you for you for this awesome gem!
Let's suppose an old User was stored before devise-security gem was added to application Gemfile. An existing User has a weak password stored. The User was expired. After some time the User need to be re-activated. After re-activation if he/she performs login with old unsafe password he/she will be able to login. I want to prevent this by forcing a password change.
I'm trying to validate the password for every user on login. If the password is not safe and recoverable module is active I'll send him/her a password reset mail.
I'm trying to implement this use case but my solution is not so good.
I have a Rails application with an User model with the following devise modules (from devise and devise-security gems)
and some devise related config files:
and a new key inside devise translation file:
So far I've managed to make this work by putting this code inside an initializer:
The problem with this approach is that after logout two flash messages will appear on login page:
(flash[:notice]) Your password has previously appeared in a data breach and should never be used. Check your Email to change your password (flash[:alert]): You need to sign in or sign up before continuing.
because error partial inside login page is looping on each flash key:
If I try to change
Warden::Manager.before_failure
hook to useflash.alert
instead offlash.notice
my custom message is overwritten with the unauthenticated message ("You need to sign in or sign up before continuing.")Is there a clever way to achieve this business logic? Am I missing something?
Please advise.
Thank you in advance