Zend_Auth_Adapter_DbTable.xml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.auth.adapter.dbtable">
  4. <title>Database Table Authentication</title>
  5. <sect2 id="zend.auth.adapter.dbtable.introduction">
  6. <title>Introduction</title>
  7. <para>
  8. <classname>Zend_Auth_Adapter_DbTable</classname> provides the ability to
  9. authenticate against credentials stored in a database table. Because
  10. <classname>Zend_Auth_Adapter_DbTable</classname> requires an instance of
  11. <classname>Zend_Db_Adapter_Abstract</classname> to be passed to its
  12. constructor, each instance is bound to a particular database
  13. connection. Other configuration options may be set through the
  14. constructor and through instance methods, one for each option.
  15. </para>
  16. <para>
  17. The available configuration options include:
  18. <itemizedlist>
  19. <listitem>
  20. <para>
  21. <code>tableName</code>: This is the name of the database
  22. table that contains the authentication credentials,
  23. and against which the database authentication query is
  24. performed.
  25. </para>
  26. </listitem>
  27. <listitem>
  28. <para>
  29. <code>identityColumn</code>: This is the name of the
  30. database table column used to represent the identity.
  31. The identity column must contain unique values, such as
  32. a username or e-mail address.
  33. </para>
  34. </listitem>
  35. <listitem>
  36. <para>
  37. <code>credentialColumn</code>: This is the name of the
  38. database table column used to represent the credential.
  39. Under a simple identity and password authentication
  40. scheme, the credential value corresponds to the
  41. password. See also the <code>credentialTreatment</code>
  42. option.
  43. </para>
  44. </listitem>
  45. <listitem>
  46. <para>
  47. <code>credentialTreatment</code>: In many cases,
  48. passwords and other sensitive data are encrypted,
  49. hashed, encoded, obscured, salted or otherwise treated
  50. through some function or algorithm. By specifying a
  51. parameterized treatment string with this method, such as
  52. <code>'MD5(?)'</code> or <code>'PASSWORD(?)'</code>, a
  53. developer may apply such arbitrary SQL upon input
  54. credential data. Since these functions are specific to
  55. the underlying RDBMS, check the database manual for the
  56. availability of such functions for your database system.
  57. </para>
  58. </listitem>
  59. </itemizedlist>
  60. </para>
  61. <example id="zend.auth.adapter.dbtable.introduction.example.basic_usage">
  62. <title>Basic Usage</title>
  63. <para>
  64. As explained in the introduction, the
  65. <classname>Zend_Auth_Adapter_DbTable</classname> constructor requires an
  66. instance of <classname>Zend_Db_Adapter_Abstract</classname> that serves as
  67. the database connection to which the authentication adapter
  68. instance is bound. First, the database connection should be
  69. created.
  70. </para>
  71. <para>
  72. The following code creates an adapter for an in-memory database,
  73. creates a simple table schema, and inserts a row against
  74. which we can perform an authentication query later. This example
  75. requires the PDO SQLite extension to be available:
  76. </para>
  77. <programlisting role="php"><![CDATA[
  78. // Create an in-memory SQLite database connection
  79. $dbAdapter = new Zend_Db_Adapter_Pdo_Sqlite(array('dbname' =>
  80. ':memory:'));
  81. // Build a simple table creation query
  82. $sqlCreate = 'CREATE TABLE [users] ('
  83. . '[id] INTEGER NOT NULL PRIMARY KEY, '
  84. . '[username] VARCHAR(50) UNIQUE NOT NULL, '
  85. . '[password] VARCHAR(32) NULL, '
  86. . '[real_name] VARCHAR(150) NULL)';
  87. // Create the authentication credentials table
  88. $dbAdapter->query($sqlCreate);
  89. // Build a query to insert a row for which authentication may succeed
  90. $sqlInsert = "INSERT INTO users (username, password, real_name) "
  91. . "VALUES ('my_username', 'my_password', 'My Real Name')";
  92. // Insert the data
  93. $dbAdapter->query($sqlInsert);
  94. ]]></programlisting>
  95. <para>
  96. With the database connection and table data available, an
  97. instance of <classname>Zend_Auth_Adapter_DbTable</classname> may be
  98. created. Configuration option values may be passed to the
  99. constructor or deferred as parameters to setter methods after
  100. instantiation:
  101. </para>
  102. <programlisting role="php"><![CDATA[
  103. // Configure the instance with constructor parameters...
  104. $authAdapter = new Zend_Auth_Adapter_DbTable(
  105. $dbAdapter,
  106. 'users',
  107. 'username',
  108. 'password'
  109. );
  110. // ...or configure the instance with setter methods
  111. $authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);
  112. $authAdapter
  113. ->setTableName('users')
  114. ->setIdentityColumn('username')
  115. ->setCredentialColumn('password')
  116. ;
  117. ]]></programlisting>
  118. <para>
  119. At this point, the authentication adapter instance is ready to
  120. accept authentication queries. In order to formulate an
  121. authentication query, the input credential values are passed to
  122. the adapter prior to calling the <code>authenticate()</code>
  123. method:
  124. </para>
  125. <programlisting role="php"><![CDATA[
  126. // Set the input credential values (e.g., from a login form)
  127. $authAdapter
  128. ->setIdentity('my_username')
  129. ->setCredential('my_password')
  130. ;
  131. // Perform the authentication query, saving the result
  132. ]]></programlisting>
  133. <para>
  134. In addition to the availability of the
  135. <code>getIdentity()</code> method upon the authentication result
  136. object, <classname>Zend_Auth_Adapter_DbTable</classname> also supports
  137. retrieving the table row upon authentication success:
  138. </para>
  139. <programlisting role="php"><![CDATA[
  140. // Print the identity
  141. echo $result->getIdentity() . "\n\n";
  142. // Print the result row
  143. print_r($authAdapter->getResultRowObject());
  144. /* Output:
  145. my_username
  146. Array
  147. (
  148. [id] => 1
  149. [username] => my_username
  150. [password] => my_password
  151. [real_name] => My Real Name
  152. )
  153. ]]></programlisting>
  154. <para>
  155. Since the table row contains the credential value, it is
  156. important to secure the values against unintended access.
  157. </para>
  158. </example>
  159. </sect2>
  160. <sect2 id="zend.auth.adapter.dbtable.advanced.storing_result_row">
  161. <title>Advanced Usage: Persisting a DbTable Result Object</title>
  162. <para>
  163. By default, <classname>Zend_Auth_Adapter_DbTable</classname> returns the
  164. identity supplied back to the auth object upon successful
  165. authentication. Another use case scenario, where developers want to
  166. store to the persistent storage mechanism of <classname>Zend_Auth</classname>
  167. an identity object containing other useful information, is solved by
  168. using the <code>getResultRowObject()</code> method to return a
  169. <code>stdClass</code> object. The following code snippet illustrates
  170. its use:
  171. </para>
  172. <programlisting role="php"><![CDATA[
  173. // authenticate with Zend_Auth_Adapter_DbTable
  174. $result = $this->_auth->authenticate($adapter);
  175. if ($result->isValid()) {
  176. // store the identity as an object where only the username and
  177. // real_name have been returned
  178. $storage = $this->_auth->getStorage();
  179. $storage->write($adapter->getResultRowObject(array(
  180. 'username',
  181. 'real_name',
  182. )));
  183. // store the identity as an object where the password column has
  184. // been omitted
  185. $storage->write($adapter->getResultRowObject(
  186. null,
  187. 'password'
  188. ));
  189. /* ... */
  190. } else {
  191. /* ... */
  192. }
  193. ]]></programlisting>
  194. </sect2>
  195. <sect2 id="zend.auth.adapter.dbtable.advanced.advanced_usage">
  196. <title>Advanced Usage By Example</title>
  197. <para>
  198. While the primary purpose of <classname>Zend_Auth</classname> (and consequently
  199. <classname>Zend_Auth_Adapter_DbTable</classname>) is primarily
  200. <emphasis role="strong">authentication</emphasis> and not
  201. <emphasis role="strong">authorization</emphasis>, there are a few
  202. instances and problems that toe the line between which domain they fit
  203. within. Depending on how you’ve decided to explain your problem, it
  204. sometimes makes sense to solve what could look like an
  205. authorization problem within the authentication adapter.
  206. </para>
  207. <para>
  208. With that disclaimer out of the way,
  209. <classname>Zend_Auth_Adapter_DbTable</classname> has some built in mechanisms that can be
  210. leveraged for additional checks at authentication time to solve
  211. some common user problems.
  212. </para>
  213. <programlisting role="php"><![CDATA[
  214. // The status field value of an account is not equal to "compromised"
  215. $adapter = new Zend_Auth_Adapter_DbTable(
  216. $db,
  217. 'users',
  218. 'username',
  219. 'password',
  220. 'MD5(?) AND status != "compromised"'
  221. );
  222. // The active field value of an account is equal to "TRUE"
  223. $adapter = new Zend_Auth_Adapter_DbTable(
  224. $db,
  225. 'users',
  226. 'username',
  227. 'password',
  228. 'MD5(?) AND active = "TRUE"'
  229. ]]></programlisting>
  230. <para>
  231. Another scenario can be the implementation of a salting mechanism.
  232. Salting is a term referring to a technique which can highly improve
  233. your application’s security. It’s based on the idea that
  234. concatenating a random string to every password makes it impossible
  235. to accomplish a successful brute force attack on the database using
  236. pre-computed hash values from a dictionary.
  237. </para>
  238. <para>
  239. Therefore, we need to modify our table to store our salt string:
  240. </para>
  241. <programlisting role="php"><![CDATA[
  242. $sqlAlter = "ALTER TABLE [users] "
  243. . "ADD COLUMN [password_salt] "
  244. . "AFTER [password]";
  245. ]]></programlisting>
  246. <para>
  247. Here’s a simple way to generate a salt string for every user at
  248. registration:
  249. </para>
  250. <programlisting role="php"><![CDATA[
  251. for ($i = 0; $i < 50; $i++) {
  252. $dynamicSalt .= chr(rand(33, 126));
  253. ]]></programlisting>
  254. <para>
  255. And now let’s build the adapter:
  256. </para>
  257. <programlisting role="php"><![CDATA[
  258. $adapter = new Zend_Auth_Adapter_DbTable(
  259. $db,
  260. 'users',
  261. 'username',
  262. 'password',
  263. "MD5(CONCAT('"
  264. . Zend_Registry::get('staticSalt')
  265. . "', ?, password_salt))"
  266. );
  267. ]]></programlisting>
  268. <note>
  269. <para>
  270. You can improve security even more by using a static salt value
  271. hard coded into your application. In the case that your database
  272. is compromised (e. g. by an SQL injection attack) but your web
  273. server is intact your data is still unusable for the attacker.
  274. </para>
  275. </note>
  276. <para>
  277. Another alternative is to use the <code>getDbSelect()</code> method
  278. of the Zend_Auth_Adapter_DbTable after the adapter has been constructed.
  279. This method will return the Zend_Db_Select object instance it will use
  280. to complete the authenticate() routine. It is important to note that
  281. this method will always return the same object regardless if authenticate()
  282. has been called or not. This object <emphasis>will not</emphasis> have any of the
  283. identity or credential information in it as those values are placed
  284. into the select object at authenticate() time.
  285. </para>
  286. <para>
  287. An example of a situation where one might want to use the getDbSelect()
  288. method would check the status of a user, in other words to see if that
  289. user's account is enabled.
  290. </para>
  291. <programlisting role="php"><![CDATA[
  292. // Continuing with the example from above
  293. $adapter = new Zend_Auth_Adapter_DbTable(
  294. $db,
  295. 'users',
  296. 'username',
  297. 'password',
  298. 'MD5(?)'
  299. );
  300. // get select object (by reference)
  301. $select = $adapter->getDbSelect();
  302. $select->where('active = "TRUE"');
  303. // authenticate, this ensures that users.active = TRUE
  304. $adapter->authenticate();
  305. ]]></programlisting>
  306. </sect2>
  307. </sect1>