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 java.io.IOException;
023import java.nio.charset.StandardCharsets;
024
025import javax.servlet.Filter;
026import javax.servlet.FilterChain;
027import javax.servlet.FilterConfig;
028import javax.servlet.ServletException;
029import javax.servlet.ServletRequest;
030import javax.servlet.ServletResponse;
031import javax.servlet.http.HttpServletRequest;
032
033import org.codehaus.plexus.util.Base64;
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036
037/**
038 *
039 */
040public class AuthSnoopFilter
041    implements Filter
042{
043    private static Logger logger = LoggerFactory.getLogger( AuthSnoopFilter.class );
044
045    public void destroy()
046    {
047    }
048
049    public void doFilter( final ServletRequest req, final ServletResponse response, final FilterChain chain )
050        throws IOException, ServletException
051    {
052        HttpServletRequest request = (HttpServletRequest) req;
053        String authHeader = request.getHeader( "Authorization" );
054
055        if ( authHeader != null )
056        {
057            logger.info( "Authorization: " + authHeader );
058            String data = authHeader.substring( "BASIC ".length() );
059            String decoded = new String( Base64.decodeBase64( data.getBytes( StandardCharsets.US_ASCII ) ) );
060            logger.info( decoded );
061            String[] creds = decoded.split( ":" );
062
063            logger.info( "User: " + creds[0] + "\nPassword: " + creds[1] );
064        }
065    }
066
067    public void init( final FilterConfig filterConfig )
068        throws ServletException
069    {
070    }
071
072}