View Javadoc
1   package org.eclipse.aether.transport.http;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.http.HttpHost;
23  import org.apache.http.auth.AuthScope;
24  import org.apache.http.auth.Credentials;
25  import org.apache.http.client.CredentialsProvider;
26  
27  /**
28   * Credentials provider that helps to isolate server from proxy credentials. Apache HttpClient uses a single provider
29   * for both server and proxy auth, using the auth scope (host, port, etc.) to select the proper credentials. With regard
30   * to redirects, we use an auth scope for server credentials that's not specific enough to not be mistaken for proxy
31   * auth. This provider helps to maintain the proper isolation.
32   */
33  final class DemuxCredentialsProvider
34      implements CredentialsProvider
35  {
36  
37      private final CredentialsProvider serverCredentialsProvider;
38  
39      private final CredentialsProvider proxyCredentialsProvider;
40  
41      private final HttpHost proxy;
42  
43      DemuxCredentialsProvider( CredentialsProvider serverCredentialsProvider,
44                                       CredentialsProvider proxyCredentialsProvider, HttpHost proxy )
45      {
46          this.serverCredentialsProvider = serverCredentialsProvider;
47          this.proxyCredentialsProvider = proxyCredentialsProvider;
48          this.proxy = proxy;
49      }
50  
51      private CredentialsProvider getDelegate( AuthScope authScope )
52      {
53          if ( proxy.getPort() == authScope.getPort() && proxy.getHostName().equalsIgnoreCase( authScope.getHost() ) )
54          {
55              return proxyCredentialsProvider;
56          }
57          return serverCredentialsProvider;
58      }
59  
60      @Override
61      public Credentials getCredentials( AuthScope authScope )
62      {
63          return getDelegate( authScope ).getCredentials( authScope );
64      }
65  
66      @Override
67      public void setCredentials( AuthScope authScope, Credentials credentials )
68      {
69          getDelegate( authScope ).setCredentials( authScope, credentials );
70      }
71  
72      @Override
73      public void clear()
74      {
75          serverCredentialsProvider.clear();
76          proxyCredentialsProvider.clear();
77      }
78  
79  }