Adding deserializer config for consul

This commit is contained in:
Nick Sharp 2017-06-27 13:49:30 +01:00
parent a7eeadb4c6
commit ec0f3b32e4
5 changed files with 70 additions and 7 deletions

View File

@ -19,7 +19,7 @@ namespace Ocelot.Authentication.Handler.Creator
{ {
var builder = app.New(); var builder = app.New();
var authenticationConfig = authOptions.Config; var authenticationConfig = authOptions.Config as IdentityServerConfig;
builder.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions builder.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
{ {

View File

@ -6,7 +6,7 @@ namespace Ocelot.Configuration
public class AuthenticationOptions public class AuthenticationOptions
{ {
public AuthenticationOptions(string provider, List<string> allowedScopes, IdentityServerConfig config) public AuthenticationOptions(string provider, List<string> allowedScopes, IAuthenticationConfig config)
{ {
Provider = provider; Provider = provider;
AllowedScopes = allowedScopes; AllowedScopes = allowedScopes;
@ -17,10 +17,10 @@ namespace Ocelot.Configuration
public List<string> AllowedScopes { get; private set; } public List<string> 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) public IdentityServerConfig(string providerRootUrl, string apiName, bool requireHttps, string apiSecret)
{ {
@ -35,4 +35,6 @@ namespace Ocelot.Configuration
public string ApiSecret { get; private set; } public string ApiSecret { get; private set; }
public bool RequireHttps { get; private set; } public bool RequireHttps { get; private set; }
} }
public interface IAuthenticationConfig {}
} }

View File

@ -9,6 +9,8 @@ using Ocelot.ServiceDiscovery;
namespace Ocelot.Configuration.Repository namespace Ocelot.Configuration.Repository
{ {
using Ocelot.AcceptanceTests;
public class ConsulOcelotConfigurationRepository : IOcelotConfigurationRepository public class ConsulOcelotConfigurationRepository : IOcelotConfigurationRepository
{ {
private readonly ConsulClient _consul; private readonly ConsulClient _consul;
@ -48,7 +50,9 @@ namespace Ocelot.Configuration.Repository
var json = Encoding.UTF8.GetString(bytes); var json = Encoding.UTF8.GetString(bytes);
var consulConfig = JsonConvert.DeserializeObject<OcelotConfiguration>(json); var settings = new JsonSerializerSettings();
settings.Converters.Add(new AuthenticationConfigConverter());
var consulConfig = JsonConvert.DeserializeObject<OcelotConfiguration>(json, settings);
return new OkResponse<IOcelotConfiguration>(consulConfig); return new OkResponse<IOcelotConfiguration>(consulConfig);
} }

View File

@ -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<string>())
{
//case "Jwt":
// setting = new
default:
setting = new IdentityServerConfig(
jsonObject["ProviderRootUrl"].Value<string>(),
jsonObject["ApiName"].Value<string>(),
jsonObject["RequireHttps"].Value<bool>(),
jsonObject["ApiSecret"].Value<string>());
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);
}
}
}

View File

@ -104,7 +104,9 @@ namespace Ocelot.AcceptanceTests
var json = reader.ReadToEnd(); var json = reader.ReadToEnd();
_config = JsonConvert.DeserializeObject<OcelotConfiguration>(json); var settings = new JsonSerializerSettings();
settings.Converters.Add(new AuthenticationConfigConverter());
_config = JsonConvert.DeserializeObject<OcelotConfiguration>(json, settings);
var response = JsonConvert.SerializeObject(true); var response = JsonConvert.SerializeObject(true);