2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-02-20 14:44:51 +08:00
|
|
|
module SecondFactorManager
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2019-06-27 07:58:06 +08:00
|
|
|
def create_totp(opts = {})
|
|
|
|
UserSecondFactor.create!({
|
|
|
|
user_id: self.id,
|
|
|
|
method: UserSecondFactor.methods[:totp],
|
|
|
|
data: ROTP::Base32.random_base32
|
|
|
|
}.merge(opts))
|
2018-02-20 14:44:51 +08:00
|
|
|
end
|
|
|
|
|
2019-06-27 07:58:06 +08:00
|
|
|
def get_totp_object(data)
|
|
|
|
ROTP::TOTP.new(data, issuer: SiteSetting.title)
|
2018-02-20 14:44:51 +08:00
|
|
|
end
|
|
|
|
|
2019-06-27 07:58:06 +08:00
|
|
|
def totp_provisioning_uri(data)
|
|
|
|
get_totp_object(data).provisioning_uri(self.email)
|
2018-02-20 14:44:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def authenticate_totp(token)
|
2019-06-27 07:58:06 +08:00
|
|
|
totps = self&.user_second_factors.totps
|
|
|
|
authenticated = false
|
|
|
|
totps.each do |totp|
|
2018-02-20 14:44:51 +08:00
|
|
|
|
2019-06-27 07:58:06 +08:00
|
|
|
last_used = 0
|
|
|
|
|
|
|
|
if totp.last_used
|
|
|
|
last_used = totp.last_used.to_i
|
|
|
|
end
|
2018-02-20 14:44:51 +08:00
|
|
|
|
2019-06-27 07:58:06 +08:00
|
|
|
authenticated = !token.blank? && totp.get_totp_object.verify_with_drift_and_prior(token, 30, last_used)
|
|
|
|
if authenticated
|
|
|
|
totp.update!(last_used: DateTime.now)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2018-02-20 14:44:51 +08:00
|
|
|
!!authenticated
|
|
|
|
end
|
|
|
|
|
|
|
|
def totp_enabled?
|
2019-03-15 15:02:04 +08:00
|
|
|
!SiteSetting.enable_sso &&
|
|
|
|
SiteSetting.enable_local_logins &&
|
|
|
|
self&.user_second_factors.totps.exists?
|
2018-02-20 14:44:51 +08:00
|
|
|
end
|
2018-06-28 16:12:32 +08:00
|
|
|
|
|
|
|
def backup_codes_enabled?
|
2019-03-15 15:02:04 +08:00
|
|
|
!SiteSetting.enable_sso &&
|
|
|
|
SiteSetting.enable_local_logins &&
|
|
|
|
self&.user_second_factors.backup_codes.exists?
|
2018-06-28 16:12:32 +08:00
|
|
|
end
|
|
|
|
|
2018-07-04 16:45:42 +08:00
|
|
|
def remaining_backup_codes
|
|
|
|
self&.user_second_factors&.backup_codes&.count
|
|
|
|
end
|
|
|
|
|
2018-06-28 16:12:32 +08:00
|
|
|
def authenticate_second_factor(token, second_factor_method)
|
|
|
|
if second_factor_method == UserSecondFactor.methods[:totp]
|
|
|
|
authenticate_totp(token)
|
|
|
|
elsif second_factor_method == UserSecondFactor.methods[:backup_codes]
|
|
|
|
authenticate_backup_code(token)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_backup_codes
|
|
|
|
codes = []
|
|
|
|
10.times do
|
|
|
|
codes << SecureRandom.hex(8)
|
|
|
|
end
|
|
|
|
|
|
|
|
codes_json = codes.map do |code|
|
|
|
|
salt = SecureRandom.hex(16)
|
|
|
|
{ salt: salt,
|
|
|
|
code_hash: hash_backup_code(code, salt)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
if self.user_second_factors.backup_codes.empty?
|
|
|
|
create_backup_codes(codes_json)
|
|
|
|
else
|
|
|
|
self.user_second_factors.where(method: UserSecondFactor.methods[:backup_codes]).destroy_all
|
|
|
|
create_backup_codes(codes_json)
|
|
|
|
end
|
|
|
|
|
|
|
|
codes
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_backup_codes(codes)
|
|
|
|
codes.each do |code|
|
|
|
|
UserSecondFactor.create!(
|
|
|
|
user_id: self.id,
|
|
|
|
data: code.to_json,
|
|
|
|
enabled: true,
|
|
|
|
method: UserSecondFactor.methods[:backup_codes]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def authenticate_backup_code(backup_code)
|
|
|
|
if !backup_code.blank?
|
|
|
|
codes = self&.user_second_factors&.backup_codes
|
|
|
|
|
|
|
|
codes.each do |code|
|
|
|
|
stored_code = JSON.parse(code.data)["code_hash"]
|
|
|
|
stored_salt = JSON.parse(code.data)["salt"]
|
|
|
|
backup_hash = hash_backup_code(backup_code, stored_salt)
|
|
|
|
next unless backup_hash == stored_code
|
|
|
|
|
|
|
|
code.update(enabled: false, last_used: DateTime.now)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
false
|
|
|
|
end
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def hash_backup_code(code, salt)
|
|
|
|
Pbkdf2.hash_password(code, salt, Rails.configuration.pbkdf2_iterations, Rails.configuration.pbkdf2_algorithm)
|
|
|
|
end
|
2018-02-20 14:44:51 +08:00
|
|
|
end
|