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 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   * Default implementation of {@link AuthenticationStrategy}
56   *
57   * @since 5.0
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       * Default instance of {@link DefaultAuthenticationStrategy}.
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      @Override
76      public List<AuthScheme> select(
77              final ChallengeType challengeType,
78              final Map<String, AuthChallenge> challenges,
79              final HttpContext context) {
80          Args.notNull(challengeType, "ChallengeType");
81          Args.notNull(challenges, "Map of auth challenges");
82          Args.notNull(context, "HTTP context");
83          final HttpClientContext clientContext = HttpClientContext.cast(context);
84          final String exchangeId = clientContext.getExchangeId();
85  
86          final List<AuthScheme> options = new ArrayList<>();
87          final Lookup<AuthSchemeFactory> registry = clientContext.getAuthSchemeRegistry();
88          if (registry == null) {
89              if (LOG.isDebugEnabled()) {
90                  LOG.debug("{} Auth scheme registry not set in the context", exchangeId);
91              }
92              return options;
93          }
94          final RequestConfig config = clientContext.getRequestConfigOrDefault();
95          Collection<String> authPrefs = challengeType == ChallengeType.TARGET ?
96                  config.getTargetPreferredAuthSchemes() : config.getProxyPreferredAuthSchemes();
97          if (authPrefs == null) {
98              authPrefs = DEFAULT_SCHEME_PRIORITY;
99          }
100         if (LOG.isDebugEnabled()) {
101             LOG.debug("{} Authentication schemes in the order of preference: {}", exchangeId, authPrefs);
102         }
103 
104         for (final String schemeName: authPrefs) {
105             final AuthChallenge challenge = challenges.get(schemeName.toLowerCase(Locale.ROOT));
106             if (challenge != null) {
107                 final AuthSchemeFactory authSchemeFactory = registry.lookup(schemeName);
108                 if (authSchemeFactory == null) {
109                     if (LOG.isWarnEnabled()) {
110                         LOG.warn("{} Authentication scheme {} not supported", exchangeId, schemeName);
111                         // Try again
112                     }
113                     continue;
114                 }
115                 final AuthScheme authScheme = authSchemeFactory.create(context);
116                 options.add(authScheme);
117             } else {
118                 if (LOG.isDebugEnabled()) {
119                     LOG.debug("{} Challenge for {} authentication scheme not available", exchangeId, schemeName);
120                 }
121             }
122         }
123         return options;
124     }
125 
126 }