Could not deserialize key data on decoding JWT python

前端 未结 7 1022
日久生厌
日久生厌 2021-01-02 07:51

I am using pyjwt library for decoding the JWT token. I got this error when I am decoding. The code was given in the documantation.

import jwt

encoded_jwt=\'         


        
7条回答
  •  离开以前
    2021-01-02 08:22

    Use the authlib library, I never managed to decode keycloak tokens with pyjwt. You need a public_key, I assume you have it.

    from authlib.jose import jwt
    key = '-----BEGIN PUBLIC KEY-----\n' + public_key + '\n-----END PUBLIC KEY-----'
    key_binary = key.encode('ascii')
    
    try:
        claims = jwt.decode(encoded,key_binary)
        claims.validate()
        #do some logic here
        #...
    

    ProTip: you may grab the public key easily from your auth server (in my case Keycloak) at some endpoint:

    url = 'http://localhost:8080/auth/realms/your_realm'
    with  urllib.request.urlopen(url) as r:
        response = r.read()
        public_key = json.loads(response)['public_key']
    

提交回复
热议问题