1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
43
44
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 }