Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase JWT library can't verify Python JWT token

So I have a client in python and a backend in PHP. The python client is using pyjwt and the php server is using Firebase JWT.

When encoding and decoding tokens within php everything works fine. But when I encode tokens from python and decode them with php the Firebase library returns an error:

Firebase\JWT\SignatureInvalidException Object
(
    [message:protected] => Signature verification failed
    [string:Exception:private] => 
    [code:protected] => 0
    [file:protected] => /var/www/vendor/firebase/php-jwt/src/JWT.php
    [line:protected] => 110
    ...

The encoding python code is the following

import jwt
...
payload = {'sub': self.client.id, 'exp': (datetime.now() + timedelta(days=1)).timestamp()}
context['token'] = jwt.encode(payload, os.environ['JWT_KEY'], algorithm='HS256')

and the PHP code is the following

$key = getenv("JWT_KEY");
return (array) \Firebase\JWT\JWT::decode($token, $key, array('HS256'));
like image 748
nicowernli Avatar asked Mar 10 '17 11:03

nicowernli


1 Answers

OK, I found the problem. The issue was the secret KEY ussed by the different parties. The key had a problem when trying to be readed by PHP and thats why it was creating different tokens. The libraries are ok, they can communicate between each other without any problem.

like image 155
nicowernli Avatar answered Sep 21 '22 08:09

nicowernli