Skip to content

Commit

Permalink
Adds support for custom identity resources and claims (#12) (#13)
Browse files Browse the repository at this point in the history
* Adds support for custom identity resources and claims

* Adds support for custom identity resources and claims.

Uses generic version for JsonConverter
  • Loading branch information
AleF83 authored Aug 21, 2019
1 parent 86ba5f7 commit c402d91
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
40 changes: 33 additions & 7 deletions src/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -45,12 +44,16 @@ public static IEnumerable<Client> GetClients()
return configClients;
}

public static IEnumerable<IdentityResource> GetIdentityResources() => new List<IdentityResource>
public static IEnumerable<IdentityResource> GetIdentityResources()
{
new IdentityResources.OpenId(),
new IdentityResources.Profile(),
new IdentityResources.Email(),
};
var standardResources = new List<IdentityResource>
{
new IdentityResources.OpenId(),
new IdentityResources.Profile(),
new IdentityResources.Email()
};
return standardResources.Union(GetCustomIdentityResources());
}

public static List<TestUser> GetUsers()
{
Expand All @@ -64,8 +67,31 @@ public static List<TestUser> GetUsers()
}
configStr = File.ReadAllText(configFilePath);
}
var configUsers = JsonConvert.DeserializeObject<List<TestUser>>(configStr);
var configUsers = JsonConvert.DeserializeObject<List<TestUser>>(configStr, new ClaimJsonConverter());
return configUsers;
}

private static IEnumerable<IdentityResource> 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<IdentityResource>();
}
identityResourcesStr = File.ReadAllText(identityResourcesFilePath);
}

var identityResourceConifgs = JsonConvert.DeserializeObject<IdentityResourceConfig[]>(identityResourcesStr);
return identityResourceConifgs.Select(c => new IdentityResource(c.Name, c.ClaimTypes));
}

private class IdentityResourceConfig
{
public string Name { get; set; }
public IEnumerable<string> ClaimTypes { get; set; }
}
}
}
28 changes: 28 additions & 0 deletions src/utils/ClaimJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Security.Claims;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace OpenIdConnectServer.Utils
{
public class ClaimJsonConverter : JsonConverter<Claim>
{
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<string>();
var val = jObject["Value"].Value<string>();

return new Claim(type, val);
}

public override bool CanRead => true;
public override bool CanWrite => false;
}
}

0 comments on commit c402d91

Please sign in to comment.