Zend_Auth_Adapter_Http.xml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.auth.adapter.http">
  4. <title>HTTP Authentication Adapter</title>
  5. <sect2 id="zend.auth.adapter.http.introduction">
  6. <title>Introduction</title>
  7. <para>
  8. <classname>Zend_Auth_Adapter_Http</classname> provides a mostly-compliant implementation of
  9. <ulink url="http://tools.ietf.org/html/rfc2617">RFC-2617</ulink>,
  10. <ulink url="http://en.wikipedia.org/wiki/Basic_authentication_scheme">Basic</ulink> and
  11. <ulink url="http://en.wikipedia.org/wiki/Digest_access_authentication">Digest</ulink> HTTP Authentication.
  12. Digest authentication is a method of HTTP authentication that improves upon Basic authentication by
  13. providing a way to authenticate without having to transmit the password in clear text across the network.
  14. </para>
  15. <para>
  16. <emphasis role="strong">Major Features:</emphasis>
  17. <itemizedlist>
  18. <listitem>
  19. <para>
  20. Supports both Basic and Digest authentication.
  21. </para>
  22. </listitem>
  23. <listitem>
  24. <para>
  25. Issues challenges in all supported schemes, so client can respond with any scheme it supports.
  26. </para>
  27. </listitem>
  28. <listitem>
  29. <para>
  30. Supports proxy authentication.
  31. </para>
  32. </listitem>
  33. <listitem>
  34. <para>
  35. Includes support for authenticating against text files and provides an interface for
  36. authenticating against other sources, such as databases.
  37. </para>
  38. </listitem>
  39. </itemizedlist>
  40. </para>
  41. <para>
  42. There are a few notable features of RFC-2617 that are not implemented yet:
  43. <itemizedlist>
  44. <listitem>
  45. <para>
  46. Nonce tracking, which would allow for "stale" support, and increased replay attack protection.
  47. </para>
  48. </listitem>
  49. <listitem>
  50. <para>
  51. Authentication with integrity checking, or "auth-int".
  52. </para>
  53. </listitem>
  54. <listitem>
  55. <para>
  56. Authentication-Info HTTP header.
  57. </para>
  58. </listitem>
  59. </itemizedlist>
  60. </para>
  61. </sect2>
  62. <sect2 id="zend.auth.adapter.design_overview">
  63. <title>Design Overview</title>
  64. <para>
  65. This adapter consists of two sub-components, the HTTP authentication class itself, and the so-called
  66. "Resolvers." The HTTP authentication class encapsulates the logic for carrying out both Basic and Digest
  67. authentication. It uses a Resolver to look up a client's identity in some data store (text file by default),
  68. and retrieve the credentials from the data store. The "resolved" credentials are then compared to the values
  69. submitted by the client to determine whether authentication is successful.
  70. </para>
  71. </sect2>
  72. <sect2 id="zend.auth.adapter.configuration_options">
  73. <title>Configuration Options</title>
  74. <para>
  75. The <classname>Zend_Auth_Adapter_Http</classname> class requires a configuration array passed to its constructor.
  76. There are several configuration options available, and some are required:
  77. <table id="zend.auth.adapter.configuration_options.table">
  78. <title>Configuration Options</title>
  79. <tgroup cols="3">
  80. <thead>
  81. <row>
  82. <entry>Option Name</entry>
  83. <entry>Required</entry>
  84. <entry>Description</entry>
  85. </row>
  86. </thead>
  87. <tbody>
  88. <row>
  89. <entry><code>accept_schemes</code></entry>
  90. <entry>Yes</entry>
  91. <entry>
  92. Determines which authentication schemes the adapter will accept from the client. Must be
  93. a space-separated list containing <code>'basic'</code> and/or <code>'digest'</code>.
  94. </entry>
  95. </row>
  96. <row>
  97. <entry><code>realm</code></entry>
  98. <entry>Yes</entry>
  99. <entry>
  100. Sets the authentication realm; usernames should be unique within a given realm.
  101. </entry>
  102. </row>
  103. <row>
  104. <entry><code>digest_domains</code></entry>
  105. <entry>Yes, when <code>'accept_schemes'</code> contains <code>'digest'</code></entry>
  106. <entry>
  107. Space-separated list of URIs for which the same authentication information is valid. The
  108. URIs need not all point to the same server.
  109. </entry>
  110. </row>
  111. <row>
  112. <entry><code>nonce_timeout</code></entry>
  113. <entry>Yes, when <code>'accept_schemes'</code> contains <code>'digest'</code></entry>
  114. <entry>
  115. Sets the number of seconds for which the nonce is valid. See notes below.
  116. </entry>
  117. </row>
  118. <row>
  119. <entry><code>proxy_auth</code></entry>
  120. <entry>No</entry>
  121. <entry>
  122. Disabled by default. Enable to perform Proxy authentication, instead of normal origin
  123. server authentication.
  124. </entry>
  125. </row>
  126. </tbody>
  127. </tgroup>
  128. </table>
  129. </para>
  130. <note>
  131. <para>
  132. The current implementation of the <code>nonce_timeout</code> has some interesting side effects. This
  133. setting is supposed to determine the valid lifetime of a given nonce, or effectively how long a client's
  134. authentication information is accepted. Currently, if it's set to 3600 (for example), it will cause the
  135. adapter to prompt the client for new credentials every hour, on the hour. This will be resolved in a
  136. future release, once nonce tracking and stale support are implemented.
  137. </para>
  138. </note>
  139. </sect2>
  140. <sect2 id="zend.auth.adapter.http.resolvers">
  141. <title>Resolvers</title>
  142. <para>
  143. The resolver's job is to take a username and realm, and return some kind of credential value. Basic
  144. authentication expects to receive the Base64 encoded version of the user's password. Digest authentication
  145. expects to receive a hash of the user's username, the realm, and their password (each separated by colons).
  146. Currently, the only supported hash algorithm is MD5.
  147. </para>
  148. <para>
  149. <classname>Zend_Auth_Adapter_Http</classname> relies on objects implementing
  150. <classname>Zend_Auth_Adapter_Http_Resolver_Interface</classname>. A text file resolver class is included with this
  151. adapter, but any other kind of resolver can be created simply by implementing the resolver interface.
  152. </para>
  153. <sect3 id="zend.auth.adapter.http.resolvers.file">
  154. <title>File Resolver</title>
  155. <para>
  156. The file resolver is a very simple class. It has a single property specifying a filename, which can also
  157. be passed to the constructor. Its <code>resolve()</code> method walks through the text file, searching
  158. for a line with a matching username and realm. The text file format similar to Apache htpasswd files:
  159. <programlisting><![CDATA[
  160. <username>:<realm>:<credentials>\n
  161. ]]></programlisting>
  162. Each line consists of three fields - username, realm, and credentials - each separated by a colon. The
  163. credentials field is opaque to the file resolver; it simply returns that value as-is to the caller.
  164. Therefore, this same file format serves both Basic and Digest authentication. In Basic authentication,
  165. the credentials field should be written in clear text. In Digest authentication, it
  166. should be the MD5 hash described above.
  167. </para>
  168. <para>
  169. There are two equally easy ways to create a File resolver:
  170. <programlisting role="php"><![CDATA[
  171. $path = 'files/passwd.txt';
  172. $resolver = new Zend_Auth_Adapter_Http_Resolver_File($path);
  173. ]]></programlisting>
  174. or
  175. <programlisting role="php"><![CDATA[
  176. $path = 'files/passwd.txt';
  177. $resolver = new Zend_Auth_Adapter_Http_Resolver_File();
  178. $resolver->setFile($path);
  179. ]]></programlisting>
  180. If the given path is empty or not readable, an exception is thrown.
  181. </para>
  182. </sect3>
  183. </sect2>
  184. <sect2 id="zend.auth.adapter.http.basic_usage">
  185. <title>Basic Usage</title>
  186. <para>
  187. First, set up an array with the required configuration values:
  188. <programlisting role="php"><![CDATA[
  189. $config = array(
  190. 'accept_schemes' => 'basic digest',
  191. 'realm' => 'My Web Site',
  192. 'digest_domains' => '/members_only /my_account',
  193. 'nonce_timeout' => 3600,
  194. );
  195. ]]></programlisting>
  196. This array will cause the adapter to accept either Basic or Digest authentication, and will require
  197. authenticated access to all the areas of the site under <code>/members_only</code> and
  198. <code>/my_account</code>. The realm value is usually displayed by the browser in the password dialog box.
  199. The <code>nonce_timeout</code>, of course, behaves as described above.
  200. </para>
  201. <para>
  202. Next, create the Zend_Auth_Adapter_Http object:
  203. <programlisting role="php"><![CDATA[
  204. $adapter = new Zend_Auth_Adapter_Http($config);
  205. ]]></programlisting>
  206. </para>
  207. <para>
  208. Since we're supporting both Basic and Digest authentication, we need two different resolver objects. Note
  209. that this could just as easily be two different classes:
  210. <programlisting role="php"><![CDATA[
  211. $basicResolver = new Zend_Auth_Adapter_Http_Resolver_File();
  212. $basicResolver->setFile('files/basicPasswd.txt');
  213. $digestResolver = new Zend_Auth_Adapter_Http_Resolver_File();
  214. $digestResolver->setFile('files/digestPasswd.txt');
  215. $adapter->setBasicResolver($basicResolver);
  216. $adapter->setDigestResolver($digestResolver);
  217. ]]></programlisting>
  218. </para>
  219. <para>
  220. Finally, we perform the authentication. The adapter needs a reference to both the Request and Response
  221. objects in order to do its job:
  222. <programlisting role="php"><![CDATA[
  223. assert($request instanceof Zend_Controller_Request_Http);
  224. assert($response instanceof Zend_Controller_Response_Http);
  225. $adapter->setRequest($request);
  226. $adapter->setResponse($response);
  227. $result = $adapter->authenticate();
  228. if (!$result->isValid()) {
  229. // Bad userame/password, or canceled password prompt
  230. }
  231. ]]></programlisting>
  232. </para>
  233. </sect2>
  234. </sect1>
  235. <!--
  236. vim:se ts=4 sw=4 et:
  237. -->