ChangePasswordTest.php 8.8 KB

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