2013-02-14 14:25:55 +01:00
|
|
|
require_relative 'shell_env'
|
|
|
|
|
2012-06-29 05:30:31 +02:00
|
|
|
module Grack
|
|
|
|
class Auth < Rack::Auth::Basic
|
2012-10-21 11:12:14 +02:00
|
|
|
attr_accessor :user, :project
|
2012-06-29 05:30:31 +02:00
|
|
|
|
2013-01-14 15:46:55 +01:00
|
|
|
def call(env)
|
|
|
|
@env = env
|
|
|
|
@request = Rack::Request.new(env)
|
|
|
|
@auth = Request.new(env)
|
2013-01-10 19:17:57 +01:00
|
|
|
|
2013-01-14 15:46:55 +01:00
|
|
|
# Need this patch due to the rails mount
|
|
|
|
@env['PATH_INFO'] = @request.path
|
|
|
|
@env['SCRIPT_NAME'] = ""
|
2012-11-26 10:23:08 +01:00
|
|
|
|
2013-01-14 15:46:55 +01:00
|
|
|
return render_not_found unless project
|
|
|
|
return unauthorized unless project.public || @auth.provided?
|
|
|
|
return bad_request if @auth.provided? && !@auth.basic?
|
2012-11-26 10:23:08 +01:00
|
|
|
|
2013-01-14 15:46:55 +01:00
|
|
|
if valid?
|
|
|
|
if @auth.provided?
|
|
|
|
@env['REMOTE_USER'] = @auth.username
|
|
|
|
end
|
|
|
|
return @app.call(env)
|
|
|
|
else
|
|
|
|
unauthorized
|
|
|
|
end
|
|
|
|
end
|
2012-06-29 15:40:23 +02:00
|
|
|
|
2013-01-14 15:46:55 +01:00
|
|
|
def valid?
|
|
|
|
if @auth.provided?
|
|
|
|
# Authentication with username and password
|
|
|
|
login, password = @auth.credentials
|
|
|
|
self.user = User.find_by_email(login) || User.find_by_username(login)
|
|
|
|
return false unless user.try(:valid_password?, password)
|
2012-11-26 10:23:08 +01:00
|
|
|
|
2013-02-14 14:17:43 +01:00
|
|
|
Gitlab::ShellEnv.set_env(user)
|
2013-01-14 15:46:55 +01:00
|
|
|
end
|
2012-07-02 10:44:45 +02:00
|
|
|
|
2012-06-29 09:43:15 +02:00
|
|
|
# Git upload and receive
|
2012-09-17 12:36:23 +02:00
|
|
|
if @request.get?
|
2012-10-21 11:12:14 +02:00
|
|
|
validate_get_request
|
2012-09-17 12:36:23 +02:00
|
|
|
elsif @request.post?
|
2012-10-21 11:12:14 +02:00
|
|
|
validate_post_request
|
2012-06-29 12:11:37 +02:00
|
|
|
else
|
|
|
|
false
|
2012-06-29 09:43:15 +02:00
|
|
|
end
|
2012-10-21 11:12:14 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def validate_get_request
|
2013-01-14 15:46:55 +01:00
|
|
|
project.public || can?(user, :download_code, project)
|
2012-10-21 11:12:14 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def validate_post_request
|
|
|
|
if @request.path_info.end_with?('git-upload-pack')
|
2013-01-14 15:46:55 +01:00
|
|
|
project.public || can?(user, :download_code, project)
|
2012-10-21 11:12:14 +02:00
|
|
|
elsif @request.path_info.end_with?('git-receive-pack')
|
|
|
|
action = if project.protected_branch?(current_ref)
|
|
|
|
:push_code_to_protected_branches
|
|
|
|
else
|
|
|
|
:push_code
|
|
|
|
end
|
|
|
|
|
|
|
|
can?(user, action, project)
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
2012-06-29 12:11:37 +02:00
|
|
|
|
2012-10-22 17:45:42 +02:00
|
|
|
def can?(object, action, subject)
|
|
|
|
abilities.allowed?(object, action, subject)
|
|
|
|
end
|
|
|
|
|
2012-06-29 12:11:37 +02:00
|
|
|
def current_ref
|
|
|
|
if @env["HTTP_CONTENT_ENCODING"] =~ /gzip/
|
2012-08-25 09:24:21 +02:00
|
|
|
input = Zlib::GzipReader.new(@request.body).read
|
2012-06-29 12:11:37 +02:00
|
|
|
else
|
2012-08-25 09:24:21 +02:00
|
|
|
input = @request.body.read
|
2012-06-29 12:11:37 +02:00
|
|
|
end
|
2012-08-25 09:24:21 +02:00
|
|
|
# Need to reset seek point
|
|
|
|
@request.body.rewind
|
2013-01-19 10:11:25 +01:00
|
|
|
/refs\/heads\/([\w\.-]+)/.match(input).to_a.last
|
2012-06-29 12:11:37 +02:00
|
|
|
end
|
2012-10-22 17:45:42 +02:00
|
|
|
|
2013-01-14 15:46:55 +01:00
|
|
|
def project
|
|
|
|
unless instance_variable_defined? :@project
|
|
|
|
# Find project by PATH_INFO from env
|
|
|
|
if m = /^\/([\w\.\/-]+)\.git/.match(@request.path_info).to_a
|
|
|
|
@project = Project.find_with_namespace(m.last)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return @project
|
|
|
|
end
|
|
|
|
|
|
|
|
PLAIN_TYPE = {"Content-Type" => "text/plain"}
|
|
|
|
|
|
|
|
def render_not_found
|
|
|
|
[404, PLAIN_TYPE, ["Not Found"]]
|
|
|
|
end
|
|
|
|
|
2012-10-22 17:45:42 +02:00
|
|
|
protected
|
|
|
|
|
|
|
|
def abilities
|
|
|
|
@abilities ||= begin
|
|
|
|
abilities = Six.new
|
|
|
|
abilities << Ability
|
|
|
|
abilities
|
|
|
|
end
|
|
|
|
end
|
2012-06-29 09:43:15 +02:00
|
|
|
end# Auth
|
|
|
|
end# Grack
|