Skip to content

Commit dcaa08e

Browse files
committed
Merge pull request #53 from mcocaro/master
Adding optional headers to send in JWT
2 parents 30a0668 + 37feebf commit dcaa08e

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

Authentication/JWT.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -119,17 +119,21 @@ public static function decode($jwt, $key = null, $allowed_algs = array())
119119
* @param string $key The secret key
120120
* @param string $alg The signing algorithm. Supported
121121
* algorithms are 'HS256', 'HS384' and 'HS512'
122+
* @param array $head An array with header elements to attach
122123
*
123124
* @return string A signed JWT
124125
* @uses jsonEncode
125126
* @uses urlsafeB64Encode
126127
*/
127-
public static function encode($payload, $key, $alg = 'HS256', $keyId = null)
128+
public static function encode($payload, $key, $alg = 'HS256', $keyId = null, $head = null)
128129
{
129130
$header = array('typ' => 'JWT', 'alg' => $alg);
130131
if ($keyId !== null) {
131132
$header['kid'] = $keyId;
132133
}
134+
if ( isset($head) && is_array($head) ) {
135+
$header = array_merge($head, $header);
136+
}
133137
$segments = array();
134138
$segments[] = JWT::urlsafeB64Encode(JWT::jsonEncode($header));
135139
$segments[] = JWT::urlsafeB64Encode(JWT::jsonEncode($payload));

tests/JWTTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,10 @@ public function testMissingAlgorithm()
228228
$this->setExpectedException('DomainException');
229229
JWT::decode($msg, 'my_key');
230230
}
231+
232+
public function testAdditionalHeaders()
233+
{
234+
$msg = JWT::encode('abc', 'my_key', 'HS256', null, array('cty' => 'test-eit;v=1'));
235+
$this->assertEquals(JWT::decode($msg, 'my_key', array('HS256')), 'abc');
236+
}
231237
}

0 commit comments

Comments
 (0)