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.http.impl.auth;
28  
29  import java.util.Locale;
30  
31  import org.apache.http.FormattedHeader;
32  import org.apache.http.Header;
33  import org.apache.http.HttpRequest;
34  import org.apache.http.auth.AUTH;
35  import org.apache.http.auth.AuthenticationException;
36  import org.apache.http.auth.ChallengeState;
37  import org.apache.http.auth.ContextAwareAuthScheme;
38  import org.apache.http.auth.Credentials;
39  import org.apache.http.auth.MalformedChallengeException;
40  import org.apache.http.protocol.HTTP;
41  import org.apache.http.protocol.HttpContext;
42  import org.apache.http.util.Args;
43  import org.apache.http.util.CharArrayBuffer;
44  
45  /**
46   * Abstract authentication scheme class that serves as a basis
47   * for all authentication schemes supported by HttpClient. This class
48   * defines the generic way of parsing an authentication challenge. It
49   * does not make any assumptions regarding the format of the challenge
50   * nor does it impose any specific way of responding to that challenge.
51   *
52   *
53   * @since 4.0
54   */
55  public abstract class AuthSchemeBase implements ContextAwareAuthScheme {
56  
57      protected ChallengeState challengeState;
58  
59      /**
60       * Creates an instance of {@code AuthSchemeBase} with the given challenge
61       * state.
62       *
63       * @since 4.2
64       *
65       * @deprecated (4.3) do not use.
66       */
67      @Deprecated
68      public AuthSchemeBase(final ChallengeState challengeState) {
69          super();
70          this.challengeState = challengeState;
71      }
72  
73      public AuthSchemeBase() {
74          super();
75      }
76  
77      /**
78       * Processes the given challenge token. Some authentication schemes
79       * may involve multiple challenge-response exchanges. Such schemes must be able
80       * to maintain the state information when dealing with sequential challenges
81       *
82       * @param header the challenge header
83       *
84       * @throws MalformedChallengeException is thrown if the authentication challenge
85       * is malformed
86       */
87      @Override
88      public void processChallenge(final Header header) throws MalformedChallengeException {
89          Args.notNull(header, "Header");
90          final String authheader = header.getName();
91          if (authheader.equalsIgnoreCase(AUTH.WWW_AUTH)) {
92              this.challengeState = ChallengeState.TARGET;
93          } else if (authheader.equalsIgnoreCase(AUTH.PROXY_AUTH)) {
94              this.challengeState = ChallengeState.PROXY;
95          } else {
96              throw new MalformedChallengeException("Unexpected header name: " + authheader);
97          }
98  
99          final CharArrayBuffer buffer;
100         int pos;
101         if (header instanceof FormattedHeader) {
102             buffer = ((FormattedHeader) header).getBuffer();
103             pos = ((FormattedHeader) header).getValuePos();
104         } else {
105             final String s = header.getValue();
106             if (s == null) {
107                 throw new MalformedChallengeException("Header value is null");
108             }
109             buffer = new CharArrayBuffer(s.length());
110             buffer.append(s);
111             pos = 0;
112         }
113         while (pos < buffer.length() && HTTP.isWhitespace(buffer.charAt(pos))) {
114             pos++;
115         }
116         final int beginIndex = pos;
117         while (pos < buffer.length() && !HTTP.isWhitespace(buffer.charAt(pos))) {
118             pos++;
119         }
120         final int endIndex = pos;
121         final String s = buffer.substring(beginIndex, endIndex);
122         if (!s.equalsIgnoreCase(getSchemeName())) {
123             throw new MalformedChallengeException("Invalid scheme identifier: " + s);
124         }
125 
126         parseChallenge(buffer, pos, buffer.length());
127     }
128 
129 
130     @Override
131     @SuppressWarnings("deprecation")
132     public Header authenticate(
133             final Credentials credentials,
134             final HttpRequest request,
135             final HttpContext context) throws AuthenticationException {
136         return authenticate(credentials, request);
137     }
138 
139     protected abstract void parseChallenge(
140             CharArrayBuffer buffer, int beginIndex, int endIndex) throws MalformedChallengeException;
141 
142     /**
143      * Returns {@code true} if authenticating against a proxy, {@code false}
144      * otherwise.
145      */
146     public boolean isProxy() {
147         return this.challengeState != null && this.challengeState == ChallengeState.PROXY;
148     }
149 
150     /**
151      * Returns {@link ChallengeState} value or {@code null} if unchallenged.
152      *
153      * @since 4.2
154      */
155     public ChallengeState getChallengeState() {
156         return this.challengeState;
157     }
158 
159     @Override
160     public String toString() {
161         final String name = getSchemeName();
162         return name != null ? name.toUpperCase(Locale.ROOT) : super.toString();
163     }
164 
165 }