Handle null in ticket serializer
This commit is contained in:
parent
78cf7f99ff
commit
d7ce42dacc
|
|
@ -11,7 +11,7 @@ namespace Microsoft.AspNet.Authentication.DataHandler.Serializer
|
||||||
{
|
{
|
||||||
public class TicketSerializer : IDataSerializer<AuthenticationTicket>
|
public class TicketSerializer : IDataSerializer<AuthenticationTicket>
|
||||||
{
|
{
|
||||||
private const int FormatVersion = 2;
|
private const int FormatVersion = 3;
|
||||||
|
|
||||||
public virtual byte[] Serialize(AuthenticationTicket model)
|
public virtual byte[] Serialize(AuthenticationTicket model)
|
||||||
{
|
{
|
||||||
|
|
@ -41,21 +41,29 @@ namespace Microsoft.AspNet.Authentication.DataHandler.Serializer
|
||||||
writer.Write(FormatVersion);
|
writer.Write(FormatVersion);
|
||||||
writer.Write(model.AuthenticationScheme);
|
writer.Write(model.AuthenticationScheme);
|
||||||
var principal = model.Principal;
|
var principal = model.Principal;
|
||||||
writer.Write(principal.Identities.Count());
|
if (principal == null)
|
||||||
foreach (var identity in principal.Identities)
|
|
||||||
{
|
{
|
||||||
var authenticationType = string.IsNullOrWhiteSpace(identity.AuthenticationType) ? string.Empty : identity.AuthenticationType;
|
// Use -1 to signal null
|
||||||
writer.Write(authenticationType);
|
writer.Write(-1);
|
||||||
WriteWithDefault(writer, identity.NameClaimType, DefaultValues.NameClaimType);
|
}
|
||||||
WriteWithDefault(writer, identity.RoleClaimType, DefaultValues.RoleClaimType);
|
else
|
||||||
writer.Write(identity.Claims.Count());
|
{
|
||||||
foreach (var claim in identity.Claims)
|
writer.Write(principal.Identities.Count());
|
||||||
|
foreach (var identity in principal.Identities)
|
||||||
{
|
{
|
||||||
WriteWithDefault(writer, claim.Type, identity.NameClaimType);
|
var authenticationType = string.IsNullOrWhiteSpace(identity.AuthenticationType) ? string.Empty : identity.AuthenticationType;
|
||||||
writer.Write(claim.Value);
|
writer.Write(authenticationType);
|
||||||
WriteWithDefault(writer, claim.ValueType, DefaultValues.StringValueType);
|
WriteWithDefault(writer, identity.NameClaimType, DefaultValues.NameClaimType);
|
||||||
WriteWithDefault(writer, claim.Issuer, DefaultValues.LocalAuthority);
|
WriteWithDefault(writer, identity.RoleClaimType, DefaultValues.RoleClaimType);
|
||||||
WriteWithDefault(writer, claim.OriginalIssuer, claim.Issuer);
|
writer.Write(identity.Claims.Count());
|
||||||
|
foreach (var claim in identity.Claims)
|
||||||
|
{
|
||||||
|
WriteWithDefault(writer, claim.Type, identity.NameClaimType);
|
||||||
|
writer.Write(claim.Value);
|
||||||
|
WriteWithDefault(writer, claim.ValueType, DefaultValues.StringValueType);
|
||||||
|
WriteWithDefault(writer, claim.Issuer, DefaultValues.LocalAuthority);
|
||||||
|
WriteWithDefault(writer, claim.OriginalIssuer, claim.Issuer);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
PropertiesSerializer.Write(writer, model.Properties);
|
PropertiesSerializer.Write(writer, model.Properties);
|
||||||
|
|
@ -69,27 +77,35 @@ namespace Microsoft.AspNet.Authentication.DataHandler.Serializer
|
||||||
}
|
}
|
||||||
string authenticationScheme = reader.ReadString();
|
string authenticationScheme = reader.ReadString();
|
||||||
int identityCount = reader.ReadInt32();
|
int identityCount = reader.ReadInt32();
|
||||||
var identities = new ClaimsIdentity[identityCount];
|
ClaimsPrincipal principal = null;
|
||||||
for (int i = 0; i != identityCount; ++i)
|
|
||||||
|
// Negative values are used to signify null
|
||||||
|
if (identityCount >= 0)
|
||||||
{
|
{
|
||||||
string authenticationType = reader.ReadString();
|
var identities = new ClaimsIdentity[identityCount];
|
||||||
string nameClaimType = ReadWithDefault(reader, DefaultValues.NameClaimType);
|
for (int i = 0; i != identityCount; ++i)
|
||||||
string roleClaimType = ReadWithDefault(reader, DefaultValues.RoleClaimType);
|
|
||||||
int count = reader.ReadInt32();
|
|
||||||
var claims = new Claim[count];
|
|
||||||
for (int index = 0; index != count; ++index)
|
|
||||||
{
|
{
|
||||||
string type = ReadWithDefault(reader, nameClaimType);
|
string authenticationType = reader.ReadString();
|
||||||
string value = reader.ReadString();
|
string nameClaimType = ReadWithDefault(reader, DefaultValues.NameClaimType);
|
||||||
string valueType = ReadWithDefault(reader, DefaultValues.StringValueType);
|
string roleClaimType = ReadWithDefault(reader, DefaultValues.RoleClaimType);
|
||||||
string issuer = ReadWithDefault(reader, DefaultValues.LocalAuthority);
|
int count = reader.ReadInt32();
|
||||||
string originalIssuer = ReadWithDefault(reader, issuer);
|
var claims = new Claim[count];
|
||||||
claims[index] = new Claim(type, value, valueType, issuer, originalIssuer);
|
for (int index = 0; index != count; ++index)
|
||||||
|
{
|
||||||
|
string type = ReadWithDefault(reader, nameClaimType);
|
||||||
|
string value = reader.ReadString();
|
||||||
|
string valueType = ReadWithDefault(reader, DefaultValues.StringValueType);
|
||||||
|
string issuer = ReadWithDefault(reader, DefaultValues.LocalAuthority);
|
||||||
|
string originalIssuer = ReadWithDefault(reader, issuer);
|
||||||
|
claims[index] = new Claim(type, value, valueType, issuer, originalIssuer);
|
||||||
|
}
|
||||||
|
identities[i] = new ClaimsIdentity(claims, authenticationType, nameClaimType, roleClaimType);
|
||||||
}
|
}
|
||||||
identities[i] = new ClaimsIdentity(claims, authenticationType, nameClaimType, roleClaimType);
|
principal = new ClaimsPrincipal(identities);
|
||||||
}
|
}
|
||||||
|
|
||||||
var properties = PropertiesSerializer.Read(reader);
|
var properties = PropertiesSerializer.Read(reader);
|
||||||
return new AuthenticationTicket(new ClaimsPrincipal(identities), properties, authenticationScheme);
|
return new AuthenticationTicket(principal, properties, authenticationScheme);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void WriteWithDefault(BinaryWriter writer, string value, string defaultValue)
|
private static void WriteWithDefault(BinaryWriter writer, string value, string defaultValue)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,57 @@
|
||||||
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Security.Claims;
|
||||||
|
using Microsoft.AspNet.Authentication.DataHandler.Serializer;
|
||||||
|
using Microsoft.AspNet.Http.Authentication;
|
||||||
|
using Shouldly;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNet.Authentication.DataHandler.Encoder
|
||||||
|
{
|
||||||
|
public class TicketSerializerTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void CanRoundTripNullPrincipal()
|
||||||
|
{
|
||||||
|
var properties = new AuthenticationProperties();
|
||||||
|
properties.RedirectUri = "bye";
|
||||||
|
var ticket = new AuthenticationTicket(properties, "Hello");
|
||||||
|
|
||||||
|
using (var stream = new MemoryStream())
|
||||||
|
using (var writer = new BinaryWriter(stream))
|
||||||
|
using (var reader = new BinaryReader(stream))
|
||||||
|
{
|
||||||
|
TicketSerializer.Write(writer, ticket);
|
||||||
|
stream.Position = 0;
|
||||||
|
var readTicket = TicketSerializer.Read(reader);
|
||||||
|
readTicket.Principal.ShouldBe(null);
|
||||||
|
readTicket.Properties.RedirectUri.ShouldBe("bye");
|
||||||
|
readTicket.AuthenticationScheme.ShouldBe("Hello");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void CanRoundTripEmptyPrincipal()
|
||||||
|
{
|
||||||
|
var properties = new AuthenticationProperties();
|
||||||
|
properties.RedirectUri = "bye";
|
||||||
|
var ticket = new AuthenticationTicket(new ClaimsPrincipal(), properties, "Hello");
|
||||||
|
|
||||||
|
using (var stream = new MemoryStream())
|
||||||
|
using (var writer = new BinaryWriter(stream))
|
||||||
|
using (var reader = new BinaryReader(stream))
|
||||||
|
{
|
||||||
|
TicketSerializer.Write(writer, ticket);
|
||||||
|
stream.Position = 0;
|
||||||
|
var readTicket = TicketSerializer.Read(reader);
|
||||||
|
readTicket.Principal.Identities.Count().ShouldBe(0);
|
||||||
|
readTicket.Properties.RedirectUri.ShouldBe("bye");
|
||||||
|
readTicket.AuthenticationScheme.ShouldBe("Hello");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue