I have the following configuration for swagger in the flask app.
def configure_swagger(app):
""" Swagger Configuration for APP """
app.config["SWAGGER"] = {"title": "Swagger-UI", "uiversion": 3}
swagger_config = {
"headers": [],
"specs": [
{
"endpoint": "apispec_1",
"route": "/apispec_1.json",
"rule_filter": lambda rule: True, # all in
"model_filter": lambda tag: True, # all in
}
],
"static_url_path": "/flasgger_static",
"swagger_ui": True,
"specs_route": "/swagger/",
}
SWAGGER_TEMPLATE = {
"securityDefinitions": {"APIKeyHeader": {"type": "apiKey", "name": "Authorization", "in": "header"}}}
swagger = Swagger(app,config=swagger_config, template=SWAGGER_TEMPLATE)
Plus I have a configure_hook(app) configuration which before sending to any API does a token validation.
def configure_hook(app):
""" Configure before request hook """
@app.before_request
def before_request():
header = request.headers.get('Authorization')
token = header.split(" ")[-1]
TokenValidation(token).validate_token()
return None
In the setup of the app setup.py, I have added both configurations if SSO is enabled.
def setup_app(app, config=None):
if os.getenv('SSO') == "ENABLE":
configure_swagger(app)
configure_hook(app)
return app
In my health check, at the endpoint /healtz . I need the swagger UI to be automatically authorised with the token.
The endpoint function is given below.
@api.route("/healtz")
@swag_from("Swagger/healtz_new.yml")
def healtz():
return_dict = {"App Version": "1.0.0", "Status": "Healthy"}
return return_dict
The Yaml file healtz.yml :
summary: "Health Check"
description: "Give the info in json"
consumes:
- "application/json"
produces:
- "application/json"
responses:
405:
description: "Invalid input"
# use the same name as above
security:
- APIKeyHeader: [ 'Authorization' ]
But whenever I am trying to load the swagger UI by calling http://0.0.0.0:8086/swagger I am getting unauthorised error, same when I call for http://0.0.0.0:8086/healtz.
I Ideally want all the endpoints in the swagger UI to be programmatically authorized using the token passed in the headers. I went through the documentation and tried adding the bearer tokens but it is not working.