Add function to generate signed tokens.
authorGunnar Wrobel <p@rdus.de>
Tue, 30 Nov 2010 05:32:09 +0000 (06:32 +0100)
committerGunnar Wrobel <p@rdus.de>
Tue, 30 Nov 2010 12:48:27 +0000 (13:48 +0100)
framework/Token/lib/Horde/Token/Base.php
framework/Token/test/Horde/Token/Unit/FileTest.php

index e711ceb..aac281a 100644 (file)
@@ -104,6 +104,21 @@ abstract class Horde_Token_Base
     abstract public function purge();
 
     /**
+     * Return a new signed token.
+     *
+     * @param string $seed  A unique ID to be included in the token.
+     *
+     * @return string A string of 6 bytes.
+     */
+    public function get($seed = '')
+    {
+        $nonce = $this->getNonce();
+        return Horde_Url::uriB64Encode(
+            $nonce . hash('sha256', $nonce . $this->_params['secret'] . $seed, true)
+        );
+    }
+
+    /**
      * Return a "number used once" (a concatenation of a timestamp and a random
      * numer).
      *
index e89888e..3e4ac81 100644 (file)
@@ -39,6 +39,12 @@ class Horde_Token_Unit_Storage_FileTest extends PHPUnit_Framework_TestCase
         }
     }
 
+    public function testToken()
+    {
+        $t = new Horde_Token_File(array('secret' => 'abc'));
+        $this->assertEquals(51, strlen($t->get()));
+    }
+
     public function testNonces()
     {
         $t = new Horde_Token_File(array('secret' => 'abc'));