Authlib jwt token decode not working inside function












1














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.










share|improve this question



























    1














    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.










    share|improve this question

























      1












      1








      1


      2





      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.










      share|improve this question













      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.







      python authentication jwt access-token authlib






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 '18 at 14:41









      Balakrishnan Sathiyakugan

      587




      587
























          1 Answer
          1






          active

          oldest

          votes


















          1














          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.






          share|improve this answer





















          • it proves that i went wrong in where I thought I wasn't. Thanks.
            – Balakrishnan Sathiyakugan
            Nov 20 '18 at 9:11











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376970%2fauthlib-jwt-token-decode-not-working-inside-function%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          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.






          share|improve this answer





















          • it proves that i went wrong in where I thought I wasn't. Thanks.
            – Balakrishnan Sathiyakugan
            Nov 20 '18 at 9:11
















          1














          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.






          share|improve this answer





















          • it proves that i went wrong in where I thought I wasn't. Thanks.
            – Balakrishnan Sathiyakugan
            Nov 20 '18 at 9:11














          1












          1








          1






          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.






          share|improve this answer












          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 19 '18 at 15:54









          lepture

          1,097810




          1,097810












          • it proves that i went wrong in where I thought I wasn't. Thanks.
            – Balakrishnan Sathiyakugan
            Nov 20 '18 at 9:11


















          • it proves that i went wrong in where I thought I wasn't. Thanks.
            – Balakrishnan Sathiyakugan
            Nov 20 '18 at 9:11
















          it proves that i went wrong in where I thought I wasn't. Thanks.
          – Balakrishnan Sathiyakugan
          Nov 20 '18 at 9:11




          it proves that i went wrong in where I thought I wasn't. Thanks.
          – Balakrishnan Sathiyakugan
          Nov 20 '18 at 9:11


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376970%2fauthlib-jwt-token-decode-not-working-inside-function%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

          How to fix TextFormField cause rebuild widget in Flutter