Skip to content

Commit 2a70294

Browse files
author
root
committed
Make authenticate method less complex
1 parent 45651e9 commit 2a70294

File tree

1 file changed

+79
-55
lines changed

1 file changed

+79
-55
lines changed

Diff for: app/controllers/smart_http_controller.rb

+79-55
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,20 @@ class SmartHttpController < ApplicationController
1010
# prevents login action to be filtered by check_if_login_required application scope filter
1111
skip_before_filter :check_if_login_required, :verify_authenticity_token
1212

13+
before_filter :extract_parameters
14+
before_filter :find_repository
15+
before_filter :check_query
1316
before_filter :authenticate
1417

1518

1619
def index
17-
1820
@request = Rack::Request.new(request.env)
1921

2022
command, @requested_file, @rpc = match_routing(@request)
2123

2224
return render_method_not_allowed if command == 'not_allowed'
2325

24-
if !command
25-
logger.error { "###### AUTHENTICATED ######" }
26-
logger.error { "project name : #{@project.identifier}" }
27-
logger.error { "repository dir : #{@repository.url}" }
28-
if !@user.nil?
29-
logger.info { "user_name : #{@user.login}" }
30-
else
31-
logger.info { "user_name : anonymous (project is public)" }
32-
end
33-
logger.error { "command not found, exiting !" }
34-
logger.error { "##########################" }
35-
return render_not_found
36-
end
37-
3826
logger.info { "###### AUTHENTICATED ######" }
39-
logger.info { "project name : #{@project.identifier}" }
40-
logger.info { "repository dir : #{@repository.url}" }
4127
logger.info { "command : #{command}" }
4228
logger.info { "rpc : #{@rpc}" }
4329
if !@user.nil?
@@ -61,55 +47,93 @@ def index
6147
private
6248

6349

64-
def authenticate
50+
def extract_parameters
6551
git_params = params[:git_params].split('/')
66-
repo_path = params[:repo_path]
67-
is_push = (git_params[0] == 'git-receive-pack' || params[:service] == 'git-receive-pack')
52+
@repo_path = params[:repo_path]
53+
@is_push = (git_params[0] == 'git-receive-pack' || params[:service] == 'git-receive-pack')
54+
55+
logger.info { "###### AUTHENTICATION ######" }
56+
logger.info { "git_params : #{git_params.join(', ')}" }
57+
logger.info { "repo_path : #{@repo_path}" }
58+
logger.info { "is_push : #{@is_push}" }
59+
end
60+
61+
62+
def find_repository
63+
@repository = Repository::Git.find_by_path(@repo_path, :loose => true)
64+
65+
if !@repository
66+
logger.error { "Repository not found, exiting !" }
67+
logger.error { "############################" }
68+
return render_not_found
69+
elsif !@repository.is_a?(Repository::Git)
70+
logger.error { "Repository is not a Git repository, exiting !" }
71+
logger.error { "############################" }
72+
return render_not_found
73+
elsif @repository.extra[:git_http] == 0
74+
logger.error { "SmartHttp is disabled for this repository '#{@repository.gitolite_repository_name}', exiting !" }
75+
logger.error { "############################" }
76+
return render_no_access
77+
end
6878

69-
query_valid = false
79+
@project = @repository.project
80+
@allow_anonymous_read = @project.is_public
81+
82+
logger.info { "project name : #{@project.identifier}" }
83+
logger.info { "public project : #{@allow_anonymous_read}" }
84+
logger.info { "repository name : #{@repository.gitolite_repository_name}" }
85+
logger.info { "repository path : #{@repository.gitolite_repository_path}" }
86+
end
87+
88+
89+
def check_query
90+
# PUSH CASE
91+
if @is_push
92+
if !is_ssl?
93+
logger.error { "Your are trying to push data without SSL!" }
94+
logger.error { "############################" }
95+
return render_no_access
96+
else
97+
if @repository.extra[:git_http] == 1
98+
logger.info { "Valid push" }
99+
elsif @repository.extra[:git_http] == 2
100+
logger.info { "Valid push" }
101+
elsif @repository.extra[:git_http] == 3
102+
logger.info { "Invalid push, HTTPS is disabled for this repository (HTTP only)" }
103+
logger.error { "############################" }
104+
return render_no_access
105+
end
106+
end
107+
end
108+
end
109+
110+
111+
def authenticate
70112
authentication_valid = true
113+
@user = nil
71114

72-
logger.info { "###### AUTHENTICATION ######" }
73-
logger.info { "git_params : #{git_params.join(', ')}" }
74-
logger.info { "repo_path : #{repo_path}" }
75-
logger.info { "is_push : #{is_push}" }
76-
77-
if (@repository = Repository::Git.find_by_path(repo_path, :loose => true)) && @repository.is_a?(Repository::Git)
78-
if (@project = @repository.project) && @repository.extra[:git_http] != 0
79-
allow_anonymous_read = @project.is_public
80-
# Push requires HTTP enabled or valid SSL
81-
# Read is ok over HTTP for public projects
82-
if @repository.extra[:git_http] == 2 || (@repository.extra[:git_http] == 1 && is_ssl?) || !is_push && allow_anonymous_read
83-
query_valid = true
84-
if is_push || (!allow_anonymous_read)
85-
authentication_valid = false
86-
authenticate_or_request_with_http_basic do |login, password|
87-
@user = User.find_by_login(login);
88-
if @user.is_a?(User)
89-
if @user.allowed_to?( :commit_access, @project ) || ((!is_push) && @user.allowed_to?( :view_changesets, @project ))
90-
authentication_valid = @user.check_password?(password)
91-
end
92-
end
93-
authentication_valid
94-
end
115+
# Push requires valid SSL
116+
# Read is ok over HTTP for public projects
117+
if @is_push || !@allow_anonymous_read
118+
119+
authentication_valid = false
120+
121+
authenticate_or_request_with_http_basic do |login, password|
122+
@user = User.find_by_login(login)
123+
if !@user.nil?
124+
if @user.allowed_to?(:commit_access, @project) || (!@is_push && @user.allowed_to?(:view_changesets, @project))
125+
authentication_valid = @user.check_password?(password)
95126
end
96127
end
128+
129+
authentication_valid
97130
end
98-
end
99131

100-
#if authentication failed, error already rendered
101-
#so, just render case where user queried a project
102-
#that's nonexistant or for which smart http isn't active
103-
if !query_valid
104-
logger.error { "Invalid query, exiting !" }
105-
logger.error { "Your may are trying to push data without SSL!" }
106-
logger.error { "############################" }
107-
return render_no_access
108132
end
109133

110-
logger.info { "############################" }
134+
logger.info { "##########################" }
111135

112-
return query_valid && authentication_valid
136+
return authentication_valid
113137
end
114138

115139

0 commit comments

Comments
 (0)