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