Skip to content

Commit

Permalink
Merge pull request #53 from mcocaro/master
Browse files Browse the repository at this point in the history
Adding optional headers to send in JWT
  • Loading branch information
robertdimarco committed Jun 22, 2015
2 parents 30a0668 + 37feebf commit dcaa08e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Authentication/JWT.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
6 changes: 6 additions & 0 deletions tests/JWTTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}

0 comments on commit dcaa08e

Please sign in to comment.