001package org.apache.maven.wagon.providers.http;
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.http.HttpHost;
023import org.apache.http.auth.AuthScope;
024
025/**
026 * @since 2.8
027 */
028public class BasicAuthScope
029{
030    private String host;
031
032    private String port;
033
034    private String realm;
035
036    /**
037     * @return the host
038     */
039    public String getHost()
040    {
041        return host;
042    }
043
044    /**
045     * @param host the host to set
046     */
047    public void setHost( String host )
048    {
049        this.host = host;
050    }
051
052    /**
053     * @return the realm
054     */
055    public String getRealm()
056    {
057        return realm;
058    }
059
060    /**
061     * @param realm the realm to set
062     */
063    public void setRealm( String realm )
064    {
065        this.realm = realm;
066    }
067
068    /**
069     * Create an authScope given the /repository/host and /repository/password
070     * and the /server/basicAuth or /server/proxyBasicAuth host, port and realm
071     * settings. The basicAuth setting should override the repository settings
072     * host and/or port if host, port or realm is set to "ANY".
073     * <p/>
074     * Realm can also be set to a specific string and will be set if
075     * /server/basicAuthentication/realm is non-null
076     *
077     * @param host The server setting's /server/host value
078     * @param port The server setting's /server/port value
079     * @return
080     */
081    public AuthScope getScope( String host, int port )
082    {
083        if ( getHost() != null //
084            && "ANY".compareTo( getHost() ) == 0 //
085            && getPort() != null //
086            && "ANY".compareTo( getPort() ) == 0 //
087            && getRealm() != null //
088            && "ANY".compareTo( getRealm() ) == 0 )
089        {
090            return AuthScope.ANY;
091        }
092        String scopeHost = host;
093        if ( getHost() != null )
094        {
095            if ( "ANY".compareTo( getHost() ) == 0 )
096            {
097                scopeHost = AuthScope.ANY_HOST;
098            }
099            else
100            {
101                scopeHost = getHost();
102            }
103        }
104
105        int scopePort = port > -1 ? port : AuthScope.ANY_PORT;
106        // -1 for server/port settings does this, but providing an override here
107        // in
108        // the BasicAuthScope config
109        if ( getPort() != null )
110        {
111            if ( "ANY".compareTo( getPort() ) == 0 )
112            {
113                scopePort = AuthScope.ANY_PORT;
114            }
115            else
116            {
117                scopePort = Integer.parseInt( getPort() );
118            }
119        }
120
121        String scopeRealm = AuthScope.ANY_REALM;
122        if ( getRealm() != null )
123        {
124            if ( "ANY".compareTo( getRealm() ) != 0 )
125            {
126                scopeRealm = getRealm();
127            }
128            else
129            {
130                scopeRealm = getRealm();
131            }
132        }
133
134        return new AuthScope( scopeHost, scopePort, scopeRealm );
135    }
136
137    /**
138     * @return the port
139     */
140    public String getPort()
141    {
142        return port;
143    }
144
145    /**
146     * @param port the port to set
147     */
148    public void setPort( String port )
149    {
150        this.port = port;
151    }
152
153    /**
154     * Given a HttpHost, return scope with overrides from appropriate basicAuth
155     * configuration.
156     * <p>
157     * Note: Protocol is ignored. AuthScope impl ignores it as well, but if that
158     * changed, there could be a problem.
159     * </p>
160     *
161     * @param targetHost
162     * @return
163     */
164    public AuthScope getScope( HttpHost targetHost )
165    {
166        return getScope( targetHost.getHostName(), targetHost.getPort() );
167    }
168}