multiuser-authentication.xml 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="learning.multiuser.authentication">
  4. <title>Authenticating Users in ZF</title>
  5. <sect2 id="learning.multiuser.authentication.intro">
  6. <title>Introduction to Authentication</title>
  7. <para>
  8. Once a web application has been able to distinguish one user from another by establishing a
  9. session, web applications typically want to validate the identity of a user. The process
  10. of validating a consumer as being authentic is "authentication." Authentication is made up
  11. of two distinctive parts: an identity and a set of credentials. It takes some variation of
  12. both presented to the application for processing so that it may authenticate a user.
  13. </para>
  14. <para>
  15. While the most common pattern of authentication revolves around usernames and passwords,
  16. it should be stated that this is not always the case. Identities are not limited to
  17. usernames. In fact, any public identifier can be used: an assigned number, social security
  18. number, or residence address. Likewise, credentials are not limited to passwords.
  19. Credentials can come in the form of protected private information: fingerprint, eye retinal
  20. scan, passphrase, or any other obscure personal information.
  21. </para>
  22. </sect2>
  23. <sect2 id="learning.multiuser.authentication.basic-usage">
  24. <title>Basic Usage of Zend_Auth</title>
  25. <para>
  26. In the following example, we will be using Zend_Auth to complete what is probably the most
  27. prolific form of authentication: username and password from a database table. This
  28. example assumes that you have already setup your application using Zend_Application, and
  29. that inside that application you have configured a database connection.
  30. </para>
  31. <para>
  32. The job of the Zend_Auth class is twofold. First, it should be able to accept an
  33. authentication adapter to use to authenticate a user. Secondly, after a successful
  34. authentication of a user, it should persist throughout each and every request that might
  35. need to know if the current user has indeed been authenticated. To persist this data,
  36. Zend_Auth consumes Zend_Session_Namespace, but you will generally never need to interact
  37. with this session object.
  38. </para>
  39. <para>
  40. Lets assume we have the following database table setup:
  41. </para>
  42. <programlisting language="php"><![CDATA[
  43. CREATE TABLE users (
  44. id INTEGER NOT NULL PRIMARY KEY,
  45. username VARCHAR(50) UNIQUE NOT NULL,
  46. password VARCHAR(32) NULL,
  47. password_salt VARCHAR(32) NULL,
  48. real_name VARCHAR(150) NULL
  49. )
  50. ]]></programlisting>
  51. <para>
  52. The above demonstrates a user table that includes a username, password, and also a
  53. password salt column. This salt column is used as part of a technique called salting that
  54. would improve the security of your database of information against brute force attacks
  55. targeting the algorithm of your password hashing.
  56. <a href="http://en.wikipedia.org/wiki/Salting_%28cryptography%29">More information</a>
  57. on salting.
  58. </para>
  59. <para>
  60. For this implementation, we must first make a simple form that we can utilized as the
  61. "login form". We will use Zend_Form to accomplish this.
  62. </para>
  63. <programlisting language="php"><![CDATA[
  64. <?php
  65. // located at application/forms/Auth/Login.php
  66. class Default_Form_Auth_Login extends Zend_Form
  67. {
  68. public function init()
  69. {
  70. $this->setMethod('post');
  71. $this->addElement(
  72. 'text', 'username', array(
  73. 'label' => 'Username:',
  74. 'required' => true,
  75. 'filters' => array('StringTrim'),
  76. ));
  77. $this->addElement('password', 'password', array(
  78. 'label' => 'Password:',
  79. 'required' => true,
  80. ));
  81. $this->addElement('submit', 'submit', array(
  82. 'ignore' => true,
  83. 'label' => 'Login',
  84. ));
  85. }
  86. }
  87. ]]></programlisting>
  88. <para>
  89. With the above form, we can now go about creating our login action for
  90. our authentication controller. This controller will be called "AuthController", and
  91. will be located at application/controllers/AuthController.php. It will have a single
  92. method called "loginAction" which will serve as the self-posting action. In other words,
  93. regardless of the url was POSTed to or GETed to, this method will handle the logic.
  94. </para>
  95. <para>
  96. The following code will demonstrate how to construct the proper adapter, integrate it
  97. with the form:
  98. </para>
  99. <programlisting language="php"><![CDATA[
  100. class AuthController extends Zend_Controller_Action
  101. {
  102. public function loginAction()
  103. {
  104. $db = $this->_getParam('db');
  105. $loginForm = new Default_Form_Auth_Login($_POST);
  106. if ($loginForm->isValid()) {
  107. $adapter = new Zend_Auth_Adapter_DbTable(
  108. $db,
  109. 'users',
  110. 'username',
  111. 'password',
  112. 'MD5(CONCAT(?, password_salt))'
  113. );
  114. $adapter->setIdentity($loginForm->getValue('username'));
  115. $adapter->setCredential($loginForm->getValue('password'));
  116. $result = $auth->authenticate($adapter);
  117. if ($result->isValid()) {
  118. $this->_helper->FlashMessenger('Successful Login');
  119. $this->redirect('/');
  120. return;
  121. }
  122. }
  123. $this->view->loginForm = $loginForm;
  124. }
  125. }
  126. ]]></programlisting>
  127. <para>
  128. The corresponding view script is quite simple for this action. It will set the current
  129. url since this form is self processing, and it will display the form. This view script is
  130. located at application/views/scripts/auth/login.phtml:
  131. </para>
  132. <programlisting language="php"><![CDATA[
  133. <?php
  134. $this->form->setAction($this->url());
  135. echo $this->form;
  136. ]]></programlisting>
  137. <para>
  138. There you have it. With these basics you can expand the general concepts to include
  139. more complex authentication scenarios. For more information on other Zend_Auth adapters,
  140. have a look in <link linkend="zend.auth">the reference guide</link>.
  141. </para>
  142. </sect2>
  143. </sect1>