Forum Discussion

apositronus's avatar
3 months ago

Controlling auth methods in Authorize

Hello,

I am doing some customization to openapi and so far failing at what I am trying to do.

My goals are:

  • Define two security schemas
  • Show only one in Authorize dialog
  • assign specific securitySchemas for a path pattern

Below is my function, but it does not do what I am expecting.

def custom_openapi():
    if app.openapi_schema:
        return app.openapi_schema
    
    openapi_schema = get_openapi(
        title="GXI",
        version="1.0.0",
        description="GXI",
        routes=app.routes,
    )
    
    root_path = "/gxi"
    openapi_schema["servers"] = [{"url": root_path}]
    
    openapi_schema["components"]["securitySchemes"] = {
        "BasicAuth": {
            "type": "http",
            "scheme": "basic"
        },
         "BearerAuth": {
            "type": "http",
            "scheme": "bearer",
            "bearerFormat": "jwt",
        }
    }
    
    openapi_schema["security"] = [
        {"BasicAuth": []}
    ]
   
    for path, methods in openapi_schema["paths"].items():
        for method in methods:
            if path.startswith("docs"):
                openapi_schema["paths"][path][method]["security"] = [{"BasicAuth": []}]
            else:
                openapi_schema["paths"][path][method]["security"] = [{"BearerAuth": []}]             

    app.openapi_schema = openapi_schema
    return app.openapi_schema

when I execute endpoint from the UI, I see the following, which does not include Bearer I am expecting.

curl -X 'POST' \
  'http://localhost:8000/gxi/v1/my_endpoint' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \



 

No RepliesBe the first to reply