1. /*
  2. * $Header: /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: 1.17 $
  4. * $Date: 2004/05/13 04:02:00 $
  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. package org.apache.commons.httpclient.auth;
  30. import org.apache.commons.codec.binary.Base64;
  31. import org.apache.commons.httpclient.Credentials;
  32. import org.apache.commons.httpclient.HttpMethod;
  33. import org.apache.commons.httpclient.UsernamePasswordCredentials;
  34. import org.apache.commons.httpclient.util.EncodingUtil;
  35. import org.apache.commons.logging.Log;
  36. import org.apache.commons.logging.LogFactory;
  37. /**
  38. * <p>
  39. * Basic authentication scheme as defined in RFC 2617.
  40. * </p>
  41. *
  42. * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
  43. * @author Rodney Waldhoff
  44. * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
  45. * @author Ortwin Gl?ck
  46. * @author Sean C. Sullivan
  47. * @author <a href="mailto:adrian@ephox.com">Adrian Sutton</a>
  48. * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
  49. * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
  50. */
  51. public class BasicScheme extends RFC2617Scheme {
  52. /** Log object for this class. */
  53. private static final Log LOG = LogFactory.getLog(BasicScheme.class);
  54. /** Whether the basic authentication process is complete */
  55. private boolean complete;
  56. /**
  57. * Default constructor for the basic authetication scheme.
  58. *
  59. * @since 3.0
  60. */
  61. public BasicScheme() {
  62. super();
  63. this.complete = false;
  64. }
  65. /**
  66. * Constructor for the basic authetication scheme.
  67. *
  68. * @param challenge authentication challenge
  69. *
  70. * @throws MalformedChallengeException is thrown if the authentication challenge
  71. * is malformed
  72. *
  73. * @deprecated Use parameterless constructor and {@link AuthScheme#processChallenge(String)}
  74. * method
  75. */
  76. public BasicScheme(final String challenge) throws MalformedChallengeException {
  77. super(challenge);
  78. this.complete = true;
  79. }
  80. /**
  81. * Returns textual designation of the basic authentication scheme.
  82. *
  83. * @return <code>basic</code>
  84. */
  85. public String getSchemeName() {
  86. return "basic";
  87. }
  88. /**
  89. * Processes the Basic challenge.
  90. *
  91. * @param challenge the challenge string
  92. *
  93. * @throws MalformedChallengeException is thrown if the authentication challenge
  94. * is malformed
  95. *
  96. * @since 3.0
  97. */
  98. public void processChallenge(String challenge)
  99. throws MalformedChallengeException
  100. {
  101. super.processChallenge(challenge);
  102. this.complete = true;
  103. }
  104. /**
  105. * Tests if the Basic authentication process has been completed.
  106. *
  107. * @return <tt>true</tt> if Basic authorization has been processed,
  108. * <tt>false</tt> otherwise.
  109. *
  110. * @since 3.0
  111. */
  112. public boolean isComplete() {
  113. return this.complete;
  114. }
  115. /**
  116. * Produces basic authorization string for the given set of
  117. * {@link Credentials}.
  118. *
  119. * @param credentials The set of credentials to be used for athentication
  120. * @param method Method name is ignored by the basic authentication scheme
  121. * @param uri URI is ignored by the basic authentication scheme
  122. * @throws InvalidCredentialsException if authentication credentials
  123. * are not valid or not applicable for this authentication scheme
  124. * @throws AuthenticationException if authorization string cannot
  125. * be generated due to an authentication failure
  126. *
  127. * @return a basic authorization string
  128. *
  129. * @deprecated Use {@link #authenticate(Credentials, HttpMethod)}
  130. */
  131. public String authenticate(Credentials credentials, String method, String uri)
  132. throws AuthenticationException {
  133. LOG.trace("enter BasicScheme.authenticate(Credentials, String, String)");
  134. UsernamePasswordCredentials usernamepassword = null;
  135. try {
  136. usernamepassword = (UsernamePasswordCredentials) credentials;
  137. } catch (ClassCastException e) {
  138. throw new InvalidCredentialsException(
  139. "Credentials cannot be used for basic authentication: "
  140. + credentials.getClass().getName());
  141. }
  142. return BasicScheme.authenticate(usernamepassword);
  143. }
  144. /**
  145. * Returns <tt>false</tt>. Basic authentication scheme is request based.
  146. *
  147. * @return <tt>false</tt>.
  148. *
  149. * @since 3.0
  150. */
  151. public boolean isConnectionBased() {
  152. return false;
  153. }
  154. /**
  155. * Produces basic authorization string for the given set of {@link Credentials}.
  156. *
  157. * @param credentials The set of credentials to be used for athentication
  158. * @param method The method being authenticated
  159. * @throws InvalidCredentialsException if authentication credentials
  160. * are not valid or not applicable for this authentication scheme
  161. * @throws AuthenticationException if authorization string cannot
  162. * be generated due to an authentication failure
  163. *
  164. * @return a basic authorization string
  165. *
  166. * @since 3.0
  167. */
  168. public String authenticate(Credentials credentials, HttpMethod method) throws AuthenticationException {
  169. LOG.trace("enter BasicScheme.authenticate(Credentials, HttpMethod)");
  170. if (method == null) {
  171. throw new IllegalArgumentException("Method may not be null");
  172. }
  173. UsernamePasswordCredentials usernamepassword = null;
  174. try {
  175. usernamepassword = (UsernamePasswordCredentials) credentials;
  176. } catch (ClassCastException e) {
  177. throw new InvalidCredentialsException(
  178. "Credentials cannot be used for basic authentication: "
  179. + credentials.getClass().getName());
  180. }
  181. return BasicScheme.authenticate(
  182. usernamepassword,
  183. method.getParams().getCredentialCharset());
  184. }
  185. /**
  186. * @deprecated Use {@link #authenticate(UsernamePasswordCredentials, String)}
  187. *
  188. * Returns a basic <tt>Authorization</tt> header value for the given
  189. * {@link UsernamePasswordCredentials}.
  190. *
  191. * @param credentials The credentials to encode.
  192. *
  193. * @return a basic authorization string
  194. */
  195. public static String authenticate(UsernamePasswordCredentials credentials) {
  196. return authenticate(credentials, "ISO-8859-1");
  197. }
  198. /**
  199. * Returns a basic <tt>Authorization</tt> header value for the given
  200. * {@link UsernamePasswordCredentials} and charset.
  201. *
  202. * @param credentials The credentials to encode.
  203. * @param charset The charset to use for encoding the credentials
  204. *
  205. * @return a basic authorization string
  206. *
  207. * @since 3.0
  208. */
  209. public static String authenticate(UsernamePasswordCredentials credentials, String charset) {
  210. LOG.trace("enter BasicScheme.authenticate(UsernamePasswordCredentials, String)");
  211. if (credentials == null) {
  212. throw new IllegalArgumentException("Credentials may not be null");
  213. }
  214. if (charset == null || charset.length() == 0) {
  215. throw new IllegalArgumentException("charset may not be null or empty");
  216. }
  217. StringBuffer buffer = new StringBuffer();
  218. buffer.append(credentials.getUserName());
  219. buffer.append(":");
  220. buffer.append(credentials.getPassword());
  221. return "Basic " + EncodingUtil.getAsciiString(
  222. Base64.encodeBase64(EncodingUtil.getBytes(buffer.toString(), charset)));
  223. }
  224. }