1. /*
  2. * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/auth/AuthScheme.java,v 1.12 2004/05/13 04:02:00 mbecke Exp $
  3. * $Revision: 1.12 $
  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.httpclient.Credentials;
  31. import org.apache.commons.httpclient.HttpMethod;
  32. /**
  33. * <p>
  34. * This interface represents an abstract challenge-response oriented
  35. * authentication scheme.
  36. * </p>
  37. * <p>
  38. * An authentication scheme should be able to support the following
  39. * functions:
  40. * <ul>
  41. * <li>Parse and process the challenge sent by the targer server
  42. * in response to request for a protected resource
  43. * <li>Provide its textual designation
  44. * <li>Provide its parameters, if available
  45. * <li>Provide the realm this authentication scheme is applicable to,
  46. * if available
  47. * <li>Generate authorization string for the given set of credentials,
  48. * request method and URI as specificed in the HTTP request line
  49. * in response to the actual authorization challenge
  50. * </ul>
  51. * </p>
  52. * <p>
  53. * Authentication schemes may ignore method name and URI parameters
  54. * if they are not relevant for the given authentication mechanism
  55. * </p>
  56. * <p>
  57. * Authentication schemes may be stateful involving a series of
  58. * challenge-response exchanges
  59. * </p>
  60. *
  61. * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
  62. * @author <a href="mailto:adrian@ephox.com">Adrian Sutton</a>
  63. *
  64. * @since 2.0beta1
  65. */
  66. public interface AuthScheme {
  67. /**
  68. * Processes the given challenge token. Some authentication schemes
  69. * may involve multiple challenge-response exchanges. Such schemes must be able
  70. * to maintain the state information when dealing with sequential challenges
  71. *
  72. * @param challenge the challenge string
  73. *
  74. * @since 3.0
  75. */
  76. void processChallenge(final String challenge) throws MalformedChallengeException;
  77. /**
  78. * Returns textual designation of the given authentication scheme.
  79. *
  80. * @return the name of the given authentication scheme
  81. */
  82. String getSchemeName();
  83. /**
  84. * Returns authentication parameter with the given name, if available.
  85. *
  86. * @param name The name of the parameter to be returned
  87. *
  88. * @return the parameter with the given name
  89. */
  90. String getParameter(final String name);
  91. /**
  92. * Returns authentication realm. If the concept of an authentication
  93. * realm is not applicable to the given authentication scheme, returns
  94. * <code>null</code>.
  95. *
  96. * @return the authentication realm
  97. */
  98. String getRealm();
  99. /**
  100. * Returns a String identifying the authentication challenge. This is
  101. * used, in combination with the host and port to determine if
  102. * authorization has already been attempted or not. Schemes which
  103. * require multiple requests to complete the authentication should
  104. * return a different value for each stage in the request.
  105. *
  106. * <p>Additionally, the ID should take into account any changes to the
  107. * authentication challenge and return a different value when appropriate.
  108. * For example when the realm changes in basic authentication it should be
  109. * considered a different authentication attempt and a different value should
  110. * be returned.</p>
  111. *
  112. * @return String a String identifying the authentication challenge. The
  113. * returned value may be null.
  114. *
  115. * @deprecated no longer used
  116. */
  117. String getID();
  118. /**
  119. * Tests if the authentication scheme is provides authorization on a per
  120. * connection basis instead of usual per request basis
  121. *
  122. * @return <tt>true</tt> if the scheme is connection based, <tt>false</tt>
  123. * if the scheme is request based.
  124. *
  125. * @since 3.0
  126. */
  127. boolean isConnectionBased();
  128. /**
  129. * Authentication process may involve a series of challenge-response exchanges.
  130. * This method tests if the authorization process has been completed, either
  131. * successfully or unsuccessfully, that is, all the required authorization
  132. * challenges have been processed in their entirety.
  133. *
  134. * @return <tt>true</tt> if the authentication process has been completed,
  135. * <tt>false</tt> otherwise.
  136. *
  137. * @since 3.0
  138. */
  139. boolean isComplete();
  140. /**
  141. * @deprecated Use {@link #authenticate(Credentials, HttpMethod)}
  142. *
  143. * Produces an authorization string for the given set of {@link Credentials},
  144. * method name and URI using the given authentication scheme in response to
  145. * the actual authorization challenge.
  146. *
  147. * @param credentials The set of credentials to be used for athentication
  148. * @param method The name of the method that requires authorization.
  149. * This parameter may be ignored, if it is irrelevant
  150. * or not applicable to the given authentication scheme
  151. * @param uri The URI for which authorization is needed.
  152. * This parameter may be ignored, if it is irrelevant or not
  153. * applicable to the given authentication scheme
  154. * @throws AuthenticationException if authorization string cannot
  155. * be generated due to an authentication failure
  156. *
  157. * @return the authorization string
  158. *
  159. * @see org.apache.commons.httpclient.HttpMethod#getName()
  160. * @see org.apache.commons.httpclient.HttpMethod#getPath()
  161. */
  162. String authenticate(Credentials credentials, String method, String uri)
  163. throws AuthenticationException;
  164. /**
  165. * Produces an authorization string for the given set of {@link Credentials}.
  166. *
  167. * @param credentials The set of credentials to be used for athentication
  168. * @param method The method being authenticated
  169. * @throws AuthenticationException if authorization string cannot
  170. * be generated due to an authentication failure
  171. *
  172. * @return the authorization string
  173. *
  174. * @since 3.0
  175. */
  176. String authenticate(Credentials credentials, HttpMethod method) throws AuthenticationException;
  177. }