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 5 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($header, $head);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If $head also contains one of the required fields (typ, alg, or - potentially - kid), this approach would overwrite it, correct? This strikes me as undesirable, but I'm open to other opinions on the matter.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it does - thought about that and leaned towards allowing it but to be safe
we can do it the other way around to ensure the other params are not
overwritten, will send updated pr shortly

Martin

On Thu, Jun 18, 2015 at 11:22 AM, Rob DiMarco [email protected]
wrote:

In Authentication/JWT.php
#53 (comment):

 {
     $header = array('typ' => 'JWT', 'alg' => $alg);
     if ($keyId !== null) {
         $header['kid'] = $keyId;
     }
  •    if ( isset($head) && is_array($head) ) {
    
  •        $header = array_merge($header, $head);
    

If $head also contains one of the required fields (typ,alg, or potentiallykid`),
this approach would overwrite it, correct? This strikes me as undesirable,
but I'm open to other opinions on the matter.


Reply to this email directly or view it on GitHub
https://github.com/firebase/php-jwt/pull/53/files#r32761604.

}
$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');
}
}