diff --git a/src/Ocelot/Authentication/Handler/Creator/AuthenticationHandlerCreator.cs b/src/Ocelot/Authentication/Handler/Creator/AuthenticationHandlerCreator.cs index 5015fefa..910b99e4 100644 --- a/src/Ocelot/Authentication/Handler/Creator/AuthenticationHandlerCreator.cs +++ b/src/Ocelot/Authentication/Handler/Creator/AuthenticationHandlerCreator.cs @@ -19,7 +19,7 @@ namespace Ocelot.Authentication.Handler.Creator { var builder = app.New(); - var authenticationConfig = authOptions.Config; + var authenticationConfig = authOptions.Config as IdentityServerConfig; builder.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions { diff --git a/src/Ocelot/Configuration/AuthenticationOptions.cs b/src/Ocelot/Configuration/AuthenticationOptions.cs index 25b65fe9..c4a49b7d 100644 --- a/src/Ocelot/Configuration/AuthenticationOptions.cs +++ b/src/Ocelot/Configuration/AuthenticationOptions.cs @@ -6,7 +6,7 @@ namespace Ocelot.Configuration public class AuthenticationOptions { - public AuthenticationOptions(string provider, List allowedScopes, IdentityServerConfig config) + public AuthenticationOptions(string provider, List allowedScopes, IAuthenticationConfig config) { Provider = provider; AllowedScopes = allowedScopes; @@ -17,10 +17,10 @@ namespace Ocelot.Configuration public List AllowedScopes { get; private set; } - public IdentityServerConfig Config { get; private set; } + public IAuthenticationConfig Config { get; private set; } } - public class IdentityServerConfig + public class IdentityServerConfig : IAuthenticationConfig { public IdentityServerConfig(string providerRootUrl, string apiName, bool requireHttps, string apiSecret) { @@ -35,4 +35,6 @@ namespace Ocelot.Configuration public string ApiSecret { get; private set; } public bool RequireHttps { get; private set; } } + + public interface IAuthenticationConfig {} } diff --git a/src/Ocelot/Configuration/Repository/ConsulOcelotConfigurationRepository.cs b/src/Ocelot/Configuration/Repository/ConsulOcelotConfigurationRepository.cs index 703d039b..34529303 100644 --- a/src/Ocelot/Configuration/Repository/ConsulOcelotConfigurationRepository.cs +++ b/src/Ocelot/Configuration/Repository/ConsulOcelotConfigurationRepository.cs @@ -9,6 +9,8 @@ using Ocelot.ServiceDiscovery; namespace Ocelot.Configuration.Repository { + using Ocelot.AcceptanceTests; + public class ConsulOcelotConfigurationRepository : IOcelotConfigurationRepository { private readonly ConsulClient _consul; @@ -48,7 +50,9 @@ namespace Ocelot.Configuration.Repository var json = Encoding.UTF8.GetString(bytes); - var consulConfig = JsonConvert.DeserializeObject(json); + var settings = new JsonSerializerSettings(); + settings.Converters.Add(new AuthenticationConfigConverter()); + var consulConfig = JsonConvert.DeserializeObject(json, settings); return new OkResponse(consulConfig); } diff --git a/src/Ocelot/JsonConverters/AuthenticationConfigConverter.cs b/src/Ocelot/JsonConverters/AuthenticationConfigConverter.cs new file mode 100644 index 00000000..e63b1fe6 --- /dev/null +++ b/src/Ocelot/JsonConverters/AuthenticationConfigConverter.cs @@ -0,0 +1,55 @@ +using System; +using Newtonsoft.Json; +using Ocelot.Configuration; + +namespace Ocelot.AcceptanceTests +{ + using Newtonsoft.Json.Linq; + public class AuthenticationConfigConverter : JsonConverter + { + public override bool CanWrite => false; + + public override bool CanRead => true; + + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + { + throw new InvalidOperationException("Use default serialization."); + } + + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + { + var jsonObject = JObject.Load(reader); + var setting = default(IAuthenticationConfig); + + if (jsonObject["Provider"] != null) + { + switch (jsonObject["Provider"].Value()) + { + //case "Jwt": + // setting = new + default: + setting = new IdentityServerConfig( + jsonObject["ProviderRootUrl"].Value(), + jsonObject["ApiName"].Value(), + jsonObject["RequireHttps"].Value(), + jsonObject["ApiSecret"].Value()); + break; + } + } + else + { + setting = new IdentityServerConfig(string.Empty, string.Empty, false, string.Empty); + } + + serializer.Populate(jsonObject.CreateReader(), setting); + return setting; + } + + public override bool CanConvert(Type objectType) + { + return objectType == typeof(IAuthenticationConfig); + } + } + + +} diff --git a/test/Ocelot.AcceptanceTests/ConfigurationInConsulTests.cs b/test/Ocelot.AcceptanceTests/ConfigurationInConsulTests.cs index 88301610..afed1164 100644 --- a/test/Ocelot.AcceptanceTests/ConfigurationInConsulTests.cs +++ b/test/Ocelot.AcceptanceTests/ConfigurationInConsulTests.cs @@ -104,7 +104,9 @@ namespace Ocelot.AcceptanceTests var json = reader.ReadToEnd(); - _config = JsonConvert.DeserializeObject(json); + var settings = new JsonSerializerSettings(); + settings.Converters.Add(new AuthenticationConfigConverter()); + _config = JsonConvert.DeserializeObject(json, settings); var response = JsonConvert.SerializeObject(true); @@ -166,4 +168,4 @@ namespace Ocelot.AcceptanceTests _steps.Dispose(); } } -} +} \ No newline at end of file