luolingchun / flask-openapi3

Generate REST API and OpenAPI documentation for your Flask project.
https://luolingchun.github.io/flask-openapi3/
MIT License
189 stars 29 forks source link

Feature request: Optionally allow to configure additional oauth parameters #5

Closed ghost closed 3 years ago

ghost commented 3 years ago

This is a swagger-ui specific feature - it would be nice to be able to configure additional OAuth configuration as supported in swagger-ui: https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/oauth2.md

Maybe an additional config on the Flask OpenAPI instance? Then those could be passed into the render method for swagger.html..

I tried this locally and it works fine :)

        window.ui.initOAuth({
            clientId: "{{ client_id }}",
        })
luolingchun commented 3 years ago

You can do it like this:

from flask_openapi3.models.security import HTTPBearer, OAuth2, OAuthFlows, OAuthFlowImplicit

info = Info(title='book API', version='1.0.0')
jwt = HTTPBearer(bearerFormat="JWT")
oauth2 = OAuth2(flows=OAuthFlows(
    implicit=OAuthFlowImplicit(
        authorizationUrl="https://example.com/api/oauth/dialog",
        scopes={
            "write:pets": "modify pets in your account",
            "read:pets": "read your pets"
        }
    )))
securitySchemes = {"jwt": jwt, "oauth2": oauth2}

class NotFoundResponse(BaseModel):
    code: int = Field(-1, description="Status Code")
    message: str = Field("Resource not found!", description="Exception Information")

app = OpenAPI(__name__, info=info, securitySchemes=securitySchemes)

security = [
    {"jwt": []},
    {"oauth2": ["write:pets", "read:pets"]}
]

@app.get('/book/<int:bid>', security=security)
def get_book(path: Path):
    ...

you will see it.

image

ghost commented 3 years ago

Yes, that part is fine. The bit I am after is to pre-poplate the client_id in swagger-ui. That can only be done in JavaScript in the swagger.html template (the window.ui.initOAuth( bit from above) as it is not part of the OpenAPI spec.

luolingchun commented 3 years ago

oooh,i got it. I will define a parameter for OpenAPI, then we can pass oauth_config when creating OpenAPI instance.

luolingchun commented 3 years ago

@martinatseequent Can you test it? see the demo

ghost commented 3 years ago

Cool, yes that works, thanks.