Module: EnforcesTwoFactorAuthentication

Extended by:
ActiveSupport::Concern
Included in:
ApplicationController, Gitlab::BaseDoorkeeperController, Oauth::TokenInfoController, Oauth::TokensController
Defined in:
app/controllers/concerns/enforces_two_factor_authentication.rb

Overview

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.

Instance Method Summary collapse

Instance Method Details

#check_two_factor_requirementObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/controllers/concerns/enforces_two_factor_authentication.rb', line 21

def check_two_factor_requirement
  return unless respond_to?(:current_user)

  if two_factor_authentication_required? && current_user_requires_two_factor?
    case self
    when GraphqlController
      render_error(
        format(
          _("Authentication error: enable 2FA in your profile settings to continue using GitLab: %{mfa_help_page}"),
          mfa_help_page: mfa_help_page_url
        ),
        status: :unauthorized
      )
    else
      redirect_to profile_two_factor_auth_path
    end
  end
end

#current_user_requires_two_factor?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'app/controllers/concerns/enforces_two_factor_authentication.rb', line 44

def current_user_requires_two_factor?
  two_factor_verifier.current_user_needs_to_setup_two_factor? && !skip_two_factor?
end

#mfa_help_page_urlObject



83
84
85
86
87
88
# File 'app/controllers/concerns/enforces_two_factor_authentication.rb', line 83

def mfa_help_page_url
  Rails.application.routes.url_helpers.help_page_url(
    'user/profile/account/two_factor_authentication.html',
    anchor: 'enable-two-factor-authentication'
  )
end

#skip_two_factor?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'app/controllers/concerns/enforces_two_factor_authentication.rb', line 75

def skip_two_factor?
  session[:skip_two_factor] && session[:skip_two_factor] > Time.current
end

#two_factor_authentication_reason(global: -> {}, group: -> {}) ⇒ Object

rubocop: disable CodeReuse/ActiveRecord



49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/concerns/enforces_two_factor_authentication.rb', line 49

def two_factor_authentication_reason(global: -> {}, group: -> {})
  if two_factor_authentication_required?
    if Gitlab::CurrentSettings.require_two_factor_authentication?
      global.call
    else
      groups = current_user.source_groups_of_two_factor_authentication_requirement.reorder(name: :asc)
      group.call(groups)
    end
  end
end

#two_factor_authentication_required?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'app/controllers/concerns/enforces_two_factor_authentication.rb', line 40

def two_factor_authentication_required?
  two_factor_verifier.two_factor_authentication_required?
end

#two_factor_grace_periodObject

rubocop: enable CodeReuse/ActiveRecord



61
62
63
# File 'app/controllers/concerns/enforces_two_factor_authentication.rb', line 61

def two_factor_grace_period
  two_factor_verifier.two_factor_grace_period
end

#two_factor_grace_period_expired?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'app/controllers/concerns/enforces_two_factor_authentication.rb', line 65

def two_factor_grace_period_expired?
  two_factor_verifier.two_factor_grace_period_expired?
end

#two_factor_skippable?Boolean

Returns:

  • (Boolean)


69
70
71
72
73
# File 'app/controllers/concerns/enforces_two_factor_authentication.rb', line 69

def two_factor_skippable?
  two_factor_authentication_required? &&
    !current_user.two_factor_enabled? &&
    !two_factor_grace_period_expired?
end

#two_factor_verifierObject



79
80
81
# File 'app/controllers/concerns/enforces_two_factor_authentication.rb', line 79

def two_factor_verifier
  @two_factor_verifier ||= Gitlab::Auth::TwoFactorAuthVerifier.new(current_user, request) # rubocop:disable Gitlab/ModuleWithInstanceVariables
end