summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRob DiMarco <rdimarco@google.com>2015-06-22 14:33:56 -0700
committerRob DiMarco <rdimarco@google.com>2015-06-22 14:33:56 -0700
commitdcaa08ea78b76c8cd2fda0ff245934fd89fe74c2 (patch)
treee782f6ededd959cefef92e7dc466559e93ea25e6
parent30a066878c44ad9676f87edf373cc7b74f0192b7 (diff)
parent37feebff87f6f7e9f3f358c0451c7bf776d1e34a (diff)
downloadphp-jwt-dcaa08ea78b76c8cd2fda0ff245934fd89fe74c2.zip
php-jwt-dcaa08ea78b76c8cd2fda0ff245934fd89fe74c2.tar.gz
php-jwt-dcaa08ea78b76c8cd2fda0ff245934fd89fe74c2.tar.bz2
Merge pull request #53 from mcocaro/master
Adding optional headers to send in JWT
-rw-r--r--Authentication/JWT.php6
-rw-r--r--tests/JWTTest.php6
2 files changed, 11 insertions, 1 deletions
diff --git a/Authentication/JWT.php b/Authentication/JWT.php
index 7d6665b..161424b 100644
--- a/Authentication/JWT.php
+++ b/Authentication/JWT.php
@@ -119,17 +119,21 @@ class JWT
* @param string $key The secret key
* @param string $alg The signing algorithm. Supported
* algorithms are 'HS256', 'HS384' and 'HS512'
+ * @param array $head An array with header elements to attach
*
* @return string A signed JWT
* @uses jsonEncode
* @uses urlsafeB64Encode
*/
- public static function encode($payload, $key, $alg = 'HS256', $keyId = null)
+ public static function encode($payload, $key, $alg = 'HS256', $keyId = null, $head = null)
{
$header = array('typ' => 'JWT', 'alg' => $alg);
if ($keyId !== null) {
$header['kid'] = $keyId;
}
+ if ( isset($head) && is_array($head) ) {
+ $header = array_merge($head, $header);
+ }
$segments = array();
$segments[] = JWT::urlsafeB64Encode(JWT::jsonEncode($header));
$segments[] = JWT::urlsafeB64Encode(JWT::jsonEncode($payload));
diff --git a/tests/JWTTest.php b/tests/JWTTest.php
index 0605e4c..2aeb201 100644
--- a/tests/JWTTest.php
+++ b/tests/JWTTest.php
@@ -228,4 +228,10 @@ class JWTTest extends PHPUnit_Framework_TestCase
$this->setExpectedException('DomainException');
JWT::decode($msg, 'my_key');
}
+
+ public function testAdditionalHeaders()
+ {
+ $msg = JWT::encode('abc', 'my_key', 'HS256', null, array('cty' => 'test-eit;v=1'));
+ $this->assertEquals(JWT::decode($msg, 'my_key', array('HS256')), 'abc');
+ }
}