Line data Source code
1 : // Copyright (C) 2018 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.httpd; 16 : 17 : import static com.google.gerrit.extensions.api.lfs.LfsDefinitions.LFS_URL_WO_AUTH_REGEX; 18 : 19 : import com.google.gerrit.extensions.client.GitBasicAuthPolicy; 20 : import com.google.gerrit.server.config.AuthConfig; 21 : import com.google.inject.Inject; 22 : import com.google.inject.servlet.ServletModule; 23 : import javax.servlet.Filter; 24 : 25 : /** Configures filter for authenticating REST requests. */ 26 : public class GerritAuthModule extends ServletModule { 27 99 : static final String NOT_AUTHORIZED_LFS_URL_REGEX = "^(?:(?!/a/))" + LFS_URL_WO_AUTH_REGEX; 28 : private final AuthConfig authConfig; 29 : 30 : @Inject 31 99 : GerritAuthModule(AuthConfig authConfig) { 32 99 : this.authConfig = authConfig; 33 99 : } 34 : 35 : @Override 36 : protected void configureServlets() { 37 99 : Class<? extends Filter> authFilter = retreiveAuthFilterFromConfig(authConfig); 38 : 39 99 : filterRegex(NOT_AUTHORIZED_LFS_URL_REGEX).through(authFilter); 40 99 : filter("/a/*").through(authFilter); 41 99 : } 42 : 43 : static Class<? extends Filter> retreiveAuthFilterFromConfig(AuthConfig authConfig) { 44 : Class<? extends Filter> authFilter; 45 99 : if (authConfig.isTrustContainerAuth()) { 46 0 : authFilter = ContainerAuthFilter.class; 47 : } else { 48 : authFilter = 49 99 : authConfig.getGitBasicAuthPolicy() == GitBasicAuthPolicy.OAUTH 50 0 : ? ProjectOAuthFilter.class 51 99 : : ProjectBasicAuthFilter.class; 52 : } 53 99 : return authFilter; 54 : } 55 : }