| | | 1 | | using Kestrun.Hosting; |
| | | 2 | | |
| | | 3 | | namespace Kestrun.OpenApi; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Helper methods for OpenAPI integration. |
| | | 7 | | /// </summary> |
| | | 8 | | public static class OpenApiHelper |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Adds a security requirement object to the OpenAPI metadata based on the specified scheme and policies. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <param name="host"> The Kestrun host instance.</param> |
| | | 14 | | /// <param name="scheme">The security scheme name.</param> |
| | | 15 | | /// <param name="policyList">List of security policies.</param> |
| | | 16 | | /// <param name="securitySchemes">The list of security schemes to which the security requirement will be added.</par |
| | | 17 | | /// <returns>A list of all security schemes involved in the requirement.</returns> |
| | | 18 | | internal static List<string> AddSecurityRequirementObject(this KestrunHost host, |
| | | 19 | | string? scheme, List<string> policyList, |
| | | 20 | | List<Dictionary<string, List<string>>> securitySchemes) |
| | | 21 | | { |
| | 0 | 22 | | ArgumentNullException.ThrowIfNull(host); |
| | 0 | 23 | | ArgumentNullException.ThrowIfNull(policyList); |
| | 0 | 24 | | ArgumentNullException.ThrowIfNull(securitySchemes); |
| | | 25 | | |
| | 0 | 26 | | var scopesByScheme = new Dictionary<string, List<string>>(StringComparer.Ordinal); |
| | 0 | 27 | | var allSchemes = new HashSet<string>(StringComparer.Ordinal); |
| | | 28 | | |
| | 0 | 29 | | AddExplicitScheme(scheme, scopesByScheme, allSchemes); |
| | 0 | 30 | | MapPoliciesToSchemes(host, policyList, scopesByScheme, allSchemes); |
| | | 31 | | |
| | 0 | 32 | | securitySchemes.Add(scopesByScheme); |
| | | 33 | | |
| | 0 | 34 | | return [.. allSchemes]; |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Adds an explicit security scheme to the scopes dictionary and all schemes set. |
| | | 39 | | /// </summary> |
| | | 40 | | /// <param name="scheme">The security scheme name.</param> |
| | | 41 | | /// <param name="scopesByScheme">The dictionary mapping schemes to their scopes.</param> |
| | | 42 | | /// <param name="allSchemes">The set of all security schemes.</param> |
| | | 43 | | private static void AddExplicitScheme( |
| | | 44 | | string? scheme, |
| | | 45 | | Dictionary<string, List<string>> scopesByScheme, |
| | | 46 | | HashSet<string> allSchemes) |
| | | 47 | | { |
| | 0 | 48 | | if (string.IsNullOrWhiteSpace(scheme)) |
| | | 49 | | { |
| | 0 | 50 | | return; |
| | | 51 | | } |
| | | 52 | | |
| | 0 | 53 | | _ = GetOrCreateScopeList(scopesByScheme, scheme); |
| | 0 | 54 | | _ = allSchemes.Add(scheme); |
| | 0 | 55 | | } |
| | | 56 | | /// <summary> |
| | | 57 | | /// Maps security policies to their corresponding security schemes. |
| | | 58 | | /// </summary> |
| | | 59 | | /// <param name="host">The Kestrun host instance.</param> |
| | | 60 | | /// <param name="policyList">List of security policies.</param> |
| | | 61 | | /// <param name="scopesByScheme">The dictionary mapping schemes to their scopes.</param> |
| | | 62 | | /// <param name="allSchemes">The set of all security schemes.</param> |
| | | 63 | | private static void MapPoliciesToSchemes( |
| | | 64 | | KestrunHost host, |
| | | 65 | | IEnumerable<string> policyList, |
| | | 66 | | Dictionary<string, List<string>> scopesByScheme, |
| | | 67 | | HashSet<string> allSchemes) |
| | | 68 | | { |
| | 0 | 69 | | foreach (var policy in policyList) |
| | | 70 | | { |
| | 0 | 71 | | var schemesForPolicy = host.RegisteredAuthentications.GetSchemesByPolicy(policy); |
| | 0 | 72 | | if (schemesForPolicy is null) |
| | | 73 | | { |
| | | 74 | | continue; |
| | | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | foreach (var schemeName in schemesForPolicy) |
| | | 78 | | { |
| | 0 | 79 | | var scopeList = GetOrCreateScopeList(scopesByScheme, schemeName); |
| | | 80 | | |
| | 0 | 81 | | if (!scopeList.Contains(policy)) |
| | | 82 | | { |
| | 0 | 83 | | scopeList.Add(policy); |
| | | 84 | | } |
| | | 85 | | |
| | 0 | 86 | | _ = allSchemes.Add(schemeName); |
| | | 87 | | } |
| | | 88 | | } |
| | 0 | 89 | | } |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Retrieves or creates the scope list for a given security scheme. |
| | | 93 | | /// </summary> |
| | | 94 | | /// <param name="scopesByScheme">The dictionary mapping schemes to their scopes.</param> |
| | | 95 | | /// <param name="schemeName">The security scheme name.</param> |
| | | 96 | | /// <returns>The list of scopes associated with the security scheme.</returns> |
| | | 97 | | private static List<string> GetOrCreateScopeList( |
| | | 98 | | Dictionary<string, List<string>> scopesByScheme, |
| | | 99 | | string schemeName) |
| | | 100 | | { |
| | 0 | 101 | | if (!scopesByScheme.TryGetValue(schemeName, out var scopeList)) |
| | | 102 | | { |
| | 0 | 103 | | scopeList = []; |
| | 0 | 104 | | scopesByScheme[schemeName] = scopeList; |
| | | 105 | | } |
| | | 106 | | |
| | 0 | 107 | | return scopeList; |
| | | 108 | | } |
| | | 109 | | } |