debian-mirror-gitlab/app/controllers/sessions_controller.rb

222 lines
6.7 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
class SessionsController < Devise::SessionsController
2018-10-15 14:42:47 +05:30
include InternalRedirect
2015-09-11 14:41:01 +05:30
include AuthenticatesWithTwoFactor
include Devise::Controllers::Rememberable
include Recaptcha::ClientHelper
2018-11-08 19:23:39 +05:30
include Recaptcha::Verify
2015-09-11 14:41:01 +05:30
2017-08-17 22:00:37 +05:30
skip_before_action :check_two_factor_requirement, only: [:destroy]
2016-04-02 18:10:28 +05:30
2016-06-02 11:05:42 +05:30
prepend_before_action :check_initial_setup, only: [:new]
prepend_before_action :authenticate_with_two_factor,
if: :two_factor_enabled?, only: [:create]
2018-11-08 19:23:39 +05:30
prepend_before_action :check_captcha, only: [:create]
2018-03-17 18:26:18 +05:30
prepend_before_action :store_redirect_uri, only: [:new]
2018-11-08 19:23:39 +05:30
prepend_before_action :ldap_servers, only: [:new, :create]
2015-09-11 14:41:01 +05:30
before_action :auto_sign_in_with_provider, only: [:new]
before_action :load_recaptcha
2015-09-11 14:41:01 +05:30
2018-03-17 18:26:18 +05:30
after_action :log_failed_login, only: [:new], if: :failed_login?
2018-11-08 19:23:39 +05:30
helper_method :captcha_enabled?
CAPTCHA_HEADER = 'X-GitLab-Show-Login-Captcha'.freeze
2014-09-02 18:07:02 +05:30
def new
set_minimum_password_length
2015-09-11 14:41:01 +05:30
super
end
def create
super do |resource|
# User has successfully signed in, so clear any unused reset token
if resource.reset_password_token.present?
2018-11-18 11:00:15 +05:30
resource.update(reset_password_token: nil,
reset_password_sent_at: nil)
2015-09-11 14:41:01 +05:30
end
2018-03-17 18:26:18 +05:30
2017-08-17 22:00:37 +05:30
# hide the signed-in notification
flash[:notice] = nil
2018-03-17 18:26:18 +05:30
log_audit_event(current_user, resource, with: authentication_method)
2017-08-17 22:00:37 +05:30
log_user_activity(current_user)
2015-09-11 14:41:01 +05:30
end
end
2017-08-17 22:00:37 +05:30
def destroy
2018-03-17 18:26:18 +05:30
Gitlab::AppLogger.info("User Logout: username=#{current_user.username} ip=#{request.remote_ip}")
2017-08-17 22:00:37 +05:30
super
# hide the signed_out notice
flash[:notice] = nil
end
2015-09-11 14:41:01 +05:30
private
2018-11-08 19:23:39 +05:30
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!
if verify_recaptcha
increment_successful_login_captcha_counter
else
increment_failed_login_captcha_counter
self.resource = resource_class.new
flash[:alert] = 'There was an error with the reCAPTCHA. Please solve the reCAPTCHA again.'
flash.delete :recaptcha_error
respond_with_navigational(resource) { render :new }
end
end
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
2018-11-18 11:00:15 +05:30
##
# 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
2018-03-17 18:26:18 +05:30
def log_failed_login
Gitlab::AppLogger.info("Failed Login: username=#{user_params[:login]} ip=#{request.remote_ip}")
end
def failed_login?
2019-01-03 12:48:30 +05:30
(options = env["warden.options"]) && options[:action] == "unauthenticated"
2018-03-17 18:26:18 +05:30
end
2016-06-02 11:05:42 +05:30
# Handle an "initial setup" state, where there's only one user, it's an admin,
# and they require a password change.
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2016-06-02 11:05:42 +05:30
def check_initial_setup
return unless User.limit(2).count == 1 # Count as much 2 to know if we have exactly one
2016-06-02 11:05:42 +05:30
user = User.admins.last
2018-03-17 18:26:18 +05:30
return unless user && user.require_password_creation_for_web?
2016-06-02 11:05:42 +05:30
2018-03-17 18:26:18 +05:30
Users::UpdateService.new(current_user, user: user).execute do |user|
2017-09-10 17:25:29 +05:30
@token = user.generate_reset_token
end
2016-06-02 11:05:42 +05:30
2017-09-10 17:25:29 +05:30
redirect_to edit_user_password_path(reset_password_token: @token),
2016-06-02 11:05:42 +05:30
notice: "Please create a password for your new account."
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2016-06-02 11:05:42 +05:30
2015-09-11 14:41:01 +05:30
def user_params
params.require(:user).permit(:login, :password, :remember_me, :otp_attempt, :device_response)
2015-09-11 14:41:01 +05:30
end
def find_user
2016-06-02 11:05:42 +05:30
if session[:otp_user_id]
2015-09-11 14:41:01 +05:30
User.find(session[:otp_user_id])
2016-06-02 11:05:42 +05:30
elsif user_params[:login]
User.by_login(user_params[:login])
2015-09-11 14:41:01 +05:30
end
end
2018-03-17 18:26:18 +05:30
def stored_redirect_uri
@redirect_to ||= stored_location_for(:redirect)
end
def store_redirect_uri
redirect_uri =
2015-04-26 12:48:37 +05:30
if request.referer.present? && (params['redirect_to_referer'] == 'yes')
2018-03-17 18:26:18 +05:30
URI(request.referer)
2015-04-26 12:48:37 +05:30
else
2018-03-17 18:26:18 +05:30
URI(request.url)
2015-04-26 12:48:37 +05:30
end
2014-09-02 18:07:02 +05:30
# Prevent a 'you are already signed in' message directly after signing:
# we should never redirect to '/users/sign_in' after signing in successfully.
2018-03-17 18:26:18 +05:30
return true if redirect_uri.path == new_user_session_path
2018-10-15 14:42:47 +05:30
redirect_to = redirect_uri.to_s if host_allowed?(redirect_uri)
2018-03-17 18:26:18 +05:30
@redirect_to = redirect_to
store_location_for(:redirect, redirect_to)
end
2016-06-02 11:05:42 +05:30
def two_factor_enabled?
2018-03-17 18:26:18 +05:30
find_user&.two_factor_enabled?
2016-06-02 11:05:42 +05:30
end
2015-09-11 14:41:01 +05:30
def auto_sign_in_with_provider
2018-11-18 11:00:15 +05:30
return unless Gitlab::Auth.omniauth_enabled?
2015-09-11 14:41:01 +05:30
provider = Gitlab.config.omniauth.auto_sign_in_with_provider
return unless provider.present?
2017-09-10 17:25:29 +05:30
# 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
# 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-09-11 14:41:01 +05:30
# to do nothing to prevent redirection loops with certain Omniauth providers.
return unless flash[:alert].blank? || flash[:alert] == I18n.t('devise.failure.unauthenticated')
2015-09-11 14:41:01 +05:30
# Prevent alert from popping up on the first page shown after authentication.
flash[:alert] = nil
2016-09-13 17:45:13 +05:30
redirect_to omniauth_authorize_path(:user, provider)
2015-09-11 14:41:01 +05:30
end
def valid_otp_attempt?(user)
2015-09-25 12:07:36 +05:30
user.validate_and_consume_otp!(user_params[:otp_attempt]) ||
2017-08-17 22:00:37 +05:30
user.invalidate_otp_backup_code!(user_params[:otp_attempt])
2015-09-11 14:41:01 +05:30
end
2018-03-17 18:26:18 +05:30
def log_audit_event(user, resource, options = {})
Gitlab::AppLogger.info("Successful Login: username=#{resource.username} ip=#{request.remote_ip} method=#{options[:with]} admin=#{resource.admin?}")
2017-09-10 17:25:29 +05:30
AuditEventService.new(user, user, options)
.for_authentication.security_event
2015-09-11 14:41:01 +05:30
end
2017-08-17 22:00:37 +05:30
def log_user_activity(user)
2017-09-10 17:25:29 +05:30
login_counter.increment
2017-08-17 22:00:37 +05:30
Users::ActivityService.new(user, 'login').execute
end
def load_recaptcha
Gitlab::Recaptcha.load_configurations!
end
2018-11-08 19:23:39 +05:30
def ldap_servers
@ldap_servers ||= Gitlab::Auth::LDAP::Config.available_servers
end
def authentication_method
if user_params[:otp_attempt]
"two-factor"
elsif user_params[:device_response]
"two-factor-via-u2f-device"
else
"standard"
end
end
2014-09-02 18:07:02 +05:30
end