2019-09-26 08:06:00 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Admin::SessionsController < ApplicationController
|
2020-09-24 11:09:51 -04:00
|
|
|
include AuthenticatesWithTwoFactorForAdminMode
|
2019-09-26 08:06:00 -04:00
|
|
|
include InternalRedirect
|
2020-04-24 11:09:37 -04:00
|
|
|
include RendersLdapServers
|
2019-09-26 08:06:00 -04:00
|
|
|
|
|
|
|
before_action :user_is_admin!
|
|
|
|
|
2020-10-05 17:08:47 -04:00
|
|
|
feature_category :authentication_and_authorization
|
|
|
|
|
2019-09-26 08:06:00 -04:00
|
|
|
def new
|
2019-12-11 07:08:10 -05:00
|
|
|
if current_user_mode.admin_mode?
|
|
|
|
redirect_to redirect_path, notice: _('Admin mode already enabled')
|
|
|
|
else
|
|
|
|
current_user_mode.request_admin_mode! unless current_user_mode.admin_mode_requested?
|
|
|
|
store_location_for(:redirect, redirect_path)
|
|
|
|
end
|
2019-09-26 08:06:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2020-03-13 08:09:22 -04:00
|
|
|
if two_factor_enabled_for_user?
|
|
|
|
admin_mode_authenticate_with_two_factor
|
|
|
|
elsif current_user_mode.enable_admin_mode!(password: user_params[:password])
|
2019-12-11 07:08:10 -05:00
|
|
|
redirect_to redirect_path, notice: _('Admin mode enabled')
|
2019-09-26 08:06:00 -04:00
|
|
|
else
|
2019-12-11 07:08:10 -05:00
|
|
|
flash.now[:alert] = _('Invalid login or password')
|
2019-09-26 08:06:00 -04:00
|
|
|
render :new
|
|
|
|
end
|
2019-12-11 07:08:10 -05:00
|
|
|
rescue Gitlab::Auth::CurrentUserMode::NotRequestedError
|
|
|
|
redirect_to new_admin_session_path, alert: _('Re-authentication period expired or never requested. Please try again')
|
2019-09-26 08:06:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
current_user_mode.disable_admin_mode!
|
|
|
|
|
|
|
|
redirect_to root_path, status: :found, notice: _('Admin mode disabled')
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def user_is_admin!
|
|
|
|
render_404 unless current_user&.admin?
|
|
|
|
end
|
2019-12-11 07:08:10 -05:00
|
|
|
|
2020-03-13 08:09:22 -04:00
|
|
|
def two_factor_enabled_for_user?
|
|
|
|
current_user&.two_factor_enabled?
|
|
|
|
end
|
|
|
|
|
2019-12-11 07:08:10 -05:00
|
|
|
def redirect_path
|
|
|
|
redirect_to_path = safe_redirect_path(stored_location_for(:redirect)) || safe_redirect_path_for_url(request.referer)
|
|
|
|
|
|
|
|
if redirect_to_path &&
|
|
|
|
excluded_redirect_paths.none? { |excluded| redirect_to_path.include?(excluded) }
|
|
|
|
redirect_to_path
|
|
|
|
else
|
|
|
|
admin_root_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def excluded_redirect_paths
|
|
|
|
[new_admin_session_path, admin_session_path]
|
|
|
|
end
|
2020-03-13 08:09:22 -04:00
|
|
|
|
|
|
|
def user_params
|
|
|
|
params.fetch(:user, {}).permit(:password, :otp_attempt, :device_response)
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_otp_attempt?(user)
|
2020-10-16 14:09:04 -04:00
|
|
|
otp_validation_result =
|
|
|
|
::Users::ValidateOtpService.new(user).execute(user_params[:otp_attempt])
|
|
|
|
valid_otp_attempt = otp_validation_result[:status] == :success
|
|
|
|
|
2020-03-26 08:07:48 -04:00
|
|
|
return valid_otp_attempt if Gitlab::Database.read_only?
|
|
|
|
|
|
|
|
valid_otp_attempt || user.invalidate_otp_backup_code!(user_params[:otp_attempt])
|
2020-03-13 08:09:22 -04:00
|
|
|
end
|
2019-09-26 08:06:00 -04:00
|
|
|
end
|