diff --git a/framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/AbpSwaggerGenServiceCollectionExtensions.cs b/framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/AbpSwaggerGenServiceCollectionExtensions.cs index 38e104fab5..b012300c6f 100644 --- a/framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/AbpSwaggerGenServiceCollectionExtensions.cs +++ b/framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/AbpSwaggerGenServiceCollectionExtensions.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using JetBrains.Annotations; using Microsoft.OpenApi.Models; using Swashbuckle.AspNetCore.SwaggerGen; +using Swashbuckle.AspNetCore.SwaggerUI; using Volo.Abp.Content; namespace Microsoft.Extensions.DependencyInjection; @@ -35,12 +36,10 @@ public static class AbpSwaggerGenServiceCollectionExtensions [NotNull] Dictionary scopes, Action setupAction = null, string authorizationEndpoint = "/connect/authorize", - string tokenEndpoint = "/connect/token", - string openIdConnectDiscoveryEndpoint = "/.well-known/openid-configuration") + string tokenEndpoint = "/connect/token") { var authorizationUrl = new Uri($"{authority.TrimEnd('/')}{authorizationEndpoint.EnsureStartsWith('/')}"); var tokenUrl = new Uri($"{authority.TrimEnd('/')}{tokenEndpoint.EnsureStartsWith('/')}"); - var openIdConnectDiscoveryUrl = new Uri($"{authority.TrimEnd('/')}{openIdConnectDiscoveryEndpoint.EnsureStartsWith('/')}"); return services .AddAbpSwaggerGen() @@ -49,8 +48,7 @@ public static class AbpSwaggerGenServiceCollectionExtensions { options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme { - Type = SecuritySchemeType.OpenIdConnect, - OpenIdConnectUrl = openIdConnectDiscoveryUrl, + Type = SecuritySchemeType.OAuth2, Flows = new OpenApiOAuthFlows { AuthorizationCode = new OpenApiOAuthFlow @@ -64,19 +62,64 @@ public static class AbpSwaggerGenServiceCollectionExtensions options.AddSecurityRequirement(new OpenApiSecurityRequirement { + { + new OpenApiSecurityScheme { - new OpenApiSecurityScheme + Reference = new OpenApiReference { - Reference = new OpenApiReference - { - Type = ReferenceType.SecurityScheme, - Id = "oauth2" - } - }, - Array.Empty() - } + Type = ReferenceType.SecurityScheme, + Id = "oauth2" + } + }, + Array.Empty() + } + }); + + setupAction?.Invoke(options); + }); + } + + public static IServiceCollection AddAbpSwaggerGenWithOidc( + this IServiceCollection services, + [NotNull] string authority, + Action setupAction = null, + List flows = null, + string openIdConnectDiscoveryEndpoint = "/.well-known/openid-configuration") + { + var openIdConnectDiscoveryUrl = new Uri($"{authority.TrimEnd('/')}{openIdConnectDiscoveryEndpoint.EnsureStartsWith('/')}"); + flows ??= new List { "authorization_code"}; + + services.Configure(swaggerUiOptions => + { + swaggerUiOptions.ConfigObject.AdditionalItems["oidcSupportedFlows"] = flows; + }); + + return services + .AddAbpSwaggerGen() + .AddSwaggerGen( + options => + { + options.AddSecurityDefinition("oidc", new OpenApiSecurityScheme + { + Type = SecuritySchemeType.OpenIdConnect, + OpenIdConnectUrl = openIdConnectDiscoveryUrl, + }); + options.AddSecurityRequirement(new OpenApiSecurityRequirement + { + { + new OpenApiSecurityScheme + { + Reference = new OpenApiReference + { + Type = ReferenceType.SecurityScheme, + Id = "oidc" + } + }, + Array.Empty() + } + }); setupAction?.Invoke(options); }); } diff --git a/framework/src/Volo.Abp.Swashbuckle/wwwroot/swagger/ui/abp.swagger.js b/framework/src/Volo.Abp.Swashbuckle/wwwroot/swagger/ui/abp.swagger.js index c143b7e75e..fa0e27dd0c 100644 --- a/framework/src/Volo.Abp.Swashbuckle/wwwroot/swagger/ui/abp.swagger.js +++ b/framework/src/Volo.Abp.Swashbuckle/wwwroot/swagger/ui/abp.swagger.js @@ -6,9 +6,11 @@ var abp = abp || {}; var excludeUrl = ["swagger.json", "connect/token"] var firstRequest = true; + var oidcSupportedFlows = configObject.oidcSupportedFlows || []; abp.appPath = configObject.baseUrl || abp.appPath; var requestInterceptor = configObject.requestInterceptor; + var responseInterceptor = configObject.responseInterceptor; configObject.requestInterceptor = async function(request) { @@ -38,6 +40,19 @@ var abp = abp || {}; return request; }; + configObject.responseInterceptor = async function(response) { + if(response.url.endsWith(".well-known/openid-configuration") && response.status === 200 && oidcSupportedFlows.length > 0) { + var openIdConnectData = JSON.parse(response.text); + openIdConnectData.grant_types_supported = oidcSupportedFlows; + response.text = JSON.stringify(openIdConnectData); + } + + if (responseInterceptor) { + responseInterceptor(response); + } + return response; + }; + return SwaggerUIBundle(configObject); } })();