I am working with the Flask-Security extension and can't for the life of me figure out where in the extension I can pass in an unauthorized handler at the time the extension is initialized. This is important to me because I don't want to redirect a user to another endpoint when they don't have a required permission. I want them to see it in the url where they are, so they retain the context of the url they don't have permission to access. My work around is to monkey patch the method onto the extension before the first request comes in:
@app.before_first_request
def monkey_patch():
"""Monkey patching the flasksecurity callback"""
current_app.extensions['security']._unauthorized_callback=lambda: abort(401)
I then use my app.errorhandler to take care of the error and return an appropriate response code.
@app.errorhandler(401)
def unauthorized(e):
return 'You not authorized to visit this page', 401
Does anyone know of a better way to do this?