Skip to content
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

Enhance Swagger to support OpenIdConnect #16574

Merged
merged 7 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using JetBrains.Annotations;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using Swashbuckle.AspNetCore.SwaggerUI;
using Volo.Abp.Content;

namespace Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -61,20 +62,70 @@ public static IServiceCollection AddAbpSwaggerGenWithOAuth(

options.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
new OpenApiSecurityScheme
Reference = new OpenApiReference
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "oauth2"
}
},
Array.Empty<string>()
}
Type = ReferenceType.SecurityScheme,
Id = "oauth2"
}
},
Array.Empty<string>()
}
});

setupAction?.Invoke(options);
});
}

public static IServiceCollection AddAbpSwaggerGenWithOidc(
this IServiceCollection services,
[NotNull] string authority,
string[] scopes = null,
string[] flows = null,
string discoveryEndpoint = null,
Action<SwaggerGenOptions> setupAction = null)
{
var discoveryUrl = discoveryEndpoint != null ?
new Uri(discoveryEndpoint) :
new Uri($"{authority.TrimEnd('/')}/.well-known/openid-configuration");

flows ??= new [] { "authorization_code" };

services.Configure<SwaggerUIOptions>(swaggerUiOptions =>
{
swaggerUiOptions.ConfigObject.AdditionalItems["oidcSupportedFlows"] = flows;
swaggerUiOptions.ConfigObject.AdditionalItems["oidcSupportedScopes"] = scopes;
swaggerUiOptions.ConfigObject.AdditionalItems["oidcDiscoveryEndpoint"] = discoveryEndpoint;
});

return services
.AddAbpSwaggerGen()
.AddSwaggerGen(
options =>
{
options.AddSecurityDefinition("oidc", new OpenApiSecurityScheme
{
Type = SecuritySchemeType.OpenIdConnect,
OpenIdConnectUrl = discoveryUrl
});

options.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "oidc"
}
},
Array.Empty<string>()
}
});
setupAction?.Invoke(options);
});
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
var abp = abp || {};

(function() {
(function () {

abp.SwaggerUIBundle = function(configObject) {
abp.SwaggerUIBundle = function (configObject) {

var excludeUrl = ["swagger.json", "connect/token"]
var firstRequest = true;
var oidcSupportedFlows = configObject.oidcSupportedFlows || [];
var oidcSupportedScopes = configObject.oidcSupportedScopes || [];
var oidcDiscoveryEndpoint = configObject.oidcDiscoveryEndpoint || [];
abp.appPath = configObject.baseUrl || abp.appPath;

var requestInterceptor = configObject.requestInterceptor;
var responseInterceptor = configObject.responseInterceptor;

configObject.requestInterceptor = async function(request) {
configObject.requestInterceptor = async function (request) {

if (request.url.includes(excludeUrl[1])) {
firstRequest = true;
Expand All @@ -22,6 +26,10 @@ var abp = abp || {};
});
firstRequest = false;
}
// Intercept .well-known request when the discoveryEndpoint is provided
if (!firstRequest && oidcDiscoveryEndpoint.length !== 0 && request.url.includes(".well-known/openid-configuration")) {
request.url = oidcDiscoveryEndpoint;
}

var antiForgeryToken = abp.security.antiForgery.getToken();
if (antiForgeryToken) {
Expand All @@ -38,6 +46,31 @@ var abp = abp || {};
return request;
};

configObject.responseInterceptor = async function (response) {
if (response.url.endsWith(".well-known/openid-configuration") && response.status === 200) {
var openIdConnectData = JSON.parse(response.text);

if (oidcDiscoveryEndpoint.length > 0) {
openIdConnectData.grant_types_supported = oidcSupportedFlows;
}

if (oidcSupportedFlows.length > 0) {
openIdConnectData.grant_types_supported = oidcSupportedFlows;
}

if (oidcSupportedScopes.length > 0) {
openIdConnectData.scopes_supported = oidcSupportedScopes;
}

response.text = JSON.stringify(openIdConnectData);
}

if (responseInterceptor) {
responseInterceptor(response);
}
return response;
};

return SwaggerUIBundle(configObject);
}
})();
Loading