-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add extension-method "HidePathItemsWithoutAcceptedRoles" for hiding a…
…ll SwaggerDocument PathItems with added Security information for OAuth2 without accepted roles; 2) Change extension-method name from "EnumsWithValuesFixFilters" to "AddEnumsWithValuesFixFilters"; 3) Update "README".
- Loading branch information
Showing
9 changed files
with
254 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
version: 1.0.{build} | ||
version: 1.1.{build} | ||
pull_requests: | ||
do_not_increment_build_number: true | ||
skip_tags: true | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
84 changes: 84 additions & 0 deletions
84
src/Unchase.Swashbuckle.AspNetCore.Extensions/Extensions/SwaggerDocumentExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Swashbuckle.AspNetCore.Swagger; | ||
|
||
namespace Unchase.Swashbuckle.AspNetCore.Extensions.Extensions | ||
{ | ||
public static class SwaggerDocumentExtensions | ||
{ | ||
public static void HidePathItemsWithoutAcceptedRoles(this SwaggerDocument swaggerDoc, IEnumerable<string> acceptedRoles) | ||
{ | ||
var acceptedRolesList = acceptedRoles.ToList(); | ||
foreach (var swaggerDocPath in swaggerDoc.Paths.Values) | ||
{ | ||
if (!HasNecessaryRoles(swaggerDocPath.Get, acceptedRolesList)) | ||
swaggerDocPath.Get = null; | ||
|
||
if (!HasNecessaryRoles(swaggerDocPath.Post, acceptedRolesList)) | ||
swaggerDocPath.Post = null; | ||
|
||
if (!HasNecessaryRoles(swaggerDocPath.Patch, acceptedRolesList)) | ||
swaggerDocPath.Patch = null; | ||
|
||
if (!HasNecessaryRoles(swaggerDocPath.Delete, acceptedRolesList)) | ||
swaggerDocPath.Delete = null; | ||
|
||
if (!HasNecessaryRoles(swaggerDocPath.Put, acceptedRolesList)) | ||
swaggerDocPath.Put = null; | ||
} | ||
} | ||
|
||
internal static bool HasNecessaryRoles(Operation swaggerDocPathOperation, List<string> acceptedRoles) | ||
{ | ||
var founded = false; | ||
if (swaggerDocPathOperation?.Security == null) | ||
return true; | ||
|
||
if (!acceptedRoles.Any()) | ||
return true; | ||
|
||
foreach (var operationSecurity in swaggerDocPathOperation.Security) | ||
{ | ||
if (founded) | ||
break; | ||
|
||
if (operationSecurity != null) | ||
{ | ||
foreach (var operationSecurityValue in operationSecurity.Values) | ||
{ | ||
if (founded) | ||
break; | ||
|
||
var operationSecurityValueRuntimeFields = operationSecurityValue?.GetType().GetRuntimeFields(); | ||
if (operationSecurityValueRuntimeFields != null) | ||
{ | ||
foreach (var operationSecurityValueRuntimeField in operationSecurityValueRuntimeFields) | ||
{ | ||
if (founded) | ||
break; | ||
|
||
if (operationSecurityValueRuntimeField?.GetValue(operationSecurityValue) is List<AuthorizeAttribute> authorizeAttributes) | ||
{ | ||
foreach (var authorizeAttribute in authorizeAttributes) | ||
{ | ||
var authorizeAttributeRoles = authorizeAttribute?.Roles?.Split(',').Select(r => r.Trim()).ToList(); | ||
var intersect = authorizeAttributeRoles?.Intersect(acceptedRoles); | ||
if (intersect == null || !intersect.Any()) | ||
{ | ||
founded = true; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return !founded; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.