3

When I try this example and if the jet token is not provided by header I get error:

{
  "msg": "Missing cookie \"access_token_cookie\""
}

example:

from flask import Flask, jsonify, request

from flask_jwt_extended import (
    JWTManager, jwt_required, create_access_token,
    jwt_refresh_token_required, create_refresh_token,
    get_jwt_identity, set_access_cookies,
    set_refresh_cookies, unset_jwt_cookies
)

from flask_jwt_extended.config import config

# NOTE: This is just a basic example of how to enable cookies. This is
#       vulnerable to CSRF attacks, and should not be used as is. See
#       csrf_protection_with_cookies.py for a more complete example!


app = Flask(__name__)

# Configure application to store JWTs in cookies. Whenever you make
# a request to a protected endpoint, you will need to send in the
# access or refresh JWT via a cookie.
app.config['JWT_TOKEN_LOCATION'] = ['cookies']

# Set the cookie paths, so that you are only sending your access token
# cookie to the access endpoints, and only sending your refresh token
# to the refresh endpoint. Technically this is optional, but it is in
# your best interest to not send additional cookies in the request if
# they aren't needed.
app.config['JWT_ACCESS_COOKIE_PATH'] = '/api/'
app.config['JWT_REFRESH_COOKIE_PATH'] = '/token/refresh'

# Disable CSRF protection for this example. In almost every case,
# this is a bad idea. See examples/csrf_protection_with_cookies.py
# for how safely store JWTs in cookies
app.config['JWT_COOKIE_CSRF_PROTECT'] = False

# Set the secret key to sign the JWTs with
app.config['JWT_SECRET_KEY'] = 'super-secret'  # Change this!

jwt = JWTManager(app)


# Use the set_access_cookie() and set_refresh_cookie() on a response
# object to set the JWTs in the response cookies. You can configure
# the cookie names and other settings via various app.config options
@app.route('/token/auth', methods=['POST'])
def login():
    # username = request.json.get('username', None)
    # password = request.json.get('password', None)
    # if username != 'test' or password != 'test':
    #     return jsonify({'login': False}), 401
    # print dir(config)
    # Create the tokens we will be sending back to the user
    access_token = create_access_token(identity="test")
    refresh_token = create_refresh_token(identity="test")

    # Set the JWT cookies in the response
    resp = jsonify({'login': True, "cookie_key": config.access_cookie_name, "cooke_value": access_token})
    set_access_cookies(resp, access_token)
    set_refresh_cookies(resp, refresh_token)
    return resp, 200


# Same thing as login here, except we are only setting a new cookie
# for the access token.
@app.route('/token/refresh', methods=['POST'])
@jwt_refresh_token_required
def refresh():
    # Create the new access token
    current_user = get_jwt_identity()
    access_token = create_access_token(identity=current_user)

    # Set the JWT access cookie in the response
    resp = jsonify({'refresh': True})
    set_access_cookies(resp, access_token)
    return resp, 200


# Because the JWTs are stored in an httponly cookie now, we cannot
# log the user out by simply deleting the cookie in the frontend.
# We need the backend to send us a response to delete the cookies
# in order to logout. unset_jwt_cookies is a helper function to
# do just that.
@app.route('/token/remove', methods=['POST'])
def logout():
    resp = jsonify({'logout': True})
    unset_jwt_cookies(resp)
    return resp, 200

# We do not need to make any changes to our protected endpoints. They
# will all still function the exact same as they do when sending the
# JWT in via a header instead of a cookie
@app.route('/api/example', methods=['GET'])
@jwt_required
def protected():
    username = get_jwt_identity()
    return jsonify({'hello': 'from {}'.format(username)}), 200


if __name__ == '__main__':
    app.run(debug=True)

But in my office I have similar setup except I am not calling

username = get_jwt_identity()

I get NoAuthorization exception get raised. how does this work ...

Ciasto piekarz
  • 7,853
  • 18
  • 101
  • 197

2 Answers2

0

It's mean you not login and flask-jwt can't find any token on your cookies.

Do you login before call this resource?

check your cookie that returned from app.

GLinBoy
  • 606
  • 1
  • 10
  • 20
  • That is not what I am asking, in the first case of example, I get json but why do I get exception raised in second case – Ciasto piekarz Nov 03 '18 at 22:32
  • last night I got same error, like you. I was trying to fix log out and every time my app throw "CSRF can't find in header" (or something like this, I can't remember clear), But fortinatly I put X-CSRF-TOKEN in header and pass to app, and log out succeeded! After that, every my REST that I called throw exception like yours. So i think you must check your cookie data; I can't put comment and forced use answer to share my solution with you, sorry. – GLinBoy Nov 04 '18 at 05:36
0

In my case it was CORS error, I was using a different api address from the website

enter image description here

Wendel
  • 2,809
  • 29
  • 28