Skip to content

Commit 269eca3

Browse files
committed
Update JWT.php
Add a method to retrieve the header. OpenIdConnect tends to add a "kid" to the header that is used to determine which key to use for verification. Getting header information allows you to figure out which key to pass to "decode". Other possible ways to handle this, allow $key passed to decode to be an array of keys. For now, let's just let people get to the header.
1 parent 2f57086 commit 269eca3

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Authentication/JWT.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,28 @@
2626
*/
2727
class JWT
2828
{
29+
30+
/**
31+
* Returns just the header portion of the jwt. This allows
32+
* you to determine which key should be used to verify
33+
* the jwt, using the "kid" field
34+
*
35+
* @param string $jwt
36+
*
37+
* @return object The JWT's header object, with fields "typ","alg", and optionally "kid"
38+
*/
39+
public static function decodeHeader($jwt) {
40+
$tks = explode('.', $jwt);
41+
if (count($tks) != 3) {
42+
throw new UnexpectedValueException('Wrong number of segments');
43+
}
44+
list($headb64, $bodyb64, $cryptob64) = $tks;
45+
if (null === ($header = JWT::jsonDecode(JWT::urlsafeB64Decode($headb64)))) {
46+
throw new UnexpectedValueException('Invalid segment encoding');
47+
}
48+
return $header;
49+
}
50+
2951
/**
3052
* Decodes a JWT string into a PHP object.
3153
*
@@ -117,6 +139,7 @@ public static function sign($msg, $key, $method = 'HS256')
117139
if (empty($methods[$method])) {
118140
throw new DomainException('Algorithm not supported');
119141
}
142+
120143
return hash_hmac($methods[$method], $msg, $key, true);
121144
}
122145

0 commit comments

Comments
 (0)