Search code examples
pythonauthenticationjwtaccess-tokenauthlib

Authlib jwt token decode not working inside function


I am using the authlib library for decoding the JWT token .

This code is working fine when i run as it is .

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]

but when I add this in to a function It's not working. through this problem I couldn't use it in the FLASK frame work. below code is not working. Please don't answer check the public key Because it's working fine in the above code.

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)

error I get is :

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.

please help me where I am went wrong. I have been struggling to move on for 2 days with this problem. please help.


Solution

  • In the function, there is an indent in secret string, this will turn the PUBLIC_KEY into an invalid key, because it is not indented well. The key would be something like this in your function

    abcdadgadgadsgasdgasdg
        adgadgadg
        adgagadgadsg
    

    You can save your secret into a file called public_key.pem, and read the data from this file.