diff --git a/src/Config.cs b/src/Config.cs index 61ea744..f7de1f7 100644 --- a/src/Config.cs +++ b/src/Config.cs @@ -2,7 +2,6 @@ using System.IO; using System.Linq; using System.Collections.Generic; -using System.Security.Claims; using IdentityServer4; using IdentityServer4.Models; using IdentityServer4.Test; @@ -45,12 +44,16 @@ public static IEnumerable GetClients() return configClients; } - public static IEnumerable GetIdentityResources() => new List + public static IEnumerable GetIdentityResources() { - new IdentityResources.OpenId(), - new IdentityResources.Profile(), - new IdentityResources.Email(), - }; + var standardResources = new List + { + new IdentityResources.OpenId(), + new IdentityResources.Profile(), + new IdentityResources.Email() + }; + return standardResources.Union(GetCustomIdentityResources()); + } public static List GetUsers() { @@ -64,8 +67,31 @@ public static List GetUsers() } configStr = File.ReadAllText(configFilePath); } - var configUsers = JsonConvert.DeserializeObject>(configStr); + var configUsers = JsonConvert.DeserializeObject>(configStr, new ClaimJsonConverter()); return configUsers; } + + private static IEnumerable GetCustomIdentityResources() + { + string identityResourcesStr = Environment.GetEnvironmentVariable("IDENTITY_RESOURCES_INLINE"); + if (string.IsNullOrWhiteSpace(identityResourcesStr)) + { + var identityResourcesFilePath = Environment.GetEnvironmentVariable("IDENTITY_RESOURCES_PATH"); + if (string.IsNullOrWhiteSpace(identityResourcesFilePath)) + { + return new List(); + } + identityResourcesStr = File.ReadAllText(identityResourcesFilePath); + } + + var identityResourceConifgs = JsonConvert.DeserializeObject(identityResourcesStr); + return identityResourceConifgs.Select(c => new IdentityResource(c.Name, c.ClaimTypes)); + } + + private class IdentityResourceConfig + { + public string Name { get; set; } + public IEnumerable ClaimTypes { get; set; } + } } } \ No newline at end of file diff --git a/src/utils/ClaimJsonConverter.cs b/src/utils/ClaimJsonConverter.cs new file mode 100644 index 0000000..14d7422 --- /dev/null +++ b/src/utils/ClaimJsonConverter.cs @@ -0,0 +1,28 @@ +using System; +using System.Security.Claims; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +namespace OpenIdConnectServer.Utils +{ + public class ClaimJsonConverter : JsonConverter + { + public override void WriteJson(JsonWriter writer, Claim value, JsonSerializer serializer) + { + throw new NotSupportedException(); + } + + public override Claim ReadJson(JsonReader reader, Type objectType, Claim existingValue, bool hasExistingValue, + JsonSerializer serializer) + { + var jObject = JObject.Load(reader); + var type = jObject["Type"].Value(); + var val = jObject["Value"].Value(); + + return new Claim(type, val); + } + + public override bool CanRead => true; + public override bool CanWrite => false; + } +} \ No newline at end of file