View Javadoc

1   /*
2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/auth/BasicScheme.java,v 1.17 2004/05/13 04:02:00 mbecke Exp $
3    * $Revision: 155418 $
4    * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
5    *
6    * ====================================================================
7    *
8    *  Copyright 2002-2004 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
27   *
28   */
29  
30  package org.apache.commons.httpclient.auth;
31  
32  import org.apache.commons.codec.binary.Base64;
33  import org.apache.commons.httpclient.Credentials;
34  import org.apache.commons.httpclient.HttpMethod;
35  import org.apache.commons.httpclient.UsernamePasswordCredentials;
36  import org.apache.commons.httpclient.util.EncodingUtil;
37  import org.apache.commons.logging.Log;
38  import org.apache.commons.logging.LogFactory;
39  
40  /***
41   * <p>
42   * Basic authentication scheme as defined in RFC 2617.
43   * </p>
44   * 
45   * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
46   * @author Rodney Waldhoff
47   * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
48   * @author Ortwin Gl?ck
49   * @author Sean C. Sullivan
50   * @author <a href="mailto:adrian@ephox.com">Adrian Sutton</a>
51   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
52   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
53   */
54  
55  public class BasicScheme extends RFC2617Scheme {
56      
57      /*** Log object for this class. */
58      private static final Log LOG = LogFactory.getLog(BasicScheme.class);
59      
60      /*** Whether the basic authentication process is complete */
61      private boolean complete;
62      
63      /***
64       * Default constructor for the basic authetication scheme.
65       * 
66       * @since 3.0
67       */
68      public BasicScheme() {
69          super();
70          this.complete = false;
71      }
72  
73      /***
74       * Constructor for the basic authetication scheme.
75       * 
76       * @param challenge authentication challenge
77       * 
78       * @throws MalformedChallengeException is thrown if the authentication challenge
79       * is malformed
80       * 
81       * @deprecated Use parameterless constructor and {@link AuthScheme#processChallenge(String)} 
82       *             method
83       */
84      public BasicScheme(final String challenge) throws MalformedChallengeException {
85          super(challenge);
86          this.complete = true;
87      }
88  
89      /***
90       * Returns textual designation of the basic authentication scheme.
91       * 
92       * @return <code>basic</code>
93       */
94      public String getSchemeName() {
95          return "basic";
96      }
97  
98      /***
99       * Processes the Basic challenge.
100      *  
101      * @param challenge the challenge string
102      * 
103      * @throws MalformedChallengeException is thrown if the authentication challenge
104      * is malformed
105      * 
106      * @since 3.0
107      */
108     public void processChallenge(String challenge) 
109         throws MalformedChallengeException 
110     {
111         super.processChallenge(challenge);
112         this.complete = true;
113     }
114 
115     /***
116      * Tests if the Basic authentication process has been completed.
117      * 
118      * @return <tt>true</tt> if Basic authorization has been processed,
119      *   <tt>false</tt> otherwise.
120      * 
121      * @since 3.0
122      */
123     public boolean isComplete() {
124         return this.complete;
125     }
126 
127     /***
128      * Produces basic authorization string for the given set of 
129      * {@link Credentials}.
130      * 
131      * @param credentials The set of credentials to be used for athentication
132      * @param method Method name is ignored by the basic authentication scheme
133      * @param uri URI is ignored by the basic authentication scheme
134      * @throws InvalidCredentialsException if authentication credentials
135      *         are not valid or not applicable for this authentication scheme
136      * @throws AuthenticationException if authorization string cannot 
137      *   be generated due to an authentication failure
138      * 
139      * @return a basic authorization string
140      * 
141      * @deprecated Use {@link #authenticate(Credentials, HttpMethod)}
142      */
143     public String authenticate(Credentials credentials, String method, String uri)
144       throws AuthenticationException {
145 
146         LOG.trace("enter BasicScheme.authenticate(Credentials, String, String)");
147 
148         UsernamePasswordCredentials usernamepassword = null;
149         try {
150             usernamepassword = (UsernamePasswordCredentials) credentials;
151         } catch (ClassCastException e) {
152             throw new InvalidCredentialsException(
153              "Credentials cannot be used for basic authentication: " 
154               + credentials.getClass().getName());
155         }
156         return BasicScheme.authenticate(usernamepassword);
157     }
158 
159     /***
160      * Returns <tt>false</tt>. Basic authentication scheme is request based.
161      * 
162      * @return <tt>false</tt>.
163      * 
164      * @since 3.0
165      */
166     public boolean isConnectionBased() {
167         return false;    
168     }
169 
170     /***
171      * Produces basic authorization string for the given set of {@link Credentials}.
172      * 
173      * @param credentials The set of credentials to be used for athentication
174      * @param method The method being authenticated
175      * @throws InvalidCredentialsException if authentication credentials
176      *         are not valid or not applicable for this authentication scheme
177      * @throws AuthenticationException if authorization string cannot 
178      *   be generated due to an authentication failure
179      * 
180      * @return a basic authorization string
181      * 
182      * @since 3.0
183      */
184     public String authenticate(Credentials credentials, HttpMethod method) throws AuthenticationException {
185 
186         LOG.trace("enter BasicScheme.authenticate(Credentials, HttpMethod)");
187 
188         if (method == null) {
189             throw new IllegalArgumentException("Method may not be null");
190         }
191         UsernamePasswordCredentials usernamepassword = null;
192         try {
193             usernamepassword = (UsernamePasswordCredentials) credentials;
194         } catch (ClassCastException e) {
195             throw new InvalidCredentialsException(
196                     "Credentials cannot be used for basic authentication: " 
197                     + credentials.getClass().getName());
198         }
199         return BasicScheme.authenticate(
200             usernamepassword, 
201             method.getParams().getCredentialCharset());
202     }
203     
204     /***
205      * @deprecated Use {@link #authenticate(UsernamePasswordCredentials, String)}
206      * 
207      * Returns a basic <tt>Authorization</tt> header value for the given 
208      * {@link UsernamePasswordCredentials}.
209      * 
210      * @param credentials The credentials to encode.
211      * 
212      * @return a basic authorization string
213      */
214     public static String authenticate(UsernamePasswordCredentials credentials) {
215         return authenticate(credentials, "ISO-8859-1");
216     }
217 
218     /***
219      * Returns a basic <tt>Authorization</tt> header value for the given 
220      * {@link UsernamePasswordCredentials} and charset.
221      * 
222      * @param credentials The credentials to encode.
223      * @param charset The charset to use for encoding the credentials
224      * 
225      * @return a basic authorization string
226      * 
227      * @since 3.0
228      */
229     public static String authenticate(UsernamePasswordCredentials credentials, String charset) {
230 
231         LOG.trace("enter BasicScheme.authenticate(UsernamePasswordCredentials, String)");
232 
233         if (credentials == null) {
234             throw new IllegalArgumentException("Credentials may not be null"); 
235         }
236         if (charset == null || charset.length() == 0) {
237             throw new IllegalArgumentException("charset may not be null or empty");
238         }
239         StringBuffer buffer = new StringBuffer();
240         buffer.append(credentials.getUserName());
241         buffer.append(":");
242         buffer.append(credentials.getPassword());
243         
244         return "Basic " + EncodingUtil.getAsciiString(
245                 Base64.encodeBase64(EncodingUtil.getBytes(buffer.toString(), charset)));
246     }
247     
248 }