Could not deserialize key data on decoding JWT python
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']
Its a good idea to use your RSA keys with OpenSSL:
openssl genrsa -out jwt-key 4096
openssl rsa -in jwt-key -pubout > jwt-key.pub
Reference: link