diff --git a/Authentication/JWT.php b/Authentication/JWT.php index 7d6665bd..161424bb 100644 --- a/Authentication/JWT.php +++ b/Authentication/JWT.php @@ -119,17 +119,21 @@ public static function decode($jwt, $key = null, $allowed_algs = array()) * @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 0605e4ca..2aeb2017 100644 --- a/tests/JWTTest.php +++ b/tests/JWTTest.php @@ -228,4 +228,10 @@ public function testMissingAlgorithm() $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'); + } }