|
5 | 5 | */
|
6 | 6 |
|
7 | 7 | using System;
|
8 |
| -using System.IdentityModel.Tokens; |
9 |
| -using System.IO; |
10 |
| -using System.Linq; |
11 |
| -using System.Security.Claims; |
| 8 | +using Microsoft.AspNetCore.DataProtection; |
| 9 | +using Microsoft.Extensions.DependencyInjection; |
12 | 10 | using Microsoft.Owin;
|
| 11 | +using Microsoft.Owin.BuilderProperties; |
13 | 12 | using Microsoft.Owin.Logging;
|
14 |
| -using Microsoft.Owin.Security; |
15 |
| -using Microsoft.Owin.Security.DataHandler; |
16 |
| -using Microsoft.Owin.Security.DataHandler.Encoder; |
17 |
| -using Microsoft.Owin.Security.DataHandler.Serializer; |
18 |
| -using Microsoft.Owin.Security.DataProtection; |
19 | 13 | using Microsoft.Owin.Security.Infrastructure;
|
| 14 | +using Microsoft.Owin.Security.Interop; |
20 | 15 |
|
21 | 16 | namespace Owin.Security.OAuth.Validation {
|
22 | 17 | public class OAuthValidationMiddleware : AuthenticationMiddleware<OAuthValidationOptions> {
|
23 | 18 | public OAuthValidationMiddleware(OwinMiddleware next, IAppBuilder app, OAuthValidationOptions options)
|
24 | 19 | : base(next, options) {
|
25 |
| - if (options.TicketFormat == null) { |
26 |
| - // Note: the purposes of the default ticket |
27 |
| - // format must match the values used by ASOS. |
28 |
| - options.TicketFormat = new EnhancedTicketDataFormat( |
29 |
| - app.CreateDataProtector( |
30 |
| - "Owin.Security.OpenIdConnect.Server.OpenIdConnectServerMiddleware", |
31 |
| - "ASOS", "Access_Token", "v1")); |
32 |
| - } |
33 |
| - |
34 |
| - if (options.Logger == null) { |
35 |
| - options.Logger = app.CreateLogger<OAuthValidationMiddleware>(); |
36 |
| - } |
37 |
| - } |
38 |
| - |
39 |
| - protected override AuthenticationHandler<OAuthValidationOptions> CreateHandler() { |
40 |
| - return new OAuthValidationHandler(); |
41 |
| - } |
42 |
| - |
43 |
| - internal sealed class EnhancedTicketDataFormat : SecureDataFormat<AuthenticationTicket> { |
44 |
| - private static readonly EnhancedTicketSerializer Serializer = new EnhancedTicketSerializer(); |
45 |
| - |
46 |
| - public EnhancedTicketDataFormat(IDataProtector protector) |
47 |
| - : base(Serializer, protector, TextEncodings.Base64Url) { |
48 |
| - } |
49 |
| - |
50 |
| - private sealed class EnhancedTicketSerializer : IDataSerializer<AuthenticationTicket> { |
51 |
| - private const int FormatVersion = 3; |
52 |
| - |
53 |
| - public byte[] Serialize(AuthenticationTicket model) { |
54 |
| - if (model == null) { |
55 |
| - throw new ArgumentNullException("model"); |
56 |
| - } |
57 |
| - |
58 |
| - using (var buffer = new MemoryStream()) |
59 |
| - using (var writer = new BinaryWriter(buffer)) { |
60 |
| - writer.Write(FormatVersion); |
61 |
| - |
62 |
| - WriteIdentity(writer, model.Identity); |
63 |
| - PropertiesSerializer.Write(writer, model.Properties); |
64 |
| - |
65 |
| - return buffer.ToArray(); |
66 |
| - } |
67 |
| - } |
68 |
| - |
69 |
| - public AuthenticationTicket Deserialize(byte[] data) { |
70 |
| - if (data == null) { |
71 |
| - throw new ArgumentNullException("data"); |
72 |
| - } |
73 |
| - |
74 |
| - using (var buffer = new MemoryStream(data)) |
75 |
| - using (var reader = new BinaryReader(buffer)) { |
76 |
| - if (reader.ReadInt32() != FormatVersion) { |
77 |
| - return null; |
78 |
| - } |
79 |
| - |
80 |
| - var identity = ReadIdentity(reader); |
81 |
| - var properties = PropertiesSerializer.Read(reader); |
82 |
| - |
83 |
| - return new AuthenticationTicket(identity, properties); |
84 |
| - } |
85 |
| - } |
86 |
| - |
87 |
| - private static void WriteIdentity(BinaryWriter writer, ClaimsIdentity identity) { |
88 |
| - writer.Write(identity.AuthenticationType); |
89 |
| - WriteWithDefault(writer, identity.NameClaimType, DefaultValues.NameClaimType); |
90 |
| - WriteWithDefault(writer, identity.RoleClaimType, DefaultValues.RoleClaimType); |
91 |
| - writer.Write(identity.Claims.Count()); |
92 |
| - |
93 |
| - foreach (var claim in identity.Claims) { |
94 |
| - WriteClaim(writer, claim, identity.NameClaimType); |
95 |
| - } |
96 |
| - |
97 |
| - var context = identity.BootstrapContext as BootstrapContext; |
98 |
| - if (context == null || string.IsNullOrEmpty(context.Token)) { |
99 |
| - writer.Write(0); |
100 |
| - } |
| 20 | + if (options.DataProtectionProvider == null) { |
| 21 | + // Create a new DI container and register |
| 22 | + // the data protection services. |
| 23 | + var services = new ServiceCollection(); |
101 | 24 |
|
102 |
| - else { |
103 |
| - writer.Write(context.Token.Length); |
104 |
| - writer.Write(context.Token); |
105 |
| - } |
106 |
| - |
107 |
| - if (identity.Actor != null) { |
108 |
| - writer.Write(true); |
109 |
| - WriteIdentity(writer, identity.Actor); |
110 |
| - } |
111 |
| - |
112 |
| - else { |
113 |
| - writer.Write(false); |
114 |
| - } |
115 |
| - } |
116 |
| - |
117 |
| - private static ClaimsIdentity ReadIdentity(BinaryReader reader) { |
118 |
| - var authenticationType = reader.ReadString(); |
119 |
| - var nameClaimType = ReadWithDefault(reader, DefaultValues.NameClaimType); |
120 |
| - var roleClaimType = ReadWithDefault(reader, DefaultValues.RoleClaimType); |
121 |
| - var count = reader.ReadInt32(); |
122 |
| - |
123 |
| - var claims = new Claim[count]; |
124 |
| - |
125 |
| - for (int index = 0; index != count; ++index) { |
126 |
| - claims[index] = ReadClaim(reader, nameClaimType); |
127 |
| - } |
128 |
| - |
129 |
| - var identity = new ClaimsIdentity(claims, authenticationType, nameClaimType, roleClaimType); |
130 |
| - |
131 |
| - int bootstrapContextSize = reader.ReadInt32(); |
132 |
| - if (bootstrapContextSize > 0) { |
133 |
| - identity.BootstrapContext = new BootstrapContext(reader.ReadString()); |
134 |
| - } |
135 |
| - |
136 |
| - if (reader.ReadBoolean()) { |
137 |
| - identity.Actor = ReadIdentity(reader); |
138 |
| - } |
139 |
| - |
140 |
| - return identity; |
141 |
| - } |
142 |
| - |
143 |
| - private static void WriteClaim(BinaryWriter writer, Claim claim, string nameClaimType) { |
144 |
| - WriteWithDefault(writer, claim.Type, nameClaimType); |
145 |
| - writer.Write(claim.Value); |
146 |
| - WriteWithDefault(writer, claim.ValueType, DefaultValues.StringValueType); |
147 |
| - WriteWithDefault(writer, claim.Issuer, DefaultValues.LocalAuthority); |
148 |
| - WriteWithDefault(writer, claim.OriginalIssuer, claim.Issuer); |
149 |
| - writer.Write(claim.Properties.Count); |
| 25 | + services.AddDataProtection(configuration => { |
| 26 | + // Try to use the application name provided by |
| 27 | + // the OWIN host as the application discriminator. |
| 28 | + var discriminator = new AppProperties(app.Properties).AppName; |
150 | 29 |
|
151 |
| - foreach (var property in claim.Properties) { |
152 |
| - writer.Write(property.Key); |
153 |
| - writer.Write(property.Value); |
| 30 | + // When an application discriminator cannot be resolved from |
| 31 | + // the OWIN host properties, generate a temporary identifier. |
| 32 | + if (string.IsNullOrEmpty(discriminator)) { |
| 33 | + discriminator = Guid.NewGuid().ToString(); |
154 | 34 | }
|
155 |
| - } |
156 | 35 |
|
157 |
| - private static Claim ReadClaim(BinaryReader reader, string nameClaimType) { |
158 |
| - var type = ReadWithDefault(reader, nameClaimType); |
159 |
| - var value = reader.ReadString(); |
160 |
| - var valueType = ReadWithDefault(reader, DefaultValues.StringValueType); |
161 |
| - var issuer = ReadWithDefault(reader, DefaultValues.LocalAuthority); |
162 |
| - var originalIssuer = ReadWithDefault(reader, issuer); |
163 |
| - var count = reader.ReadInt32(); |
| 36 | + configuration.ApplicationDiscriminator = discriminator; |
| 37 | + }); |
164 | 38 |
|
165 |
| - var claim = new Claim(type, value, valueType, issuer, originalIssuer); |
| 39 | + var container = services.BuildServiceProvider(); |
166 | 40 |
|
167 |
| - for (var index = 0; index != count; ++index) { |
168 |
| - claim.Properties.Add(key: reader.ReadString(), value: reader.ReadString()); |
169 |
| - } |
170 |
| - |
171 |
| - return claim; |
172 |
| - } |
173 |
| - |
174 |
| - private static void WriteWithDefault(BinaryWriter writer, string value, string defaultValue) { |
175 |
| - if (string.Equals(value, defaultValue, StringComparison.Ordinal)) { |
176 |
| - writer.Write(DefaultValues.DefaultStringPlaceholder); |
177 |
| - } |
178 |
| - |
179 |
| - else { |
180 |
| - writer.Write(value); |
181 |
| - } |
182 |
| - } |
| 41 | + // Resolve a data protection provider from the services container. |
| 42 | + options.DataProtectionProvider = container.GetRequiredService<IDataProtectionProvider>(); |
| 43 | + } |
183 | 44 |
|
184 |
| - private static string ReadWithDefault(BinaryReader reader, string defaultValue) { |
185 |
| - string value = reader.ReadString(); |
186 |
| - if (string.Equals(value, DefaultValues.DefaultStringPlaceholder, StringComparison.Ordinal)) { |
187 |
| - return defaultValue; |
188 |
| - } |
| 45 | + if (options.AccessTokenFormat == null) { |
| 46 | + // Note: the following purposes must match the ones used by ASOS. |
| 47 | + var protector = options.DataProtectionProvider.CreateProtector( |
| 48 | + "OpenIdConnectServerMiddleware", "ASOS", "Access_Token", "v1"); |
189 | 49 |
|
190 |
| - return value; |
191 |
| - } |
| 50 | + options.AccessTokenFormat = new AspNetTicketDataFormat(new DataProtectorShim(protector)); |
| 51 | + } |
192 | 52 |
|
193 |
| - private static class DefaultValues { |
194 |
| - public const string DefaultStringPlaceholder = "\0"; |
195 |
| - public const string NameClaimType = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name"; |
196 |
| - public const string RoleClaimType = "http://schemas.microsoft.com/ws/2008/06/identity/claims/role"; |
197 |
| - public const string LocalAuthority = "LOCAL AUTHORITY"; |
198 |
| - public const string StringValueType = "http://www.w3.org/2001/XMLSchema#string"; |
199 |
| - } |
| 53 | + if (options.Logger == null) { |
| 54 | + options.Logger = app.CreateLogger<OAuthValidationMiddleware>(); |
200 | 55 | }
|
201 | 56 | }
|
| 57 | + |
| 58 | + protected override AuthenticationHandler<OAuthValidationOptions> CreateHandler() { |
| 59 | + return new OAuthValidationHandler(); |
| 60 | + } |
202 | 61 | }
|
203 | 62 | }
|
0 commit comments