소스 검색

added unicodePwd creation support according to http://msdn.microsoft.com/en-us/library/cc223248(PROT.10).aspx

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@17461 44c647ce-9c0f-0410-b52a-842ac1e357ba
sgehrig 16 년 전
부모
커밋
805c562dfd
2개의 변경된 파일61개의 추가작업 그리고 9개의 파일을 삭제
  1. 36 9
      library/Zend/Ldap/Attribute.php
  2. 25 0
      tests/Zend/Ldap/AttributeTest.php

+ 36 - 9
library/Zend/Ldap/Attribute.php

@@ -29,10 +29,11 @@
  */
 class Zend_Ldap_Attribute
 {
-    const PASSWORD_HASH_MD5  = 'md5';
-    const PASSWORD_HASH_SMD5 = 'smd5';
-    const PASSWORD_HASH_SHA  = 'sha';
-    const PASSWORD_HASH_SSHA = 'ssha';
+    const PASSWORD_HASH_MD5   = 'md5';
+    const PASSWORD_HASH_SMD5  = 'smd5';
+    const PASSWORD_HASH_SHA   = 'sha';
+    const PASSWORD_HASH_SSHA  = 'ssha';
+    const PASSWORD_UNICODEPWD = 'unicodePwd';
 
     /**
      * Sets a LDAP attribute.
@@ -253,15 +254,23 @@ class Zend_Ldap_Attribute
     /**
      * Sets a LDAP password.
      *
-     * @param  array  $data
-     * @param  string $password
-     * @param  string $hashType
-     * @param  string $attribName
+     * @param  array       $data
+     * @param  string      $password
+     * @param  string      $hashType
+     * @param  string|null $attribName
      * @return null
      */
     public static function setPassword(array &$data, $password, $hashType = self::PASSWORD_HASH_MD5,
-        $attribName = 'userPassword')
+        $attribName = null)
     {
+        if ($attribName === null) {
+            if ($hashType === self::PASSWORD_UNICODEPWD) {
+                $attribName = 'unicodePwd';
+            } else {
+                $attribName = 'userPassword';
+            }
+        }
+
         $hash = self::createPassword($password, $hashType);
         self::setAttribute($data, $attribName, $hash, false);
     }
@@ -276,6 +285,24 @@ class Zend_Ldap_Attribute
     public static function createPassword($password, $hashType = self::PASSWORD_HASH_MD5)
     {
         switch ($hashType) {
+            case self::PASSWORD_UNICODEPWD:
+                /* see:
+                 * http://msdn.microsoft.com/en-us/library/cc223248(PROT.10).aspx
+                 */
+                $password = '"' . $password . '"';
+                if (function_exists('mb_convert_encoding')) {
+                    $password = mb_convert_encoding($password, 'UTF-16LE', 'UTF-8');
+                } else if (function_exists('iconv')) {
+                    $password = iconv('UTF-8', 'UTF-16LE', $password);
+                } else {
+                    $len = strlen($password);
+                    $new = '';
+                    for($i=0; $i < $len; $i++) {
+                        $new .= $password[$i] . "\x00";
+                    }
+                    $password = $new;
+                }
+                return $password;
             case self::PASSWORD_HASH_SSHA:
                 $salt    = substr(sha1(uniqid(mt_rand(), true), true), 0, 4);
                 $rawHash = sha1($password . $salt, true) . $salt;

+ 25 - 0
tests/Zend/Ldap/AttributeTest.php

@@ -196,6 +196,23 @@ class Zend_Ldap_AttributeTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('{MD5}bJuLJ96h3bhF+WqiVnxnVA==', $password);
     }
 
+    public function testPasswordSettingUnicodePwd()
+    {
+        $data=array();
+        Zend_Ldap_Attribute::setPassword($data, 'new', Zend_Ldap_Attribute::PASSWORD_UNICODEPWD);
+        $password=Zend_Ldap_Attribute::getAttribute($data, 'unicodePwd', 0);
+        $this->assertEquals("\x22\x00\x6E\x00\x65\x00\x77\x00\x22\x00", $password);
+    }
+
+    public function testPasswordSettingCustomAttribute()
+    {
+        $data=array();
+        Zend_Ldap_Attribute::setPassword($data, 'pa$$w0rd',
+            Zend_Ldap_Attribute::PASSWORD_HASH_SHA, 'myAttribute');
+        $password=Zend_Ldap_Attribute::getAttribute($data, 'myAttribute', 0);
+        $this->assertNotNull($password);
+    }
+
     public function testSetAttributeWithObject()
     {
         $data=array();
@@ -501,4 +518,12 @@ class Zend_Ldap_AttributeTest extends PHPUnit_Framework_TestCase
         $this->assertEquals(16, strlen($binary));
         $this->assertEquals(md5($password, true), $binary);
     }
+
+    public function testPasswordGenerationUnicodePwd()
+    {
+        $password = 'new';
+        $unicodePwd = Zend_Ldap_Attribute::createPassword($password, Zend_Ldap_Attribute::PASSWORD_UNICODEPWD);
+        $this->assertEquals(10, strlen($unicodePwd));
+        $this->assertEquals("\x22\x00\x6E\x00\x65\x00\x77\x00\x22\x00", $unicodePwd);
+    }
 }