Skip to content

Instantly share code, notes, and snippets.

@neevai
Created January 31, 2019 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neevai/afd8d2e4703fa92a42981368e8a03bad to your computer and use it in GitHub Desktop.
Save neevai/afd8d2e4703fa92a42981368e8a03bad to your computer and use it in GitHub Desktop.
Apply HTTP basic auth just to the admin interface
# 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