@@ -10,34 +10,20 @@ class SmartHttpController < ApplicationController
10
10
# prevents login action to be filtered by check_if_login_required application scope filter
11
11
skip_before_filter :check_if_login_required , :verify_authenticity_token
12
12
13
+ before_filter :extract_parameters
14
+ before_filter :find_repository
15
+ before_filter :check_query
13
16
before_filter :authenticate
14
17
15
18
16
19
def index
17
-
18
20
@request = Rack ::Request . new ( request . env )
19
21
20
22
command , @requested_file , @rpc = match_routing ( @request )
21
23
22
24
return render_method_not_allowed if command == 'not_allowed'
23
25
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
-
38
26
logger . info { "###### AUTHENTICATED ######" }
39
- logger . info { "project name : #{ @project . identifier } " }
40
- logger . info { "repository dir : #{ @repository . url } " }
41
27
logger . info { "command : #{ command } " }
42
28
logger . info { "rpc : #{ @rpc } " }
43
29
if !@user . nil?
@@ -61,55 +47,93 @@ def index
61
47
private
62
48
63
49
64
- def authenticate
50
+ def extract_parameters
65
51
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
68
78
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
70
112
authentication_valid = true
113
+ @user = nil
71
114
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 )
95
126
end
96
127
end
128
+
129
+ authentication_valid
97
130
end
98
- end
99
131
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
108
132
end
109
133
110
- logger . info { "############################ " }
134
+ logger . info { "##########################" }
111
135
112
- return query_valid && authentication_valid
136
+ return authentication_valid
113
137
end
114
138
115
139
0 commit comments