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.gerrit.server.CurrentUser; 18 : import com.google.inject.Inject; 19 : import com.google.inject.Provider; 20 : import com.google.inject.Singleton; 21 : import java.io.IOException; 22 : import javax.servlet.Filter; 23 : import javax.servlet.FilterChain; 24 : import javax.servlet.FilterConfig; 25 : import javax.servlet.ServletException; 26 : import javax.servlet.ServletRequest; 27 : import javax.servlet.ServletResponse; 28 : import javax.servlet.http.HttpServletResponse; 29 : 30 : /** Requires the user to be authenticated over HTTP. */ 31 : @Singleton 32 : class RequireIdentifiedUserFilter implements Filter { 33 : private final Provider<CurrentUser> user; 34 : 35 : @Inject 36 99 : RequireIdentifiedUserFilter(Provider<CurrentUser> user) { 37 99 : this.user = user; 38 99 : } 39 : 40 : @Override 41 99 : public void init(FilterConfig filterConfig) {} 42 : 43 : @Override 44 99 : public void destroy() {} 45 : 46 : @Override 47 : public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 48 : throws IOException, ServletException { 49 34 : if (user.get().isIdentifiedUser()) { 50 34 : chain.doFilter(request, response); 51 : } else { 52 31 : HttpServletResponse res = (HttpServletResponse) response; 53 31 : res.sendError(HttpServletResponse.SC_UNAUTHORIZED); 54 : } 55 34 : } 56 : }