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