Closed ghost closed 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.
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.
oooh,i got it.
I will define a parameter for OpenAPI
, then we can pass oauth_config
when creating OpenAPI
instance.
@martinatseequent Can you test it? see the demo
Cool, yes that works, thanks.
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 :)