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.annotation.NotThreadSafe;
35  import org.apache.http.auth.AUTH;
36  import org.apache.http.auth.AuthenticationException;
37  import org.apache.http.auth.ChallengeState;
38  import org.apache.http.auth.ContextAwareAuthScheme;
39  import org.apache.http.auth.Credentials;
40  import org.apache.http.auth.MalformedChallengeException;
41  import org.apache.http.protocol.HTTP;
42  import org.apache.http.protocol.HttpContext;
43  import org.apache.http.util.Args;
44  import org.apache.http.util.CharArrayBuffer;
45  
46  /**
47   * Abstract authentication scheme class that serves as a basis
48   * for all authentication schemes supported by HttpClient. This class
49   * defines the generic way of parsing an authentication challenge. It
50   * does not make any assumptions regarding the format of the challenge
51   * nor does it impose any specific way of responding to that challenge.
52   *
53   *
54   * @since 4.0
55   */
56  @NotThreadSafe
57  public abstract class AuthSchemeBase implements ContextAwareAuthScheme {
58  
59      private ChallengeState challengeState;
60  
61      /**
62       * Creates an instance of <tt>AuthSchemeBase</tt> with the given challenge
63       * state.
64       *
65       * @since 4.2
66       *
67       * @deprecated (4.3) do not use.
68       */
69      @Deprecated
70      public AuthSchemeBase(final ChallengeState challengeState) {
71          super();
72          this.challengeState = challengeState;
73      }
74  
75      public AuthSchemeBase() {
76          super();
77      }
78  
79      /**
80       * Processes the given challenge token. Some authentication schemes
81       * may involve multiple challenge-response exchanges. Such schemes must be able
82       * to maintain the state information when dealing with sequential challenges
83       *
84       * @param header the challenge header
85       *
86       * @throws MalformedChallengeException is thrown if the authentication challenge
87       * is malformed
88       */
89      public void processChallenge(final Header header) throws MalformedChallengeException {
90          Args.notNull(header, "Header");
91          final String authheader = header.getName();
92          if (authheader.equalsIgnoreCase(AUTH.WWW_AUTH)) {
93              this.challengeState = ChallengeState.TARGET;
94          } else if (authheader.equalsIgnoreCase(AUTH.PROXY_AUTH)) {
95              this.challengeState = ChallengeState.PROXY;
96          } else {
97              throw new MalformedChallengeException("Unexpected header name: " + authheader);
98          }
99  
100         final CharArrayBuffer buffer;
101         int pos;
102         if (header instanceof FormattedHeader) {
103             buffer = ((FormattedHeader) header).getBuffer();
104             pos = ((FormattedHeader) header).getValuePos();
105         } else {
106             final String s = header.getValue();
107             if (s == null) {
108                 throw new MalformedChallengeException("Header value is null");
109             }
110             buffer = new CharArrayBuffer(s.length());
111             buffer.append(s);
112             pos = 0;
113         }
114         while (pos < buffer.length() && HTTP.isWhitespace(buffer.charAt(pos))) {
115             pos++;
116         }
117         final int beginIndex = pos;
118         while (pos < buffer.length() && !HTTP.isWhitespace(buffer.charAt(pos))) {
119             pos++;
120         }
121         final int endIndex = pos;
122         final String s = buffer.substring(beginIndex, endIndex);
123         if (!s.equalsIgnoreCase(getSchemeName())) {
124             throw new MalformedChallengeException("Invalid scheme identifier: " + s);
125         }
126 
127         parseChallenge(buffer, pos, buffer.length());
128     }
129 
130 
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</code> if authenticating against a proxy, <code>false</code>
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</code> 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         if (name != null) {
163             return name.toUpperCase(Locale.ENGLISH);
164         } else {
165             return super.toString();
166         }
167     }
168 
169 }