Skip to content

1342 allow to set permissions to exclude to empty array overriding defaults #1347

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

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
24 changes: 24 additions & 0 deletions DevProxy.Abstractions/Plugins/BasePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,30 @@ public override async Task InitializeAsync(InitArgs e, CancellationToken cancell
}
}

/// <summary>
/// <para>Evaluates the <paramref name="key"/> array property.
/// If the property exists, the <paramref name="configuredList"/> value is used;
/// otherwise, the default <paramref name="defaultList"/> is applied.</para>
/// <para>If the property is <i>null</i>, it is interpreted as an empty array (<i>[]</i>).</para>
/// <para>Note: This is necessary because .NET configuration binding cannot differentiate between an empty array,
/// a null value, or a missing property in appsettings.json.
/// See at <see cref="https://github.com/dotnet/runtime/issues/58930"/>
/// </para>
/// </summary>
/// <param name="key">The array property name</param>
/// <param name="configuredList">The configured list of string values</param>
/// <param name="defaultList">The default list of string values</param>
/// <returns>Returns the result list of string values</returns>
protected virtual IEnumerable<string>? GetConfigurationValue(string key, IEnumerable<string>? configuredList,
IEnumerable<string>? defaultList = default)
{
ArgumentNullException.ThrowIfNull(key, nameof(key));

var keyExists = ConfigurationSection.GetChildren().Any(f => string.Equals(key, f.Key, StringComparison.Ordinal));
configuredList = configuredList?.Where(static p => !string.IsNullOrEmpty(p));
return keyExists ? configuredList ?? [] : defaultList;
}

private async Task<(bool IsValid, IEnumerable<string> ValidationErrors)> ValidatePluginConfigAsync(CancellationToken cancellationToken)
{
if (!ProxyConfiguration.ValidateSchemas)
Expand Down
2 changes: 0 additions & 2 deletions DevProxy.Plugins/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,9 @@ internal static string ToCamelCase(this string str)
{
if (string.IsNullOrEmpty(str))
{

return str;
}


return char.ToLowerInvariant(str[0]) + str[1..];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,7 @@ public override async Task InitializeAsync(InitArgs e, CancellationToken cancell

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

// we need to do it this way because .NET doesn't distinguish between
// an empty array and a null value and we want to be able to tell
// if the user hasn't specified a value and we should use the default
// set or if they have specified an empty array and we shouldn't exclude
// any permissions
if (Configuration.PermissionsToExclude is null)
{
Configuration.PermissionsToExclude = ["profile", "openid", "offline_access", "email"];
}
else
{
// remove empty strings
Configuration.PermissionsToExclude = Configuration.PermissionsToExclude.Where(p => !string.IsNullOrEmpty(p));
}
InitializePermissionsToExclude();
}

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

private void InitializePermissionsToExclude()
{
var key = nameof(GraphMinimalPermissionsGuidancePluginConfiguration.PermissionsToExclude)
.ToCamelCase();

string[] defaultPermissionsToExclude = ["profile", "openid", "offline_access", "email"];
Configuration.PermissionsToExclude = GetConfigurationValue(key, Configuration.PermissionsToExclude, defaultPermissionsToExclude);
}

private async Task EvaluateMinimalScopesAsync(
IEnumerable<(string method, string url)> endpoints,
IEnumerable<string> permissionsFromAccessToken,
Expand Down