mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 15:50:36 -05:00
- 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>
34 lines
1.4 KiB
Ruby
34 lines
1.4 KiB
Ruby
require "net/http"
|
|
|
|
# Checks a password against the Have I Been Pwned breach corpus with the
|
|
# k-anonymity range API: only the first 5 characters of its SHA-1 are sent,
|
|
# never the password. If the service can't be reached the password is
|
|
# allowed (logged), so an outage doesn't stop anyone changing a password.
|
|
# PWNED_PASSWORDS_CHECK=false turns it off (e.g. no outbound internet).
|
|
module PwnedPasswords
|
|
API = "https://api.pwnedpasswords.com/range/".freeze
|
|
|
|
mattr_accessor :checker, default: ->(password) { lookup(password) }
|
|
|
|
def self.enabled? = ENV["PWNED_PASSWORDS_CHECK"] != "false"
|
|
def self.breached?(password) = enabled? && password.present? && checker.call(password)
|
|
|
|
def self.lookup(password)
|
|
sha1 = Digest::SHA1.hexdigest(password).upcase
|
|
prefix, suffix = sha1[0, 5], sha1[5..]
|
|
uri = URI("#{API}#{prefix}")
|
|
req = Net::HTTP::Get.new(uri)
|
|
req["Add-Padding"] = "true"
|
|
req["User-Agent"] = "Simple-Aircraft-Manager"
|
|
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true, open_timeout: 3, read_timeout: 3) { _1.request(req) }
|
|
return false unless res.is_a?(Net::HTTPSuccess)
|
|
res.body.each_line.any? do |line|
|
|
hash, count = line.strip.split(":")
|
|
hash == suffix && count.to_i.positive?
|
|
end
|
|
rescue StandardError => e
|
|
Rails.logger.warn("[pwned passwords] check skipped: #{e.class}: #{e.message}")
|
|
false
|
|
end
|
|
end
|