Zend_Auth_Adapter_Http.xml 12 KB

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