ChangePasswordTest.php 8.6 KB

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