Flask_JWT에서 사용하는 라이브러리, Flask-JWT-Extended를 사용하면 좀 더 편리하게 사용할 수 있다고 하므로 이를 활용해보도록 하겠다.
Flask-JWT-Extended
pip install flask-jwt-extended
https://flask-jwt-extended.readthedocs.io/en/stable/basic_usage/
app = Flask(__name__)
app.config["JWT_SECRET_KEY"] = "super-secret"
jwt = JWTManager(app)
create_access_token(identity=username)
from flask_jwt_extended import create_access_token
access_token = create_access_token(identity=username)
## return jsonify(access_token=access_token)
from flask import Flask, jsonify
from flask_jwt_extended import JWTManager
from flask_jwt_extended import create_access_token
app = Flask(__name__)
app.config["JWT_SECRET_KEY"] = "munjibang"
jwt = JWTManager(app)
@app.route("/users/login", methods=['POST'])
def home():
user_id = "id1"
password = "password1"
access_token = create_access_token(identity=user_id)
print(access_token)
return jsonify({"access_token":access_token})
if __name__ == '__main__':
app.run('0.0.0.0', port=5000, debug=True)
https://flask-jwt-extended.readthedocs.io/en/stable/add_custom_data_claims/ 이를 참고하자. https://github.com/vimalloc/flask-jwt-extended/issues/317
@app.route("/login", methods=["POST"])
def login():
username = request.json.get("username", None)
password = request.json.get("password", None)
if username != "test" or password != "test":
return jsonify({"msg": "Bad username or password"}), 401
# You can use the additional_claims argument to either add
# custom claims or override default claims in the JWT.
additional_claims = {"aud": "some_audience", "foo": "bar"}
access_token = create_access_token(username, additional_claims=additional_claims)
return jsonify(access_token=access_token)
get_jwt_identity()
를 사용하면 식별자로 넣어준 값들을 조회 할 수 있다 jwt_required()
를 사용한다
@app.route("/users/protected")
@jwt_required()
def protected():
current_user_id = get_jwt_identity()
return jsonify(logged_in_as=current_user_id), 200
if __name__ == '__main__':
app.run('0.0.0.0', port=5000, debug=True)
@jwt_required()에 optional 값을 True로 주면 유효 토큰이 있는지 없는지에 따라 분기처리를 할 수 있다
@jwt_required(optional=True)
def place(place_id):
current_identity = get_jwt_identity()
if not current_identity:
return jsonify({'result':'fail'})
places = to_place_detail_dto(
list(db.places.find({'_id': ObjectId(place_id)})))
return jsonify({'places': places})
토큰이 만료된 토큰으로 요청시 {"msg": "Token has expired"} 라는 메시지가 자동으로 전송되는데 우리는 서버사이드랜더링이었으므로 이를 제어해서 다른 처리를 해줘야 했다.
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
https://flask-jwt-extended.readthedocs.io/en/stable/refreshing_tokens/
@app.route('/signout', methods=['POST'])
def signout():
response = make_response(redirect(url_for("places_all")))
unset_jwt_cookies(response)
return response
user_identity_loader(),
를 사용하면 된다