2019-09-26 12:06:00 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Admin::SessionsController < ApplicationController
|
2020-09-24 15:09:51 +00:00
|
|
|
include AuthenticatesWithTwoFactorForAdminMode
|
2019-09-26 12:06:00 +00:00
|
|
|
include InternalRedirect
|
2020-04-24 15:09:37 +00:00
|
|
|
include RendersLdapServers
|
2019-09-26 12:06:00 +00:00
|
|
|
|
|
|
|
before_action :user_is_admin!
|
|
|
|
|
2020-10-05 21:08:47 +00:00
|
|
|
feature_category :authentication_and_authorization
|
|
|
|
|
2019-09-26 12:06:00 +00:00
|
|
|
def new
|
2019-12-11 12:08:10 +00: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 12:06:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2020-03-13 12:09:22 +00: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 12:08:10 +00:00
|
|
|
redirect_to redirect_path, notice: _('Admin mode enabled')
|
2019-09-26 12:06:00 +00:00
|
|
|
else
|
2019-12-11 12:08:10 +00:00
|
|
|
flash.now[:alert] = _('Invalid login or password')
|
2019-09-26 12:06:00 +00:00
|
|
|
render :new
|
|
|
|
end
|
2019-12-11 12:08:10 +00: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 12:06:00 +00: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 12:08:10 +00:00
|
|
|
|
2020-03-13 12:09:22 +00:00
|
|
|
def two_factor_enabled_for_user?
|
|
|
|
current_user&.two_factor_enabled?
|
|
|
|
end
|
|
|
|
|
2019-12-11 12:08:10 +00: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 12:09:22 +00:00
|
|
|
|
|
|
|
def user_params
|
|
|
|
params.fetch(:user, {}).permit(:password, :otp_attempt, :device_response)
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_otp_attempt?(user)
|
2020-10-16 18:09:04 +00:00
|
|
|
otp_validation_result =
|
2022-05-12 09:08:08 +00:00
|
|
|
::Users::ValidateManualOtpService.new(user).execute(user_params[:otp_attempt])
|
2020-10-16 18:09:04 +00:00
|
|
|
valid_otp_attempt = otp_validation_result[:status] == :success
|
|
|
|
|
2021-08-02 09:10:09 +00:00
|
|
|
return valid_otp_attempt if Gitlab::Database.read_only?
|
2020-03-26 12:07:48 +00:00
|
|
|
|
|
|
|
valid_otp_attempt || user.invalidate_otp_backup_code!(user_params[:otp_attempt])
|
2020-03-13 12:09:22 +00:00
|
|
|
end
|
2019-09-26 12:06:00 +00:00
|
|
|
end
|