Zend_Auth.xml 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.auth.introduction">
  4. <title>Introduction</title>
  5. <para>
  6. <classname>Zend_Auth</classname> provides an API for authentication and includes concrete
  7. authentication adapters for common use case scenarios.
  8. </para>
  9. <para>
  10. <classname>Zend_Auth</classname> is concerned only with <emphasis>authentication</emphasis>
  11. and not with <emphasis>authorization</emphasis>. Authentication is loosely defined as
  12. determining whether an entity actually is what it purports to be (i.e., identification),
  13. based on some set of credentials. Authorization, the process of deciding whether to allow
  14. an entity access to, or to perform operations upon, other entities is outside the scope of
  15. <classname>Zend_Auth</classname>. For more information about authorization and access
  16. control with Zend Framework, please see <link
  17. linkend="zend.acl"><classname>Zend_Acl</classname></link>.
  18. </para>
  19. <note>
  20. <para>
  21. The <classname>Zend_Auth</classname> class implements the Singleton pattern - only one
  22. instance of the class is available - through its static
  23. <methodname>getInstance()</methodname> method. This means that using the
  24. <emphasis>new</emphasis> operator and the <emphasis>clone</emphasis> keyword will not
  25. work with the <classname>Zend_Auth</classname> class; use
  26. <methodname>Zend_Auth::getInstance()</methodname> instead.
  27. </para>
  28. </note>
  29. <sect2 id="zend.auth.introduction.adapters">
  30. <title>Adapters</title>
  31. <para>
  32. A <classname>Zend_Auth</classname> adapter is used to authenticate against a particular
  33. type of authentication service, such as LDAP, RDBMS, or file-based storage. Different
  34. adapters are likely to have vastly different options and behaviors, but some basic
  35. things are common among authentication adapters. For example, accepting authentication
  36. credentials (including a purported identity), performing queries against the
  37. authentication service, and returning results are common to
  38. <classname>Zend_Auth</classname> adapters.
  39. </para>
  40. <para>
  41. Each <classname>Zend_Auth</classname> adapter class implements
  42. <classname>Zend_Auth_Adapter_Interface</classname>. This interface defines one method,
  43. <methodname>authenticate()</methodname>, that an adapter class must implement for
  44. performing an authentication query. Each adapter class must be prepared prior to
  45. calling <methodname>authenticate()</methodname>. Such adapter preparation includes
  46. setting up credentials (e.g., username and password) and defining values for
  47. adapter-specific configuration options, such as database connection settings for a
  48. database table adapter.
  49. </para>
  50. <para>
  51. The following is an example authentication adapter that requires a username and
  52. password to be set for authentication. Other details, such as how the authentication
  53. service is queried, have been omitted for brevity:
  54. <programlisting language="php"><![CDATA[
  55. class MyAuthAdapter implements Zend_Auth_Adapter_Interface
  56. {
  57. /**
  58. * Sets username and password for authentication
  59. *
  60. * @return void
  61. */
  62. public function __construct($username, $password)
  63. {
  64. // ...
  65. }
  66. /**
  67. * Performs an authentication attempt
  68. *
  69. * @throws Zend_Auth_Adapter_Exception If authentication cannot
  70. * be performed
  71. * @return Zend_Auth_Result
  72. */
  73. public function authenticate()
  74. {
  75. // ...
  76. }
  77. }
  78. ]]></programlisting>
  79. As indicated in its docblock, <methodname>authenticate()</methodname> must return an
  80. instance of <classname>Zend_Auth_Result</classname> (or of a class derived from
  81. <classname>Zend_Auth_Result</classname>). If for some reason performing an
  82. authentication query is impossible, <methodname>authenticate()</methodname> should
  83. throw an exception that derives from
  84. <classname>Zend_Auth_Adapter_Exception</classname>.
  85. </para>
  86. </sect2>
  87. <sect2 id="zend.auth.introduction.results">
  88. <title>Results</title>
  89. <para>
  90. <classname>Zend_Auth</classname> adapters return an instance of
  91. <classname>Zend_Auth_Result</classname> with <methodname>authenticate()</methodname> in
  92. order to represent the results of an authentication attempt. Adapters populate the
  93. <classname>Zend_Auth_Result</classname> object upon construction, so that the following
  94. four methods provide a basic set of user-facing operations that are common to the
  95. results of <classname>Zend_Auth</classname> adapters:
  96. <itemizedlist>
  97. <listitem>
  98. <para>
  99. <methodname>isValid()</methodname> - returns true if and only if the result
  100. represents a successful authentication attempt
  101. </para>
  102. </listitem>
  103. <listitem>
  104. <para>
  105. <methodname>getCode()</methodname> - returns a
  106. <classname>Zend_Auth_Result</classname> constant identifier for determining
  107. the type of authentication failure or whether success has occurred. This
  108. may be used in situations where the developer wishes to distinguish among
  109. several authentication result types. This allows developers to maintain
  110. detailed authentication result statistics, for example. Another use of this
  111. feature is to provide specific, customized messages to users for usability
  112. reasons, though developers are encouraged to consider the risks of
  113. providing such detailed reasons to users, instead of a general
  114. authentication failure message. For more information, see the notes below.
  115. </para>
  116. </listitem>
  117. <listitem>
  118. <para>
  119. <methodname>getIdentity()</methodname> - returns the identity of the
  120. authentication attempt
  121. </para>
  122. </listitem>
  123. <listitem>
  124. <para>
  125. <methodname>getMessages()</methodname> - returns an array of messages
  126. regarding a failed authentication attempt
  127. </para>
  128. </listitem>
  129. </itemizedlist>
  130. </para>
  131. <para>
  132. A developer may wish to branch based on the type of authentication result in order to
  133. perform more specific operations. Some operations developers might find useful are
  134. locking accounts after too many unsuccessful password attempts, flagging an IP address
  135. after too many nonexistent identities are attempted, and providing specific, customized
  136. authentication result messages to the user. The following result codes are available:
  137. <programlisting language="php"><![CDATA[
  138. Zend_Auth_Result::SUCCESS
  139. Zend_Auth_Result::FAILURE
  140. Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND
  141. Zend_Auth_Result::FAILURE_IDENTITY_AMBIGUOUS
  142. Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID
  143. Zend_Auth_Result::FAILURE_UNCATEGORIZED
  144. ]]></programlisting>
  145. </para>
  146. <para>
  147. The following example illustrates how a developer may branch on the result code:
  148. <programlisting language="php"><![CDATA[
  149. // inside of AuthController / loginAction
  150. $result = $this->_auth->authenticate($adapter);
  151. switch ($result->getCode()) {
  152. case Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND:
  153. /** do stuff for nonexistent identity **/
  154. break;
  155. case Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID:
  156. /** do stuff for invalid credential **/
  157. break;
  158. case Zend_Auth_Result::SUCCESS:
  159. /** do stuff for successful authentication **/
  160. break;
  161. default:
  162. /** do stuff for other failure **/
  163. break;
  164. }
  165. ]]></programlisting>
  166. </para>
  167. </sect2>
  168. <sect2 id="zend.auth.introduction.persistence">
  169. <title>Identity Persistence</title>
  170. <para>
  171. Authenticating a request that includes authentication credentials is useful per se, but
  172. it is also important to support maintaining the authenticated identity without having
  173. to present the authentication credentials with each request.
  174. </para>
  175. <para>
  176. HTTP is a stateless protocol, however, and techniques such as cookies and sessions have
  177. been developed in order to facilitate maintaining state across multiple requests in
  178. server-side web applications.
  179. </para>
  180. <sect3 id="zend.auth.introduction.persistence.default">
  181. <title>Default Persistence in the PHP Session</title>
  182. <para>
  183. By default, <classname>Zend_Auth</classname> provides persistent storage of the
  184. identity from a successful authentication attempt using the PHP session. Upon a
  185. successful authentication attempt,
  186. <classname>Zend_Auth::authenticate()</classname> stores the identity from the
  187. authentication result into persistent storage. Unless configured otherwise,
  188. <classname>Zend_Auth</classname> uses a storage class named
  189. <classname>Zend_Auth_Storage_Session</classname>, which, in turn, uses
  190. <link linkend="zend.session"><classname>Zend_Session</classname></link>. A custom
  191. class may instead be used by providing an object that implements
  192. <classname>Zend_Auth_Storage_Interface</classname> to
  193. <classname>Zend_Auth::setStorage()</classname>.
  194. </para>
  195. <note>
  196. <para>
  197. If automatic persistent storage of the identity is not appropriate for a
  198. particular use case, then developers may forgo using the
  199. <classname>Zend_Auth</classname> class altogether, instead using an adapter
  200. class directly.
  201. </para>
  202. </note>
  203. <example id="zend.auth.introduction.persistence.default.example">
  204. <title>Modifying the Session Namespace</title>
  205. <para>
  206. <classname>Zend_Auth_Storage_Session</classname> uses a session namespace of
  207. 'Zend_Auth'. This namespace may be overridden by passing a different value to
  208. the constructor of <classname>Zend_Auth_Storage_Session</classname>, and this
  209. value is internally passed along to the constructor of
  210. <classname>Zend_Session_Namespace</classname>. This should occur before
  211. authentication is attempted, since
  212. <classname>Zend_Auth::authenticate()</classname> performs the automatic storage
  213. of the identity.
  214. <programlisting language="php"><![CDATA[
  215. // Save a reference to the Singleton instance of Zend_Auth
  216. $auth = Zend_Auth::getInstance();
  217. // Use 'someNamespace' instead of 'Zend_Auth'
  218. $auth->setStorage(new Zend_Auth_Storage_Session('someNamespace'));
  219. /**
  220. * @todo Set up the auth adapter, $authAdapter
  221. */
  222. // Authenticate, saving the result, and persisting the identity on
  223. // success
  224. $result = $auth->authenticate($authAdapter);
  225. ]]></programlisting>
  226. </para>
  227. </example>
  228. </sect3>
  229. <sect3 id="zend.auth.introduction.persistence.custom">
  230. <title>Implementing Customized Storage</title>
  231. <para>
  232. Sometimes developers may need to use a different identity storage mechanism than
  233. that provided by <classname>Zend_Auth_Storage_Session</classname>. For such cases
  234. developers may simply implement <classname>Zend_Auth_Storage_Interface</classname>
  235. and supply an instance of the class to
  236. <classname>Zend_Auth::setStorage()</classname>.
  237. </para>
  238. <example id="zend.auth.introduction.persistence.custom.example">
  239. <title>Using a Custom Storage Class</title>
  240. <para>
  241. In order to use an identity persistence storage class other than
  242. <classname>Zend_Auth_Storage_Session</classname>, a developer implements
  243. <classname>Zend_Auth_Storage_Interface</classname>:
  244. <programlisting language="php"><![CDATA[
  245. class MyStorage implements Zend_Auth_Storage_Interface
  246. {
  247. /**
  248. * Returns true if and only if storage is empty
  249. *
  250. * @throws Zend_Auth_Storage_Exception If it is impossible to
  251. * determine whether storage
  252. * is empty
  253. * @return boolean
  254. */
  255. public function isEmpty()
  256. {
  257. /**
  258. * @todo implementation
  259. */
  260. }
  261. /**
  262. * Returns the contents of storage
  263. *
  264. * Behavior is undefined when storage is empty.
  265. *
  266. * @throws Zend_Auth_Storage_Exception If reading contents from
  267. * storage is impossible
  268. * @return mixed
  269. */
  270. public function read()
  271. {
  272. /**
  273. * @todo implementation
  274. */
  275. }
  276. /**
  277. * Writes $contents to storage
  278. *
  279. * @param mixed $contents
  280. * @throws Zend_Auth_Storage_Exception If writing $contents to
  281. * storage is impossible
  282. * @return void
  283. */
  284. public function write($contents)
  285. {
  286. /**
  287. * @todo implementation
  288. */
  289. }
  290. /**
  291. * Clears contents from storage
  292. *
  293. * @throws Zend_Auth_Storage_Exception If clearing contents from
  294. * storage is impossible
  295. * @return void
  296. */
  297. public function clear()
  298. {
  299. /**
  300. * @todo implementation
  301. */
  302. }
  303. }
  304. ]]></programlisting>
  305. </para>
  306. <para>
  307. In order to use this custom storage class,
  308. <classname>Zend_Auth::setStorage()</classname> is invoked before an
  309. authentication query is attempted:
  310. <programlisting language="php"><![CDATA[
  311. // Instruct Zend_Auth to use the custom storage class
  312. Zend_Auth::getInstance()->setStorage(new MyStorage());
  313. /**
  314. * @todo Set up the auth adapter, $authAdapter
  315. */
  316. // Authenticate, saving the result, and persisting the identity on
  317. // success
  318. $result = Zend_Auth::getInstance()->authenticate($authAdapter);
  319. ]]></programlisting>
  320. </para>
  321. </example>
  322. </sect3>
  323. </sect2>
  324. <sect2 id="zend.auth.introduction.using">
  325. <title>Usage</title>
  326. <para>
  327. There are two provided ways to use <classname>Zend_Auth</classname> adapters:
  328. <orderedlist>
  329. <listitem>
  330. <para>
  331. indirectly, through <classname>Zend_Auth::authenticate()</classname>
  332. </para>
  333. </listitem>
  334. <listitem>
  335. <para>
  336. directly, through the adapter's <methodname>authenticate()</methodname> method
  337. </para>
  338. </listitem>
  339. </orderedlist>
  340. </para>
  341. <para>
  342. The following example illustrates how to use a <classname>Zend_Auth</classname> adapter
  343. indirectly, through the use of the <classname>Zend_Auth</classname> class:
  344. <programlisting language="php"><![CDATA[
  345. // Get a reference to the singleton instance of Zend_Auth
  346. $auth = Zend_Auth::getInstance();
  347. // Set up the authentication adapter
  348. $authAdapter = new MyAuthAdapter($username, $password);
  349. // Attempt authentication, saving the result
  350. $result = $auth->authenticate($authAdapter);
  351. if (!$result->isValid()) {
  352. // Authentication failed; print the reasons why
  353. foreach ($result->getMessages() as $message) {
  354. echo "$message\n";
  355. }
  356. } else {
  357. // Authentication succeeded; the identity ($username) is stored
  358. // in the session
  359. // $result->getIdentity() === $auth->getIdentity()
  360. // $result->getIdentity() === $username
  361. }
  362. ]]></programlisting>
  363. </para>
  364. <para>
  365. Once authentication has been attempted in a request, as in the above example, it is a
  366. simple matter to check whether a successfully authenticated identity exists:
  367. <programlisting language="php"><![CDATA[
  368. $auth = Zend_Auth::getInstance();
  369. if ($auth->hasIdentity()) {
  370. // Identity exists; get it
  371. $identity = $auth->getIdentity();
  372. }
  373. ]]></programlisting>
  374. </para>
  375. <para>
  376. To remove an identity from persistent storage, simply use the
  377. <methodname>clearIdentity()</methodname> method. This typically would be used for
  378. implementing an application "logout" operation:
  379. <programlisting language="php"><![CDATA[
  380. Zend_Auth::getInstance()->clearIdentity();
  381. ]]></programlisting>
  382. </para>
  383. <para>
  384. When the automatic use of persistent storage is inappropriate for a particular use
  385. case, a developer may simply bypass the use of the <classname>Zend_Auth</classname>
  386. class, using an adapter class directly. Direct use of an adapter class involves
  387. configuring and preparing an adapter object and then calling its
  388. <methodname>authenticate()</methodname> method. Adapter-specific details are discussed
  389. in the documentation for each adapter. The following example directly utilizes
  390. <emphasis>MyAuthAdapter</emphasis>:
  391. <programlisting language="php"><![CDATA[
  392. // Set up the authentication adapter
  393. $authAdapter = new MyAuthAdapter($username, $password);
  394. // Attempt authentication, saving the result
  395. $result = $authAdapter->authenticate();
  396. if (!$result->isValid()) {
  397. // Authentication failed; print the reasons why
  398. foreach ($result->getMessages() as $message) {
  399. echo "$message\n";
  400. }
  401. } else {
  402. // Authentication succeeded
  403. // $result->getIdentity() === $username
  404. }
  405. ]]></programlisting>
  406. </para>
  407. </sect2>
  408. </sect1>
  409. <!--
  410. vim:se ts=4 sw=4 et:
  411. -->