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 java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.Collection;
33 import java.util.Collections;
34 import java.util.List;
35 import java.util.Locale;
36 import java.util.Map;
37
38 import org.apache.hc.client5.http.AuthenticationStrategy;
39 import org.apache.hc.client5.http.auth.AuthChallenge;
40 import org.apache.hc.client5.http.auth.AuthScheme;
41 import org.apache.hc.client5.http.auth.AuthSchemeFactory;
42 import org.apache.hc.client5.http.auth.ChallengeType;
43 import org.apache.hc.client5.http.auth.StandardAuthScheme;
44 import org.apache.hc.client5.http.config.RequestConfig;
45 import org.apache.hc.client5.http.protocol.HttpClientContext;
46 import org.apache.hc.core5.annotation.Contract;
47 import org.apache.hc.core5.annotation.ThreadingBehavior;
48 import org.apache.hc.core5.http.config.Lookup;
49 import org.apache.hc.core5.http.protocol.HttpContext;
50 import org.apache.hc.core5.util.Args;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54
55
56
57
58
59 @Contract(threading = ThreadingBehavior.STATELESS)
60 public class DefaultAuthenticationStrategy implements AuthenticationStrategy {
61
62 private static final Logger LOG = LoggerFactory.getLogger(DefaultAuthenticationStrategy.class);
63
64
65
66
67 public static final DefaultAuthenticationStrategy INSTANCE = new DefaultAuthenticationStrategy();
68
69 private static final List<String> DEFAULT_SCHEME_PRIORITY =
70 Collections.unmodifiableList(Arrays.asList(
71 StandardAuthScheme.BEARER,
72 StandardAuthScheme.DIGEST,
73 StandardAuthScheme.BASIC));
74
75 protected List<String> getSchemePriority() {
76 return DEFAULT_SCHEME_PRIORITY;
77 }
78
79 @Override
80 public List<AuthScheme> select(
81 final ChallengeType challengeType,
82 final Map<String, AuthChallenge> challenges,
83 final HttpContext context) {
84 Args.notNull(challengeType, "ChallengeType");
85 Args.notNull(challenges, "Map of auth challenges");
86 Args.notNull(context, "HTTP context");
87 final HttpClientContext clientContext = HttpClientContext.cast(context);
88 final String exchangeId = clientContext.getExchangeId();
89
90 final List<AuthScheme> options = new ArrayList<>();
91 final Lookup<AuthSchemeFactory> registry = clientContext.getAuthSchemeRegistry();
92 if (registry == null) {
93 if (LOG.isDebugEnabled()) {
94 LOG.debug("{} Auth scheme registry not set in the context", exchangeId);
95 }
96 return options;
97 }
98 final RequestConfig config = clientContext.getRequestConfigOrDefault();
99 Collection<String> authPrefs = challengeType == ChallengeType.TARGET ?
100 config.getTargetPreferredAuthSchemes() : config.getProxyPreferredAuthSchemes();
101 if (authPrefs == null) {
102 authPrefs = getSchemePriority();
103 }
104 if (LOG.isDebugEnabled()) {
105 LOG.debug("{} Authentication schemes in the order of preference: {}", exchangeId, authPrefs);
106 }
107
108 for (final String schemeName: authPrefs) {
109 final AuthChallenge challenge = challenges.get(schemeName.toLowerCase(Locale.ROOT));
110 if (challenge != null) {
111 final AuthSchemeFactory authSchemeFactory = registry.lookup(schemeName);
112 if (authSchemeFactory == null) {
113 if (LOG.isWarnEnabled()) {
114 LOG.warn("{} Authentication scheme {} not supported", exchangeId, schemeName);
115
116 }
117 continue;
118 }
119 final AuthScheme authScheme = authSchemeFactory.create(context);
120 options.add(authScheme);
121 } else {
122 if (LOG.isDebugEnabled()) {
123 LOG.debug("{} Challenge for {} authentication scheme not available", exchangeId, schemeName);
124 }
125 }
126 }
127 return options;
128 }
129
130 }