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
|
2020-09-24 15:09:51 +00:00
|
|
|
before_action :check_two_factor_requirement, except: [:route_not_found]
|
2020-08-05 21:09:40 +00:00
|
|
|
|
|
|
|
# to include this in controllers inheriting from `ActionController::Metal`
|
|
|
|
# we need to add this block
|
|
|
|
if respond_to?(:helper_method)
|
|
|
|
helper_method :two_factor_grace_period_expired?, :two_factor_skippable?
|
|
|
|
end
|
2017-02-06 16:07:37 +00:00
|
|
|
end
|
|
|
|
|
2017-03-07 18:48:57 +00:00
|
|
|
def check_two_factor_requirement
|
2020-08-05 21:09:40 +00:00
|
|
|
return unless respond_to?(:current_user)
|
|
|
|
|
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?
|
2020-09-02 15:10:54 +00:00
|
|
|
two_factor_verifier.two_factor_authentication_required?
|
2017-02-06 16:07:37 +00:00
|
|
|
end
|
|
|
|
|
2020-04-21 15:21:10 +00:00
|
|
|
def current_user_requires_two_factor?
|
2020-09-02 15:10:54 +00:00
|
|
|
two_factor_verifier.current_user_needs_to_setup_two_factor? && !skip_two_factor?
|
2020-04-21 15:21:10 +00:00
|
|
|
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
|
2020-09-02 15:10:54 +00:00
|
|
|
groups = current_user.source_groups_of_two_factor_authentication_requirement.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
|
2020-09-02 15:10:54 +00:00
|
|
|
two_factor_verifier.two_factor_grace_period
|
2017-02-06 16:07:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def two_factor_grace_period_expired?
|
2020-09-02 15:10:54 +00:00
|
|
|
two_factor_verifier.two_factor_grace_period_expired?
|
2017-02-06 16:07:37 +00:00
|
|
|
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
|
2020-09-02 15:10:54 +00:00
|
|
|
|
|
|
|
def two_factor_verifier
|
|
|
|
@two_factor_verifier ||= Gitlab::Auth::TwoFactorAuthVerifier.new(current_user) # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
|
|
|
end
|
2017-02-06 16:07:37 +00:00
|
|
|
end
|
2020-04-21 15:21:10 +00:00
|
|
|
|
|
|
|
EnforcesTwoFactorAuthentication.prepend_if_ee('EE::EnforcesTwoFactorAuthentication')
|