diff --git a/src/Ocelot/Configuration/AuthenticationOptions.cs b/src/Ocelot/Configuration/AuthenticationOptions.cs index 0021a8034..85ad3bba9 100644 --- a/src/Ocelot/Configuration/AuthenticationOptions.cs +++ b/src/Ocelot/Configuration/AuthenticationOptions.cs @@ -23,8 +23,7 @@ public AuthenticationOptions(List allowedScopes, string authenticationPr public AuthenticationOptions(List allowedScopes, string[] authenticationProviderKeys, List requiredRole, string scopeKey, string roleKey, string policyName) { AllowedScopes = allowedScopes; - AuthenticationProviderKey = string.Empty; - AuthenticationProviderKeys = authenticationProviderKeys ?? Array.Empty(); + BuildAuthenticationProviderKeys(null, authenticationProviderKeys); PolicyName = policyName; RequiredRole = requiredRole; ScopeKey = scopeKey; @@ -41,6 +40,8 @@ private void BuildAuthenticationProviderKeys(string legacyKey, string[] keys) keys ??= Array.Empty(); if (string.IsNullOrEmpty(legacyKey)) { + AuthenticationProviderKeys = keys; + AuthenticationProviderKey = string.Empty; return; } diff --git a/src/Ocelot/Configuration/Creator/AuthenticationOptionsCreator.cs b/src/Ocelot/Configuration/Creator/AuthenticationOptionsCreator.cs index 86d0ea43c..75f9a79c3 100644 --- a/src/Ocelot/Configuration/Creator/AuthenticationOptionsCreator.cs +++ b/src/Ocelot/Configuration/Creator/AuthenticationOptionsCreator.cs @@ -4,5 +4,5 @@ namespace Ocelot.Configuration.Creator; public class AuthenticationOptionsCreator : IAuthenticationOptionsCreator { - public AuthenticationOptions Create(FileRoute route) => new(route.AuthenticationOptions); + public AuthenticationOptions Create(FileRoute route) => new(route?.AuthenticationOptions ?? new()); } diff --git a/src/Ocelot/Configuration/File/FileAuthenticationOptions.cs b/src/Ocelot/Configuration/File/FileAuthenticationOptions.cs index ad1ed648a..6d972092d 100644 --- a/src/Ocelot/Configuration/File/FileAuthenticationOptions.cs +++ b/src/Ocelot/Configuration/File/FileAuthenticationOptions.cs @@ -30,28 +30,11 @@ public FileAuthenticationOptions(FileAuthenticationOptions from) public override string ToString() => new StringBuilder() .Append($"{nameof(AuthenticationProviderKey)}:'{AuthenticationProviderKey}',") .Append($"{nameof(AuthenticationProviderKeys)}:[{string.Join(',', AuthenticationProviderKeys.Select(x => $"'{x}'"))}],") - .Append($"{nameof(AllowedScopes)}:[{string.Join(',', AllowedScopes.Select(x => $"'{x}'"))}]") + .Append($"{nameof(AllowedScopes)}:[{string.Join(',', AllowedScopes.Select(x => $"'{x}'"))}],") + .Append($"{nameof(RequiredRole)}:[").AppendJoin(',', RequiredRole).Append("],") + .Append($"{nameof(ScopeKey)}:[").AppendJoin(',', ScopeKey).Append("],") + .Append($"{nameof(RoleKey)}:[").AppendJoin(',', RoleKey).Append("],") + .Append($"{nameof(PolicyName)}:[").AppendJoin(',', PolicyName).Append(']') .ToString(); - - public string ToString2() - { - var sb = new StringBuilder(); - sb.Append($"{nameof(AuthenticationProviderKey)}:{AuthenticationProviderKey},{nameof(AllowedScopes)}:["); - sb.AppendJoin(',', AllowedScopes); - sb.Append("]"); - sb.Append($",{nameof(RequiredRole)}:["); - sb.AppendJoin(',', RequiredRole); - sb.Append("]"); - sb.Append($",{nameof(ScopeKey)}:["); - sb.AppendJoin(',', ScopeKey); - sb.Append("]"); - sb.Append($",{nameof(RoleKey)}:["); - sb.AppendJoin(',', RoleKey); - sb.Append("]"); - sb.Append($",{nameof(PolicyName)}:["); - sb.AppendJoin(',', PolicyName); - sb.Append("]"); - return sb.ToString(); - } } } diff --git a/test/Ocelot.UnitTests/Configuration/AuthenticationOptionsCreatorTests.cs b/test/Ocelot.UnitTests/Configuration/AuthenticationOptionsCreatorTests.cs index d0e8d599e..d9cd320fa 100644 --- a/test/Ocelot.UnitTests/Configuration/AuthenticationOptionsCreatorTests.cs +++ b/test/Ocelot.UnitTests/Configuration/AuthenticationOptionsCreatorTests.cs @@ -46,7 +46,7 @@ public void Create_OptionsObjIsNotNull_CreatedSuccessfully(bool isAuthentication // Arrange string authenticationProviderKey = !isAuthenticationProviderKeys ? "Test" : null; string[] authenticationProviderKeys = isAuthenticationProviderKeys ? - new string[] { "Test #1", "Test #2" } : null; + new string[] { "Test #1", "Test #2" } : Array.Empty(); var fileRoute = new FileRoute() { AuthenticationOptions = new FileAuthenticationOptions @@ -56,11 +56,13 @@ public void Create_OptionsObjIsNotNull_CreatedSuccessfully(bool isAuthentication AuthenticationProviderKeys = authenticationProviderKeys, }, }; - var expected = new AuthenticationOptionsBuilder() - .WithAllowedScopes(fileRoute.AuthenticationOptions?.AllowedScopes) - .WithAuthenticationProviderKey(authenticationProviderKey) - .WithAuthenticationProviderKeys(authenticationProviderKeys) - .Build(); + + var b = new AuthenticationOptionsBuilder() + .WithAllowedScopes(fileRoute.AuthenticationOptions?.AllowedScopes); + b = isAuthenticationProviderKeys + ? b.WithAuthenticationProviderKeys(authenticationProviderKeys) + : b.WithAuthenticationProviderKey(authenticationProviderKey); + var expected = b.Build(); // Act var actual = _authOptionsCreator.Create(fileRoute); diff --git a/test/Ocelot.UnitTests/Configuration/Validation/FileConfigurationFluentValidatorTests.cs b/test/Ocelot.UnitTests/Configuration/Validation/FileConfigurationFluentValidatorTests.cs index 7fe8729da..f619ffaa6 100644 --- a/test/Ocelot.UnitTests/Configuration/Validation/FileConfigurationFluentValidatorTests.cs +++ b/test/Ocelot.UnitTests/Configuration/Validation/FileConfigurationFluentValidatorTests.cs @@ -432,8 +432,7 @@ public void Configuration_is_invalid_with_invalid_authentication_provider() this.Given(x => x.GivenAConfiguration(route)) .When(x => x.WhenIValidateTheConfiguration()) .Then(x => x.ThenTheResultIsNotValid()) - .And(x => x.ThenTheErrorMessageAtPositionIs(0, "Authentication Options AuthenticationProviderKey:'Test',AuthenticationProviderKeys:['Test #1','Test #2'],AllowedScopes:[] is unsupported authentication provider")) - .And(x => x.ThenTheErrorMessageAtPositionIs(0, "Authentication Options AuthenticationProviderKey:Test,AllowedScopes:[],RequiredRole:[],ScopeKey:[],RoleKey:[],PolicyName:[] is unsupported authentication provider")) + .And(x => x.ThenTheErrorMessageAtPositionIs(0, "Authentication Options AuthenticationProviderKey:'Test',AuthenticationProviderKeys:['Test #1','Test #2'],AllowedScopes:[],RequiredRole:[],ScopeKey:[],RoleKey:[],PolicyName:[] is unsupported authentication provider")) .BDDfy(); } diff --git a/test/Ocelot.UnitTests/Configuration/Validation/RouteFluentValidatorTests.cs b/test/Ocelot.UnitTests/Configuration/Validation/RouteFluentValidatorTests.cs index 12ea69eee..a15123e41 100644 --- a/test/Ocelot.UnitTests/Configuration/Validation/RouteFluentValidatorTests.cs +++ b/test/Ocelot.UnitTests/Configuration/Validation/RouteFluentValidatorTests.cs @@ -234,8 +234,7 @@ public void should_not_be_valid_if_specified_authentication_provider_isnt_regist this.Given(_ => GivenThe(fileRoute)) .When(_ => WhenIValidate()) .Then(_ => ThenTheResultIsInvalid()) - .And(_ => ThenTheErrorsContains($"Authentication Options AuthenticationProviderKey:'JwtLads',AuthenticationProviderKeys:[],AllowedScopes:[] is unsupported authentication provider")) - .And(_ => ThenTheErrorsContains($"Authentication Options AuthenticationProviderKey:JwtLads,AllowedScopes:[],RequiredRole:[],ScopeKey:[],RoleKey:[],PolicyName:[] is unsupported authentication provider")) + .And(_ => ThenTheErrorsContains($"Authentication Options AuthenticationProviderKey:'JwtLads',AuthenticationProviderKeys:[],AllowedScopes:[],RequiredRole:[],ScopeKey:[],RoleKey:[],PolicyName:[] is unsupported authentication provider")) .BDDfy(); }