简体   繁体   English

Firebase JWT库无法验证Python JWT令牌

[英]Firebase JWT library can't verify Python JWT token

So I have a client in python and a backend in PHP. 所以我在python中有一个客户端,在PHP中有一个后端。 The python client is using pyjwt and the php server is using Firebase JWT . python客户端使用pyjwt ,php服务器使用Firebase JWT

When encoding and decoding tokens within php everything works fine. 在php中编码和解码令牌时,一切正常。 But when I encode tokens from python and decode them with php the Firebase library returns an error: 但是当我从python编码令牌并用php解码它们时,Firebase库会返回一个错误:

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 编码python代码如下

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 和PHP代码如下

$key = getenv("JWT_KEY");
return (array) \Firebase\JWT\JWT::decode($token, $key, array('HS256'));

OK, I found the problem. 好的,我发现了问题。 The issue was the secret KEY ussed by the different parties. 问题是各方使用的密钥KEY。 The key had a problem when trying to be readed by PHP and thats why it was creating different tokens. 当试图被PHP引导时,密钥出现了问题,这就是为什么它会创建不同的令牌。 The libraries are ok, they can communicate between each other without any problem. 这些库没问题,它们之间可以毫无问题地相互通信。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM