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:
rickyandClaude Opus 5.5 committed 2026-11-07 15:17:56 -05:00
1 parent a6bfe922be
commit 14316bb60f
41 files changed
+600 -45

No files matched your search

+14 -1
View File
@@ -1,6 +1,6 @@
class Admin::UsersController < ApplicationController
before_action :require_admin!
before_action :set_user, only: %i[edit update destroy disable enable]
before_action :set_user, only: %i[edit update destroy disable enable unlock reset_two_factor]
def index
@users = User.order(:email_address).includes(:certificates, aircraft_roles: :aircraft)
@@ -39,6 +39,19 @@ class Admin::UsersController < ApplicationController
# Ends their access and their ability to sign (AC 120-78B 2.2.6.4) while
# keeping their account, so the signatures they made stay attributed.
def unlock
@user.unlock!
redirect_to admin_users_path, notice: "#{@user.email_address} unlocked."
end
# For a user who lost their authenticator: they sign in with their password
# alone and can set it up again.
def reset_two_factor
@user.disable_otp!
Rails.logger.info("[auth] #{current_user.email_address} turned off two-factor for #{@user.email_address}")
redirect_to admin_users_path, notice: "Two-factor turned off for #{@user.email_address}. Ask them to set it up again from their profile."
end
def disable
return redirect_to admin_users_path, alert: "You can't disable yourself." if @user == current_user
@user.update!(disabled_at: Time.current)
@@ -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)
+17 -3
View File
@@ -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)
+1 -1
View File
@@ -24,7 +24,7 @@ class PasswordsController < ApplicationController
@user.sessions.destroy_all # a reset signs out every device
redirect_to new_session_path, notice: "Password has been reset."
else
redirect_to edit_password_path(params[:token]), alert: "Passwords did not match."
redirect_to edit_password_path(params[:token]), alert: @user.errors.full_messages.to_sentence.presence || "Enter a new password."
end
end
+31 -8
View File
@@ -1,6 +1,8 @@
class SessionsController < ApplicationController
allow_unauthenticated_access only: %i[ new create ]
rate_limit to: 10, within: 3.minutes, only: :create, with: -> { redirect_to new_session_url, alert: "Try again later." }
allow_unauthenticated_access only: %i[new create two_factor verify_two_factor]
rate_limit to: 10, within: 3.minutes, only: %i[create verify_two_factor], with: -> { redirect_to new_session_url, alert: "Try again later." }
FAILED = "Try another email address or password. After #{User::LOCK_AFTER} failed tries in a row an account is locked for #{User::LOCK_FOR.inspect}.".freeze
def new
end
@@ -8,15 +10,36 @@ class SessionsController < ApplicationController
def create
return redirect_to new_session_path, alert: "Sign in with #{Sso.label}." unless Sso.password_login?
if (user = User.authenticate_by(params.permit(:email_address, :password))) && user.disabled?
if (user = User.authenticate_with_lockout(email_address: params[:email_address], password: params[:password])) && user.disabled?
redirect_to new_session_path, alert: "This account is disabled."
elsif user
return_to = after_authentication_url
reset_session # fresh session id after signing in
start_new_session_for user
redirect_to return_to
begin_sign_in user, return_to: after_authentication_url
else
redirect_to new_session_path, alert: "Try another email address or password."
redirect_to new_session_path(email_address: params[:email_address].to_s.first(200)), alert: FAILED
end
end
# Second step for accounts with an authenticator app.
def two_factor
redirect_to new_session_path, alert: "Sign in again." unless pending_otp_user
end
def verify_two_factor
user = pending_otp_user
return redirect_to(new_session_path, alert: "That took too long. Sign in again.") unless user
if user.locked?
reset_session
redirect_to new_session_path, alert: FAILED
elsif user.verify_second_factor(params[:code])
user.clear_failed_sign_ins!
return_to = after_authentication_url
reset_session
start_new_session_for user
notice = user.recovery_codes_left < 3 ? "Signed in. You have #{user.recovery_codes_left} recovery codes left: make new ones on your profile." : nil
redirect_to return_to, notice: notice
else
user.register_failed_sign_in!
redirect_to two_factor_session_path, alert: "That code didn't match. Try the current code from your authenticator app, or a recovery code."
end
end
+1 -3
View File
@@ -26,9 +26,7 @@ class SignInLinksController < ApplicationController
def consume
return redirect_to new_session_path, alert: "This account is disabled." if @user.disabled?
reset_session # fresh session id after signing in
start_new_session_for @user # also spends the link
redirect_to root_path, notice: "Signed in."
begin_sign_in @user, return_to: root_path, notice: "Signed in." # signing in also spends the link
end
private
+48
View File
@@ -0,0 +1,48 @@
# Profile → Two-factor: set up an authenticator app (TOTP) for password and
# emailed-link sign-in, turn it off, or make new recovery codes.
class TwoFactorsController < ApplicationController
rate_limit to: 10, within: 5.minutes, only: %i[create destroy recovery_codes], with: -> { redirect_to edit_profile_path(anchor: "two-factor"), alert: "Try again later." }
def new
return redirect_to(back_path, notice: "Two-factor is already on.") if current_user.otp_enabled?
session[:pending_otp_secret] ||= User.new_otp_secret
@secret = session[:pending_otp_secret]
svg = RQRCode::QRCode.new(User.otp_uri(@secret, current_user.email_address)).as_svg(module_size: 4, use_path: true, viewbox: true)
@qr_data_uri = "data:image/svg+xml;base64,#{Base64.strict_encode64(svg)}"
end
def create
secret = session[:pending_otp_secret]
return redirect_to(new_two_factor_path(return_to: params[:return_to])) unless secret
if User.valid_otp?(secret, params[:code])
session.delete(:pending_otp_secret)
@codes = current_user.enable_otp!(secret)
log_security("turned on two-factor")
render :codes
else
redirect_to new_two_factor_path(return_to: params[:return_to]), alert: "That code didn't match. Check the time on your phone and try the current code."
end
end
def recovery_codes
return redirect_to(edit_profile_path(anchor: "two-factor"), alert: "That code didn't match.") unless current_user.verify_second_factor(params[:code])
@codes = current_user.enable_otp!(current_user.otp_secret)
log_security("made new recovery codes")
render :codes
end
def destroy
return redirect_to(edit_profile_path(anchor: "two-factor"), alert: "That code didn't match.") unless current_user.verify_second_factor(params[:code])
current_user.disable_otp!
log_security("turned off two-factor")
redirect_to edit_profile_path(anchor: "two-factor"), notice: "Two-factor is off."
end
private
# Back to a signing invitation the user came from, else the profile.
def back_path = params[:return_to].to_s.match?(%r{\A/sign/[\w-]+\z}) ? params[:return_to] : edit_profile_path(anchor: "two-factor")
helper_method :back_path
def log_security(what) = Rails.logger.info("[auth] #{current_user.email_address} #{what}")
end