-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
How to verify a jwt ? #214
Comments
see https://github.com/firebase/php-jwt/blob/master/src/JWT.php#L69 |
Thanks @cottton, that solved the problem :) Also I would like to know one more thing: How to I revoke a jwt token ? |
Actually you cannot.
The JWT gives the client access as long
|
I'm trying to verify tokens in a test environment (now that I am able to successfully create tokens)... but I get this error after I manually change the secret / key for the decode attempt:
what do I need to do to NOT get the fatal error? |
|
@cottton - solved it... I needed to add require for each of the exception classes: require_once 'php-jwt-master-src/BeforeValidException.php'; and in my try catch I had to use:
|
What if I need to decode token first and then only verify it agains a key, how can I do this? I'll need to get "kid" value from the token before checking it signature, but JWT::decode() already requires key to use. |
To decode a string without verifying it against the public key, you can do this: use Firebase\JWT\JWT;
$tks = explode('.', $jwt);
if (count($tks) != 3) {
throw new UnexpectedValueException('Wrong number of segments');
}
list($headb64, $bodyb64, $cryptob64) = $tks;
$payload = JWT::jsonDecode(JWT::urlsafeB64Decode($bodyb64)); This is not supported directly in the library because of the possibility someone could use this unintentionally without verifying the signature. |
Had the same problem.
I wrote a method to get the header.
*1 (For those who dont know) The |
Do NOT use the body (content) without verifying the JWT! |
Thanks, I ended up duplicating code from JWT::decode partially to just decode header. I think it should have some separate method to just decode JWT. For example npm package "jsonwebtoken" has separate method to decode and verify and that's perfectly ok. |
@vedmant Instead of decoding the header yourself, you can pass in a map of $possible_keys = [
'kid1' => 'my_key1',
'kid2' => 'my_key2',
];
$decoded = JWT::decode( $msg, $possible_keys, ['RS256'] ); It would be very dangerous to offer a method to decode without validating. It would be too easy for a distracted developer to accidentally use this instead of the method which also validates, opening up a significant vulnerability in their application. |
@psignoret Thanks, I actually ended up doing exactly this. I also had to decode jwks into keys, here is the code if someone needs it: use phpseclib\Crypt\RSA;
use phpseclib\Math\BigInteger;
/**
* Convert jwks to key
*
* @param array $data
* @return RSA
*/
protected function jwksToKey(array $data): RSA
{
switch ($data['kty']) {
case 'RSA':
$rsa = new RSA();
$n = new BigInteger('0x' . bin2hex(JWT::urlsafeB64Decode($data['n'])), 16);
$e = new BigInteger('0x' . bin2hex(JWT::urlsafeB64Decode($data['e'])), 16);
if (array_key_exists('d', $data)) {
throw new UnexpectedValueException('RSA private key isn\'t supported');
} else {
$pem_string = $rsa->_convertPublicKey($n, $e);
}
$rsa->loadKey($pem_string);
return $rsa;
default:
throw new UnexpectedValueException('Unknown key type');
}
} |
@cottton You do not need to decode the header to use the $keys = [
'abcdefg' => $publicKey1,
'1234567' => $publicKey2,
];
// throws an exception if the `kid` is empty or doesn't exist, otherwise it uses the
// mapped array value to verify the signature, e.g. "$publicKey1"
$payload = Firebase\JWT\JWT::decode($jwt, $keys, ['RS256']); |
Suppose I get a jwt string xxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxxx
Now, how will I verify this jwt ???
How will I convert this string back to an array to decode / verify it ? @bshaffer @mbleigh @ultrasaurus
The text was updated successfully, but these errors were encountered: