View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.client5.http.impl;
29  
30  import org.apache.hc.client5.http.HttpRoute;
31  import org.apache.hc.client5.http.auth.StandardAuthScheme;
32  import org.apache.hc.client5.http.auth.AuthScope;
33  import org.apache.hc.client5.http.auth.CredentialsStore;
34  import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
35  import org.apache.hc.core5.annotation.Internal;
36  import org.apache.hc.core5.http.HttpHost;
37  import org.apache.hc.core5.http.HttpRequest;
38  import org.apache.hc.core5.net.URIAuthority;
39  import org.apache.hc.core5.util.Args;
40  
41  /**
42   * Authentication support methods.
43   *
44   * @since 5.0
45   */
46  @Internal
47  public class AuthSupport {
48  
49      public static void extractFromAuthority(
50              final String scheme,
51              final URIAuthority authority,
52              final CredentialsStore credentialsStore) {
53          Args.notNull(credentialsStore, "Credentials store");
54          if (authority == null) {
55              return;
56          }
57          final String userInfo = authority.getUserInfo();
58          if (userInfo == null) {
59              return;
60          }
61          final int atColon = userInfo.indexOf(':');
62          final String userName = atColon >= 0 ? userInfo.substring(0, atColon) : userInfo;
63          final char[] password = atColon >= 0 ? userInfo.substring(atColon + 1).toCharArray() : null;
64  
65          credentialsStore.setCredentials(
66                  new AuthScope(scheme, authority.getHostName(), authority.getPort(), null, StandardAuthScheme.BASIC),
67                  new UsernamePasswordCredentials(userName, password));
68      }
69  
70      public static HttpHost resolveAuthTarget(final HttpRequest request, final HttpRoute route) {
71          Args.notNull(request, "Request");
72          Args.notNull(route, "Route");
73          final URIAuthority authority = request.getAuthority();
74          final String scheme = request.getScheme();
75          final HttpHost target = authority != null ? new HttpHost(scheme, authority) : route.getTargetHost();
76          if (target.getPort() < 0) {
77              return new HttpHost(
78                      target.getSchemeName(),
79                      target.getHostName(),
80                      route.getTargetHost().getPort());
81          }
82          return target;
83      }
84  
85  }