ChangePasswordTest.php 8.7 KB

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