Browse Source

Add AddAbpSwaggerGenWithOidc

pull/16574/head
liangshiwei 3 years ago
parent
commit
8fb15b378c
  1. 71
      framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/AbpSwaggerGenServiceCollectionExtensions.cs
  2. 15
      framework/src/Volo.Abp.Swashbuckle/wwwroot/swagger/ui/abp.swagger.js

71
framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/AbpSwaggerGenServiceCollectionExtensions.cs

@ -3,6 +3,7 @@ using System.Collections.Generic;
using JetBrains.Annotations; using JetBrains.Annotations;
using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen; using Swashbuckle.AspNetCore.SwaggerGen;
using Swashbuckle.AspNetCore.SwaggerUI;
using Volo.Abp.Content; using Volo.Abp.Content;
namespace Microsoft.Extensions.DependencyInjection; namespace Microsoft.Extensions.DependencyInjection;
@ -35,12 +36,10 @@ public static class AbpSwaggerGenServiceCollectionExtensions
[NotNull] Dictionary<string, string> scopes, [NotNull] Dictionary<string, string> scopes,
Action<SwaggerGenOptions> setupAction = null, Action<SwaggerGenOptions> setupAction = null,
string authorizationEndpoint = "/connect/authorize", string authorizationEndpoint = "/connect/authorize",
string tokenEndpoint = "/connect/token", string tokenEndpoint = "/connect/token")
string openIdConnectDiscoveryEndpoint = "/.well-known/openid-configuration")
{ {
var authorizationUrl = new Uri($"{authority.TrimEnd('/')}{authorizationEndpoint.EnsureStartsWith('/')}"); var authorizationUrl = new Uri($"{authority.TrimEnd('/')}{authorizationEndpoint.EnsureStartsWith('/')}");
var tokenUrl = new Uri($"{authority.TrimEnd('/')}{tokenEndpoint.EnsureStartsWith('/')}"); var tokenUrl = new Uri($"{authority.TrimEnd('/')}{tokenEndpoint.EnsureStartsWith('/')}");
var openIdConnectDiscoveryUrl = new Uri($"{authority.TrimEnd('/')}{openIdConnectDiscoveryEndpoint.EnsureStartsWith('/')}");
return services return services
.AddAbpSwaggerGen() .AddAbpSwaggerGen()
@ -49,8 +48,7 @@ public static class AbpSwaggerGenServiceCollectionExtensions
{ {
options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
{ {
Type = SecuritySchemeType.OpenIdConnect, Type = SecuritySchemeType.OAuth2,
OpenIdConnectUrl = openIdConnectDiscoveryUrl,
Flows = new OpenApiOAuthFlows Flows = new OpenApiOAuthFlows
{ {
AuthorizationCode = new OpenApiOAuthFlow AuthorizationCode = new OpenApiOAuthFlow
@ -64,19 +62,64 @@ public static class AbpSwaggerGenServiceCollectionExtensions
options.AddSecurityRequirement(new OpenApiSecurityRequirement options.AddSecurityRequirement(new OpenApiSecurityRequirement
{ {
{
new OpenApiSecurityScheme
{ {
new OpenApiSecurityScheme Reference = new OpenApiReference
{ {
Reference = new OpenApiReference Type = ReferenceType.SecurityScheme,
{ Id = "oauth2"
Type = ReferenceType.SecurityScheme, }
Id = "oauth2" },
} Array.Empty<string>()
}, }
Array.Empty<string>() });
}
setupAction?.Invoke(options);
});
}
public static IServiceCollection AddAbpSwaggerGenWithOidc(
this IServiceCollection services,
[NotNull] string authority,
Action<SwaggerGenOptions> setupAction = null,
List<string> flows = null,
string openIdConnectDiscoveryEndpoint = "/.well-known/openid-configuration")
{
var openIdConnectDiscoveryUrl = new Uri($"{authority.TrimEnd('/')}{openIdConnectDiscoveryEndpoint.EnsureStartsWith('/')}");
flows ??= new List<string> { "authorization_code"};
services.Configure<SwaggerUIOptions>(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<string>()
}
});
setupAction?.Invoke(options); setupAction?.Invoke(options);
}); });
} }

15
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 excludeUrl = ["swagger.json", "connect/token"]
var firstRequest = true; var firstRequest = true;
var oidcSupportedFlows = configObject.oidcSupportedFlows || [];
abp.appPath = configObject.baseUrl || abp.appPath; abp.appPath = configObject.baseUrl || abp.appPath;
var requestInterceptor = configObject.requestInterceptor; var requestInterceptor = configObject.requestInterceptor;
var responseInterceptor = configObject.responseInterceptor;
configObject.requestInterceptor = async function(request) { configObject.requestInterceptor = async function(request) {
@ -38,6 +40,19 @@ var abp = abp || {};
return request; 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); return SwaggerUIBundle(configObject);
} }
})(); })();

Loading…
Cancel
Save