Search code examples
pythonflaskswaggerflask-restplus

How to protect swagger documentation with password


I am building a flask app and need to add password for swagger documentation in production, but dont know how. Here is my code:

api = Api(
    version='1.0',
    title='API',
    description='Main API',
    doc='/doc',
    authorizations=authorizations)
...
api.init_app(app)

This documentation shouldnt be public for anyone to see, right? but i cant find a way to add password to it. Any suggestion would be awsome.


Solution

  • I know its very late, but still.

    class MyApi(Api):
        def render_doc(self):
            view = super().render_doc()
            if current_user.is_authenticated and current_user.has_role('admin'):
                return view
            return redirect(url_for('security.login', next=request.url))
    

    You need to modify this method which returns end view function.