LCOV - code coverage report
Current view: top level - httpd - GetUserFilter.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 26 28 92.9 %
Date: 2022-11-19 15:00:39 Functions: 6 6 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2012 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 com.google.common.collect.ImmutableMap;
      18             : import com.google.gerrit.server.CurrentUser;
      19             : import com.google.gerrit.server.config.GerritServerConfig;
      20             : import com.google.inject.Inject;
      21             : import com.google.inject.Provider;
      22             : import com.google.inject.Singleton;
      23             : import com.google.inject.servlet.ServletModule;
      24             : import java.io.IOException;
      25             : import javax.servlet.Filter;
      26             : import javax.servlet.FilterChain;
      27             : import javax.servlet.FilterConfig;
      28             : import javax.servlet.ServletException;
      29             : import javax.servlet.ServletRequest;
      30             : import javax.servlet.ServletResponse;
      31             : import javax.servlet.http.HttpServletResponse;
      32             : import org.eclipse.jgit.lib.Config;
      33             : 
      34             : /**
      35             :  * Stores user as a request attribute and/or response header, so servlets and reverse proxies can
      36             :  * access it outside of the request/response scope.
      37             :  */
      38             : @Singleton
      39             : public class GetUserFilter implements Filter {
      40             : 
      41             :   public static final String USER_ATTR_KEY = "User";
      42             : 
      43             :   public static class GetUserFilterModule extends ServletModule {
      44             : 
      45             :     private final boolean reqEnabled;
      46             :     private final boolean resEnabled;
      47             : 
      48             :     @Inject
      49          99 :     GetUserFilterModule(@GerritServerConfig Config cfg) {
      50          99 :       reqEnabled = cfg.getBoolean("http", "addUserAsRequestAttribute", true);
      51          99 :       resEnabled = cfg.getBoolean("http", "addUserAsResponseHeader", false);
      52          99 :     }
      53             : 
      54             :     @Override
      55             :     protected void configureServlets() {
      56          99 :       if (resEnabled || reqEnabled) {
      57          99 :         ImmutableMap.Builder<String, String> initParams = ImmutableMap.builder();
      58          99 :         if (reqEnabled) {
      59          99 :           initParams.put("reqEnabled", "");
      60             :         }
      61          99 :         if (resEnabled) {
      62           0 :           initParams.put("resEnabled", "");
      63             :         }
      64          99 :         filter("/*").through(GetUserFilter.class, initParams.build());
      65             :       }
      66          99 :     }
      67             :   }
      68             : 
      69             :   private final Provider<CurrentUser> userProvider;
      70             : 
      71             :   private boolean reqEnabled;
      72             :   private boolean resEnabled;
      73             : 
      74             :   @Inject
      75          99 :   GetUserFilter(Provider<CurrentUser> userProvider) {
      76          99 :     this.userProvider = userProvider;
      77          99 :   }
      78             : 
      79             :   @Override
      80             :   public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
      81             :       throws IOException, ServletException {
      82          38 :     CurrentUser user = userProvider.get();
      83          38 :     if (user != null && user.isIdentifiedUser()) {
      84          37 :       String loggableName = user.asIdentifiedUser().getLoggableName();
      85          37 :       if (reqEnabled) {
      86          37 :         req.setAttribute(USER_ATTR_KEY, loggableName);
      87             :       }
      88          37 :       if (resEnabled && resp instanceof HttpServletResponse) {
      89           0 :         ((HttpServletResponse) resp).addHeader(USER_ATTR_KEY, loggableName);
      90             :       }
      91             :     }
      92          38 :     chain.doFilter(req, resp);
      93          38 :   }
      94             : 
      95             :   @Override
      96          99 :   public void destroy() {}
      97             : 
      98             :   @Override
      99             :   public void init(FilterConfig arg0) {
     100          99 :     reqEnabled = arg0.getInitParameter("reqEnabled") != null ? true : false;
     101          99 :     resEnabled = arg0.getInitParameter("resEnabled") != null ? true : false;
     102          99 :   }
     103             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750