-
Notifications
You must be signed in to change notification settings - Fork 408
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: Add support for OAuth 2.0 authorization for Swagger UI #4036
Comments
Thank you @leandrodamascena for your clarification. Is it possible to implement myself using?:
I don't know much about Swagger UI, but it seems it support OAuth 2.0 configuration. Could that be included into the external bundles?
https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/ |
Unfortunately, no. This
I'm afraid you can't do that. Swagger UI is a project designed to enable developers to import OpenAPI/Swagger files and display them interactively in the browser. To incorporate support for OAuth2.0 and other authentication methods, we must change our OpenAPI utility to inject the configuration into the OpenAPI file. Let me talk with the team to see our timeline for implementing this. I'll return here as soon as I have updates. Thanks |
Thank you. But what's the purpose of the Swagger UI OAuth 2.0 configuration options then? https://swagger.io/docs/open-source-tools/swagger-ui/usage/oauth2/ Would that be configuration for fetching the OpenAPI spec itself?
What I need is a Swagger UI for the OpenAPI spec, optionally protected by OIDC, and with an interface like the following, where users press "SSO Login", which fetches a token for calling all defined API endpoints: ![]() |
My apologies for the misunderstanding. I realize now that what you're asking for is to protect the Swagger UI with OIDC/OAuth, rather than specific routes. Got it now. However, I remain skeptical that this will work seamlessly without additional adjustments. We define the SwaggerUI at this line, and even if you bring your own JavaScript files and define The best approach here would be to introduce new parameters in the enable_swagger method, allowing customers to configure OAuth for SwaggerUI. |
I'm adding this issue to our backlog to work on it in the next sprints. |
Thank you.
Actually, protecting the Swagger UI is not a requirement, but just a nice feature. However, I want the Swagger UI to be able to execute the Authorization Code Grant Flow (OAuth 2.0) to acquire an access token for calling the listed API endpoints. I hope, I'm clear now :) |
Making requests from the Swagger UI using OAuth 2.0 would be really good. Right now I'm not sure what's needed, so we need to investigate more, but it doesn't seem to be too hard. I can try to work on this next week if you want. |
I'm starting to work on this now. |
I can help you out, if you need. I made some manually edits of the generated OpenAPI spec, then ran Swagger UI from a Docker container as follows:
Swagger UI provides several ways to provide these arguments: config file, hard-coded, etc. With |
Awesome, thanks for this hints, I actually didn't had the full picture in my head how the Swagger UI would integrate with OAuth2, so your comment led me to learn all about securitySchemes :D I'll try to write something and will get back to you to get feedback. |
Nice, if I can help with anything regarding the implementation, feel free to reach out. I would like to start contributing to the project myself :) The tricky part is supplying the Swagger UI OAuth 2.0 configuration, which was provided as Docker environment variables above: https://swagger.io/docs/open-source-tools/swagger-ui/usage/oauth2/ |
Oh I would love for you to contribute! 🎉 Here are my ideas:
Right now the models are already there, but they are not used anywhere. The OpenAPI spec says we can add security to both the top API declaration and individual operations. So here's an idea (napkin programming here :P):
oauth2_flows = OAuthFlows(authorizationCode=OAuthFlowAuthorizationCode(authorizationUrl="https://example.com/oauth/authorize", tokenUrl="https://example.com/oauth/token", scopes={"read": "Grants read access"})
app.get_openapi_schema(title="...", security_schemes=[OAuth2(flows=oauth2_flows)])
app.get_openapi_schema(..., security=[{"OAuth2": ["admin"]}]) And finally, we need to add the same
@app.get("/admin", security=[{"OAuth2": ["admin"]}]) Notice that this is just about generating the OpenAPI schema, not about enforcing any of those security schemas :D Does any of this makes sense? Would you like to try it? :D If you're on Discord we can always have a chat if you ever get stuck. |
I've went ahead and started the implementation. Still some tasks to do (check the PR description), let me know if you want to take some! |
Thank you @rubenfonseca. I commented on your other PR. Great job updating the docs with a clear explanation on usage. I will get back to you tomorrow or Friday. I have been extremely busy at LEGO for the past 3 days, so I haven't had much time helping you out here |
|
This is now released under 2.37.0 version! |
Use case
I want to protect and add authorization headers to requests sent using the Swagger UI, as endpoints are protected by an API Gateway REST Lambda authorizer:
https://docs.powertools.aws.dev/lambda/python/latest/core/event_handler/api_gateway/#enabling-swaggerui
Is this possible to achieve using the current Swagger UI implementation in powertools-lambda-python?
Solution/User Experience
I would like to provide configuration similar to how it's done for swagger-ui-py
Alternative solutions
No response
Acknowledgment
The text was updated successfully, but these errors were encountered: