2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-02-20 01:44:51 -05:00
|
|
|
module SecondFactorManager
|
2019-12-08 23:29:48 -05:00
|
|
|
TOTP_ALLOWED_DRIFT_SECONDS = 30
|
|
|
|
|
2018-02-20 01:44:51 -05:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2020-01-15 05:27:12 -05:00
|
|
|
SecondFactorAuthenticationResult = Struct.new(
|
2022-04-13 08:04:09 -04:00
|
|
|
:ok,
|
|
|
|
:error,
|
|
|
|
:reason,
|
|
|
|
:backup_enabled,
|
|
|
|
:security_key_enabled,
|
|
|
|
:totp_enabled,
|
|
|
|
:multiple_second_factor_methods,
|
|
|
|
:used_2fa_method,
|
2020-01-15 05:27:12 -05:00
|
|
|
)
|
|
|
|
|
2019-06-26 19:58:06 -04:00
|
|
|
def create_totp(opts = {})
|
2019-12-16 19:33:51 -05:00
|
|
|
require_rotp
|
2019-06-26 19:58:06 -04:00
|
|
|
UserSecondFactor.create!({
|
|
|
|
user_id: self.id,
|
|
|
|
method: UserSecondFactor.methods[:totp],
|
2019-12-08 23:29:48 -05:00
|
|
|
data: ROTP::Base32.random
|
2019-06-26 19:58:06 -04:00
|
|
|
}.merge(opts))
|
2018-02-20 01:44:51 -05:00
|
|
|
end
|
|
|
|
|
2019-06-26 19:58:06 -04:00
|
|
|
def get_totp_object(data)
|
2019-12-16 19:33:51 -05:00
|
|
|
require_rotp
|
2020-02-20 16:35:30 -05:00
|
|
|
ROTP::TOTP.new(data, issuer: SiteSetting.title.gsub(":", ""))
|
2018-02-20 01:44:51 -05:00
|
|
|
end
|
|
|
|
|
2019-06-26 19:58:06 -04:00
|
|
|
def totp_provisioning_uri(data)
|
|
|
|
get_totp_object(data).provisioning_uri(self.email)
|
2018-02-20 01:44:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def authenticate_totp(token)
|
2019-06-26 19:58:06 -04:00
|
|
|
totps = self&.user_second_factors.totps
|
|
|
|
authenticated = false
|
|
|
|
totps.each do |totp|
|
2018-02-20 01:44:51 -05:00
|
|
|
|
2019-06-26 19:58:06 -04:00
|
|
|
last_used = 0
|
|
|
|
|
|
|
|
if totp.last_used
|
|
|
|
last_used = totp.last_used.to_i
|
|
|
|
end
|
2018-02-20 01:44:51 -05:00
|
|
|
|
2019-12-16 19:33:51 -05:00
|
|
|
authenticated = !token.blank? && totp.totp_object.verify(
|
2019-12-08 23:29:48 -05:00
|
|
|
token,
|
|
|
|
drift_ahead: TOTP_ALLOWED_DRIFT_SECONDS,
|
|
|
|
drift_behind: TOTP_ALLOWED_DRIFT_SECONDS,
|
|
|
|
after: last_used
|
|
|
|
)
|
|
|
|
|
2019-06-26 19:58:06 -04:00
|
|
|
if authenticated
|
|
|
|
totp.update!(last_used: DateTime.now)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2018-02-20 01:44:51 -05:00
|
|
|
!!authenticated
|
|
|
|
end
|
|
|
|
|
|
|
|
def totp_enabled?
|
2021-02-08 05:04:33 -05:00
|
|
|
!SiteSetting.enable_discourse_connect &&
|
2019-03-15 03:02:04 -04:00
|
|
|
SiteSetting.enable_local_logins &&
|
|
|
|
self&.user_second_factors.totps.exists?
|
2018-02-20 01:44:51 -05:00
|
|
|
end
|
2018-06-28 04:12:32 -04:00
|
|
|
|
|
|
|
def backup_codes_enabled?
|
2021-02-08 05:04:33 -05:00
|
|
|
!SiteSetting.enable_discourse_connect &&
|
2019-03-15 03:02:04 -04:00
|
|
|
SiteSetting.enable_local_logins &&
|
|
|
|
self&.user_second_factors.backup_codes.exists?
|
2018-06-28 04:12:32 -04:00
|
|
|
end
|
|
|
|
|
2019-10-01 22:08:41 -04:00
|
|
|
def security_keys_enabled?
|
2021-02-08 05:04:33 -05:00
|
|
|
!SiteSetting.enable_discourse_connect &&
|
2019-10-01 22:08:41 -04:00
|
|
|
SiteSetting.enable_local_logins &&
|
|
|
|
self&.security_keys.where(factor_type: UserSecurityKey.factor_types[:second_factor], enabled: true).exists?
|
|
|
|
end
|
|
|
|
|
2020-01-15 05:27:12 -05:00
|
|
|
def has_any_second_factor_methods_enabled?
|
2022-12-07 17:41:22 -05:00
|
|
|
totp_enabled? || security_keys_enabled?
|
2020-01-15 05:27:12 -05:00
|
|
|
end
|
|
|
|
|
2020-01-09 19:45:56 -05:00
|
|
|
def has_multiple_second_factor_methods?
|
2020-01-15 05:27:12 -05:00
|
|
|
security_keys_enabled? && totp_or_backup_codes_enabled?
|
|
|
|
end
|
|
|
|
|
|
|
|
def totp_or_backup_codes_enabled?
|
|
|
|
totp_enabled? || backup_codes_enabled?
|
|
|
|
end
|
|
|
|
|
|
|
|
def only_security_keys_enabled?
|
|
|
|
security_keys_enabled? && !totp_or_backup_codes_enabled?
|
|
|
|
end
|
|
|
|
|
|
|
|
def only_totp_or_backup_codes_enabled?
|
|
|
|
!security_keys_enabled? && totp_or_backup_codes_enabled?
|
2020-01-09 19:45:56 -05:00
|
|
|
end
|
|
|
|
|
2018-07-04 04:45:42 -04:00
|
|
|
def remaining_backup_codes
|
|
|
|
self&.user_second_factors&.backup_codes&.count
|
|
|
|
end
|
|
|
|
|
2020-01-15 05:27:12 -05:00
|
|
|
def authenticate_second_factor(params, secure_session)
|
|
|
|
ok_result = SecondFactorAuthenticationResult.new(true)
|
|
|
|
return ok_result if !security_keys_enabled? && !totp_or_backup_codes_enabled?
|
|
|
|
|
|
|
|
second_factor_token = params[:second_factor_token]
|
|
|
|
second_factor_method = params[:second_factor_method]&.to_i
|
|
|
|
|
|
|
|
if second_factor_method.blank? || UserSecondFactor.methods[second_factor_method].blank?
|
|
|
|
return invalid_second_factor_method_result
|
|
|
|
end
|
|
|
|
|
|
|
|
if !valid_second_factor_method_for_user?(second_factor_method)
|
|
|
|
return not_enabled_second_factor_method_result
|
|
|
|
end
|
|
|
|
|
|
|
|
case second_factor_method
|
|
|
|
when UserSecondFactor.methods[:totp]
|
2022-04-13 08:04:09 -04:00
|
|
|
if authenticate_totp(second_factor_token)
|
|
|
|
ok_result.used_2fa_method = UserSecondFactor.methods[:totp]
|
|
|
|
return ok_result
|
|
|
|
else
|
|
|
|
return invalid_totp_or_backup_code_result
|
|
|
|
end
|
2020-01-15 05:27:12 -05:00
|
|
|
when UserSecondFactor.methods[:backup_codes]
|
2022-04-13 08:04:09 -04:00
|
|
|
if authenticate_backup_code(second_factor_token)
|
|
|
|
ok_result.used_2fa_method = UserSecondFactor.methods[:backup_codes]
|
|
|
|
return ok_result
|
|
|
|
else
|
|
|
|
return invalid_totp_or_backup_code_result
|
|
|
|
end
|
2020-01-15 05:27:12 -05:00
|
|
|
when UserSecondFactor.methods[:security_key]
|
2022-04-13 08:04:09 -04:00
|
|
|
if authenticate_security_key(secure_session, second_factor_token)
|
|
|
|
ok_result.used_2fa_method = UserSecondFactor.methods[:security_key]
|
|
|
|
return ok_result
|
|
|
|
else
|
|
|
|
return invalid_security_key_result
|
|
|
|
end
|
2018-06-28 04:12:32 -04:00
|
|
|
end
|
2020-01-15 05:27:12 -05:00
|
|
|
|
|
|
|
# if we have gotten down to this point without being
|
|
|
|
# OK or invalid something has gone very weird.
|
|
|
|
invalid_second_factor_method_result
|
|
|
|
rescue ::Webauthn::SecurityKeyError => err
|
|
|
|
invalid_security_key_result(err.message)
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_second_factor_method_for_user?(method)
|
|
|
|
case method
|
|
|
|
when UserSecondFactor.methods[:totp]
|
|
|
|
return totp_enabled?
|
|
|
|
when UserSecondFactor.methods[:backup_codes]
|
|
|
|
return backup_codes_enabled?
|
|
|
|
when UserSecondFactor.methods[:security_key]
|
|
|
|
return security_keys_enabled?
|
|
|
|
end
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def authenticate_security_key(secure_session, security_key_credential)
|
|
|
|
::Webauthn::SecurityKeyAuthenticationService.new(
|
|
|
|
self,
|
|
|
|
security_key_credential,
|
|
|
|
challenge: Webauthn.challenge(self, secure_session),
|
|
|
|
rp_id: Webauthn.rp_id(self, secure_session),
|
|
|
|
origin: Discourse.base_url
|
|
|
|
).authenticate_security_key
|
|
|
|
end
|
|
|
|
|
|
|
|
def invalid_totp_or_backup_code_result
|
|
|
|
invalid_second_factor_authentication_result(
|
|
|
|
I18n.t("login.invalid_second_factor_code"),
|
|
|
|
"invalid_second_factor"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def invalid_security_key_result(error_message = nil)
|
|
|
|
invalid_second_factor_authentication_result(
|
|
|
|
error_message || I18n.t("login.invalid_security_key"),
|
|
|
|
"invalid_security_key"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def invalid_second_factor_method_result
|
|
|
|
invalid_second_factor_authentication_result(
|
|
|
|
I18n.t("login.invalid_second_factor_method"),
|
|
|
|
"invalid_second_factor_method"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def not_enabled_second_factor_method_result
|
|
|
|
invalid_second_factor_authentication_result(
|
|
|
|
I18n.t("login.not_enabled_second_factor_method"),
|
|
|
|
"not_enabled_second_factor_method"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def invalid_second_factor_authentication_result(error_message, reason)
|
|
|
|
SecondFactorAuthenticationResult.new(
|
|
|
|
false,
|
|
|
|
error_message,
|
|
|
|
reason,
|
|
|
|
backup_codes_enabled?,
|
|
|
|
security_keys_enabled?,
|
|
|
|
totp_enabled?,
|
|
|
|
has_multiple_second_factor_methods?
|
|
|
|
)
|
2018-06-28 04:12:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def generate_backup_codes
|
|
|
|
codes = []
|
|
|
|
10.times do
|
2020-01-20 23:32:06 -05:00
|
|
|
codes << SecureRandom.hex(16)
|
2018-06-28 04:12:32 -04:00
|
|
|
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|
|
2020-01-15 05:27:12 -05:00
|
|
|
parsed_data = JSON.parse(code.data)
|
|
|
|
stored_code = parsed_data["code_hash"]
|
|
|
|
stored_salt = parsed_data["salt"]
|
2018-06-28 04:12:32 -04:00
|
|
|
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
|
2019-12-16 19:33:51 -05:00
|
|
|
|
|
|
|
def require_rotp
|
|
|
|
require 'rotp' if !defined? ROTP
|
|
|
|
end
|
2018-02-20 01:44:51 -05:00
|
|
|
end
|