sessions_controller.rb 3.9 KB
Newer Older
1
class SessionsController < Devise::SessionsController
2
  include AuthenticatesWithTwoFactor
3
  include Devise::Controllers::Rememberable
4
  include Recaptcha::ClientHelper
5

6 7
  skip_before_action :check_2fa_requirement, only: [:destroy]

8
  prepend_before_action :check_initial_setup, only: [:new]
9 10
  prepend_before_action :authenticate_with_two_factor,
    if: :two_factor_enabled?, only: [:create]
11
  prepend_before_action :store_redirect_path, only: [:new]
12

13
  before_action :auto_sign_in_with_provider, only: [:new]
14
  before_action :load_recaptcha
15

16
  def new
17
    set_minimum_password_length
D
Douwe Maan 已提交
18 19
    @ldap_servers = if Gitlab.config.ldap.enabled
      Gitlab::LDAP::Config.servers
V
Valery Sizov 已提交
20
    else
D
Douwe Maan 已提交
21
      []
22 23
    end

24 25 26 27
    super
  end

  def create
28
    super do |resource|
R
Robert Speicher 已提交
29
      # User has successfully signed in, so clear any unused reset token
30 31 32 33
      if resource.reset_password_token.present?
        resource.update_attributes(reset_password_token: nil,
                                   reset_password_sent_at: nil)
      end
34 35
      # hide the signed-in notification
      flash[:notice] = nil
36
      log_audit_event(current_user, with: authentication_method)
37
    end
38
  end
39

J
jnoortheen 已提交
40 41 42 43 44 45
  def destroy
    super
    # hide the signed_out notice
    flash[:notice] = nil
  end

46 47
  private

48 49 50
  # Handle an "initial setup" state, where there's only one user, it's an admin,
  # and they require a password change.
  def check_initial_setup
51
    return unless User.limit(2).count == 1 # Count as much 2 to know if we have exactly one
52 53 54 55 56 57 58 59 60 61 62 63

    user = User.admins.last

    return unless user && user.require_password?

    token = user.generate_reset_token
    user.save

    redirect_to edit_user_password_path(reset_password_token: token),
      notice: "Please create a password for your new account."
  end

64
  def user_params
65
    params.require(:user).permit(:login, :password, :remember_me, :otp_attempt, :device_response)
66 67
  end

R
Robert Speicher 已提交
68
  def find_user
69
    if session[:otp_user_id]
R
Robert Speicher 已提交
70
      User.find(session[:otp_user_id])
71 72
    elsif user_params[:login]
      User.by_login(user_params[:login])
R
Robert Speicher 已提交
73 74
    end
  end
75

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
  def store_redirect_path
    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

    # Prevent a 'you are already signed in' message directly after signing:
    # we should never redirect to '/users/sign_in' after signing in successfully.
    unless redirect_path == new_user_session_path
      store_location_for(:redirect, redirect_path)
    end
  end
R
Robert Speicher 已提交
95

96 97 98 99
  def two_factor_enabled?
    find_user.try(:two_factor_enabled?)
  end

100 101 102 103
  def auto_sign_in_with_provider
    provider = Gitlab.config.omniauth.auto_sign_in_with_provider
    return unless provider.present?

104 105
    # 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
106 107
    # to do nothing to prevent redirection loops with certain Omniauth providers.
    return unless flash[:alert].blank? || flash[:alert] == I18n.t('devise.failure.unauthenticated')
108

109
    # Prevent alert from popping up on the first page shown after authentication.
110 111
    flash[:alert] = nil

112
    redirect_to omniauth_authorize_path(:user, provider)
113 114
  end

R
Robert Speicher 已提交
115
  def valid_otp_attempt?(user)
116
    user.validate_and_consume_otp!(user_params[:otp_attempt]) ||
117
      user.invalidate_otp_backup_code!(user_params[:otp_attempt])
118
  end
V
Valery Sizov 已提交
119 120 121 122 123

  def log_audit_event(user, options = {})
    AuditEventService.new(user, user, options).
      for_authentication.security_event
  end
124 125 126 127

  def load_recaptcha
    Gitlab::Recaptcha.load_configurations!
  end
128 129 130 131 132 133 134 135 136 137

  def authentication_method
    if user_params[:otp_attempt]
      "two-factor"
    elsif user_params[:device_response]
      "two-factor-via-u2f-device"
    else
      "standard"
    end
  end
138
end