sessions_controller.rb 7.5 KB
Newer Older
1 2
# frozen_string_literal: true

3
class SessionsController < Devise::SessionsController
4
  include InternalRedirect
5
  include AuthenticatesWithTwoFactor
6
  include Devise::Controllers::Rememberable
7
  include Recaptcha::ClientHelper
8
  include Recaptcha::Verify
9

10
  skip_before_action :check_two_factor_requirement, only: [:destroy]
11 12
  # replaced with :require_no_authentication_without_flash
  skip_before_action :require_no_authentication, only: [:new, :create]
13

14
  prepend_before_action :check_initial_setup, only: [:new]
15
  prepend_before_action :authenticate_with_two_factor,
16
    if: -> { action_name == 'create' && two_factor_enabled? }
17
  prepend_before_action :check_captcha, only: [:create]
T
Toon Claes 已提交
18
  prepend_before_action :store_redirect_uri, only: [:new]
19
  prepend_before_action :ldap_servers, only: [:new, :create]
20
  prepend_before_action :require_no_authentication_without_flash, only: [:new, :create]
21
  prepend_before_action :ensure_password_authentication_enabled!, if: -> { action_name == 'create' && password_based_login? }
22

23
  before_action :auto_sign_in_with_provider, only: [:new]
24
  before_action :load_recaptcha
25

26
  after_action :log_failed_login, if: -> { action_name == 'new' && failed_login? }
27 28 29 30
  helper_method :captcha_enabled?

  CAPTCHA_HEADER = 'X-GitLab-Show-Login-Captcha'.freeze

31
  def new
32
    set_minimum_password_length
33

34 35 36 37
    super
  end

  def create
38
    super do |resource|
R
Robert Speicher 已提交
39
      # User has successfully signed in, so clear any unused reset token
40
      if resource.reset_password_token.present?
L
Lin Jen-Shin 已提交
41 42
        resource.update(reset_password_token: nil,
                        reset_password_sent_at: nil)
43
      end
44

45 46
      # hide the signed-in notification
      flash[:notice] = nil
47
      log_audit_event(current_user, resource, with: authentication_method)
48
      log_user_activity(current_user)
49
    end
50
  end
51

J
jnoortheen 已提交
52
  def destroy
53
    Gitlab::AppLogger.info("User Logout: username=#{current_user.username} ip=#{request.remote_ip}")
J
jnoortheen 已提交
54 55 56 57 58
    super
    # hide the signed_out notice
    flash[:notice] = nil
  end

59 60
  private

61 62 63 64 65 66 67 68
  def require_no_authentication_without_flash
    require_no_authentication

    if flash[:alert] == I18n.t('devise.failure.already_authenticated')
      flash[:alert] = nil
    end
  end

69 70 71 72 73 74 75 76 77 78
  def captcha_enabled?
    request.headers[CAPTCHA_HEADER] && Gitlab::Recaptcha.enabled?
  end

  # From https://github.com/plataformatec/devise/wiki/How-To:-Use-Recaptcha-with-Devise#devisepasswordscontroller
  def check_captcha
    return unless user_params[:password].present?
    return unless captcha_enabled?
    return unless Gitlab::Recaptcha.load_configurations!

79 80 81 82 83
    if verify_recaptcha
      increment_successful_login_captcha_counter
    else
      increment_failed_login_captcha_counter

84
      self.resource = resource_class.new
85
      flash[:alert] = _('There was an error with the reCAPTCHA. Please solve the reCAPTCHA again.')
86 87 88 89 90 91
      flash.delete :recaptcha_error

      respond_with_navigational(resource) { render :new }
    end
  end

92 93 94 95 96 97 98 99 100 101 102 103 104 105
  def increment_failed_login_captcha_counter
    Gitlab::Metrics.counter(
      :failed_login_captcha_total,
      'Number of failed CAPTCHA attempts for logins'.freeze
    ).increment
  end

  def increment_successful_login_captcha_counter
    Gitlab::Metrics.counter(
      :successful_login_captcha_total,
      'Number of successful CAPTCHA attempts for logins'.freeze
    ).increment
  end

106 107 108 109 110 111 112 113
  ##
  # We do have some duplication between lib/gitlab/auth/activity.rb here, but
  # leaving this method here because of backwards compatibility.
  #
  def login_counter
    @login_counter ||= Gitlab::Metrics.counter(:user_session_logins_total, 'User sign in count')
  end

114
  def log_failed_login
115
    Gitlab::AppLogger.info("Failed Login: username=#{user_params[:login]} ip=#{request.remote_ip}")
