ChangePasswordTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Ldap
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Zend_Ldap_OnlineTestCase
  24. */
  25. require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'OnlineTestCase.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Ldap
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Ldap_ChangePasswordTest extends Zend_Ldap_OnlineTestCase
  34. {
  35. public function testAddNewUserWithPasswordOpenLdap()
  36. {
  37. if ($this->_getLdap()->getRootDse()->getServerType() !==
  38. Zend_Ldap_Node_RootDse::SERVER_TYPE_OPENLDAP) {
  39. $this->markTestSkipped('Test can only be run on an OpenLDAP server');
  40. }
  41. $dn = $this->_createDn('uid=newuser,');
  42. $data = array();
  43. $password = 'pa$$w0rd';
  44. Zend_Ldap_Attribute::setAttribute($data, 'uid', 'newuser', false);
  45. Zend_Ldap_Attribute::setAttribute($data, 'objectClass', 'account', true);
  46. Zend_Ldap_Attribute::setAttribute($data, 'objectClass', 'simpleSecurityObject', true);
  47. Zend_Ldap_Attribute::setPassword($data, $password,
  48. Zend_Ldap_Attribute::PASSWORD_HASH_SSHA, 'userPassword');
  49. try {
  50. $this->_getLdap()->add($dn, $data);
  51. $this->assertType('Zend_Ldap', $this->_getLdap()->bind($dn, $password));
  52. $this->_getLdap()->bind();
  53. $this->_getLdap()->delete($dn);
  54. } catch (Zend_Ldap_Exception $e) {
  55. $this->_getLdap()->bind();
  56. if ($this->_getLdap()->exists($dn)) {
  57. $this->_getLdap()->delete($dn);
  58. }
  59. $this->fail($e->getMessage());
  60. }
  61. }
  62. public function testChangePasswordWithUserAccountOpenLdap()
  63. {
  64. if ($this->_getLdap()->getRootDse()->getServerType() !==
  65. Zend_Ldap_Node_RootDse::SERVER_TYPE_OPENLDAP) {
  66. $this->markTestSkipped('Test can only be run on an OpenLDAP server');
  67. }
  68. $dn = $this->_createDn('uid=newuser,');
  69. $data = array();
  70. $password = 'pa$$w0rd';
  71. Zend_Ldap_Attribute::setAttribute($data, 'uid', 'newuser', false);
  72. Zend_Ldap_Attribute::setAttribute($data, 'objectClass', 'account', true);
  73. Zend_Ldap_Attribute::setAttribute($data, 'objectClass', 'simpleSecurityObject', true);
  74. Zend_Ldap_Attribute::setPassword($data, $password,
  75. Zend_Ldap_Attribute::PASSWORD_HASH_SSHA, 'userPassword');
  76. try {
  77. $this->_getLdap()->add($dn, $data);
  78. $this->_getLdap()->bind($dn, $password);
  79. $newPasswd = 'newpasswd';
  80. $newData = array();
  81. Zend_Ldap_Attribute::setPassword($newData, $newPasswd,
  82. Zend_Ldap_Attribute::PASSWORD_HASH_SHA, 'userPassword');
  83. $this->_getLdap()->update($dn, $newData);
  84. try {
  85. $this->_getLdap()->bind($dn, $password);
  86. $this->fail('Expected exception not thrown');
  87. } catch (Zend_Ldap_Exception $zle) {
  88. $message = $zle->getMessage();
  89. $this->assertTrue(strstr($message, 'Invalid credentials') ||
  90. strstr($message, 'Server is unwilling to perform'));
  91. }
  92. $this->assertType('Zend_Ldap', $this->_getLdap()->bind($dn, $newPasswd));
  93. $this->_getLdap()->bind();
  94. $this->_getLdap()->delete($dn);
  95. } catch (Zend_Ldap_Exception $e) {
  96. $this->_getLdap()->bind();
  97. if ($this->_getLdap()->exists($dn)) {
  98. $this->_getLdap()->delete($dn);
  99. }
  100. $this->fail($e->getMessage());
  101. }
  102. }
  103. public function testAddNewUserWithPasswordActiveDirectory()
  104. {
  105. if ($this->_getLdap()->getRootDse()->getServerType() !==
  106. Zend_Ldap_Node_RootDse::SERVER_TYPE_ACTIVEDIRECTORY) {
  107. $this->markTestSkipped('Test can only be run on an ActiveDirectory server');
  108. }
  109. $options = $this->_getLdap()->getOptions();
  110. if ($options['useSsl'] !== true && $options['useStartTls'] !== true) {
  111. $this->markTestSkipped('Test can only be run on an SSL or TLS secured connection');
  112. }
  113. $dn = $this->_createDn('cn=New User,');
  114. $data = array();
  115. $password = 'pa$$w0rd';
  116. Zend_Ldap_Attribute::setAttribute($data, 'cn', 'New User', false);
  117. Zend_Ldap_Attribute::setAttribute($data, 'displayName', 'New User', false);
  118. Zend_Ldap_Attribute::setAttribute($data, 'sAMAccountName', 'newuser', false);
  119. Zend_Ldap_Attribute::setAttribute($data, 'userAccountControl', 512, false);
  120. Zend_Ldap_Attribute::setAttribute($data, 'objectClass', 'person', true);
  121. Zend_Ldap_Attribute::setAttribute($data, 'objectClass', 'organizationalPerson', true);
  122. Zend_Ldap_Attribute::setAttribute($data, 'objectClass', 'user', true);
  123. Zend_Ldap_Attribute::setPassword($data, $password,
  124. Zend_Ldap_Attribute::PASSWORD_UNICODEPWD, 'unicodePwd');
  125. try {
  126. $this->_getLdap()->add($dn, $data);
  127. $this->assertType('Zend_Ldap', $this->_getLdap()->bind($dn, $password));
  128. $this->_getLdap()->bind();
  129. $this->_getLdap()->delete($dn);
  130. } catch (Zend_Ldap_Exception $e) {
  131. $this->_getLdap()->bind();
  132. if ($this->_getLdap()->exists($dn)) {
  133. $this->_getLdap()->delete($dn);
  134. }
  135. $this->fail($e->getMessage());
  136. }
  137. }
  138. public function testChangePasswordWithUserAccountActiveDirectory()
  139. {
  140. if ($this->_getLdap()->getRootDse()->getServerType() !==
  141. Zend_Ldap_Node_RootDse::SERVER_TYPE_ACTIVEDIRECTORY) {
  142. $this->markTestSkipped('Test can only be run on an ActiveDirectory server');
  143. }
  144. $options = $this->_getLdap()->getOptions();
  145. if ($options['useSsl'] !== true && $options['useStartTls'] !== true) {
  146. $this->markTestSkipped('Test can only be run on an SSL or TLS secured connection');
  147. }
  148. $dn = $this->_createDn('cn=New User,');
  149. $data = array();
  150. $password = 'pa$$w0rd';
  151. Zend_Ldap_Attribute::setAttribute($data, 'cn', 'New User', false);
  152. Zend_Ldap_Attribute::setAttribute($data, 'displayName', 'New User', false);
  153. Zend_Ldap_Attribute::setAttribute($data, 'sAMAccountName', 'newuser', false);
  154. Zend_Ldap_Attribute::setAttribute($data, 'userAccountControl', 512, false);
  155. Zend_Ldap_Attribute::setAttribute($data, 'objectClass', 'person', true);
  156. Zend_Ldap_Attribute::setAttribute($data, 'objectClass', 'organizationalPerson', true);
  157. Zend_Ldap_Attribute::setAttribute($data, 'objectClass', 'user', true);
  158. Zend_Ldap_Attribute::setPassword($data, $password,
  159. Zend_Ldap_Attribute::PASSWORD_UNICODEPWD, 'unicodePwd');
  160. try {
  161. $this->_getLdap()->add($dn, $data);
  162. $this->_getLdap()->bind($dn, $password);
  163. $newPasswd = 'newpasswd';
  164. $newData = array();
  165. Zend_Ldap_Attribute::setPassword($newData, $newPasswd, Zend_Ldap_Attribute::PASSWORD_UNICODEPWD);
  166. $this->_getLdap()->update($dn, $newData);
  167. try {
  168. $this->_getLdap()->bind($dn, $password);
  169. $this->fail('Expected exception not thrown');
  170. } catch (Zend_Ldap_Exception $zle) {
  171. $message = $zle->getMessage();
  172. $this->assertTrue(strstr($message, 'Invalid credentials') ||
  173. strstr($message, 'Server is unwilling to perform'));
  174. }
  175. $this->assertType('Zend_Ldap', $this->_getLdap()->bind($dn, $newPasswd));
  176. $this->_getLdap()->bind();
  177. $this->_getLdap()->delete($dn);
  178. } catch (Zend_Ldap_Exception $e) {
  179. $this->_getLdap()->bind();
  180. if ($this->_getLdap()->exists($dn)) {
  181. $this->_getLdap()->delete($dn);
  182. }
  183. $this->fail($e->getMessage());
  184. }
  185. }
  186. }