2018-09-14 05:42:05 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-06 16:07:37 +00:00
|
|
|
# == EnforcesTwoFactorAuthentication
|
|
|
|
#
|
|
|
|
# Controller concern to enforce two-factor authentication requirements
|
|
|
|
#
|
2017-03-07 18:48:57 +00:00
|
|
|
# Upon inclusion, adds `check_two_factor_requirement` as a before_action,
|
|
|
|
# and makes `two_factor_grace_period_expired?` and `two_factor_skippable?`
|
2017-02-06 16:07:37 +00:00
|
|
|
# available as view helpers.
|
|
|
|
module EnforcesTwoFactorAuthentication
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
2017-03-07 18:48:57 +00:00
|
|
|
before_action :check_two_factor_requirement
|
2017-02-06 16:07:37 +00:00
|
|
|
helper_method :two_factor_grace_period_expired?, :two_factor_skippable?
|
|
|
|
end
|
|
|
|
|
2017-03-07 18:48:57 +00:00
|
|
|
def check_two_factor_requirement
|
2020-04-21 15:21:10 +00:00
|
|
|
if two_factor_authentication_required? && current_user_requires_two_factor?
|
2017-02-06 16:07:37 +00:00
|
|
|
redirect_to profile_two_factor_auth_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def two_factor_authentication_required?
|
2018-02-02 18:39:55 +00:00
|
|
|
Gitlab::CurrentSettings.require_two_factor_authentication? ||
|
2019-06-18 16:18:14 +00:00
|
|
|
current_user.try(:require_two_factor_authentication_from_group?) ||
|
|
|
|
current_user.try(:ultraauth_user?)
|
2017-02-06 16:07:37 +00:00
|
|
|
end
|
|
|
|
|
2020-04-21 15:21:10 +00:00
|
|
|
def current_user_requires_two_factor?
|
|
|
|
current_user && !current_user.temp_oauth_email? && !current_user.two_factor_enabled? && !skip_two_factor?
|
|
|
|
end
|
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-03-08 11:09:15 +00:00
|
|
|
def two_factor_authentication_reason(global: -> {}, group: -> {})
|
|
|
|
if two_factor_authentication_required?
|
2018-02-02 18:39:55 +00:00
|
|
|
if Gitlab::CurrentSettings.require_two_factor_authentication?
|
2017-03-08 11:09:15 +00:00
|
|
|
global.call
|
|
|
|
else
|
2017-03-09 19:38:13 +00:00
|
|
|
groups = current_user.expanded_groups_requiring_two_factor_authentication.reorder(name: :asc)
|
2017-03-08 11:09:15 +00:00
|
|
|
group.call(groups)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-03-08 11:09:15 +00:00
|
|
|
|
2017-02-06 16:07:37 +00:00
|
|
|
def two_factor_grace_period
|
2018-02-02 18:39:55 +00:00
|
|
|
periods = [Gitlab::CurrentSettings.two_factor_grace_period]
|
2017-03-14 13:34:21 +00:00
|
|
|
periods << current_user.two_factor_grace_period if current_user.try(:require_two_factor_authentication_from_group?)
|
2017-02-06 16:07:37 +00:00
|
|
|
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?
|
2017-03-07 18:51:22 +00:00
|
|
|
session[:skip_two_factor] && session[:skip_two_factor] > Time.current
|
2017-02-06 16:07:37 +00:00
|
|
|
end
|
|
|
|
end
|
2020-04-21 15:21:10 +00:00
|
|
|
|
|
|
|
EnforcesTwoFactorAuthentication.prepend_if_ee('EE::EnforcesTwoFactorAuthentication')
|