Created
January 31, 2019 13:34
-
-
Save neevai/afd8d2e4703fa92a42981368e8a03bad to your computer and use it in GitHub Desktop.
Apply HTTP basic auth just to the admin interface
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
# The docs say "Unfortunately, there is no easy way of applying HTTP Basic Auth just to your admin interface." | |
# But this is not true :) | |
# First: | |
# pip install flask-admin | |
# pip install flask-basicauth | |
from flask_admin import Admin | |
from flask_admin.contrib.sqla import ModelView | |
from flask_admin.base import expose, AdminIndexView | |
from flask_basicauth import BasicAuth | |
auth = BasicAuth() | |
class SecureModelView(ModelView): | |
def is_accessible(self): | |
return auth.authenticate() | |
def inaccessible_callback(self, name, **kwargs): | |
return auth.challenge() | |
class SecureAdminIndexView(AdminIndexView): | |
@expose() | |
@auth.required | |
def index(self): | |
return super(SecureAdminIndexView, self).index() | |
admin = Admin(index_view=SecureAdminIndexView()) | |
# admin.add_view(SecureModelView(Order, db.session)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment