'connexion swagger api with auth0 not forcing auth on url path only defined route

I'm creating an API with Flask/Swagger/Auth0. I would like to have ALL my Swagger defined inputs auth protected. The configuration below, protects the defined base route in my main.py but the subsequent calls to other defined endpoints in the swagger.yml succeed with out the Auth Header:

app = connexion.App(__name__, specification_dir="./")
CORS(app.app)

# Cead the swagger.yml file to configure the endpoints
app.add_api("swagger.yml")
application = app.app # expose global WSGI application object

# Create a URL route in our application for "/"
@application.route("/pyapi")
@cross_origin(headers=["Content-Type", "Authorization"])
@requires_auth
def home():
    """
    This function just responds to the browser URL
    localhost:5000/
    :return:        the rendered template "home.html"
    """

    # Set the pagination configuration
    return render_template("home.html")

if __name__ == "__main__":
    application.run(debug=True)

Defined route in main.py

$ curl 'http://192.168.56.102:8500/pyapi' 
{   "code":
"authorization_header_missing",   "description": "Authorization header
is expected" }

Defined in swagger.yml

$ curl 'http://192.168.56.102:8500/pyapi/industries' 
[   {
    "id": 1,
    "naics_desc": "Accommodation and Food Services"   },   {
    "id": 2,
    "naics_desc": "Administrative and Support and Waste Management and Remediation Services"   },   {
    "id": 3,
    "naics_desc": "Agriculture Forestry Fishing and Hunting"   },

If I add before_request to my route, I do get protected endpoints, however; once authenticated all swagger endpoints are returning home.html vs. the swagger endpoint routing/results.

# Create a URL route in our application for "/"
@application.before_request
@application.route("/pyapi")
@cross_origin(headers=["Content-Type", "Authorization"])
@requires_auth
def home():
    """
    This function just responds to the browser URL
    localhost:5000/
    :return:        the rendered template "home.html"
    """

    # Set the pagination configuration
    return render_template("home.html")

if __name__ == "__main__":
    application.run(debug=True)

$ curl 'http://192.168.56.102:8500/pyapi/industries' --header 'authorization: Bearer my-long-token-here'

<meta charset="UTF-8">
<title>Application Home Page</title> </head> <body>
<h2>
    API IS ALIVE!!!
</h2> </body> </html>

Is there a method or option in Flask/Connexion that allows me to follow or redirect back to the desired URL/path once authenticated without having to manage all my routes in main.py?!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source