2018-09-14 05:42:05 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-07-11 10:24:11 +00:00
|
|
|
class SessionsController < Devise::SessionsController
|
2018-05-02 18:25:21 +00:00
|
|
|
include InternalRedirect
|
2015-05-14 18:24:05 +00:00
|
|
|
include AuthenticatesWithTwoFactor
|
2016-05-31 02:17:26 +00:00
|
|
|
include Devise::Controllers::Rememberable
|
2015-12-27 17:03:06 +00:00
|
|
|
include Recaptcha::ClientHelper
|
2018-06-21 18:13:08 +00:00
|
|
|
include Recaptcha::Verify
|
2015-05-09 21:04:02 +00:00
|
|
|
|
2017-03-07 18:48:57 +00:00
|
|
|
skip_before_action :check_two_factor_requirement, only: [:destroy]
|
2019-04-24 13:23:07 +00:00
|
|
|
# replaced with :require_no_authentication_without_flash
|
|
|
|
skip_before_action :require_no_authentication, only: [:new, :create]
|
2016-01-24 00:44:46 +00:00
|
|
|
|
2016-03-02 22:48:49 +00:00
|
|
|
prepend_before_action :check_initial_setup, only: [:new]
|
2016-04-07 09:19:29 +00:00
|
|
|
prepend_before_action :authenticate_with_two_factor,
|
2019-02-27 07:41:14 +00:00
|
|
|
if: -> { action_name == 'create' && two_factor_enabled? }
|
2018-06-21 18:13:08 +00:00
|
|
|
prepend_before_action :check_captcha, only: [:create]
|
2017-09-19 07:44:58 +00:00
|
|
|
prepend_before_action :store_redirect_uri, only: [:new]
|
2018-06-21 18:13:08 +00:00
|
|
|
prepend_before_action :ldap_servers, only: [:new, :create]
|
2019-04-24 13:23:07 +00:00
|
|
|
prepend_before_action :require_no_authentication_without_flash, only: [:new, :create]
|
2019-02-27 07:41:14 +00:00
|
|
|
prepend_before_action :ensure_password_authentication_enabled!, if: -> { action_name == 'create' && password_based_login? }
|
2019-04-24 13:23:07 +00:00
|
|
|
|
2015-05-27 15:40:21 +00:00
|
|
|
before_action :auto_sign_in_with_provider, only: [:new]
|
2019-07-18 08:27:02 +00:00
|
|
|
before_action :store_unauthenticated_sessions, only: [:new]
|
|
|
|
before_action :save_failed_login, if: :action_new_and_failed_login?
|
2015-12-28 20:21:34 +00:00
|
|
|
before_action :load_recaptcha
|
2019-10-23 15:06:29 +00:00
|
|
|
before_action :frontend_tracking_data, only: [:new]
|
2015-03-31 01:19:01 +00:00
|
|
|
|
2019-07-18 08:27:02 +00:00
|
|
|
after_action :log_failed_login, if: :action_new_and_failed_login?
|
|
|
|
|
|
|
|
helper_method :captcha_enabled?, :captcha_on_login_required?
|
2018-06-21 18:13:08 +00:00
|
|
|
|
2019-07-26 07:05:50 +00:00
|
|
|
# protect_from_forgery is already prepended in ApplicationController but
|
|
|
|
# authenticate_with_two_factor which signs in the user is prepended before
|
|
|
|
# that here.
|
|
|
|
# We need to make sure CSRF token is verified before authenticating the user
|
|
|
|
# because Devise.clean_up_csrf_token_on_authentication is set to true by
|
|
|
|
# default to avoid CSRF token fixation attacks. Authenticating the user first
|
|
|
|
# would cause the CSRF token to be cleared and then
|
|
|
|
# RequestForgeryProtection#verify_authenticity_token would fail because of
|
|
|
|
# token mismatch.
|
2020-02-21 15:09:05 +00:00
|
|
|
protect_from_forgery with: :exception, prepend: true, except: :destroy
|
2019-07-26 07:05:50 +00:00
|
|
|
|
2019-08-31 19:21:10 +00:00
|
|
|
CAPTCHA_HEADER = 'X-GitLab-Show-Login-Captcha'
|
2019-07-18 08:27:02 +00:00
|
|
|
MAX_FAILED_LOGIN_ATTEMPTS = 5
|
2018-06-21 18:13:08 +00:00
|
|
|
|
2014-07-11 10:24:11 +00:00
|
|
|
def new
|
2016-06-03 10:48:11 +00:00
|
|
|
set_minimum_password_length
|
2014-10-13 11:39:54 +00:00
|
|
|
|
2014-07-11 10:24:11 +00:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2015-04-08 18:26:04 +00:00
|
|
|
super do |resource|
|
2015-05-09 00:41:53 +00:00
|
|
|
# User has successfully signed in, so clear any unused reset token
|
2015-04-08 18:26:04 +00:00
|
|
|
if resource.reset_password_token.present?
|
2018-07-02 10:43:06 +00:00
|
|
|
resource.update(reset_password_token: nil,
|
|
|
|
reset_password_sent_at: nil)
|
2015-04-08 18:26:04 +00:00
|
|
|
end
|
2018-01-11 16:34:01 +00:00
|
|
|
|
2019-10-10 00:06:44 +00:00
|
|
|
if resource.deactivated?
|
|
|
|
resource.activate
|
|
|
|
flash[:notice] = _('Welcome back! Your account had been deactivated due to inactivity but is now reactivated.')
|
|
|
|
else
|
|
|
|
# hide the default signed-in notification
|
|
|
|
flash[:notice] = nil
|
|
|
|
end
|
|
|
|
|
2017-08-23 04:40:16 +00:00
|
|
|
log_audit_event(current_user, resource, with: authentication_method)
|
2016-10-05 14:41:32 +00:00
|
|
|
log_user_activity(current_user)
|
2015-04-08 18:26:04 +00:00
|
|
|
end
|
2014-07-11 10:24:11 +00:00
|
|
|
end
|
2015-03-31 01:19:01 +00:00
|
|
|
|
2016-12-06 17:02:30 +00:00
|
|
|
def destroy
|
2017-08-23 04:40:16 +00:00
|
|
|
Gitlab::AppLogger.info("User Logout: username=#{current_user.username} ip=#{request.remote_ip}")
|
2016-12-06 17:02:30 +00:00
|
|
|
super
|
|
|
|
# hide the signed_out notice
|
|
|
|
flash[:notice] = nil
|
|
|
|
end
|
|
|
|
|
2015-03-31 01:19:01 +00:00
|
|
|
private
|
|
|
|
|
2019-04-24 13:23:07 +00:00
|
|
|
def require_no_authentication_without_flash
|
|
|
|
require_no_authentication
|
|
|
|
|
|
|
|
if flash[:alert] == I18n.t('devise.failure.already_authenticated')
|
|
|
|
flash[:alert] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-21 18:13:08 +00:00
|
|
|
def captcha_enabled?
|
|
|
|
request.headers[CAPTCHA_HEADER] && Gitlab::Recaptcha.enabled?
|
|
|
|
end
|
|
|
|
|
2019-07-18 08:27:02 +00:00
|
|
|
def captcha_on_login_required?
|
|
|
|
Gitlab::Recaptcha.enabled_on_login? && unverified_anonymous_user?
|
|
|
|
end
|
|
|
|
|
2018-06-21 18:13:08 +00:00
|
|
|
# From https://github.com/plataformatec/devise/wiki/How-To:-Use-Recaptcha-with-Devise#devisepasswordscontroller
|
|
|
|
def check_captcha
|
|
|
|
return unless user_params[:password].present?
|
2019-07-18 08:27:02 +00:00
|
|
|
return unless captcha_enabled? || captcha_on_login_required?
|
2018-06-21 18:13:08 +00:00
|
|
|
return unless Gitlab::Recaptcha.load_configurations!
|
|
|
|
|
2018-06-22 06:25:00 +00:00
|
|
|
if verify_recaptcha
|
|
|
|
increment_successful_login_captcha_counter
|
|
|
|
else
|
|
|
|
increment_failed_login_captcha_counter
|
|
|
|
|
2018-06-21 18:13:08 +00:00
|
|
|
self.resource = resource_class.new
|
2019-04-08 14:17:45 +00:00
|
|
|
flash[:alert] = _('There was an error with the reCAPTCHA. Please solve the reCAPTCHA again.')
|
2018-06-21 18:13:08 +00:00
|
|
|
flash.delete :recaptcha_error
|
|
|
|
|
|
|
|
respond_with_navigational(resource) { render :new }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-22 06:25:00 +00:00
|
|
|
def increment_failed_login_captcha_counter
|
|
|
|
Gitlab::Metrics.counter(
|
|
|
|
:failed_login_captcha_total,
|
2019-08-31 19:21:10 +00:00
|
|
|
'Number of failed CAPTCHA attempts for logins'
|
2018-06-22 06:25:00 +00:00
|
|
|
).increment
|
|
|
|
end
|
|
|
|
|
|
|
|
def increment_successful_login_captcha_counter
|
|
|
|
Gitlab::Metrics.counter(
|
|
|
|
:successful_login_captcha_total,
|
2019-08-31 19:21:10 +00:00
|
|
|
'Number of successful CAPTCHA attempts for logins'
|
2018-06-22 06:25:00 +00:00
|
|
|
).increment
|
|
|
|
end
|
|
|
|
|
2018-07-23 13:13:11 +00:00
|
|
|
##
|
|
|
|
# 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
|
|
|
|
|
2017-08-23 04:40:16 +00:00
|
|
|
def log_failed_login
|
2017-09-29 21:34:47 +00:00
|
|
|
Gitlab::AppLogger.info("Failed Login: username=#{user_params[:login]} ip=#{request.remote_ip}")
|
2017-08-23 04:40:16 +00:00
|
|
|
end
|
|
|
|
|
2019-07-18 08:27:02 +00:00
|
|
|
def action_new_and_failed_login?
|
|
|
|
action_name == 'new' && failed_login?
|
|
|
|
end
|
|
|
|
|
|
|
|
def save_failed_login
|
|
|
|
session[:failed_login_attempts] ||= 0
|
|
|
|
session[:failed_login_attempts] += 1
|
|
|
|
end
|
|
|
|
|
2017-08-23 04:40:16 +00:00
|
|
|
def failed_login?
|
2018-10-26 16:51:22 +00:00
|
|
|
(options = request.env["warden.options"]) && options[:action] == "unauthenticated"
|
2017-08-23 04:40:16 +00:00
|
|
|
end
|
|
|
|
|
2019-07-18 08:27:02 +00:00
|
|
|
# storing sessions per IP lets us check if there are associated multiple
|
|
|
|
# anonymous sessions with one IP and prevent situations when there are
|
|
|
|
# multiple attempts of logging in
|
|
|
|
def store_unauthenticated_sessions
|
|
|
|
return if current_user
|
|
|
|
|
|
|
|
Gitlab::AnonymousSession.new(request.remote_ip, session_id: request.session.id).store_session_id_per_ip
|
|
|
|
end
|
|
|
|
|
2016-03-02 22:48:49 +00:00
|
|
|
# Handle an "initial setup" state, where there's only one user, it's an admin,
|
|
|
|
# and they require a password change.
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-03-02 22:48:49 +00:00
|
|
|
def check_initial_setup
|
2016-06-06 13:50:58 +00:00
|
|
|
return unless User.limit(2).count == 1 # Count as much 2 to know if we have exactly one
|
2016-03-02 22:48:49 +00:00
|
|
|
|
|
|
|
user = User.admins.last
|
|
|
|
|
2017-11-23 13:16:14 +00:00
|
|
|
return unless user && user.require_password_creation_for_web?
|
2016-03-02 22:48:49 +00:00
|
|
|
|
2017-09-27 09:48:33 +00:00
|
|
|
Users::UpdateService.new(current_user, user: user).execute do |user|
|
2017-06-22 09:27:37 +00:00
|
|
|
@token = user.generate_reset_token
|
|
|
|
end
|
2016-03-02 22:48:49 +00:00
|
|
|
|
2017-06-22 09:27:37 +00:00
|
|
|
redirect_to edit_user_password_path(reset_password_token: @token),
|
2019-04-08 14:17:45 +00:00
|
|
|
notice: _("Please create a password for your new account.")
|
2016-03-02 22:48:49 +00:00
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-03-02 22:48:49 +00:00
|
|
|
|
2018-12-12 14:45:55 +00:00
|
|
|
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
|
|
|
|
|
2015-05-06 02:16:45 +00:00
|
|
|
def user_params
|
2016-06-06 04:50:39 +00:00
|
|
|
params.require(:user).permit(:login, :password, :remember_me, :otp_attempt, :device_response)
|
2015-05-06 02:16:45 +00:00
|
|
|
end
|
|
|
|
|
2015-05-09 00:41:53 +00:00
|
|
|
def find_user
|
2016-04-07 09:19:29 +00:00
|
|
|
if session[:otp_user_id]
|
2015-05-09 00:41:53 +00:00
|
|
|
User.find(session[:otp_user_id])
|
2016-04-07 09:19:29 +00:00
|
|
|
elsif user_params[:login]
|
|
|
|
User.by_login(user_params[:login])
|
2015-05-09 00:41:53 +00:00
|
|
|
end
|
|
|
|
end
|
2015-12-27 17:03:06 +00:00
|
|
|
|
2017-09-19 07:44:58 +00:00
|
|
|
def stored_redirect_uri
|
|
|
|
@redirect_to ||= stored_location_for(:redirect)
|
|
|
|
end
|
|
|
|
|
|
|
|
def store_redirect_uri
|
|
|
|
redirect_uri =
|
2015-08-12 15:20:01 +00:00
|
|
|
if request.referer.present? && (params['redirect_to_referer'] == 'yes')
|
2017-09-19 07:44:58 +00:00
|
|
|
URI(request.referer)
|
2015-08-12 15:20:01 +00:00
|
|
|
else
|
2017-09-19 07:44:58 +00:00
|
|
|
URI(request.url)
|
2015-08-12 15:20:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Prevent a 'you are already signed in' message directly after signing:
|
|
|
|
# we should never redirect to '/users/sign_in' after signing in successfully.
|
2017-09-19 07:44:58 +00:00
|
|
|
return true if redirect_uri.path == new_user_session_path
|
|
|
|
|
2018-05-02 18:25:21 +00:00
|
|
|
redirect_to = redirect_uri.to_s if host_allowed?(redirect_uri)
|
2017-09-19 07:44:58 +00:00
|
|
|
|
|
|
|
@redirect_to = redirect_to
|
|
|
|
store_location_for(:redirect, redirect_to)
|
|
|
|
end
|
|
|
|
|
2016-04-07 09:19:29 +00:00
|
|
|
def two_factor_enabled?
|
2017-09-19 07:44:58 +00:00
|
|
|
find_user&.two_factor_enabled?
|
2016-04-07 09:19:29 +00:00
|
|
|
end
|
|
|
|
|
2015-05-27 15:40:21 +00:00
|
|
|
def auto_sign_in_with_provider
|
2018-07-13 10:39:31 +00:00
|
|
|
return unless Gitlab::Auth.omniauth_enabled?
|
|
|
|
|
2015-05-27 15:40:21 +00:00
|
|
|
provider = Gitlab.config.omniauth.auto_sign_in_with_provider
|
|
|
|
return unless provider.present?
|
|
|
|
|
2017-03-23 13:49:59 +00:00
|
|
|
# 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
|
|
|
|
|
2015-12-27 17:03:06 +00:00
|
|
|
# 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
|
2015-05-27 15:40:21 +00:00
|
|
|
# to do nothing to prevent redirection loops with certain Omniauth providers.
|
|
|
|
return unless flash[:alert].blank? || flash[:alert] == I18n.t('devise.failure.unauthenticated')
|
2015-12-27 17:03:06 +00:00
|
|
|
|
2015-05-27 15:40:21 +00:00
|
|
|
# Prevent alert from popping up on the first page shown after authentication.
|
2015-12-27 17:03:06 +00:00
|
|
|
flash[:alert] = nil
|
|
|
|
|
2016-07-25 17:40:40 +00:00
|
|
|
redirect_to omniauth_authorize_path(:user, provider)
|
2015-05-27 15:40:21 +00:00
|
|
|
end
|
|
|
|
|
2015-05-09 00:41:53 +00:00
|
|
|
def valid_otp_attempt?(user)
|
2015-09-20 01:16:18 +00:00
|
|
|
user.validate_and_consume_otp!(user_params[:otp_attempt]) ||
|
2016-12-15 22:14:20 +00:00
|
|
|
user.invalidate_otp_backup_code!(user_params[:otp_attempt])
|
2015-05-06 02:16:45 +00:00
|
|
|
end
|
2015-07-03 11:54:50 +00:00
|
|
|
|
2017-08-23 04:40:16 +00:00
|
|
|
def log_audit_event(user, resource, options = {})
|
2017-09-28 03:54:52 +00:00
|
|
|
Gitlab::AppLogger.info("Successful Login: username=#{resource.username} ip=#{request.remote_ip} method=#{options[:with]} admin=#{resource.admin?}")
|
2017-06-21 13:48:12 +00:00
|
|
|
AuditEventService.new(user, user, options)
|
|
|
|
.for_authentication.security_event
|
2015-07-03 11:54:50 +00:00
|
|
|
end
|
2015-12-28 20:21:34 +00:00
|
|
|
|
2016-10-05 14:41:32 +00:00
|
|
|
def log_user_activity(user)
|
2017-05-23 14:23:43 +00:00
|
|
|
login_counter.increment
|
2020-01-06 09:07:42 +00:00
|
|
|
Users::ActivityService.new(user).execute
|
2016-10-05 14:41:32 +00:00
|
|
|
end
|
|
|
|
|
2015-12-28 20:21:34 +00:00
|
|
|
def load_recaptcha
|
|
|
|
Gitlab::Recaptcha.load_configurations!
|
|
|
|
end
|
2016-06-06 04:52:06 +00:00
|
|
|
|
2018-06-21 18:13:08 +00:00
|
|
|
def ldap_servers
|
2019-11-01 00:06:02 +00:00
|
|
|
@ldap_servers ||= begin
|
|
|
|
if Gitlab::Auth::LDAP::Config.sign_in_enabled?
|
|
|
|
Gitlab::Auth::LDAP::Config.available_servers
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
2018-06-21 18:13:08 +00:00
|
|
|
end
|
|
|
|
|
2019-07-18 08:27:02 +00:00
|
|
|
def unverified_anonymous_user?
|
|
|
|
exceeded_failed_login_attempts? || exceeded_anonymous_sessions?
|
|
|
|
end
|
|
|
|
|
|
|
|
def exceeded_failed_login_attempts?
|
|
|
|
session.fetch(:failed_login_attempts, 0) > MAX_FAILED_LOGIN_ATTEMPTS
|
|
|
|
end
|
|
|
|
|
|
|
|
def exceeded_anonymous_sessions?
|
|
|
|
Gitlab::AnonymousSession.new(request.remote_ip).stored_sessions >= MAX_FAILED_LOGIN_ATTEMPTS
|
|
|
|
end
|
|
|
|
|
2016-06-06 04:52:06 +00:00
|
|
|
def authentication_method
|
|
|
|
if user_params[:otp_attempt]
|
|
|
|
"two-factor"
|
|
|
|
elsif user_params[:device_response]
|
|
|
|
"two-factor-via-u2f-device"
|
|
|
|
else
|
|
|
|
"standard"
|
|
|
|
end
|
|
|
|
end
|
2019-10-23 15:06:29 +00:00
|
|
|
|
|
|
|
def frontend_tracking_data
|
|
|
|
# We want tracking data pushed to the frontend when the user is _in_ the control group
|
|
|
|
frontend_experimentation_tracking_data(:signup_flow, 'start') unless experiment_enabled?(:signup_flow)
|
|
|
|
end
|
2014-07-11 10:24:11 +00:00
|
|
|
end
|
2019-09-13 13:26:31 +00:00
|
|
|
|
|
|
|
SessionsController.prepend_if_ee('EE::SessionsController')
|