Search code examples
fastapiauth0okta

JWT set did not contain any usable keys


While setting up Auth0 authentication with our okta application from fastapi, we received the following error,

jwt.exceptions.PyJWKSetError: The JWK Set did not contain any usable keys

We followed guidelines as detailed in the following link for the implementation of the fast api authorization with auth0.

https://auth0.com/blog/build-and-secure-fastapi-server-with-auth0/

The following code is used to verify the created token. The given error appears in the first try block of the verify function.

class VerifyToken():
"""Does all the token verification using PyJWT"""
def __init__(self, token):
    self.token = token
    self.config = set_up()
    print(self.config)
    # This gets the JWKS from a given URL and does processing so you can
    # use any of the keys available
    jwks_url = f'https://{self.config["DOMAIN"]}/.well-known/jwks.json'
    self.jwks_client = jwt.PyJWKClient(jwks_url)
def verify(self):
    # This gets the 'kid' from the passed token
    try:
        self.signing_key = self.jwks_client.get_signing_key_from_jwt(
            self.token
        ).key
    except jwt.exceptions.PyJWKClientError as error:
        print(error)
        return {"status": "error", "msg": error.__str__()}
    except jwt.exceptions.DecodeError as error:
        return {"status": "error", "msg": error.__str__()}
    try:
        print(self.config)
        payload = jwt.decode(
            self.token,
            self.signing_key,
            algorithms=self.config["ALGORITHMS"],
            audience=self.config["API_AUDIENCE"],
            issuer=self.config["ISSUER"],
            options={"verify_exp": False}
        )
    except Exception as e:
        return {"status": "error", "message": str(e)}
    return payload

Solution

  • If this error occurs, kindly check the pyjwt library you have installed. It doesnt work with the default pyjwt library in python. One has to install pyjwt[crypto] in the following manner,

    pip install pyjwt[crypto]