'I get jwt.exceptions.DecodeError: Invalid header padding

So I have a simple flask app. Logging in creates a token:

token = jwt.encode({'user': token_data}, app.config['SECRET_KEY']).decode('utf-8')

The middleware looks like this:

def token_required(f):
    @wraps(f)
    def decorated(*args, **kwargs):
        data = request.headers['Authorization'].encode('ascii', 'ignore')
        token = str.replace(str(data), 'Bearer ', '')
        if not token:
            return jsonify({'message': 'Token is missing'}), 401
        data = jwt.decode(token, app.config['SECRET_KEY'], algorithms=['HS256'])['sub']
        return f(*args, **kwargs)

    return decorated

Then I run a protected route that is @token_required and get the error. jwt.exceptions.DecodeError: Invalid header padding

Can't add another utf-8 to the middleware token, as I cant use it with str What can I do?



Solution 1:[1]

So I removed .encode('ascii', 'ignore') and also ['sub'] and it seems to work

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ani Amirjanyan