001package org.apache.maven.wagon.tck.http.fixture;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.commons.codec.binary.Base64;
023
024import java.io.IOException;
025
026import javax.servlet.Filter;
027import javax.servlet.FilterChain;
028import javax.servlet.FilterConfig;
029import javax.servlet.ServletException;
030import javax.servlet.ServletRequest;
031import javax.servlet.ServletResponse;
032import javax.servlet.http.HttpServletRequest;
033import javax.servlet.http.HttpServletResponse;
034
035public class ProxyAuthenticationFilter
036    implements Filter
037{
038
039    private final String username;
040
041    private final String password;
042
043    public ProxyAuthenticationFilter( final String username, final String password )
044    {
045        this.username = username;
046        this.password = password;
047    }
048
049    public void destroy()
050    {
051    }
052
053    public void doFilter( final ServletRequest req, final ServletResponse resp, final FilterChain chain )
054        throws IOException, ServletException
055    {
056        HttpServletRequest request = (HttpServletRequest) req;
057        HttpServletResponse response = (HttpServletResponse) resp;
058
059        String header = request.getHeader( "Proxy-Authorization" );
060        if ( header == null )
061        {
062            response.setStatus( HttpServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED );
063            response.addHeader( "Proxy-Authenticate", "Basic realm=\"Squid proxy-caching web server\"" );
064            return;
065        }
066        else
067        {
068            String data = header.substring( "BASIC ".length() );
069            data = new String( Base64.decodeBase64( data ) );
070            String[] creds = data.split( ":" );
071
072            if ( !creds[0].equals( username ) || !creds[1].equals( password ) )
073            {
074                response.sendError( HttpServletResponse.SC_UNAUTHORIZED );
075            }
076        }
077
078        chain.doFilter( req, resp );
079    }
080
081    public void init( final FilterConfig filterConfig )
082        throws ServletException
083    {
084    }
085
086}