gitlab-org--gitlab-foss/lib/gitlab/backend/grack_auth.rb

106 lines
2.4 KiB
Ruby
Raw Normal View History

2013-02-14 13:25:55 +00:00
require_relative 'shell_env'
require_relative 'grack_helpers'
2013-02-14 13:25:55 +00:00
module Grack
class Auth < Rack::Auth::Basic
include Helpers
attr_accessor :user, :project, :env
def call(env)
@env = env
@request = Rack::Request.new(env)
@auth = Request.new(env)
2013-01-10 18:17:57 +00:00
# Need this patch due to the rails mount
# Need this if under RELATIVE_URL_ROOT
unless Gitlab.config.gitlab.relative_url_root.empty?
# If website is mounted using relative_url_root need to remove it first
@env['PATH_INFO'] = @request.path.sub(Gitlab.config.gitlab.relative_url_root,'')
else
@env['PATH_INFO'] = @request.path
end
@env['SCRIPT_NAME'] = ""
auth!
end
private
def auth!
return render_not_found unless project
if @auth.provided?
return bad_request unless @auth.basic?
# Authentication with username and password
login, password = @auth.credentials
2013-05-24 17:36:28 +00:00
# Allow authentication for GitLab CI service
# if valid token passed
if login == "gitlab-ci-token" && project.gitlab_ci?
token = project.gitlab_ci_service.token
if token.present? && token == password && service_name == 'git-upload-pack'
return @app.call(env)
end
end
@user = authenticate_user(login, password)
2013-05-24 17:36:28 +00:00
if @user
Gitlab::ShellEnv.set_env(@user)
@env['REMOTE_USER'] = @auth.username
else
return unauthorized
end
else
return unauthorized unless project.public?
end
if authorized_git_request?
@app.call(env)
else
unauthorized
end
end
def authorized_git_request?
authorize_request(service_name)
end
def authenticate_user(login, password)
2013-07-16 08:28:19 +00:00
auth = Gitlab::Auth.new
auth.find(login, password)
2013-05-24 17:36:28 +00:00
end
def authorize_request(service)
case service
when 'git-upload-pack'
# Serve only upload request.
# Authorization on push will be serverd by update hook in repository
Gitlab::GitAccess.new.download_allowed?(user, project)
else
true
end
end
def service_name
if @request.get?
@request.params['service']
elsif @request.post?
File.basename(@request.path)
else
nil
end
end
def project
@project ||= project_by_path(@request.path_info)
end
end
end