Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for custom identity resources and claims #12

Merged
merged 2 commits into from
Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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; }
}
}
}
32 changes: 32 additions & 0 deletions src/utils/ClaimJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Security.Claims;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace OpenIdConnectServer.Utils
{
public class ClaimJsonConverter : JsonConverter
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public class ClaimJsonConverter : JsonConverter
public class ClaimJsonConverter : JsonConverter<Claim>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you, missed that, it is fixed now

{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotSupportedException();
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, 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 CanConvert(Type objectType)
{
return objectType == typeof(Claim);
}

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