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

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

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 17
  after_action :log_failed_login, only: [:new]

18
  def new
19
    set_minimum_password_length
20
    @ldap_servers = Gitlab::LDAP::Config.available_servers
21

22 23 24 25
    super
  end

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

J
jnoortheen 已提交
39
  def destroy
40
    Gitlab::AppLogger.info("User Logout: username=#{current_user.username} ip=#{request.remote_ip}")
J
jnoortheen 已提交
41 42 43 44 45
    super
    # hide the signed_out notice
    flash[:notice] = nil
  end

46 47
  private

48
  def log_failed_login
49
    Gitlab::AppLogger.info("Failed Login: username=#{user_params[:login]} ip=#{request.remote_ip}") if failed_login?
50 51 52 53 54 55
  end

  def failed_login?
    (options = env["warden.options"]) && options[:action] == "unauthenticated"
  end

56
  def login_counter
B
Ben Kochie 已提交
57
    @login_counter ||= Gitlab::Metrics.counter(:user_session_logins_total, 'User sign in count')
K
Kevin Lyda 已提交
58 59
  end

60 61 62
  # 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
63
    return unless User.limit(2).count == 1 # Count as much 2 to know if we have exactly one
64 65 66

    user = User.admins.last

67
    return unless user && user.require_password_creation?
68

69
    Users::UpdateService.new(user).execute do |user|
J
James Lopez 已提交
70 71
      @token = user.generate_reset_token
    end
72

J
James Lopez 已提交
73
    redirect_to edit_user_password_path(reset_password_token: @token),
74 75 76
      notice: "Please create a password for your new account."
  end

77
  def user_params
78
    params.require(:user).permit(:login, :password, :remember_me, :otp_attempt, :device_response)
79 80
  end

R
Robert Speicher 已提交
81
  def find_user
82
    if session[:otp_user_id]
R
Robert Speicher 已提交
83
      User.find(session[:otp_user_id])
84 85
    elsif user_params[:login]
      User.by_login(user_params[:login])
R
Robert Speicher 已提交
86 87
    end
  end
88

89 90 91 92 93
  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
94
          referer_uri.request_uri
95 96 97 98 99 100 101 102 103
        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.
104
    unless URI(redirect_path).path == new_user_session_path
105 106 107
      store_location_for(:redirect, redirect_path)
    end
  end
R
Robert Speicher 已提交
108

109 110 111 112
  def two_factor_enabled?
    find_user.try(:two_factor_enabled?)
  end

113 114 115 116
  def auto_sign_in_with_provider
    provider = Gitlab.config.omniauth.auto_sign_in_with_provider
    return unless provider.present?

117 118 119 120
    # If a "auto_sign_in" query parameter is set to a falsy value, don't auto sign-in.
    # Otherwise, the default is to auto sign-in.
    return if Gitlab::Utils.to_boolean(params[:auto_sign_in]) == false

121 122
    # 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
123 124
    # to do nothing to prevent redirection loops with certain Omniauth providers.
    return unless flash[:alert].blank? || flash[:alert] == I18n.t('devise.failure.unauthenticated')
125

126
    # Prevent alert from popping up on the first page shown after authentication.
127 128
    flash[:alert] = nil

129
    redirect_to omniauth_authorize_path(:user, provider)
130 131
  end

R
Robert Speicher 已提交
132
  def valid_otp_attempt?(user)
133
    user.validate_and_consume_otp!(user_params[:otp_attempt]) ||
134
      user.invalidate_otp_backup_code!(user_params[:otp_attempt])
135
  end
V
Valery Sizov 已提交
136

137
  def log_audit_event(user, resource, options = {})
138
    Gitlab::AppLogger.info("Successful Login: username=#{resource.username} ip=#{request.remote_ip} method=#{options[:with]} admin=#{resource.admin?}")
139 140
    AuditEventService.new(user, user, options)
      .for_authentication.security_event
V
Valery Sizov 已提交
141
  end
142

143
  def log_user_activity(user)
144
    login_counter.increment
145 146 147
    Users::ActivityService.new(user, 'login').execute
  end

148 149 150
  def load_recaptcha
    Gitlab::Recaptcha.load_configurations!
  end
151 152 153 154 155 156 157 158 159 160

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