Zend_Auth_Adapter_Http.xml 12 KB

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