3

With flask_jwt_extended, whenever I'm trying to send a POST request with the following decorators:

@jwt_refresh_token_required
@jwt_required

I am having this 401 error:

{
    "msg": "Missing CSRF token"
}

When I use a GET instead, it's working fine. I have read the documentation that talk about double submit protection, but that does not solve my problem. Any ideas how I could fix my issue? The code to reproduce the problem is below.

Below is the structure of my code:

- src/__init.py__ # where I put all configs
- src/auth.py # where are the endpoints

init.py

login_serializer = URLSafeTimedSerializer(SERIALIZER_SECRET_KEY)
jwt = JWTManager()


def create_app():
    app = Flask(__name__)
    app.config["SECRET_KEY"] = SERIALIZER_SECRET_KEY
    app.config['JWT_SECRET_KEY'] = JWT_SECRET_KEY
    app.config['JWT_TOKEN_LOCATION'] = ['cookies']
    app.config['JWT_COOKIE_CSRF_PROTECT'] = True  
    db.init_app(app)
    jwt.init_app(app)

    # blueprint for auth routes in our app
    from .auth import auth as auth_blueprint
    app.register_blueprint(auth_blueprint)

    # blueprint for non-auth parts of app
    from .routes import main as main_blueprint
    app.register_blueprint(main_blueprint)

    return app

auth.py

import logging
from flask import Blueprint, request, current_app as app, jsonify
from werkzeug.security import generate_password_hash, check_password_hash
from . import login_serializer, jwt
from flask_jwt_extended import (jwt_required, jwt_refresh_token_required, 
                                get_jwt_identity, get_raw_jwt, unset_jwt_cookies,
                                current_user, create_access_token, create_refresh_token, set_access_cookies, set_refresh_cookies)

auth = Blueprint('auth', __name__)


def set_response_cookies(token_identity, resp=None, token_types=["access", "refresh"]):
    """
    Helper function to set cookies in response
    """
    logging.warning("Setting cookies")
    resp = jsonify(resp)
    token_types.sort()
    if token_types == ["access", "refresh"]:
        access_token = create_access_token(identity = token_identity)
        refresh_token = create_refresh_token(identity = token_identity)
        if not resp:
            resp = jsonify({"access_token": access_token, "refresh_token": refresh_token})
        set_access_cookies(resp, access_token)
        set_refresh_cookies(resp, refresh_token)
        return resp 
    elif token_types == ["access"]:
        access_token = create_access_token(identity = token_identity)
        if not resp:
            resp = jsonify({"access_token": access_token})
        set_access_cookies(resp, access_token)
        return resp 
    elif token_types == ["refresh"]:
        refresh_token = create_refresh_token(identity = token_identity)
        if not resp:
            resp = jsonify({"refresh_token": refresh_token})
        set_refresh_cookies(resp, refresh_token)
        return resp 
    else:
        raise ValueError("Wrong Call to this function")


@jwt.user_claims_loader
def add_claims_to_access_token(identity):
    """
    """
    return {
        'email': identity
    }


@jwt.user_loader_callback_loader
def user_loader_callback(identity):
    """
    Ignore Here, but I use it to get a User object (not mentionned here) from a Token.
    """
    return User.objects(
        email=identity,
    ).first()


@auth.route('/token', methods=['POST'])
def token_post():
    """ obj =
    {"email": "email", "password": "password"} => Tokens 
    """
    obj = request.get_json()
    resp = set_response_cookies(obj["email"], {"token": True}, ["access", "refresh"])
    return resp, 200    


@auth.route('/token/access', methods=['POST'])
@jwt_refresh_token_required
def refresh_access_cookies():
    if current_user:
        resp = set_response_cookies(current_user.email, {"token_refreshed": True}, ["access"])
    return resp, 200

So, here, all I have to do to reproduce the error is:

  1. Make a POST request to /token => In postman, my response will get all cookies and headers.

  2. Make a POST request to /token/access => Give the error mentioned above.

Abilys38
  • 303
  • 4
  • 16

1 Answers1

1

On your configuration, you enabled JWT_COOKIE_CSRF_PROTECT to true.
For devep purpose, the error will be gone if you can set it to False which may not safe.
On production, You need to pass csrf_token on your request header.
I think this links can help you.
https://flask-jwt-extended.readthedocs.io/en/stable/tokens_in_cookies/ (see the last section)
https://flask-wtf.readthedocs.io/en/stable/csrf.html

hwwwi
  • 176
  • 4
  • Thanks for your help. I confirm that when I set to False, I have no error. However, is there any way to add the csrf in my header directly in my /token endpoint ? Moreover, I'm not clear on the exact key/value the endpoint is expecting in the Header. Is it expecting something like: X-CSRF-TOKEN: csrf_value ? – Abilys38 May 31 '20 at 17:39