2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
# == EnforcesTwoFactorAuthentication
|
|
|
|
#
|
|
|
|
# Controller concern to enforce two-factor authentication requirements
|
|
|
|
#
|
|
|
|
# Upon inclusion, adds `check_two_factor_requirement` as a before_action,
|
|
|
|
# and makes `two_factor_grace_period_expired?` and `two_factor_skippable?`
|
|
|
|
# available as view helpers.
|
|
|
|
module EnforcesTwoFactorAuthentication
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
before_action :check_two_factor_requirement
|
|
|
|
helper_method :two_factor_grace_period_expired?, :two_factor_skippable?
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_two_factor_requirement
|
2019-09-04 21:01:54 +05:30
|
|
|
if two_factor_authentication_required? && current_user && !current_user.temp_oauth_email? && !current_user.two_factor_enabled? && !skip_two_factor?
|
2017-08-17 22:00:37 +05:30
|
|
|
redirect_to profile_two_factor_auth_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def two_factor_authentication_required?
|
2018-03-17 18:26:18 +05:30
|
|
|
Gitlab::CurrentSettings.require_two_factor_authentication? ||
|
2019-09-30 21:07:59 +05:30
|
|
|
current_user.try(:require_two_factor_authentication_from_group?) ||
|
|
|
|
current_user.try(:ultraauth_user?)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-08-17 22:00:37 +05:30
|
|
|
def two_factor_authentication_reason(global: -> {}, group: -> {})
|
|
|
|
if two_factor_authentication_required?
|
2018-03-17 18:26:18 +05:30
|
|
|
if Gitlab::CurrentSettings.require_two_factor_authentication?
|
2017-08-17 22:00:37 +05:30
|
|
|
global.call
|
|
|
|
else
|
|
|
|
groups = current_user.expanded_groups_requiring_two_factor_authentication.reorder(name: :asc)
|
|
|
|
group.call(groups)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
def two_factor_grace_period
|
2018-03-17 18:26:18 +05:30
|
|
|
periods = [Gitlab::CurrentSettings.two_factor_grace_period]
|
2017-08-17 22:00:37 +05:30
|
|
|
periods << current_user.two_factor_grace_period if current_user.try(:require_two_factor_authentication_from_group?)
|
|
|
|
periods.min
|
|
|
|
end
|
|
|
|
|
|
|
|
def two_factor_grace_period_expired?
|
|
|
|
date = current_user.otp_grace_period_started_at
|
|
|
|
date && (date + two_factor_grace_period.hours) < Time.current
|
|
|
|
end
|
|
|
|
|
|
|
|
def two_factor_skippable?
|
|
|
|
two_factor_authentication_required? &&
|
|
|
|
!current_user.two_factor_enabled? &&
|
|
|
|
!two_factor_grace_period_expired?
|
|
|
|
end
|
|
|
|
|
|
|
|
def skip_two_factor?
|
|
|
|
session[:skip_two_factor] && session[:skip_two_factor] > Time.current
|
|
|
|
end
|
|
|
|
end
|