gpt4 book ai didi

python - Authlib jwt token 解码在函数内部不起作用

转载 作者:行者123 更新时间:2023-12-01 08:44:04 24 4
gpt4 key购买 nike

我正在使用authlib用于解码 JWT token 的库。

当我按原样运行时,这段代码工作正常。

from authlib.specs.rfc7519 import jwt
encoded_jwt = '''eyJ0eXAiOiJKV1Qi.....'''
secret = b'''-----BEGIN PUBLIC KEY-----
.....
-----END PUBLIC KEY-----'''

claims = jwt.decode(encoded_jwt, secret)
print(type(claims))
import json

json_parse = json.loads(json.dumps(claims))
email = json_parse['http://wso2.org/claims/emailaddress']
print(email)
roles = json_parse['http://wso2.org/claims/role']
print(roles)
email, roles[-1]

但是当我将其添加到函数中时它不起作用。通过这个问题我无法在 FLASK 框架中使用它。下面的代码不起作用。请不要回答检查公钥,因为它在上面的代码中工作正常。

def getsessions():
from authlib.specs.rfc7519 import jwt
encoded_jwt = '''eyJ0eXAiOiJ....'''
secret = b'''-----BEGIN PUBLIC KEY-----
............
-----END PUBLIC KEY-----'''

claims = jwt.decode(encoded_jwt, secret)
print(type(claims))
import json

json_parse = json.loads(json.dumps(claims))
email = json_parse['http://wso2.org/claims/emailaddress']
print(email)
roles = json_parse['http://wso2.org/claims/role']
print(roles)
email, roles[-1]

email,role=getsessions()

print(email)

我得到的错误是:

Traceback (most recent call last): File "/home/sathiyakugan/PycharmProjects/JWTsample/ss.py", line 50, in email,role=getsessions() File "/home/sathiyakugan/PycharmProjects/JWTsample/ss.py", line 39, in getsessions claims = jwt.decode(encoded_jwt, secret) File "/home/sathiyakugan/PycharmProjects/Python/venv/lib/python3.5/site-packages/authlib/specs/rfc7519/jwt.py", line 119, in decode data = self._jws.deserialize_compact(s, key_func, decode_payload) File "/home/sathiyakugan/PycharmProjects/Python/venv/lib/python3.5/site-packages/authlib/specs/rfc7515/jws.py", line 108, in deserialize_compact self._algorithms, jws_header, payload, key) File "/home/sathiyakugan/PycharmProjects/Python/venv/lib/python3.5/site-packages/authlib/specs/rfc7515/util.py", line 14, in prepare_algorithm_key key = algorithm.prepare_public_key(key) File "/home/sathiyakugan/PycharmProjects/Python/venv/lib/python3.5/site-packages/authlib/specs/rfc7518/_backends/_key_cryptography.py", line 28, in prepare_public_key return load_pem_public_key(key, backend=default_backend()) File "/home/sathiyakugan/PycharmProjects/Python/venv/lib/python3.5/site-packages/cryptography/hazmat/primitives/serialization.py", line 24, in load_pem_public_key return backend.load_pem_public_key(data) File "/home/sathiyakugan/PycharmProjects/Python/venv/lib/python3.5/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1040, in load_pem_public_key self._handle_key_loading_error() File "/home/sathiyakugan/PycharmProjects/Python/venv/lib/python3.5/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1291, in _handle_key_loading_error raise ValueError("Could not deserialize key data.") ValueError: Could not deserialize key data.

请帮我看看我哪里出错了。我已经为这个问题挣扎了两天。请帮忙。

最佳答案

在函数中,secret 字符串中有一个缩进,这会将 PUBLIC_KEY 变成无效 key ,因为它缩进得不好。关键是你的函数中是这样的

abcdadgadgadsgasdgasdg
adgadgadg
adgagadgadsg

您可以将 secret 保存到名为public_key.pem的文件中,并从此文件中读取数据。

关于python - Authlib jwt token 解码在函数内部不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53376970/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com