我正在尝试解码我从Auth0获得的JWT。当我去jwt.io时,他们有一个可以放入JWT的解码器,它会告诉你关于JWT每个部分的所有信息。我可以看到所有信息都是正确的。当我尝试自己解码时,我得到了这个错误。 我从我的Auth0注册客户端信息中获取了密钥,并且有一条说明:客户端密钥不是base64编码的。在使用它之前,我是否需要对这个秘密进行base64编码?
ValueError: Could not unserialize key data.
终端
>>> import jwt
>>> secret = secret
>>> encoded_jwt = encoded_jwt
>>> decoded_jwt = jwt.decode(encoded_jwt, secret, algorithm="RS256")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.6/site-packages/jwt/api_jwt.py", line 78, in decode
jwt, key=key, algorithms=algorithms, options=options, **kwargs
File "/usr/local/lib/python3.6/site-packages/jwt/api_jws.py", line 140, in decode
key, algorithms)
File "/usr/local/lib/python3.6/site-packages/jwt/api_jws.py", line 204, in _verify_signature
key = alg_obj.prepare_key(key)
File "/usr/local/lib/python3.6/site-packages/jwt/algorithms.py", line 207, in prepare_key
key = load_pem_public_key(key, backend=default_backend())
File "/usr/local/lib/python3.6/site-packages/cryptography/hazmat/primitives/serialization.py", line 24, in load_pem_public_key
return backend.load_pem_public_key(data)
File "/usr/local/lib/python3.6/site-packages/cryptography/hazmat/backends/multibackend.py", line 314, in load_pem_public_key
return b.load_pem_public_key(data)
File "/usr/local/lib/python3.6/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1110, in load_pem_public_key
self._handle_key_loading_error()
File "/usr/local/lib/python3.6/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1325, in _handle_key_loading_error
raise ValueError("Could not unserialize key data.")
ValueError: Could not unserialize key data.
答案 0 :(得分:1)
由于您没有提及PUBLIC KEY或私钥,看起来您正在尝试使用&#34; RS256&#34;算法,但您的令牌使用&#34; HS256&#34;。
尝试:
decoding_jwt = jwt.decode(encoded_jwt,secret,algorithm =&#34; HS256&#34;)
而不是:
decoding_jwt = jwt.decode(encoded_jwt,secret,algorithm =&#34; RS256&#34;)
如果您喜欢这个地址,可以使用base64自由编码密钥: https://www.base64encode.org/
您可以通过检查&#34; secret base64 encoded&#34;来验证编码密钥。复选框在jwt.io,在VERIFY SIGNATURE部分下。