Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding optional headers to send in JWT #53

Merged
merged 6 commits into from
Jun 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
}
}