multiuser-authentication.xml 7.0 KB

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