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

+89 -1
View File
@@ -22,7 +22,86 @@ class User < ApplicationRecord
validates :email_address, presence: true, uniqueness: true,
format: { with: URI::MailTo::EMAIL_REGEXP }
validates :password, length: { minimum: 8 }, allow_nil: true
PASSWORD_MIN = 12
validates :password, length: { minimum: PASSWORD_MIN, maximum: 72 }, allow_nil: true
validate :password_not_breached
# Lockout: this many failed sign-ins in a row (password, authenticator
# code, or password at signing) locks the account for a while, from
# whatever address they come. The user is emailed.
LOCK_AFTER = 10
LOCK_FOR = 15.minutes
encrypts :otp_secret
OTP_ISSUER = "Simple Aircraft Manager".freeze
RECOVERY_CODES = 10
# Password sign-in with lockout. Returns the user, or nil (wrong details,
# or locked: the same answer, so it doesn't reveal which).
def self.authenticate_with_lockout(email_address:, password:)
user = find_by(email_address: email_address.to_s.strip.downcase)
return if user&.locked?
authed = authenticate_by(email_address: email_address.to_s, password: password.to_s)
authed ? authed.tap(&:clear_failed_sign_ins!) : user&.register_failed_sign_in!
end
def locked? = locked_until&.future?
# Counts a failed attempt; locks the account (and returns nil) at the limit.
def register_failed_sign_in!
self.class.where(id: id).update_all("failed_sign_ins = failed_sign_ins + 1")
reload
if failed_sign_ins >= LOCK_AFTER
update_columns(failed_sign_ins: 0, locked_until: LOCK_FOR.from_now)
SessionsMailer.locked(self).deliver_later if MailSettings.configured?
Rails.logger.warn("[auth] #{email_address} locked after #{LOCK_AFTER} failed sign-ins")
end
nil
end
def clear_failed_sign_ins!
update_columns(failed_sign_ins: 0, locked_until: nil) if failed_sign_ins.positive? || locked_until
end
def unlock! = update_columns(failed_sign_ins: 0, locked_until: nil)
# --- Authenticator app (TOTP) ---
def otp_enabled? = otp_enabled_at.present? && otp_secret.present?
def self.new_otp_secret = ROTP::Base32.random
def self.otp_uri(secret, email) = ROTP::TOTP.new(secret, issuer: OTP_ISSUER).provisioning_uri(email)
# A code from the authenticator for this secret, allowing 30 s of clock drift.
def self.valid_otp?(secret, code, after: nil)
ROTP::TOTP.new(secret, issuer: OTP_ISSUER).verify(code.to_s.gsub(/\s/, ""), drift_behind: 30, drift_ahead: 30, after: after)
end
# Turns it on with a confirmed secret; returns the recovery codes to show once.
def enable_otp!(secret)
codes = Array.new(RECOVERY_CODES) { SecureRandom.alphanumeric(10).downcase.scan(/.{5}/).join("-") }
update!(otp_secret: secret, otp_enabled_at: Time.current, otp_last_used_at: nil, otp_recovery_codes: codes.map { recovery_digest(_1) })
codes
end
def disable_otp! = update!(otp_secret: nil, otp_enabled_at: nil, otp_last_used_at: nil, otp_recovery_codes: nil)
# An authenticator code (each accepted once) or an unused recovery code.
def verify_second_factor(code)
return false unless otp_enabled?
code = code.to_s.strip.downcase
if code.gsub(/\s/, "").match?(/\A\d{6}\z/)
step = self.class.valid_otp?(otp_secret, code, after: otp_last_used_at)
step ? update_columns(otp_last_used_at: step) && true : false
else
digests = Array(otp_recovery_codes)
match = digests.find { ActiveSupport::SecurityUtils.secure_compare(_1, recovery_digest(code)) }
match ? update_columns(otp_recovery_codes: digests - [ match ]) && true : false
end
end
def recovery_codes_left = Array(otp_recovery_codes).size
# Aircraft visible to this user: admins see the whole fleet.
def accessible_aircraft
@@ -38,4 +117,13 @@ class User < ApplicationRecord
def display_name
name.presence || email_address
end
private
def recovery_digest(code) = OpenSSL::HMAC.hexdigest("SHA256", Rails.application.key_generator.generate_key("otp-recovery", 32), code.to_s.strip.downcase)
def password_not_breached
return if password.blank? || errors.include?(:password)
errors.add(:password, "has appeared in a data breach (Have I Been Pwned); choose a different one") if PwnedPasswords.breached?(password)
end
end