Zend_Auth_Adapter_Http.xml 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <sect1 id="zend.auth.adapter.http">
  2. <title>HTTP 认证适配器</title>
  3. <sect2 id="zend.auth.adapter.http.introduction">
  4. <title>简介</title>
  5. <para>
  6. <code>Zend_Auth_Adapter_Http</code>提供一个大部分符合<ulink url="http://tools.ietf.org/html/rfc2617">RFC-2617</ulink>的<ulink url="http://en.wikipedia.org/wiki/Basic_authentication_scheme">基本</ulink> 和 <ulink url="http://en.wikipedia.org/wiki/Digest_access_authentication">摘要</ulink> HTTP 认证。摘要认证是一个认证的方法,它在基本认证的基础上做了改进,不需要在网络上传输明文密码。
  7. </para>
  8. <para>
  9. <emphasis role="strong">主要特性:</emphasis>
  10. <itemizedlist>
  11. <listitem>
  12. <para>
  13. 支持基本和摘要认证。
  14. </para>
  15. </listitem>
  16. <listitem>
  17. <para>
  18. 对所有支持的 schemes 发出请求,所以客户端可以响应任何它所支持的 scheme。
  19. </para>
  20. </listitem>
  21. <listitem>
  22. <para>
  23. 支持代理认证。
  24. </para>
  25. </listitem>
  26. <listitem>
  27. <para>
  28. 包括支持文本文件认证和提供一个接口来支持其它资源认证,例如数据库。
  29. </para>
  30. </listitem>
  31. </itemizedlist>
  32. </para>
  33. <para>
  34. 还有一些显著的 RFC-2617 特性没有实现:
  35. <itemizedlist>
  36. <listitem>
  37. <para>
  38. Nonce 跟踪,它将考虑 "stale" 支持,和增强的重放供给保护。
  39. </para>
  40. </listitem>
  41. <listitem>
  42. <para>
  43. 带完整性检查的认证,或 "auth-int"。
  44. </para>
  45. </listitem>
  46. <listitem>
  47. <para>
  48. HTTP 认证信息头。
  49. </para>
  50. </listitem>
  51. </itemizedlist>
  52. </para>
  53. </sect2>
  54. <sect2 id="zend.auth.adapter.design_overview">
  55. <title>设计回顾</title>
  56. <para>
  57. 这个适配器包括两个子组件,HTTP authentication类本身和所谓的 "Resolvers"。HTTP authentication类封装了实现基本和摘要认证的逻辑。它使用Resolver在数据存储(缺省是文本文件)中查找客户的身份并取出证书。"resolved" 的证书和客户的输入相比较来决定认证是否成功。
  58. </para>
  59. </sect2>
  60. <sect2 id="zend.auth.adapter.configuration_options">
  61. <title>配置选项</title>
  62. <para>
  63. <code>Zend_Auth_Adapter_Http</code> 类需要一个配置数组传递给它的构造器。有若干个配置选项有效,其中一些是必需的:
  64. <table id="zend.auth.adapter.configuration_options.table">
  65. <title>配置选项</title>
  66. <tgroup cols="3">
  67. <thead>
  68. <row>
  69. <entry>选项名称</entry>
  70. <entry>是否必须</entry>
  71. <entry>描述</entry>
  72. </row>
  73. </thead>
  74. <tbody>
  75. <row>
  76. <entry><code>accept_schemes</code></entry>
  77. <entry>是</entry>
  78. <entry>
  79. 决定适配器从客户端接受哪个认证schemes。必须是包括<code>'basic'</code> 和/或 <code>'digest'</code>的空格分隔的列表。
  80. </entry>
  81. </row>
  82. <row>
  83. <entry><code>realm</code></entry>
  84. <entry>是</entry>
  85. <entry>
  86. 设置认证领域;在给定的领域里用户名必须是唯一的。
  87. </entry>
  88. </row>
  89. <row>
  90. <entry><code>digest_domains</code></entry>
  91. <entry>是,当<code>'accept_schemes'</code> 包含 <code>'digest'</code></entry>
  92. <entry>
  93. 相同认证信息是有效的空格分隔的URIs列表。 URIs不需要都只想同一个服务器。
  94. </entry>
  95. </row>
  96. <row>
  97. <entry><code>nonce_timeout</code></entry>
  98. <entry>是,当<code>'accept_schemes'</code> 包含 <code>'digest'</code></entry>
  99. <entry>
  100. 设置nonce有效的秒数。参见下面的注解。
  101. </entry>
  102. </row>
  103. <row>
  104. <entry><code>proxy_auth</code></entry>
  105. <entry>否</entry>
  106. <entry>
  107. 缺省是 Disabled 。Enable 后执行代理认证而不是正常的源服务器认证。
  108. </entry>
  109. </row>
  110. </tbody>
  111. </tgroup>
  112. </table>
  113. </para>
  114. <note>
  115. <para>
  116. 当前的<code>nonce_timeout</code>实现有一些有趣的副作用。这个设置用来决定给定的nonce的有效寿命,或者客户端的认证信息多长时间被有效地接受。目前,如果它被设置为3600(例如),它将导致适配器准点地每个小时提示客户端提供新的证书。一旦nonce跟踪和stale支持实现,这在将来的版本中被解决。
  117. </para>
  118. </note>
  119. </sect2>
  120. <sect2 id="zend.auth.adapter.http.resolvers">
  121. <title>Resolvers</title>
  122. <para>
  123. Resolver的作用是接受用户名和领域,并返回证书值。基本认证期望接收用户密码的Base64编码版本。摘要认证期望接收用户的用户名,领域和密码(用冒号分隔)的hash。当前,唯一支持的hash算法是MD5。
  124. </para>
  125. <para>
  126. <code>Zend_Auth_Adapter_Http</code> 依靠对象实现 <code>Zend_Auth_Adapter_Http_Resolver_Interface</code>。适配器中包含一个文本文件resolver类,但任何其它种类的resolver可以通过实现resolver接口来创建。
  127. </para>
  128. <sect3 id="zend.auth.adapter.http.resolvers.file">
  129. <title>文件 Resolver</title>
  130. <para>
  131. 文件resolver是一个非常简单的类。它有一个单个的属性指定一个文件名,它也可以被传递给构造器。它的<code>resolve()</code>方法遍历文本文件,用匹配用户名和领域来搜索一行。文本文件的格式和Apache htpasswd文件类似: <programlisting><![CDATA[
  132. <username>:<realm>:<credentials>\n
  133. ]]>
  134. </programlisting>每行包含三个字段 - 用户名,领域 和 证书 - 用冒号分隔。证书字段对文件resolver不透明;它被简单地不做修改地返回给调用者。所以,这同样的文件格式对基本和摘要认证都适合。对基本认证,证书字段应该是Base64编码的用户的密码。对摘要认证,它应该是上述的MD5 hash。
  135. </para>
  136. <para>
  137. 有两个同等简单的方法创建文件 Resolver:
  138. <programlisting role="php"><![CDATA[
  139. $path = 'files/passwd.txt';
  140. $resolver = new Zend_Auth_Adapter_Http_Resolver_File($path);
  141. ]]>
  142. </programlisting>
  143. 或者
  144. <programlisting role="php"><![CDATA[
  145. $path = 'files/passwd.txt';
  146. $resolver = new Zend_Auth_Adapter_Http_Resolver_File();
  147. $resolver->setFile($path);
  148. ]]>
  149. </programlisting>
  150. 如果给定的路径是空的或不可读,将抛出一个异常。
  151. </para>
  152. </sect3>
  153. </sect2>
  154. <sect2 id="zend.auth.adapter.http.basic_usage">
  155. <title>基本用法</title>
  156. <para>
  157. 首先,建立一个带有必需的配置值得数组:
  158. <programlisting role="php"><![CDATA[
  159. $config = array(
  160. 'accept_schemes' => 'basic digest',
  161. 'realm' => 'My Web Site',
  162. 'digest_domains' => '/members_only /my_account',
  163. 'nonce_timeout' => 3600,
  164. );
  165. ]]>
  166. </programlisting>
  167. 这个数组将使适配器接收基本或者摘要认证,并将请求对所有在<code>/members_only</code> 和<code>/my_account</code>之下的区域认证访问。领域值通常在浏览器中密码对话框中显示。<code>nonce_timeout</code>,当然,会有上述的行为。
  168. </para>
  169. <para>
  170. 下一步, 创建 Zend_Auth_Adapter_Http 对象:
  171. <programlisting role="php"><![CDATA[
  172. $adapter = new Zend_Auth_Adapter_Http($config);
  173. ]]>
  174. </programlisting>
  175. </para>
  176. <para>
  177. 因为我们对基本和摘要认证都支持,所以我们需要两个不同的resolver对象。注意这仅仅简单地是两个不同的类:
  178. <programlisting role="php"><![CDATA[
  179. $basicResolver = new Zend_Auth_Adapter_Http_Resolver_File();
  180. $basicResolver->setFile('files/basicPasswd.txt');
  181. $digestResolver = new Zend_Auth_Adapter_Http_Resolver_File();
  182. $digestResolver->setFile('files/digestPasswd.txt');
  183. $adapter->setBasicResolver($basicResolver);
  184. $adapter->setDigestResolver($digestResolver);
  185. ]]>
  186. </programlisting>
  187. </para>
  188. <para>
  189. 最后,我们执行认证。为了完成认证,适配器对请求(Request)和响应(Response)都需要一个reference:
  190. <programlisting role="php"><![CDATA[
  191. assert($request instanceof Zend_Controller_Request_Http);
  192. assert($response instanceof Zend_Controller_Response_Http);
  193. $adapter->setRequest($request);
  194. $adapter->setResponse($response);
  195. $result = $adapter->authenticate();
  196. if (!$result->isValid()) {
  197. // 错误的 userame/password,或者取消了密码提示
  198. }
  199. ]]>
  200. </programlisting>
  201. </para>
  202. </sect2>
  203. </sect1>
  204. <!--
  205. vim:se ts=4 sw=4 et:
  206. -->