Repository navigation
Expand file tree
/
Copy pathTicketSerializer.cs
More file actions
142 lines (131 loc) · 5.87 KB
/
Copy pathTicketSerializer.cs
File metadata and controls
142 lines (131 loc) · 5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// 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;
using System.IO;
using System.Linq;
using System.Security.Claims;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Authentication.DataHandler.Serializer
{
public class TicketSerializer : IDataSerializer<AuthenticationTicket>
{
private const int FormatVersion = 3;
public virtual byte[] Serialize(AuthenticationTicket model)
{
using (var memory = new MemoryStream())
{
using (var writer = new BinaryWriter(memory))
{
Write(writer, model);
}
return memory.ToArray();
}
}
public virtual AuthenticationTicket Deserialize(byte[] data)
{
using (var memory = new MemoryStream(data))
{
using (var reader = new BinaryReader(memory))
{
return Read(reader);
}
}
}
public static void Write([NotNull] BinaryWriter writer, [NotNull] AuthenticationTicket model)
{
writer.Write(FormatVersion);
writer.Write(model.AuthenticationScheme);
var principal = model.Principal;
if (principal == null)
{
// Use -1 to signal null
writer.Write(-1);
}
else
{
writer.Write(principal.Identities.Count());
foreach (var identity in principal.Identities)
{
var authenticationType = string.IsNullOrWhiteSpace(identity.AuthenticationType) ? string.Empty : identity.AuthenticationType;
writer.Write(authenticationType);
WriteWithDefault(writer, identity.NameClaimType, DefaultValues.NameClaimType);
WriteWithDefault(writer, identity.RoleClaimType, DefaultValues.RoleClaimType);
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);
}
public static AuthenticationTicket Read([NotNull] BinaryReader reader)
{
if (reader.ReadInt32() != FormatVersion)
{
return null;
}
string authenticationScheme = reader.ReadString();
int identityCount = reader.ReadInt32();
ClaimsPrincipal principal = null;
// Negative values are used to signify null
if (identityCount >= 0)
{
var identities = new ClaimsIdentity[identityCount];
for (int i = 0; i != identityCount; ++i)
{
string authenticationType = reader.ReadString();
string nameClaimType = ReadWithDefault(reader, DefaultValues.NameClaimType);
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 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);
}
principal = new ClaimsPrincipal(identities);
}
var properties = PropertiesSerializer.Read(reader);
return new AuthenticationTicket(principal, properties, authenticationScheme);
}
private static void WriteWithDefault(BinaryWriter writer, string value, string defaultValue)
{
if (string.Equals(value, defaultValue, StringComparison.Ordinal))
{
writer.Write(DefaultValues.DefaultStringPlaceholder);
}
else
{
writer.Write(value);
}
}
private static string ReadWithDefault(BinaryReader reader, string defaultValue)
{
string value = reader.ReadString();
if (string.Equals(value, DefaultValues.DefaultStringPlaceholder, StringComparison.Ordinal))
{
return defaultValue;
}
return value;
}
private static class DefaultValues
{
public const string DefaultStringPlaceholder = "\0";
public const string NameClaimType = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name";
public const string RoleClaimType = "http://schemas.microsoft.com/ws/2008/06/identity/claims/role";
public const string LocalAuthority = "LOCAL AUTHORITY";
public const string StringValueType = "http://www.w3.org/2001/XMLSchema#string";
}
}
}