multiuser-authentication.xml 7.0 KB

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