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
035/**
036 * 
037 */
038public class ProxyAuthenticationFilter
039    implements Filter
040{
041
042    private final String username;
043
044    private final String password;
045
046    public ProxyAuthenticationFilter( final String username, final String password )
047    {
048        this.username = username;
049        this.password = password;
050    }
051
052    public void destroy()
053    {
054    }
055
056    public void doFilter( final ServletRequest req, final ServletResponse resp, final FilterChain chain )
057        throws IOException, ServletException
058    {
059        HttpServletRequest request = (HttpServletRequest) req;
060        HttpServletResponse response = (HttpServletResponse) resp;
061
062        String header = request.getHeader( "Proxy-Authorization" );
063        if ( header == null )
064        {
065            response.setStatus( HttpServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED );
066            response.addHeader( "Proxy-Authenticate", "Basic realm=\"Squid proxy-caching web server\"" );
067            return;
068        }
069        else
070        {
071            String data = header.substring( "BASIC ".length() );
072            data = new String( Base64.decodeBase64( data ) );
073            String[] creds = data.split( ":" );
074
075            if ( !creds[0].equals( username ) || !creds[1].equals( password ) )
076            {
077                response.sendError( HttpServletResponse.SC_UNAUTHORIZED );
078            }
079        }
080
081        chain.doFilter( req, resp );
082    }
083
084    public void init( final FilterConfig filterConfig )
085        throws ServletException
086    {
087    }
088
089}