mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 15:50:36 -05:00
Sign-in hardening: lockout, stronger passwords, authenticator codes
- 10 failed sign-ins in a row (password, authenticator code, or password at signing) lock an account for 15 minutes from any address; the user is emailed; admins can unlock. Same message as a wrong password. - Passwords: at least 12 characters (max 72), refused if found in Have I Been Pwned (k-anonymity range API; fails open; PWNED_PASSWORDS_CHECK=false) - Optional TOTP (authenticator app) with 10 recovery codes for password and emailed-link sign-in, and at signing (auth method password_totp); codes accepted once; admins can reset; SSO sign-in unaffected - Demo/test passwords updated; guide, README and procedures document Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a6bfe922be
commit
14316bb60f
41 files changed
+600
-45
No files matched your search
@@ -58,6 +58,29 @@ module Authentication
|
||||
end
|
||||
end
|
||||
|
||||
# Password or emailed-link sign-in: straight in, or first the
|
||||
# authenticator code if the user has one set up (SSO skips this; the
|
||||
# provider handles its own second factor).
|
||||
OTP_PENDING_FOR = 5.minutes
|
||||
|
||||
def begin_sign_in(user, return_to:, notice: nil)
|
||||
reset_session # fresh session id after signing in
|
||||
if user.otp_enabled?
|
||||
session[:otp_user_id] = user.id
|
||||
session[:otp_started_at] = Time.current.to_i
|
||||
session[:return_to_after_authenticating] = return_to
|
||||
redirect_to two_factor_session_path
|
||||
else
|
||||
start_new_session_for user
|
||||
redirect_to return_to, notice: notice
|
||||
end
|
||||
end
|
||||
|
||||
def pending_otp_user
|
||||
return unless (id = session[:otp_user_id]) && Time.zone.at(session[:otp_started_at].to_i) > OTP_PENDING_FOR.ago
|
||||
User.active.find_by(id: id)
|
||||
end
|
||||
|
||||
def terminate_session
|
||||
Current.session.destroy
|
||||
cookies.delete(:session_id)
|
||||
|
||||
@@ -11,10 +11,20 @@ module EntrySigning
|
||||
session[:after_reauth] = signing_page_path # where single sign-on returns after signing in again
|
||||
end
|
||||
|
||||
# Password re-entered now, or single sign-on completed in the last few minutes.
|
||||
# Password re-entered now (with an authenticator code too, if the user has
|
||||
# one), or single sign-on completed in the last few minutes. Wrong
|
||||
# passwords and codes count toward the account lockout.
|
||||
def reauthenticated_method
|
||||
if params[:password].present?
|
||||
return "password" if current_user.authenticate_password(params[:password])
|
||||
return if current_user.locked?
|
||||
unless current_user.authenticate_password(params[:password])
|
||||
current_user.register_failed_sign_in!
|
||||
return
|
||||
end
|
||||
return "password" unless current_user.otp_enabled?
|
||||
return "password_totp" if current_user.verify_second_factor(params[:otp_code])
|
||||
current_user.register_failed_sign_in!
|
||||
return
|
||||
elsif current_user.sso_uid? && (at = session[:reauthenticated_at]) && Time.zone.at(at) > EntrySigner::REAUTH_WINDOW.ago
|
||||
return "sso"
|
||||
end
|
||||
@@ -27,7 +37,11 @@ module EntrySigning
|
||||
unless params[:confirm] == "1"
|
||||
return signing_failed(entry, "Tick the box to confirm you're signing this entry.")
|
||||
end
|
||||
return signing_failed(entry, "Re-enter your password (or sign in again with #{Sso.label}) to sign.") unless method
|
||||
unless method
|
||||
return signing_failed(entry, "This account is locked for a few minutes after too many failed tries.") if current_user.reload.locked?
|
||||
return signing_failed(entry, "Re-enter your password#{' and a current authenticator code' if current_user.otp_enabled?} (or sign in again with #{Sso.label}) to sign.")
|
||||
end
|
||||
current_user.clear_failed_sign_ins!
|
||||
|
||||
signature = EntrySigner.new(entry, user: current_user, certificate: certificate, auth_method: method, request: request).sign!
|
||||
session.delete(:reauthenticated_at)
|
||||
|
||||
Reference in new issue
Block a user