debian-mirror-gitlab/lib/gitlab/backend/grack_auth.rb

165 lines
4.8 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
module Grack
2015-09-25 12:07:36 +05:30
class AuthSpawner
def self.call(env)
# Avoid issues with instance variables in Grack::Auth persisting across
# requests by creating a new instance for each request.
Auth.new({}).call(env)
end
end
2014-09-02 18:07:02 +05:30
class Auth < Rack::Auth::Basic
attr_accessor :user, :project, :env
def call(env)
@env = env
@request = Rack::Request.new(env)
@auth = Request.new(env)
2015-09-25 12:07:36 +05:30
@ci = false
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
# Need this patch due to the rails mount
2014-09-02 18:07:02 +05:30
# 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'] = ""
2015-04-26 12:48:37 +05:30
auth!
2015-11-26 14:37:03 +05:30
lfs_response = Gitlab::Lfs::Router.new(project, @user, @request).try_call
return lfs_response unless lfs_response.nil?
if @user.nil? && !@ci
2015-04-26 12:48:37 +05:30
unauthorized
2014-09-02 18:07:02 +05:30
else
render_not_found
end
end
private
def auth!
2015-04-26 12:48:37 +05:30
return unless @auth.provided?
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
return bad_request unless @auth.basic?
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
# Authentication with username and password
login, password = @auth.credentials
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
# Allow authentication for GitLab CI service
# if valid token passed
2015-09-25 12:07:36 +05:30
if ci_request?(login, password)
@ci = true
2015-04-26 12:48:37 +05:30
return
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
@user = authenticate_user(login, password)
2014-09-02 18:07:02 +05:30
end
2015-09-25 12:07:36 +05:30
def ci_request?(login, password)
matched_login = /(?<s>^[a-zA-Z]*-ci)-token$/.match(login)
if project && matched_login.present? && git_cmd == 'git-upload-pack'
2015-11-26 14:37:03 +05:30
underscored_service = matched_login['s'].underscore
2014-09-02 18:07:02 +05:30
2015-12-23 02:04:40 +05:30
if underscored_service == 'gitlab_ci'
return project && project.valid_build_token?(password)
elsif Service.available_services_names.include?(underscored_service)
2015-09-25 12:07:36 +05:30
service_method = "#{underscored_service}_service"
service = project.send(service_method)
return service && service.activated? && service.valid_token?(password)
2014-09-02 18:07:02 +05:30
end
end
false
end
2015-04-26 12:48:37 +05:30
def oauth_access_token_check(login, password)
if login == "oauth2" && git_cmd == 'git-upload-pack' && password.present?
token = Doorkeeper::AccessToken.by_token(password)
token && token.accessible? && User.find_by(id: token.resource_owner_id)
end
end
2014-09-02 18:07:02 +05:30
def authenticate_user(login, password)
user = Gitlab::Auth.find_with_user_password(login, password)
2015-04-26 12:48:37 +05:30
unless user
user = oauth_access_token_check(login, password)
end
# If the user authenticated successfully, we reset the auth failure count
# from Rack::Attack for that IP. A client may attempt to authenticate
# with a username and blank password first, and only after it receives
# a 401 error does it present a password. Resetting the count prevents
# false positives from occurring.
#
# Otherwise, we let Rack::Attack know there was a failed authentication
# attempt from this IP. This information is stored in the Rails cache
# (Redis) and will be used by the Rack::Attack middleware to decide
# whether to block requests from this IP.
config = Gitlab.config.rack_attack.git_basic_auth
if config.enabled
if user
# A successful login will reset the auth failure count from this IP
Rack::Attack::Allow2Ban.reset(@request.ip, config)
else
banned = Rack::Attack::Allow2Ban.filter(@request.ip, config) do
# Unless the IP is whitelisted, return true so that Allow2Ban
# increments the counter (stored in Rails.cache) for the IP
if config.ip_whitelist.include?(@request.ip)
false
else
true
end
end
if banned
Rails.logger.info "IP #{@request.ip} failed to login " \
"as #{login} but has been temporarily banned from Git auth"
end
end
end
user
2014-09-02 18:07:02 +05:30
end
def git_cmd
if @request.get?
@request.params['service']
elsif @request.post?
File.basename(@request.path)
else
nil
end
end
def project
2015-04-26 12:48:37 +05:30
return @project if defined?(@project)
@project = project_by_path(@request.path_info)
2014-09-02 18:07:02 +05:30
end
def project_by_path(path)
if m = /^([\w\.\/-]+)\.git/.match(path).to_a
path_with_namespace = m.last
path_with_namespace.gsub!(/\.wiki$/, '')
2015-04-26 12:48:37 +05:30
path_with_namespace[0] = '' if path_with_namespace.start_with?('/')
2014-09-02 18:07:02 +05:30
Project.find_with_namespace(path_with_namespace)
end
end
def render_not_found
2015-04-26 12:48:37 +05:30
[404, { "Content-Type" => "text/plain" }, ["Not Found"]]
2014-09-02 18:07:02 +05:30
end
end
end