mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-23 00:52:51 +08:00
Adding deserializer config for consul
This commit is contained in:
parent
a7eeadb4c6
commit
ec0f3b32e4
@ -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
|
||||
{
|
||||
|
@ -6,7 +6,7 @@ namespace Ocelot.Configuration
|
||||
|
||||
public class AuthenticationOptions
|
||||
{
|
||||
public AuthenticationOptions(string provider, List<string> allowedScopes, IdentityServerConfig config)
|
||||
public AuthenticationOptions(string provider, List<string> allowedScopes, IAuthenticationConfig config)
|
||||
{
|
||||
Provider = provider;
|
||||
AllowedScopes = allowedScopes;
|
||||
@ -17,10 +17,10 @@ namespace Ocelot.Configuration
|
||||
|
||||
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)
|
||||
{
|
||||
@ -35,4 +35,6 @@ namespace Ocelot.Configuration
|
||||
public string ApiSecret { get; private set; }
|
||||
public bool RequireHttps { get; private set; }
|
||||
}
|
||||
|
||||
public interface IAuthenticationConfig {}
|
||||
}
|
||||
|
@ -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<OcelotConfiguration>(json);
|
||||
var settings = new JsonSerializerSettings();
|
||||
settings.Converters.Add(new AuthenticationConfigConverter());
|
||||
var consulConfig = JsonConvert.DeserializeObject<OcelotConfiguration>(json, settings);
|
||||
|
||||
return new OkResponse<IOcelotConfiguration>(consulConfig);
|
||||
}
|
||||
|
55
src/Ocelot/JsonConverters/AuthenticationConfigConverter.cs
Normal file
55
src/Ocelot/JsonConverters/AuthenticationConfigConverter.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -104,7 +104,9 @@ namespace Ocelot.AcceptanceTests
|
||||
|
||||
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);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user