gitlab-org--gitlab-foss/app/controllers/projects/git_http_client_controller.rb

110 lines
3.1 KiB
Ruby
Raw Normal View History

2016-07-20 16:41:26 +00:00
# This file should be identical in GitLab Community Edition and Enterprise Edition
class Projects::GitHttpClientController < Projects::ApplicationController
include ActionController::HttpAuthentication::Basic
include KerberosSpnegoHelper
attr_reader :authentication_result, :redirected_path
delegate :actor, :authentication_abilities, to: :authentication_result, allow_nil: true
delegate :type, to: :authentication_result, allow_nil: true, prefix: :auth_result
alias_method :user, :actor
alias_method :authenticated_user, :actor
2016-07-20 16:41:26 +00: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
private
def download_request?
raise NotImplementedError
end
def upload_request?
raise NotImplementedError
end
2016-07-20 16:41:26 +00:00
def authenticate_user
2016-09-16 14:07:21 +00:00
@authentication_result = Gitlab::Auth::Result.new
2016-07-20 16:41:26 +00:00
if allow_basic_auth? && basic_auth_provided?
login, password = user_name_and_password(request)
if handle_basic_authentication(login, password)
2016-07-20 16:41:26 +00:00
return # Allow access
end
elsif allow_kerberos_spnego_auth? && spnego_provided?
2016-09-20 13:41:41 +00:00
kerberos_user = find_kerberos_user
2016-09-20 13:41:41 +00:00
if kerberos_user
@authentication_result = Gitlab::Auth::Result.new(
2016-09-20 13:41:41 +00:00
kerberos_user, nil, :kerberos, Gitlab::Auth.full_authentication_abilities)
2016-07-20 16:41:26 +00:00
send_final_spnego_response
return # Allow access
end
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 16:41:26 +00:00
end
send_challenges
2018-07-02 10:43:06 +00:00
render plain: "HTTP Basic: Access denied\n", status: :unauthorized
rescue Gitlab::Auth::MissingPersonalAccessTokenError
render_missing_personal_access_token
2016-07-20 16:41:26 +00: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 project
parse_repo_path unless defined?(@project)
2016-07-20 16:41:26 +00:00
@project
end
2016-07-20 16:41:26 +00:00
def parse_repo_path
@project, @wiki, @redirected_path = Gitlab::RepoPath.parse("#{params[:namespace_id]}/#{params[:project_id]}")
2016-07-20 16:41:26 +00:00
end
def render_missing_personal_access_token
render plain: "HTTP Basic: Access denied\n" \
"You must use a personal access token with 'api' scope for Git over HTTP.\n" \
"You can generate one at #{profile_personal_access_tokens_url}",
2018-07-02 10:43:06 +00:00
status: :unauthorized
end
2016-07-20 16:41:26 +00:00
def repository
wiki? ? project.wiki.repository : project.repository
end
def wiki?
parse_repo_path unless defined?(@wiki)
2016-07-20 16:41:26 +00:00
@wiki
2016-07-20 16:41:26 +00:00
end
def handle_basic_authentication(login, password)
@authentication_result = Gitlab::Auth.find_for_git_client(
login, password, project: project, ip: request.ip)
2017-05-16 19:58:46 +00:00
@authentication_result.success?
end
2016-09-16 14:07:21 +00:00
def ci?
authentication_result.ci?(project)
2016-09-16 14:07:21 +00:00
end
2016-07-20 16:41:26 +00:00
end