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  package org.apache.hc.client5.http.impl;
28  
29  import java.util.Collections;
30  import java.util.HashMap;
31  import java.util.List;
32  import java.util.Locale;
33  import java.util.Map;
34  
35  import org.apache.hc.client5.http.auth.AuthChallenge;
36  import org.apache.hc.client5.http.auth.AuthScheme;
37  import org.apache.hc.client5.http.auth.AuthSchemeFactory;
38  import org.apache.hc.client5.http.auth.AuthScope;
39  import org.apache.hc.client5.http.auth.ChallengeType;
40  import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
41  import org.apache.hc.client5.http.auth.StandardAuthScheme;
42  import org.apache.hc.client5.http.config.RequestConfig;
43  import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
44  import org.apache.hc.client5.http.impl.auth.BasicScheme;
45  import org.apache.hc.client5.http.impl.auth.BasicSchemeFactory;
46  import org.apache.hc.client5.http.impl.auth.DigestScheme;
47  import org.apache.hc.client5.http.impl.auth.DigestSchemeFactory;
48  import org.apache.hc.client5.http.protocol.HttpClientContext;
49  import org.apache.hc.core5.http.config.Registry;
50  import org.apache.hc.core5.http.config.RegistryBuilder;
51  import org.apache.hc.core5.http.message.BasicNameValuePair;
52  import org.junit.Assert;
53  import org.junit.Test;
54  
55  /**
56   * Simple tests for {@link DefaultAuthenticationStrategy}.
57   */
58  @SuppressWarnings("boxing") // test code
59  public class TestAuthenticationStrategy {
60  
61      @Test
62      public void testSelectInvalidInput() throws Exception {
63          final DefaultAuthenticationStrategy authStrategy = new DefaultAuthenticationStrategy();
64          final HttpClientContext context = HttpClientContext.create();
65          try {
66              authStrategy.select(null, Collections.<String, AuthChallenge>emptyMap(), context);
67              Assert.fail("NullPointerException expected");
68          } catch (final NullPointerException ex) {
69          }
70          try {
71              authStrategy.select(ChallengeType.TARGET, null, context);
72              Assert.fail("NullPointerException expected");
73          } catch (final NullPointerException ex) {
74          }
75          try {
76              authStrategy.select(ChallengeType.TARGET, Collections.<String, AuthChallenge>emptyMap(), null);
77              Assert.fail("NullPointerException expected");
78          } catch (final NullPointerException ex) {
79          }
80      }
81  
82      @Test
83      public void testSelectNoSchemeRegistry() throws Exception {
84          final DefaultAuthenticationStrategy authStrategy = new DefaultAuthenticationStrategy();
85          final HttpClientContext context = HttpClientContext.create();
86  
87          final Map<String, AuthChallenge> challenges = new HashMap<>();
88          challenges.put(StandardAuthScheme.BASIC.toLowerCase(Locale.ROOT), new AuthChallenge(ChallengeType.TARGET, StandardAuthScheme.BASIC,
89                  new BasicNameValuePair("realm", "test")));
90          challenges.put(StandardAuthScheme.DIGEST.toLowerCase(Locale.ROOT), new AuthChallenge(ChallengeType.TARGET, StandardAuthScheme.DIGEST,
91                  new BasicNameValuePair("realm", "test"), new BasicNameValuePair("nonce", "1234")));
92  
93          final List<AuthScheme> authSchemes = authStrategy.select(ChallengeType.TARGET, challenges, context);
94          Assert.assertNotNull(authSchemes);
95          Assert.assertEquals(0, authSchemes.size());
96      }
97  
98      @Test
99      public void testUnsupportedScheme() throws Exception {
100         final DefaultAuthenticationStrategy authStrategy = new DefaultAuthenticationStrategy();
101         final HttpClientContext context = HttpClientContext.create();
102 
103         final Map<String, AuthChallenge> challenges = new HashMap<>();
104         challenges.put(StandardAuthScheme.BASIC.toLowerCase(Locale.ROOT), new AuthChallenge(ChallengeType.TARGET, StandardAuthScheme.BASIC,
105                 new BasicNameValuePair("realm", "realm1")));
106         challenges.put(StandardAuthScheme.DIGEST.toLowerCase(Locale.ROOT), new AuthChallenge(ChallengeType.TARGET, StandardAuthScheme.DIGEST,
107                 new BasicNameValuePair("realm", "realm2"), new BasicNameValuePair("nonce", "1234")));
108         challenges.put("whatever", new AuthChallenge(ChallengeType.TARGET, "Whatever",
109                 new BasicNameValuePair("realm", "realm3")));
110 
111         final Registry<AuthSchemeFactory> authSchemeRegistry = RegistryBuilder.<AuthSchemeFactory>create()
112             .register(StandardAuthScheme.BASIC, BasicSchemeFactory.INSTANCE)
113             .register(StandardAuthScheme.DIGEST, DigestSchemeFactory.INSTANCE).build();
114         context.setAuthSchemeRegistry(authSchemeRegistry);
115 
116         final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
117         credentialsProvider.setCredentials(new AuthScope("somehost", 80),
118                 new UsernamePasswordCredentials("user", "pwd".toCharArray()));
119         context.setCredentialsProvider(credentialsProvider);
120 
121         final List<AuthScheme> authSchemes = authStrategy.select(ChallengeType.TARGET, challenges, context);
122         Assert.assertNotNull(authSchemes);
123         Assert.assertEquals(2, authSchemes.size());
124         final AuthScheme authScheme1 = authSchemes.get(0);
125         Assert.assertTrue(authScheme1 instanceof DigestScheme);
126         final AuthScheme authScheme2 = authSchemes.get(1);
127         Assert.assertTrue(authScheme2 instanceof BasicScheme);
128     }
129 
130     @Test
131     public void testCustomAuthPreference() throws Exception {
132         final DefaultAuthenticationStrategy authStrategy = new DefaultAuthenticationStrategy();
133         final RequestConfig config = RequestConfig.custom()
134             .setTargetPreferredAuthSchemes(Collections.singletonList(StandardAuthScheme.BASIC))
135             .build();
136 
137         final HttpClientContext context = HttpClientContext.create();
138 
139         final Map<String, AuthChallenge> challenges = new HashMap<>();
140         challenges.put(StandardAuthScheme.BASIC.toLowerCase(Locale.ROOT), new AuthChallenge(ChallengeType.TARGET, StandardAuthScheme.BASIC,
141                 new BasicNameValuePair("realm", "realm1")));
142         challenges.put(StandardAuthScheme.DIGEST.toLowerCase(Locale.ROOT), new AuthChallenge(ChallengeType.TARGET, StandardAuthScheme.DIGEST,
143                 new BasicNameValuePair("realm", "realm2"), new BasicNameValuePair("nonce", "1234")));
144 
145         final Registry<AuthSchemeFactory> authSchemeRegistry = RegistryBuilder.<AuthSchemeFactory>create()
146             .register(StandardAuthScheme.BASIC, BasicSchemeFactory.INSTANCE)
147             .register(StandardAuthScheme.DIGEST, DigestSchemeFactory.INSTANCE).build();
148         context.setAuthSchemeRegistry(authSchemeRegistry);
149         context.setRequestConfig(config);
150 
151         final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
152         credentialsProvider.setCredentials(new AuthScope("somehost", 80),
153                 new UsernamePasswordCredentials("user", "pwd".toCharArray()));
154         context.setCredentialsProvider(credentialsProvider);
155 
156         final List<AuthScheme> authSchemes = authStrategy.select(ChallengeType.TARGET, challenges, context);
157         Assert.assertNotNull(authSchemes);
158         Assert.assertEquals(1, authSchemes.size());
159         final AuthScheme authScheme1 = authSchemes.get(0);
160         Assert.assertTrue(authScheme1 instanceof BasicScheme);
161     }
162 
163 }