2014-07-11 06:24:11 -04:00
|
|
|
class SessionsController < Devise::SessionsController
|
2015-05-14 14:24:05 -04:00
|
|
|
include AuthenticatesWithTwoFactor
|
2016-05-30 22:17:26 -04:00
|
|
|
include Devise::Controllers::Rememberable
|
2015-12-27 12:03:06 -05:00
|
|
|
include Recaptcha::ClientHelper
|
2015-05-09 17:04:02 -04:00
|
|
|
|
2017-03-07 13:48:57 -05:00
|
|
|
skip_before_action :check_two_factor_requirement, only: [:destroy]
|
2016-01-23 19:44:46 -05:00
|
|
|
|
2016-03-02 17:48:49 -05:00
|
|
|
prepend_before_action :check_initial_setup, only: [:new]
|
2016-04-07 05:19:29 -04:00
|
|
|
prepend_before_action :authenticate_with_two_factor,
|
|
|
|
if: :two_factor_enabled?, only: [:create]
|
2015-08-12 11:20:01 -04:00
|
|
|
prepend_before_action :store_redirect_path, only: [:new]
|
2016-03-02 17:48:49 -05:00
|
|
|
|
2015-05-27 11:40:21 -04:00
|
|
|
before_action :auto_sign_in_with_provider, only: [:new]
|
2015-12-28 15:21:34 -05:00
|
|
|
before_action :load_recaptcha
|
2015-03-30 21:19:01 -04:00
|
|
|
|
2014-07-11 06:24:11 -04:00
|
|
|
def new
|
2016-06-03 06:48:11 -04:00
|
|
|
set_minimum_password_length
|
2017-02-22 10:10:32 -05:00
|
|
|
@ldap_servers =
|
|
|
|
if Gitlab.config.ldap.enabled
|
|
|
|
Gitlab::LDAP::Config.servers
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
2014-10-13 07:39:54 -04:00
|
|
|
|
2014-07-11 06:24:11 -04:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2015-04-08 14:26:04 -04:00
|
|
|
super do |resource|
|
2015-05-08 20:41:53 -04:00
|
|
|
# User has successfully signed in, so clear any unused reset token
|
2015-04-08 14:26:04 -04:00
|
|
|
if resource.reset_password_token.present?
|
|
|
|
resource.update_attributes(reset_password_token: nil,
|
|
|
|
reset_password_sent_at: nil)
|
|
|
|
end
|
2016-11-29 11:31:01 -05:00
|
|
|
# hide the signed-in notification
|
|
|
|
flash[:notice] = nil
|
2016-06-06 00:52:06 -04:00
|
|
|
log_audit_event(current_user, with: authentication_method)
|
2016-10-05 10:41:32 -04:00
|
|
|
log_user_activity(current_user)
|
2015-04-08 14:26:04 -04:00
|
|
|
end
|
2014-07-11 06:24:11 -04:00
|
|
|
end
|
2015-03-30 21:19:01 -04:00
|
|
|
|
2016-12-06 12:02:30 -05:00
|
|
|
def destroy
|
|
|
|
super
|
|
|
|
# hide the signed_out notice
|
|
|
|
flash[:notice] = nil
|
|
|
|
end
|
|
|
|
|
2015-03-30 21:19:01 -04:00
|
|
|
private
|
|
|
|
|
2017-05-23 10:23:43 -04:00
|
|
|
def login_counter
|
2017-05-29 08:19:43 -04:00
|
|
|
@login_counter ||= Gitlab::Metrics.counter(:user_session_logins, 'User sign in count')
|
2017-05-16 10:32:07 -04:00
|
|
|
end
|
|
|
|
|
2016-03-02 17:48:49 -05:00
|
|
|
# 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
|
2016-06-06 09:50:58 -04:00
|
|
|
return unless User.limit(2).count == 1 # Count as much 2 to know if we have exactly one
|
2016-03-02 17:48:49 -05:00
|
|
|
|
|
|
|
user = User.admins.last
|
|
|
|
|
|
|
|
return unless user && user.require_password?
|
|
|
|
|
2017-06-23 05:34:07 -04:00
|
|
|
Users::UpdateService.new(user).execute do |user|
|
2017-06-22 05:27:37 -04:00
|
|
|
@token = user.generate_reset_token
|
|
|
|
end
|
2016-03-02 17:48:49 -05:00
|
|
|
|
2017-06-22 05:27:37 -04:00
|
|
|
redirect_to edit_user_password_path(reset_password_token: @token),
|
2016-03-02 17:48:49 -05:00
|
|
|
notice: "Please create a password for your new account."
|
|
|
|
end
|
|
|
|
|
2015-05-05 22:16:45 -04:00
|
|
|
def user_params
|
2016-06-06 00:50:39 -04:00
|
|
|
params.require(:user).permit(:login, :password, :remember_me, :otp_attempt, :device_response)
|
2015-05-05 22:16:45 -04:00
|
|
|
end
|
|
|
|
|
2015-05-08 20:41:53 -04:00
|
|
|
def find_user
|
2016-04-07 05:19:29 -04:00
|
|
|
if session[:otp_user_id]
|
2015-05-08 20:41:53 -04:00
|
|
|
User.find(session[:otp_user_id])
|
2016-04-07 05:19:29 -04:00
|
|
|
elsif user_params[:login]
|
|
|
|
User.by_login(user_params[:login])
|
2015-05-08 20:41:53 -04:00
|
|
|
end
|
|
|
|
end
|
2015-12-27 12:03:06 -05:00
|
|
|
|
2015-08-12 11:20:01 -04:00
|
|
|
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
|
2017-04-02 11:54:19 -04:00
|
|
|
referer_uri.request_uri
|
2015-08-12 11:20:01 -04:00
|
|
|
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.
|
2017-03-23 09:49:59 -04:00
|
|
|
unless URI(redirect_path).path == new_user_session_path
|
2015-08-12 11:20:01 -04:00
|
|
|
store_location_for(:redirect, redirect_path)
|
|
|
|
end
|
|
|
|
end
|
2015-05-08 20:41:53 -04:00
|
|
|
|
2016-04-07 05:19:29 -04:00
|
|
|
def two_factor_enabled?
|
|
|
|
find_user.try(:two_factor_enabled?)
|
|
|
|
end
|
|
|
|
|
2015-05-27 11:40:21 -04:00
|
|
|
def auto_sign_in_with_provider
|
|
|
|
provider = Gitlab.config.omniauth.auto_sign_in_with_provider
|
|
|
|
return unless provider.present?
|
|
|
|
|
2017-03-23 09:49:59 -04: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 12:03:06 -05: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 11:40:21 -04: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 12:03:06 -05:00
|
|
|
|
2015-05-27 11:40:21 -04:00
|
|
|
# Prevent alert from popping up on the first page shown after authentication.
|
2015-12-27 12:03:06 -05:00
|
|
|
flash[:alert] = nil
|
|
|
|
|
2016-07-25 13:40:40 -04:00
|
|
|
redirect_to omniauth_authorize_path(:user, provider)
|
2015-05-27 11:40:21 -04:00
|
|
|
end
|
|
|
|
|
2015-05-08 20:41:53 -04:00
|
|
|
def valid_otp_attempt?(user)
|
2015-09-19 21:16:18 -04:00
|
|
|
user.validate_and_consume_otp!(user_params[:otp_attempt]) ||
|
2016-12-15 17:14:20 -05:00
|
|
|
user.invalidate_otp_backup_code!(user_params[:otp_attempt])
|
2015-05-05 22:16:45 -04:00
|
|
|
end
|
2015-07-03 07:54:50 -04:00
|
|
|
|
|
|
|
def log_audit_event(user, options = {})
|
2017-06-21 09:48:12 -04:00
|
|
|
AuditEventService.new(user, user, options)
|
|
|
|
.for_authentication.security_event
|
2015-07-03 07:54:50 -04:00
|
|
|
end
|
2015-12-28 15:21:34 -05:00
|
|
|
|
2016-10-05 10:41:32 -04:00
|
|
|
def log_user_activity(user)
|
2017-05-23 10:23:43 -04:00
|
|
|
login_counter.increment
|
2016-10-05 10:41:32 -04:00
|
|
|
Users::ActivityService.new(user, 'login').execute
|
|
|
|
end
|
|
|
|
|
2015-12-28 15:21:34 -05:00
|
|
|
def load_recaptcha
|
|
|
|
Gitlab::Recaptcha.load_configurations!
|
|
|
|
end
|
2016-06-06 00:52:06 -04: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
|
2014-07-11 06:24:11 -04:00
|
|
|
end
|