flask-jwt
flask-jwt copied to clipboard
authenticate and identity must be created and imported prior to jwt.init_app? better way for application factory?
trafficstars
authenticate and identity must be created and imported prior to jwt.init_app? is there a better way for application factory?
# auth_handlers.py
@jwt.authentication_handler
def authenticate(username, password):
user = username_table.get(username, None)
if user and safe_str_cmp(user.password.encode('utf-8'), password.encode('utf-8')):
return user
@jwt.identity_handler
def identity(payload):
user_id = payload['identity']
return userid_table.get(user_id, None)
#extensions.py
from flask_jwt import JWT
jwt = JWT()
etc...
# app.py
from extensions import jwt
from flask_jwt import jwt_required
def create_app():
app = Flask(__name__)
app.debug = True
app.config['SECRET_KEY'] = 'super-secret'
register_extenstions(app)
return app
def register_extensions(app):
another_app.init_app(app)
another_app.init_app(app)
from auth_handlers import authenticate, identity <<<<<< This looks really ugly
jwt.init_app(app)
another_app.init_app(app)
another_app.init_app(app)
another_app.init_app(app)
app = create_app()
@app.route('/protected')
@jwt_required()
def protected():
return '%s' % current_identity
if __name__ == '__main__':
app.run(port=1234)
@mattupstate love to get your input on this. Thanks!