sessions_controller.rb 2.9 KB
Newer Older
1
class SessionsController < Devise::SessionsController
2
  include AuthenticatesWithTwoFactor
3

4
  prepend_before_action :authenticate_with_two_factor, only: [:create]
5
  before_action :auto_sign_in_with_provider, only: [:new]
6

7
  def new
8 9 10 11 12 13 14 15 16 17 18
    redirect_path =
      if request.referer.present? && (params['redirect_to_referer'] == 'yes')
        referer_uri = URI(request.referer)
        if referer_uri.host == Gitlab.config.gitlab.host
          referer_uri.path
        else
          request.fullpath
        end
      else
        request.fullpath
      end
M
Marin Jankovski 已提交
19

20 21
    # Prevent a 'you are already signed in' message directly after signing:
    # we should never redirect to '/users/sign_in' after signing in successfully.
22
    unless redirect_path == new_user_session_path
23 24
      store_location_for(:redirect, redirect_path)
    end
25

26
    if Gitlab.config.ldap.enabled
27
      @ldap_servers = Gitlab::LDAP::Config.servers
28 29
    end

30 31 32 33
    super
  end

  def create
34
    super do |resource|
R
Robert Speicher 已提交
35
      # User has successfully signed in, so clear any unused reset token
36 37 38 39 40
      if resource.reset_password_token.present?
        resource.update_attributes(reset_password_token: nil,
                                   reset_password_sent_at: nil)
      end
    end
41
  end
42 43 44

  private

45 46 47 48
  def user_params
    params.require(:user).permit(:login, :password, :remember_me, :otp_attempt)
  end

R
Robert Speicher 已提交
49 50 51 52 53 54 55 56
  def find_user
    if user_params[:login]
      User.by_login(user_params[:login])
    elsif user_params[:otp_attempt] && session[:otp_user_id]
      User.find(session[:otp_user_id])
    end
  end

57
  def authenticate_with_two_factor
R
Robert Speicher 已提交
58 59
    user = self.resource = find_user

60
    return unless user && user.two_factor_enabled?
61

R
Robert Speicher 已提交
62 63 64 65 66
    if user_params[:otp_attempt].present? && session[:otp_user_id]
      if valid_otp_attempt?(user)
        # Remove any lingering user data from login
        session.delete(:otp_user_id)

67
        sign_in(user) and return
68
      else
69
        flash.now[:alert] = 'Invalid two-factor code.'
70 71 72
        render :two_factor and return
      end
    else
R
Robert Speicher 已提交
73
      if user && user.valid_password?(user_params[:password])
74
        prompt_for_two_factor(user)
75 76 77
      end
    end
  end
78

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
  def auto_sign_in_with_provider
    provider = Gitlab.config.omniauth.auto_sign_in_with_provider
    return unless provider.present?

    # Auto sign in with an Omniauth provider only if the standard "you need to sign-in" alert is 
    # registered or no alert at all. In case of another alert (such as a blocked user), it is safer  
    # to do nothing to prevent redirection loops with certain Omniauth providers.
    return unless flash[:alert].blank? || flash[:alert] == I18n.t('devise.failure.unauthenticated')
    
    # Prevent alert from popping up on the first page shown after authentication.
    flash[:alert] = nil 
    
    redirect_to omniauth_authorize_path(:user, provider.to_sym)
  end

R
Robert Speicher 已提交
94 95 96
  def valid_otp_attempt?(user)
    user.valid_otp?(user_params[:otp_attempt]) ||
    user.invalidate_otp_backup_code!(user_params[:otp_attempt])
97
  end
98
end