Created
June 8, 2019 22:26
-
-
Save matabares/e776b3489ff016fb5824d244de78f5a0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import logging | |
from flask import Flask, jsonify, request | |
from flask_jwt_extended import ( | |
JWTManager, jwt_required, create_access_token, | |
get_jwt_identity | |
) | |
app = Flask(__name__) | |
app.config.from_object('config') | |
jwt = JWTManager(app) | |
#LOGGING | |
logger = logging.getLogger(__name__) | |
logger.setLevel(logging.INFO) | |
handler = logging.FileHandler('testiandolabapi.log') | |
handler.setLevel(logging.INFO) | |
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') | |
handler.setFormatter(formatter) | |
logger.addHandler(handler) | |
@app.route('/') | |
def f(): | |
return os.getenv('FLASK_ENV', 'default') | |
#return f"{app.config['ONEVAR']}" | |
# Provide a method to create access tokens. The create_access_token() | |
# function is used to actually generate the token, and you can return | |
# it to the caller however you choose. | |
@app.route('/login', methods=['POST']) | |
def login(): | |
if not request.is_json: | |
return jsonify({"msg": "Missing JSON in request"}), 400 | |
username = request.json.get('username', None) | |
password = request.json.get('password', None) | |
if not username: | |
return jsonify({"msg": "Missing username parameter"}), 400 | |
if not password: | |
return jsonify({"msg": "Missing password parameter"}), 400 | |
if username != 'test' or password != 'test': | |
return jsonify({"msg": "Bad username or password"}), 401 | |
# Identity can be any data that is json serializable | |
access_token = create_access_token(identity=username) | |
return jsonify(access_token=access_token), 200 | |
@app.route('/test', methods=['GET']) | |
@jwt_required | |
def protected(): | |
# Access the identity of the current user with get_jwt_identity | |
current_user = get_jwt_identity() | |
return jsonify(logged_in_as=current_user), 200 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment