James Moger
2012-02-02 d394d950100a97b7d73f0e162b64b0b8f3cef988
commit | author | age
8c9a20 1 /*
JM 2  * Copyright 2011 gitblit.com.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.gitblit;
17
18 import java.text.MessageFormat;
19
20 import com.gitblit.Constants.AccessRestrictionType;
21 import com.gitblit.models.RepositoryModel;
22 import com.gitblit.models.UserModel;
23 import com.gitblit.utils.StringUtils;
24
892570 25 /**
JM 26  * The GitFilter is an AccessRestrictionFilter which ensures that Git client
27  * requests for push, clone, or view restricted repositories are authenticated
28  * and authorized.
29  * 
30  * @author James Moger
31  * 
32  */
8c9a20 33 public class GitFilter extends AccessRestrictionFilter {
JM 34
35     protected final String gitReceivePack = "/git-receive-pack";
36
37     protected final String gitUploadPack = "/git-upload-pack";
38
39     protected final String[] suffixes = { gitReceivePack, gitUploadPack, "/info/refs", "/HEAD",
40             "/objects" };
41
892570 42     /**
JM 43      * Extract the repository name from the url.
44      * 
45      * @param url
46      * @return repository name
47      */
8c9a20 48     @Override
JM 49     protected String extractRepositoryName(String url) {
50         String repository = url;
892570 51         // get the repository name from the url by finding a known url suffix
8c9a20 52         for (String urlSuffix : suffixes) {
JM 53             if (repository.indexOf(urlSuffix) > -1) {
54                 repository = repository.substring(0, repository.indexOf(urlSuffix));
55             }
56         }
57         return repository;
58     }
59
892570 60     /**
JM 61      * Analyze the url and returns the action of the request. Return values are
62      * either "/git-receive-pack" or "/git-upload-pack".
63      * 
831469 64      * @param serverUrl
892570 65      * @return action of the request
JM 66      */
8c9a20 67     @Override
892570 68     protected String getUrlRequestAction(String suffix) {
8c9a20 69         if (!StringUtils.isEmpty(suffix)) {
JM 70             if (suffix.startsWith(gitReceivePack)) {
71                 return gitReceivePack;
72             } else if (suffix.startsWith(gitUploadPack)) {
73                 return gitUploadPack;
74             } else if (suffix.contains("?service=git-receive-pack")) {
75                 return gitReceivePack;
76             } else if (suffix.contains("?service=git-upload-pack")) {
77                 return gitUploadPack;
d40adc 78             } else {
JM 79                 return gitUploadPack;
8c9a20 80             }
JM 81         }
82         return null;
83     }
84
892570 85     /**
JM 86      * Determine if the repository requires authentication.
87      * 
88      * @param repository
89      * @return true if authentication required
90      */
8c9a20 91     @Override
JM 92     protected boolean requiresAuthentication(RepositoryModel repository) {
93         return repository.accessRestriction.atLeast(AccessRestrictionType.PUSH);
94     }
95
892570 96     /**
JM 97      * Determine if the user can access the repository and perform the specified
98      * action.
99      * 
100      * @param repository
101      * @param user
102      * @param action
103      * @return true if user may execute the action on the repository
104      */
8c9a20 105     @Override
892570 106     protected boolean canAccess(RepositoryModel repository, UserModel user, String action) {
5450d0 107         if (!GitBlit.getBoolean(Keys.git.enableGitServlet, true)) {
JM 108             // Git Servlet disabled
109             return false;
110         }
831469 111         boolean readOnly = repository.isFrozen;
JM 112         if (readOnly || repository.accessRestriction.atLeast(AccessRestrictionType.PUSH)) {
efe8ec 113             boolean authorizedUser = user.canAccessRepository(repository);
892570 114             if (action.equals(gitReceivePack)) {
8c9a20 115                 // Push request
831469 116                 if (!readOnly && authorizedUser) {
8c9a20 117                     // clone-restricted or push-authorized
JM 118                     return true;
119                 } else {
120                     // user is unauthorized to push to this repository
121                     logger.warn(MessageFormat.format("user {0} is not authorized to push to {1}",
122                             user.username, repository));
123                     return false;
124                 }
892570 125             } else if (action.equals(gitUploadPack)) {
8c9a20 126                 // Clone request
JM 127                 boolean cloneRestricted = repository.accessRestriction
128                         .atLeast(AccessRestrictionType.CLONE);
129                 if (!cloneRestricted || (cloneRestricted && authorizedUser)) {
130                     // push-restricted or clone-authorized
131                     return true;
132                 } else {
133                     // user is unauthorized to clone this repository
134                     logger.warn(MessageFormat.format("user {0} is not authorized to clone {1}",
135                             user.username, repository));
136                     return false;
137                 }
138             }
139         }
140         return true;
141     }
142 }