Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from functools import wraps
- from flask import Flask, request, jsonify
- from flask_restful import Resource, Api
- from flask_jwt_extended import (
- JWTManager, jwt_optional, get_jwt_identity, create_access_token
- )
- app = Flask(__name__)
- app.secret_key = 'super-secret' # Change this!
- api = Api(app)
- jwt = JWTManager(app)
- def simulate_limiter(func):
- @wraps(func)
- def wrapper(*args, **kwargs):
- identity = get_jwt_identity()
- if identity:
- print("Limiter hit with user {}".format(identity))
- else:
- print("Decorator hit with a user not logged in")
- return func(*args, **kwargs)
- return wrapper
- @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
- ret = {'access_token': create_access_token(identity=username)}
- return jsonify(ret), 200
- class HelloWorld(Resource):
- method_decorators = [simulate_limiter, jwt_optional]
- def get(self):
- identity = get_jwt_identity()
- if identity:
- return {'hello': identity}
- else:
- return {'hello': 'pesant'}
- api.add_resource(HelloWorld, '/')
- if __name__ == '__main__':
- app.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement