2012-06-29 05:30:31 +02:00
|
|
|
module Grack
|
|
|
|
class Auth < Rack::Auth::Basic
|
|
|
|
|
|
|
|
def valid?
|
2012-06-29 09:43:15 +02:00
|
|
|
# Authentication with username and password
|
|
|
|
email, password = @auth.credentials
|
|
|
|
user = User.find_by_email(email)
|
2012-06-29 15:40:23 +02:00
|
|
|
return false unless user.try(:valid_password?, password)
|
|
|
|
|
2012-07-02 10:44:45 +02:00
|
|
|
# Set GL_USER env variable
|
|
|
|
ENV['GL_USER'] = email
|
|
|
|
# Pass Gitolite update hook
|
|
|
|
ENV['GL_BYPASS_UPDATE_HOOK'] = "true"
|
|
|
|
|
2012-09-17 12:36:23 +02:00
|
|
|
# Need this patch due to the rails mount
|
|
|
|
@env['PATH_INFO'] = @request.path
|
|
|
|
@env['SCRIPT_NAME'] = ""
|
2012-06-29 09:43:15 +02:00
|
|
|
|
|
|
|
# Find project by PATH_INFO from env
|
2012-09-17 12:36:23 +02:00
|
|
|
if m = /^\/([\w-]+).git/.match(@request.path_info).to_a
|
2012-06-29 09:43:15 +02:00
|
|
|
return false unless project = Project.find_by_path(m.last)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Git upload and receive
|
2012-09-17 12:36:23 +02:00
|
|
|
if @request.get?
|
2012-06-29 09:43:15 +02:00
|
|
|
true
|
2012-09-17 12:36:23 +02:00
|
|
|
elsif @request.post?
|
|
|
|
if @request.path_info.end_with?('git-upload-pack')
|
2012-06-29 09:43:15 +02:00
|
|
|
return project.dev_access_for?(user)
|
2012-09-17 12:36:23 +02:00
|
|
|
elsif @request.path_info.end_with?('git-receive-pack')
|
2012-06-29 12:11:37 +02:00
|
|
|
if project.protected_branches.map(&:name).include?(current_ref)
|
|
|
|
project.master_access_for?(user)
|
|
|
|
else
|
|
|
|
project.dev_access_for?(user)
|
|
|
|
end
|
2012-06-29 09:43:15 +02:00
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
2012-06-29 12:11:37 +02:00
|
|
|
else
|
|
|
|
false
|
2012-06-29 09:43:15 +02:00
|
|
|
end
|
|
|
|
end# valid?
|
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
|
|
|
|
/refs\/heads\/([\w-]+)/.match(input).to_a.first
|
2012-06-29 12:11:37 +02:00
|
|
|
end
|
2012-06-29 09:43:15 +02:00
|
|
|
end# Auth
|
|
|
|
end# Grack
|