question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Swagger UI: Using implicit flow for OAuth2/OpenIdConnect

See original GitHub issue

After some trial and error I’ve managed to configure the OAuth2 authorization against AWS Cognito in the Swagger UI client.

Ideally, I would have used the OpenIdConnectUrl configuration in the SwaggerSecuritySchemeType.OpenIdConnect settings. Which seemed to work to an extent, as it was correctly downloading the configuration from the endpoint published by AWS Cognito and then redirecting to the login page.

However, Swagger UI was not receiving the configuration for the implicit flow, so the process was failing due to the missing response_type=token parameter in the query string.

As a workaround, the following configuration works when using OAuth2 config for Cognito. I’m leaving it here so that it can be found by others trying to use a similar setup.

// Note: This is a custom POCO used to retrieve the settings
var swaggerOptions = app.ApplicationServices.GetService<IOptions<SwaggerUIOptions>>()?.Value;

app.UseSwaggerUi3WithApiExplorer(settings =>
            {
                settings.GeneratorSettings.OperationProcessors.Add(new OperationSecurityScopeProcessor("JWT token"));

                settings.OAuth2Client = new OAuth2ClientSettings()
                {
                    AppName = "MyDemoApp",
                    ClientId = swaggerOptions.SwaggerClientId
                };

                settings.GeneratorSettings.DocumentProcessors.Add(new SecurityDefinitionAppender("JWT token",
                    new SwaggerSecurityScheme
                    {
                        Type = SwaggerSecuritySchemeType.OAuth2,
                        Flow = SwaggerOAuth2Flow.Implicit,
                        Flows = new OpenApiOAuthFlows() {
                            Implicit = new OpenApiOAuthFlow()
                            {
                                Scopes = new Dictionary<string, string> { { "openid", "User Profile" } },
                                AuthorizationUrl = swaggerOptions.AuthorizationUrl,
                                TokenUrl = swaggerOptions.TokenUrl
                            }
                        }
                    }));
                });

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:7
  • Comments:15 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
zyofengcommented, Feb 17, 2020

Sorry to dig up an old ticket, but can we get an example on how to configure OpenIDConnect client with swaggerui3? Currently I am manually setting up the SecurityScheme as Oauth2 with Implicit flow and various endpoints but would be keen to use proper discovery endpoint instead.

2reactions
jsanchocommented, Aug 17, 2018

brilliant, thanks for that, I’ll try to get some time to give this a shot 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configure OAuth2 implicit flow for Swagger UI - taithienbo
In this post, I share some example codes of how to enable OAuth2 implicit flow within Swagger UI to obtain an access token...
Read more >
OAuth 2.0
Implicit – This flow requires the client to retrieve an access token directly. ... To describe an API protected using OAuth 2.0, first,...
Read more >
OAuth2 / OpenID Connect configuration in Spring Boot and ...
This works fine as I can authenticate via Keycloak's implicit flow. Now I want to secure only some endpoints and not all in...
Read more >
Swagger UI: Using implicit flow for OAuth2/OpenIdConnect
Swagger UI : Using implicit flow for OAuth2/OpenIdConnect. ... Coming soon: A brand new website interface for an even better experience!
Read more >
Secure Applications with OAuth2 and OpenID Connect in ...
Implicit – This flow is suitable for public clients. This flow exposes tokens to the front channels i.e. tokens will be visible to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found