multiuser-authentication.xml 6.9 KB

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