Python jose.jwt.decode在rs256算法上失败

Python jose.jwt.decode在rs256算法上失败,python,jwt,decoding,python-jose,Python,Jwt,Decoding,Python Jose,我尝试在Auth.py中使用jose.JWT.decode解码JWT,但总是出现错误: jose.exceptions.JWKError:无法反序列化密钥数据。 经过大量的调查,我把它归结为RS256算法。 这正是它崩溃的地方: payload = jwt.decode( token, rsa_key, algorithms=ALGORITHMS, audience=API_AUDIENCE,

我尝试在Auth.py中使用jose.JWT.decode解码JWT,但总是出现错误:
jose.exceptions.JWKError:无法反序列化密钥数据
。 经过大量的调查,我把它归结为RS256算法。 这正是它崩溃的地方:

payload = jwt.decode(
            token,
            rsa_key,
            algorithms=ALGORITHMS,
            audience=API_AUDIENCE,
            issuer='https://' + AUTH0_DOMAIN + '/',
        )
这是一个有效的令牌示例:

2.我的意思是:我的意思是:我的意思是:我的意思是:我的意思是:我的意思是:我的意思是我的意思是,我的意思是:我的意思是,我的意思是,我的意思是,我的意思是,我的意思是,我的意思是,我的意思是,我的意思是,我的意思是,我的意思是我的意思是,我的意思是我的意思是我的意思是我的意思是,我的意思是我的意思是我的意思是我的意思是我的意思是。我的意思是我的意思是。我的意思是我的意思是我的意思是我的意思是。我的意思是我的意思是我的意思是。我的意思是我的意思是我的意思是我的意思是我的意思是。我的意思是我的意思是我的意思是我的意思是我的意思是。我的意思是我的意思是我的意思是。我的意思是我的意思是一个中国政府的一个中国政府的一个中国政府的一个中国政府的一个中国政府的一个中国政府的一个中国政府的一个中国政府的一个中国政府的一个中国政府的一个中国政府的一个中国政府的一个中国政府的一个中国政府的一个中国政府的一个中国政府的一个中国政府在一个中国政府的一个中国政府的一个中国政府的一个中国政府的一个中国政府的一个中国政府的一个中国政府的一个中国政府的一个中国政府的一个中国政府的一个中国政府的一个中国政府的一个中国政府的一个中国政府的一个中国政府一个中国政府的一个中国政府的一个中国政府的一个中国政府的一个中国政府的一个中国政府的一个中国政府的一个中国政府的一个中国政府的一个中国政府的一个中国政府的一个中国政府的一个中国政府的一个中国的一个中国政府的一个中国的一个中国的KJK7DBNUKFD2ULMMEHM-2xQC2ZM0zO17qTJ-zia0lHy3Z6MK9-NBF4WXLFIDTIWD9WYVQJXCG40YUMKN2YRILFDIGFCRYBISQP7CU-UsOEa0irgNf5zUXmYLhp1DgV-fFxTfRB0nX6O5Sf29tfOMNQ
它的解码没有问题

这是我的密码。我从@app.route调用requires_auth函数

import json
from flask import request, _request_ctx_stack, abort
from functools import wraps
from jose import jwt
from urllib.request import urlopen
import sys


AUTH0_DOMAIN = 'ofineo.eu.auth0.com'
ALGORITHMS = ['RS256']
API_AUDIENCE = 'coffee'

# AuthError Exception
'''
AuthError Exception
A standardized way to communicate auth failure modes
'''


class AuthError(Exception):
    def __init__(self, error, status_code):
        self.error = error
        self.status_code = status_code


# Auth Header


def get_token_auth_header():
    """Obtains the Access Token from the Authorization Header
    """
    auth = request.headers.get('Authorization', None)
    if not auth:
        raise AuthError({
            'code': 'authorization_header_missing',
            'description': 'Authorization header is expected.'
        }, 401)

    parts = auth.split()
    if parts[0].lower() != 'bearer':
        raise AuthError({
            'code': 'invalid_header',
            'description': 'Authorization header must start with "Bearer".'
        }, 401)

    elif len(parts) == 1:
        raise AuthError({
            'code': 'invalid_header',
            'description': 'Token not found.'
        }, 401)

    elif len(parts) > 2:
        raise AuthError({
            'code': 'invalid_header',
            'description': 'Authorization header must be bearer token.'
        }, 401)

    token = parts[1]

    return token


def check_permissions(permission, payload):
    if 'permissions' not in payload:
        raise AuthError({
            'code': 'invalid_claims',
            'description': 'Permissions not included in JWT.'
        }, 400)

    if permission not in payload['permissions']:
        raise AuthError({
            'code': 'unauthorized',
            'description': 'Permission not found.'
        }, 403)
    return True


def verify_decode_jwt(token):
    jsonurl = urlopen(f'https://{AUTH0_DOMAIN}/.well-known/jwks.json')
    jwks = json.loads(jsonurl.read())

    unverified_header = jwt.get_unverified_header(token)
    rsa_key = {}
    if 'kid' not in unverified_header:
        raise AuthError({
            'code': 'invalid_header',
            'description': 'Authorization malformed.'
        }, 401)

    for key in jwks['keys']:

        if key['kid'] == unverified_header['kid']:
            rsa_key = {
                'kty': key['kty'],
                'kid': key['kid'],
                'use': key['use'],
                'n': key['n'],
                'e': key['e']
            }
    if rsa_key:
        try:
            payload = jwt.decode(
                token,
                rsa_key,
                algorithms=ALGORITHMS,
                audience=API_AUDIENCE,
                issuer='https://' + AUTH0_DOMAIN + '/',
            )

            return payload

        except jwt.ExpiredSignatureError:
            raise AuthError({
                'code': 'token_expired',
                'description': 'Token expired.'
            }, 401)

        except jwt.JWTClaimsError:
            raise AuthError({
                'code': 'invalid_claims',
                'description': 'Incorrect claims. Please, check the audience and issuer.'
            }, 401)

        except jwt.JWTError:
            raise AuthError({
                'code': 'invalid signature',
                'description': 'the signature is invalid in some way'
            }, 401)

        except Exception:
            raise AuthError({
                'code': 'invalid_header',
                'description': 'Unable to parse authentication token.'
            }, 400)

    raise AuthError({
                'code': 'invalid_header',
                'description': 'Unable to find the appropriate key.'
            }, 400)


def requires_auth(permission=''):
    def requires_auth_decorator(f):
        @wraps(f)
        def wrapper(*args, **kwargs):
            token = get_token_auth_header()
            try:
                payload = verify_decode_jwt(token)
                check_permissions(permission, payload)
            except Exception as e:
                print(e)
                abort(401)

            return f(payload, *args, **kwargs)
        return wrapper
    return requires_auth_decorator

我是否正确使用了该库???

感谢您添加代码来使用。下次请为那些询问详细信息的人添加评论,以通知他们更改。现在我尝试了你的代码,特别是
verify\u decode\u jwt
函数,它对我来说很好,我刚刚得到了一个异常
jose.exceptions.ExpiredSignatureError:Signature已经过期了,这是正常的,在添加
选项={verify\u exp:False}之后,
decode
调用中,有效载荷被解码,没有任何异常。我无法重现您的异常。我在无法分析身份验证令牌的情况下遇到此错误