mirror of https://github.com/Squidex/squidex.git
21 changed files with 315 additions and 168 deletions
@ -0,0 +1,62 @@ |
|||||
|
// ==========================================================================
|
||||
|
// ClaimsPrincipalConverter.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Linq; |
||||
|
using System.Security.Claims; |
||||
|
using Newtonsoft.Json; |
||||
|
|
||||
|
namespace Squidex.Infrastructure.Json |
||||
|
{ |
||||
|
public sealed class ClaimsPrincipalConverter : JsonClassConverter<ClaimsPrincipal> |
||||
|
{ |
||||
|
private sealed class JsonIdentity |
||||
|
{ |
||||
|
[JsonProperty] |
||||
|
public string AuthenticationType { get; set; } |
||||
|
|
||||
|
[JsonProperty] |
||||
|
public JsonClaim[] Claims { get; set; } |
||||
|
} |
||||
|
|
||||
|
private sealed class JsonClaim |
||||
|
{ |
||||
|
[JsonProperty] |
||||
|
public string Type { get; set; } |
||||
|
|
||||
|
[JsonProperty] |
||||
|
public string Value { get; set; } |
||||
|
} |
||||
|
|
||||
|
protected override void WriteValue(JsonWriter writer, ClaimsPrincipal value, JsonSerializer serializer) |
||||
|
{ |
||||
|
var jsonIdentities = |
||||
|
value.Identities.Select(identity => |
||||
|
new JsonIdentity |
||||
|
{ |
||||
|
Claims = identity.Claims.Select(c => |
||||
|
{ |
||||
|
return new JsonClaim { Type = c.Type, Value = c.Value }; |
||||
|
}).ToArray(), |
||||
|
AuthenticationType = identity.AuthenticationType |
||||
|
}).ToArray(); |
||||
|
|
||||
|
serializer.Serialize(writer, jsonIdentities); |
||||
|
} |
||||
|
|
||||
|
protected override ClaimsPrincipal ReadValue(JsonReader reader, JsonSerializer serializer) |
||||
|
{ |
||||
|
var jsonIdentities = serializer.Deserialize<JsonIdentity[]>(reader); |
||||
|
|
||||
|
return new ClaimsPrincipal( |
||||
|
jsonIdentities.Select(identity => |
||||
|
new ClaimsIdentity( |
||||
|
identity.Claims.Select(c => new Claim(c.Type, c.Value)), |
||||
|
identity.AuthenticationType))); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,46 @@ |
|||||
|
// ==========================================================================
|
||||
|
// JsonClassConverter.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
using Newtonsoft.Json; |
||||
|
|
||||
|
namespace Squidex.Infrastructure.Json |
||||
|
{ |
||||
|
public abstract class JsonClassConverter<T> : JsonConverter where T : class |
||||
|
{ |
||||
|
public sealed override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) |
||||
|
{ |
||||
|
if (reader.TokenType == JsonToken.Null) |
||||
|
{ |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
return ReadValue(reader, serializer); |
||||
|
} |
||||
|
|
||||
|
public sealed override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) |
||||
|
{ |
||||
|
if (value == null) |
||||
|
{ |
||||
|
writer.WriteNull(); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
WriteValue(writer, (T)value, serializer); |
||||
|
} |
||||
|
|
||||
|
public override bool CanConvert(Type objectType) |
||||
|
{ |
||||
|
return objectType == typeof(T); |
||||
|
} |
||||
|
|
||||
|
protected abstract void WriteValue(JsonWriter writer, T value, JsonSerializer serializer); |
||||
|
|
||||
|
protected abstract T ReadValue(JsonReader reader, JsonSerializer serializer); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,54 @@ |
|||||
|
// ==========================================================================
|
||||
|
// ClaimsPrincipalConverterTests.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Linq; |
||||
|
using System.Security.Claims; |
||||
|
using Squidex.Infrastructure.TestHelpers; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Squidex.Infrastructure.Json |
||||
|
{ |
||||
|
public class ClaimsPrincipalConverterTests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Should_serialize_and_deserialize() |
||||
|
{ |
||||
|
var value = new ClaimsPrincipal( |
||||
|
new[] |
||||
|
{ |
||||
|
new ClaimsIdentity( |
||||
|
new[] |
||||
|
{ |
||||
|
new Claim("email", "me@email.de"), |
||||
|
new Claim("username", "me@email.de"), |
||||
|
}, |
||||
|
"Cookie"), |
||||
|
new ClaimsIdentity( |
||||
|
new[] |
||||
|
{ |
||||
|
new Claim("user_id", "12345"), |
||||
|
new Claim("login", "me"), |
||||
|
}, |
||||
|
"Google") |
||||
|
}); |
||||
|
|
||||
|
var result = value.SerializeAndDeserializeAndReturn(new ClaimsPrincipalConverter()); |
||||
|
|
||||
|
Assert.Equal(value.Identities.ElementAt(0).AuthenticationType, result.Identities.ElementAt(0).AuthenticationType); |
||||
|
Assert.Equal(value.Identities.ElementAt(1).AuthenticationType, result.Identities.ElementAt(1).AuthenticationType); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_serialize_and_deserialize_null_principal() |
||||
|
{ |
||||
|
ClaimsPrincipal value = null; |
||||
|
|
||||
|
value.SerializeAndDeserialize(new ClaimsPrincipalConverter()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue