2016-07-20 12:41:26 -04:00
|
|
|
# This file should be identical in GitLab Community Edition and Enterprise Edition
|
|
|
|
|
|
|
|
class Projects::GitHttpClientController < Projects::ApplicationController
|
|
|
|
include ActionController::HttpAuthentication::Basic
|
|
|
|
include KerberosSpnegoHelper
|
|
|
|
|
2016-09-16 07:34:05 -04:00
|
|
|
attr_reader :authentication_result
|
|
|
|
|
|
|
|
delegate :actor, :authentication_abilities, to: :authentication_result, allow_nil: true
|
|
|
|
|
|
|
|
alias_method :user, :actor
|
2016-07-20 12:41:26 -04:00
|
|
|
|
|
|
|
# Git clients will not know what authenticity token to send along
|
|
|
|
skip_before_action :verify_authenticity_token
|
|
|
|
skip_before_action :repository
|
|
|
|
before_action :authenticate_user
|
|
|
|
before_action :ensure_project_found!
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-11-21 10:31:51 -05:00
|
|
|
def download_request?
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def upload_request?
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2016-07-20 12:41:26 -04:00
|
|
|
def authenticate_user
|
2016-09-16 10:07:21 -04:00
|
|
|
@authentication_result = Gitlab::Auth::Result.new
|
|
|
|
|
2016-07-20 12:41:26 -04:00
|
|
|
if allow_basic_auth? && basic_auth_provided?
|
|
|
|
login, password = user_name_and_password(request)
|
2016-08-08 06:01:25 -04:00
|
|
|
|
2016-09-15 12:54:24 -04:00
|
|
|
if handle_basic_authentication(login, password)
|
2016-07-20 12:41:26 -04:00
|
|
|
return # Allow access
|
|
|
|
end
|
|
|
|
elsif allow_kerberos_spnego_auth? && spnego_provided?
|
2016-09-20 09:41:41 -04:00
|
|
|
kerberos_user = find_kerberos_user
|
2016-09-16 07:34:05 -04:00
|
|
|
|
2016-09-20 09:41:41 -04:00
|
|
|
if kerberos_user
|
2016-09-16 07:34:05 -04:00
|
|
|
@authentication_result = Gitlab::Auth::Result.new(
|
2016-09-20 09:41:41 -04:00
|
|
|
kerberos_user, nil, :kerberos, Gitlab::Auth.full_authentication_abilities)
|
2016-07-20 12:41:26 -04:00
|
|
|
|
|
|
|
send_final_spnego_response
|
|
|
|
return # Allow access
|
|
|
|
end
|
2016-11-02 17:50:44 -04:00
|
|
|
elsif project && download_request? && Guest.can?(:download_code, project)
|
|
|
|
@authentication_result = Gitlab::Auth::Result.new(nil, project, :none, [:download_code])
|
|
|
|
|
|
|
|
return # Allow access
|
2016-07-20 12:41:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
send_challenges
|
|
|
|
render plain: "HTTP Basic: Access denied\n", status: 401
|
2016-09-06 17:32:39 -04:00
|
|
|
rescue Gitlab::Auth::MissingPersonalTokenError
|
2016-08-25 18:26:20 -04:00
|
|
|
render_missing_personal_token
|
2016-07-20 12:41:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def basic_auth_provided?
|
|
|
|
has_basic_credentials?(request)
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_challenges
|
|
|
|
challenges = []
|
|
|
|
challenges << 'Basic realm="GitLab"' if allow_basic_auth?
|
|
|
|
challenges << spnego_challenge if allow_kerberos_spnego_auth?
|
|
|
|
headers['Www-Authenticate'] = challenges.join("\n") if challenges.any?
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_project_found!
|
|
|
|
render_not_found if project.blank?
|
|
|
|
end
|
|
|
|
|
|
|
|
def project
|
|
|
|
return @project if defined?(@project)
|
|
|
|
|
|
|
|
project_id, _ = project_id_with_suffix
|
|
|
|
if project_id.blank?
|
|
|
|
@project = nil
|
|
|
|
else
|
|
|
|
@project = Project.find_with_namespace("#{params[:namespace_id]}/#{project_id}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# This method returns two values so that we can parse
|
|
|
|
# params[:project_id] (untrusted input!) in exactly one place.
|
|
|
|
def project_id_with_suffix
|
|
|
|
id = params[:project_id] || ''
|
|
|
|
|
|
|
|
%w[.wiki.git .git].each do |suffix|
|
|
|
|
if id.end_with?(suffix)
|
|
|
|
# Be careful to only remove the suffix from the end of 'id'.
|
|
|
|
# Accidentally removing it from the middle is how security
|
|
|
|
# vulnerabilities happen!
|
|
|
|
return [id.slice(0, id.length - suffix.length), suffix]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Something is wrong with params[:project_id]; do not pass it on.
|
|
|
|
[nil, nil]
|
|
|
|
end
|
|
|
|
|
2016-08-15 16:47:29 -04:00
|
|
|
def render_missing_personal_token
|
2016-08-17 18:21:18 -04:00
|
|
|
render plain: "HTTP Basic: Access denied\n" \
|
|
|
|
"You have 2FA enabled, please use a personal access token for Git over HTTP.\n" \
|
2016-08-15 16:47:29 -04:00
|
|
|
"You can generate one at #{profile_personal_access_tokens_url}",
|
|
|
|
status: 401
|
2016-08-11 12:42:34 -04:00
|
|
|
end
|
|
|
|
|
2016-07-20 12:41:26 -04:00
|
|
|
def repository
|
|
|
|
_, suffix = project_id_with_suffix
|
|
|
|
if suffix == '.wiki.git'
|
|
|
|
project.wiki.repository
|
|
|
|
else
|
|
|
|
project.repository
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_not_found
|
|
|
|
render plain: 'Not Found', status: :not_found
|
|
|
|
end
|
|
|
|
|
2016-09-16 07:34:05 -04:00
|
|
|
def handle_basic_authentication(login, password)
|
|
|
|
@authentication_result = Gitlab::Auth.find_for_git_client(
|
|
|
|
login, password, project: project, ip: request.ip)
|
2016-08-19 13:10:41 -04:00
|
|
|
|
2016-09-16 07:34:05 -04:00
|
|
|
return false unless @authentication_result.success?
|
2016-09-08 13:32:20 -04:00
|
|
|
|
2016-09-16 07:34:05 -04:00
|
|
|
if download_request?
|
|
|
|
authentication_has_download_access?
|
2016-08-25 18:26:20 -04:00
|
|
|
else
|
2016-09-16 07:34:05 -04:00
|
|
|
authentication_has_upload_access?
|
2016-08-25 18:26:20 -04:00
|
|
|
end
|
2016-09-16 07:34:05 -04:00
|
|
|
end
|
|
|
|
|
2016-09-16 10:07:21 -04:00
|
|
|
def ci?
|
2016-09-20 04:24:47 -04:00
|
|
|
authentication_result.ci?(project)
|
2016-09-16 10:07:21 -04:00
|
|
|
end
|
|
|
|
|
2016-09-16 07:34:05 -04:00
|
|
|
def authentication_has_download_access?
|
|
|
|
has_authentication_ability?(:download_code) || has_authentication_ability?(:build_download_code)
|
|
|
|
end
|
|
|
|
|
|
|
|
def authentication_has_upload_access?
|
|
|
|
has_authentication_ability?(:push_code)
|
|
|
|
end
|
2016-09-15 12:54:24 -04:00
|
|
|
|
2016-09-16 10:07:21 -04:00
|
|
|
def has_authentication_ability?(capability)
|
|
|
|
(authentication_abilities || []).include?(capability)
|
2016-07-20 12:41:26 -04:00
|
|
|
end
|
2016-08-19 13:10:41 -04:00
|
|
|
|
2016-09-16 10:07:21 -04:00
|
|
|
def authentication_project
|
|
|
|
authentication_result.project
|
2016-08-08 06:01:25 -04:00
|
|
|
end
|
2016-07-20 12:41:26 -04:00
|
|
|
end
|