Skip to content

Commit c9f3779

Browse files
1342 allow to set permissions to exclude to empty array overriding defaults (#1347)
* adjustments * Add function to define the list based on config key presence and provided default * Add method to initialize permissions * Set initial PermissionsToExclude property to empty array * fix: Throw ArgumentNullException if null * fix: Rename list to configuredList * fix: Revert intial value of PermissionsToExclude to null * fix: Add explanatory comment * Cosmetic updates --------- Co-authored-by: Waldek Mastykarz <waldek@mastykarz.nl>
1 parent 1131f24 commit c9f3779

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

DevProxy.Abstractions/Plugins/BasePlugin.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,30 @@ public override async Task InitializeAsync(InitArgs e, CancellationToken cancell
114114
}
115115
}
116116

117+
/// <summary>
118+
/// <para>Evaluates the <paramref name="key"/> array property.
119+
/// If the property exists, the <paramref name="configuredList"/> value is used;
120+
/// otherwise, the default <paramref name="defaultList"/> is applied.</para>
121+
/// <para>If the property is <i>null</i>, it is interpreted as an empty array (<i>[]</i>).</para>
122+
/// <para>Note: This is necessary because .NET configuration binding cannot differentiate between an empty array,
123+
/// a null value, or a missing property in appsettings.json.
124+
/// See at <see cref="https://github.com/dotnet/runtime/issues/58930"/>
125+
/// </para>
126+
/// </summary>
127+
/// <param name="key">The array property name</param>
128+
/// <param name="configuredList">The configured list of string values</param>
129+
/// <param name="defaultList">The default list of string values</param>
130+
/// <returns>Returns the result list of string values</returns>
131+
protected virtual IEnumerable<string>? GetConfigurationValue(string key, IEnumerable<string>? configuredList,
132+
IEnumerable<string>? defaultList = default)
133+
{
134+
ArgumentNullException.ThrowIfNull(key, nameof(key));
135+
136+
var keyExists = ConfigurationSection.GetChildren().Any(f => string.Equals(key, f.Key, StringComparison.Ordinal));
137+
configuredList = configuredList?.Where(static p => !string.IsNullOrEmpty(p));
138+
return keyExists ? configuredList ?? [] : defaultList;
139+
}
140+
117141
private async Task<(bool IsValid, IEnumerable<string> ValidationErrors)> ValidatePluginConfigAsync(CancellationToken cancellationToken)
118142
{
119143
if (!ProxyConfiguration.ValidateSchemas)

DevProxy.Plugins/Extensions/StringExtensions.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,9 @@ internal static string ToCamelCase(this string str)
6767
{
6868
if (string.IsNullOrEmpty(str))
6969
{
70-
7170
return str;
7271
}
7372

74-
7573
return char.ToLowerInvariant(str[0]) + str[1..];
7674
}
7775

DevProxy.Plugins/Reporting/GraphMinimalPermissionsGuidancePlugin.cs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,7 @@ public override async Task InitializeAsync(InitArgs e, CancellationToken cancell
6161

6262
_graphUtils = ActivatorUtilities.CreateInstance<GraphUtils>(e.ServiceProvider);
6363

64-
// we need to do it this way because .NET doesn't distinguish between
65-
// an empty array and a null value and we want to be able to tell
66-
// if the user hasn't specified a value and we should use the default
67-
// set or if they have specified an empty array and we shouldn't exclude
68-
// any permissions
69-
if (Configuration.PermissionsToExclude is null)
70-
{
71-
Configuration.PermissionsToExclude = ["profile", "openid", "offline_access", "email"];
72-
}
73-
else
74-
{
75-
// remove empty strings
76-
Configuration.PermissionsToExclude = Configuration.PermissionsToExclude.Where(p => !string.IsNullOrEmpty(p));
77-
}
64+
InitializePermissionsToExclude();
7865
}
7966

8067
public override async Task AfterRecordingStopAsync(RecordingArgs e, CancellationToken cancellationToken)
@@ -220,6 +207,15 @@ public override async Task AfterRecordingStopAsync(RecordingArgs e, Cancellation
220207
Logger.LogTrace("Left {Name}", nameof(AfterRecordingStopAsync));
221208
}
222209

210+
private void InitializePermissionsToExclude()
211+
{
212+
var key = nameof(GraphMinimalPermissionsGuidancePluginConfiguration.PermissionsToExclude)
213+
.ToCamelCase();
214+
215+
string[] defaultPermissionsToExclude = ["profile", "openid", "offline_access", "email"];
216+
Configuration.PermissionsToExclude = GetConfigurationValue(key, Configuration.PermissionsToExclude, defaultPermissionsToExclude);
217+
}
218+
223219
private async Task EvaluateMinimalScopesAsync(
224220
IEnumerable<(string method, string url)> endpoints,
225221
IEnumerable<string> permissionsFromAccessToken,

0 commit comments

Comments
 (0)