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.testing.auth;
29  
30  import java.util.List;
31  
32  import org.apache.hc.core5.http.HttpException;
33  import org.apache.hc.core5.http.NameValuePair;
34  import org.apache.hc.core5.http.ProtocolException;
35  
36  abstract class AbstractAuthenticationHandler implements AuthenticationHandler<String> {
37  
38      abstract String getSchemeName();
39  
40      @Override
41      public final String challenge(final List<NameValuePair> params) {
42          final StringBuilder buf = new StringBuilder();
43          buf.append(getSchemeName());
44          if (params != null && params.size() > 0) {
45              buf.append(" ");
46              for (int i = 0; i < params.size(); i++) {
47                  if (i > 0) {
48                      buf.append(", ");
49                  }
50                  final NameValuePair param = params.get(i);
51                  buf.append(param.getName()).append("=\"").append(param.getValue()).append("\"");
52              }
53          }
54          return buf.toString();
55      }
56  
57      abstract String decodeChallenge(String challenge);
58  
59      public final String extractAuthToken(final String challengeResponse) throws HttpException {
60          final int i = challengeResponse.indexOf(' ');
61          if (i == -1) {
62              throw new ProtocolException("Invalid " + getSchemeName() + " challenge response");
63          }
64          final String schemeName = challengeResponse.substring(0, i);
65          if (schemeName.equalsIgnoreCase(getSchemeName())) {
66              final String s = challengeResponse.substring(i + 1).trim();
67              try {
68                  return decodeChallenge(s);
69              } catch (final IllegalArgumentException ex) {
70                  throw new ProtocolException("Malformed " + getSchemeName() + " credentials");
71              }
72          } else {
73              throw new ProtocolException("Unexpected challenge type");
74          }
75      }
76  
77  }