116 117 118
  end

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

122 123
  # Handle an "initial setup" state, where there's only one user, it's an admin,
  # and they require a password change.
124
  # rubocop: disable CodeReuse/ActiveRecord
125
  def check_initial_setup
126
    return unless User.limit(2).count == 1 # Count as much 2 to know if we have exactly one
127 128 129

    user = User.admins.last

130
    return unless user && user.require_password_creation_for_web?
131

J
James Lopez 已提交
132
    Users::UpdateService.new(current_user, user: user).execute do |user|
J
James Lopez 已提交
133 134
      @token = user.generate_reset_token
    end
135

J
James Lopez 已提交
136
    redirect_to edit_user_password_path(reset_password_token: @token),
137
      notice: _("Please create a password for your new account.")
138
  end
139
  # rubocop: enable CodeReuse/ActiveRecord
140

141 142 143 144 145 146 147 148
  def ensure_password_authentication_enabled!
    render_403 unless Gitlab::CurrentSettings.password_authentication_enabled_for_web?
  end

  def password_based_login?
    user_params[:login].present? || user_params[:password].present?
  end

149
  def user_params
150
    params.require(:user).permit(:login, :password, :remember_me, :otp_attempt, :device_response)
151 152
  end

R
Robert Speicher 已提交
153
  def find_user
154
    if session[:otp_user_id]
R
Robert Speicher 已提交
155
      User.find(session[:otp_user_id])
156 157
    elsif user_params[:login]
      User.by_login(user_params[:login])
R
Robert Speicher 已提交
158 159
    end
  end
160

T
Toon Claes 已提交
161 162 163 164 165 166
  def stored_redirect_uri
    @redirect_to ||= stored_location_for(:redirect)
  end

  def store_redirect_uri
    redirect_uri =
167
      if request.referer.present? && (params['redirect_to_referer'] == 'yes')
T
Toon Claes 已提交
168
        URI(request.referer)
169
      else
T
Toon Claes 已提交
170
        URI(request.url)
171 172 173 174
      end

    # Prevent a 'you are already signed in' message directly after signing:
    # we should never redirect to '/users/sign_in' after signing in successfully.
T
Toon Claes 已提交
175 176
    return true if redirect_uri.path == new_user_session_path

177
    redirect_to = redirect_uri.to_s if host_allowed?(redirect_uri)
T
Toon Claes 已提交
178 179 180 181 182

    @redirect_to = redirect_to
    store_location_for(:redirect, redirect_to)
  end

183
  def two_factor_enabled?
T
Toon Claes 已提交
184
    find_user&.two_factor_enabled?
185 186
  end

187
  def auto_sign_in_with_provider
188 189
    return unless Gitlab::Auth.omniauth_enabled?

190 191 192
    provider = Gitlab.config.omniauth.auto_sign_in_with_provider
    return unless provider.present?

193 194 195 196
    # 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

197 198
    # 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
199 200
    # to do nothing to prevent redirection loops with certain Omniauth providers.
    return unless flash[:alert].blank? || flash[:alert] == I18n.t('devise.failure.unauthenticated')
201

202
    # Prevent alert from popping up on the first page shown after authentication.
203 204
    flash[:alert] = nil

205
    redirect_to omniauth_authorize_path(:user, provider)
206 207
  end

R
Robert Speicher 已提交
208
  def valid_otp_attempt?(user)
209
    user.validate_and_consume_otp!(user_params[:otp_attempt]) ||
210
      user.invalidate_otp_backup_code!(user_params[:otp_attempt])
211
  end
V
Valery Sizov 已提交
212

213
  def log_audit_event(user, resource, options = {})
214
    Gitlab::AppLogger.info("Successful Login: username=#{resource.username} ip=#{request.remote_ip} method=#{options[:with]} admin=#{resource.admin?}")
215 216
    AuditEventService.new(user, user, options)
      .for_authentication.security_event
V
Valery Sizov 已提交
217
  end
218

219
  def log_user_activity(user)
220
    login_counter.increment
221 222 223
    Users::ActivityService.new(user, 'login').execute
  end

224 225 226
  def load_recaptcha
    Gitlab::Recaptcha.load_configurations!
  end
227

228 229 230 231
  def ldap_servers
    @ldap_servers ||= Gitlab::Auth::LDAP::Config.available_servers
  end

232 233 234 235 236 237 238 239 240
  def authentication_method
    if user_params[:otp_attempt]
      "two-factor"
    elsif user_params[:device_response]
      "two-factor-via-u2f-device"
    else
      "standard"
    end
  end
241
end