authenticates_with_two_factor.rb 3.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
# == AuthenticatesWithTwoFactor
#
# Controller concern to handle two-factor authentication
#
# Upon inclusion, skips `require_no_authentication` on `:create`.
module AuthenticatesWithTwoFactor
  extend ActiveSupport::Concern

  included do
    # This action comes from DeviseController, but because we call `sign_in`
    # manually, not skipping this action would cause a "You are already signed
    # in." error message to be shown upon successful login.
13
    skip_before_action :require_no_authentication, only: [:create], raise: false
14 15 16 17 18 19 20 21 22 23 24 25
  end

  # Store the user's ID in the session for later retrieval and render the
  # two factor code prompt
  #
  # The user must have been authenticated with a valid login and password
  # before calling this method!
  #
  # user - User record
  #
  # Returns nil
  def prompt_for_two_factor(user)
26 27 28
    # Set @user for Devise views
    @user = user # rubocop:disable Gitlab/ModuleWithInstanceVariables

29
    return locked_user_redirect(user) unless user.can?(:log_in)
30

31
    session[:otp_user_id] = user.id
32 33 34 35
    setup_u2f_authentication(user)
    render 'devise/sessions/two_factor'
  end

36 37 38 39 40
  def locked_user_redirect(user)
    flash.now[:alert] = 'Invalid Login or password'
    render 'devise/sessions/new'
  end

41 42
  def authenticate_with_two_factor
    user = self.resource = find_user
43
    return locked_user_redirect(user) unless user.can?(:log_in)
44

45
    if user_params[:otp_attempt].present? && session[:otp_user_id]
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
      authenticate_with_two_factor_via_otp(user)
    elsif user_params[:device_response].present? && session[:otp_user_id]
      authenticate_with_two_factor_via_u2f(user)
    elsif user && user.valid_password?(user_params[:password])
      prompt_for_two_factor(user)
    end
  end

  private

  def authenticate_with_two_factor_via_otp(user)
    if valid_otp_attempt?(user)
      # Remove any lingering user data from login
      session.delete(:otp_user_id)

      remember_me(user) if user_params[:remember_me] == '1'
62
      user.save!
63
      sign_in(user, message: :two_factor_authenticated)
64
    else
65
      user.increment_failed_attempts!
66
      Gitlab::AppLogger.info("Failed Login: user=#{user.username} ip=#{request.remote_ip} method=OTP")
67
      flash.now[:alert] = 'Invalid two-factor code.'
68
      prompt_for_two_factor(user)
69 70 71 72 73
    end
  end

  # Authenticate using the response from a U2F (universal 2nd factor) device
  def authenticate_with_two_factor_via_u2f(user)
74
    if U2fRegistration.authenticate(user, u2f_app_id, user_params[:device_response], session[:challenge])
75 76
      # Remove any lingering user data from login
      session.delete(:otp_user_id)
77
      session.delete(:challenge)
78

79
      remember_me(user) if user_params[:remember_me] == '1'
80
      sign_in(user, message: :two_factor_authenticated)
81
    else
82
      user.increment_failed_attempts!
83
      Gitlab::AppLogger.info("Failed Login: user=#{user.username} ip=#{request.remote_ip} method=U2F")
84 85 86 87 88 89 90 91 92 93
      flash.now[:alert] = 'Authentication via U2F device failed.'
      prompt_for_two_factor(user)
    end
  end

  # Setup in preparation of communication with a U2F (universal 2nd factor) device
  # Actual communication is performed using a Javascript API
  def setup_u2f_authentication(user)
    key_handles = user.u2f_registrations.pluck(:key_handle)
    u2f = U2F::U2F.new(u2f_app_id)
94

95 96
    if key_handles.present?
      sign_requests = u2f.authentication_requests(key_handles)
97 98
      session[:challenge] ||= u2f.challenge
      gon.push(u2f: { challenge: session[:challenge], app_id: u2f_app_id,
99
                      sign_requests: sign_requests })
100
    end
101 102
  end
end