Authlib jwt 令牌解码在函数内部不起作用
Authlib jwt token decode not working inside function
我正在使用 authlib 库来解码 JWT 令牌。
当我按原样 运行 时,这段代码工作正常。
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 框架中使用它。下面的代码不起作用。请不要回答检查 public 键,因为它在上面的代码中工作正常。
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.
请帮我看看哪里出错了。我一直在努力解决这个问题 2 天。请帮忙。
在函数中,secret
字符串有一个缩进,这会将 PUBLIC_KEY 变成无效键,因为它没有很好地缩进。在你的函数中,关键是这样的
abcdadgadgadsgasdgasdg
adgadgadg
adgagadgadsg
您可以将 secret
保存到名为 public_key.pem
的文件中,然后从该文件中读取数据。
我正在使用 authlib 库来解码 JWT 令牌。
当我按原样 运行 时,这段代码工作正常。
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 框架中使用它。下面的代码不起作用。请不要回答检查 public 键,因为它在上面的代码中工作正常。
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.
请帮我看看哪里出错了。我一直在努力解决这个问题 2 天。请帮忙。
在函数中,secret
字符串有一个缩进,这会将 PUBLIC_KEY 变成无效键,因为它没有很好地缩进。在你的函数中,关键是这样的
abcdadgadgadsgasdgasdg
adgadgadg
adgagadgadsg
您可以将 secret
保存到名为 public_key.pem
的文件中,然后从该文件中读取数据。