This repository has been archived by the owner on Sep 18, 2021. It is now read-only.
/
InternalOwinExtensions.cs
537 lines (430 loc) · 21.3 KB
/
InternalOwinExtensions.cs
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/*
* Copyright 2014, 2015 Dominick Baier, Brock Allen
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using Autofac;
using Autofac.Integration.Owin;
using IdentityServer3.Core.Configuration;
using IdentityServer3.Core.Configuration.Hosting;
using IdentityServer3.Core.Models;
using IdentityServer3.Core.Services;
using Microsoft.Owin;
using Microsoft.Owin.Security;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
namespace IdentityServer3.Core.Extensions
{
internal static class InternalOwinExtensions
{
public static string GetRequestId(this IOwinContext context)
{
if (context == null) throw new ArgumentNullException("context");
return context.Environment.GetRequestId();
}
public static void SetRequestId(this IDictionary<string, object> env, string id)
{
if (env == null) throw new ArgumentNullException("env");
env[Constants.OwinEnvironment.RequestId] = id;
}
public static void SetIdentityServerHost(this IDictionary<string, object> env, string value)
{
if (env == null) throw new ArgumentNullException("env");
env[Constants.OwinEnvironment.IdentityServerHost] = value;
}
public static void SetIdentityServerBasePath(this IDictionary<string, object> env, string value)
{
if (env == null) throw new ArgumentNullException("env");
env[Constants.OwinEnvironment.IdentityServerBasePath] = value;
}
public static T ResolveDependency<T>(this IOwinContext context)
{
if (context == null) throw new ArgumentNullException("context");
return (T)context.ResolveDependency(typeof(T));
}
public static object ResolveDependency(this IOwinContext context, Type type)
{
if (context == null) throw new ArgumentNullException("context");
var scope = context.GetAutofacLifetimeScope();
return scope.ResolveOptional(type);
}
public static IEnumerable<AuthenticationDescription> GetExternalAuthenticationProviders(this IOwinContext context, IEnumerable<string> filter = null)
{
if (context == null) throw new ArgumentNullException("context");
var types = context.Authentication.GetAuthenticationTypes().Where(x => !Constants.IdentityServerAuthenticationTypes.Contains(x.AuthenticationType));
if (filter != null && filter.Any())
{
types = types.Where(x => filter.Contains(x.AuthenticationType));
}
return types;
}
public static bool IsValidExternalAuthenticationProvider(this IOwinContext context, string name)
{
if (context == null) throw new ArgumentNullException("context");
return context.Authentication.GetAuthenticationTypes().Any(x => x.AuthenticationType == name);
}
public static IEnumerable<LoginPageLink> GetLinksFromProviders(this IOwinContext context, IEnumerable<AuthenticationDescription> types, string signInMessageId)
{
if (context == null) throw new ArgumentNullException("context");
if (types != null)
{
return types.Select(p => new LoginPageLink
{
Type = p.AuthenticationType,
Text = p.Caption,
Href = context.GetExternalProviderLoginUrl(p.AuthenticationType, signInMessageId)
});
}
return Enumerable.Empty<LoginPageLink>();
}
public static IEnumerable<LoginPageLink> FilterHiddenLinks(this IEnumerable<LoginPageLink> links)
{
if (links == null) throw new ArgumentNullException("links");
return links.Where(x => x.Text.IsPresent());
}
public static async Task<Microsoft.Owin.Security.AuthenticateResult> GetAuthenticationFrom(this IOwinContext context, string authenticationType)
{
if (context == null) throw new ArgumentNullException("context");
if (authenticationType.IsMissing()) throw new ArgumentNullException("authenticationType");
return await context.Authentication.AuthenticateAsync(authenticationType);
}
public static async Task<ClaimsIdentity> GetIdentityFrom(this IOwinContext context, string authenticationType)
{
if (context == null) throw new ArgumentNullException("context");
var result = await context.GetAuthenticationFrom(authenticationType);
if (result != null &&
result.Identity != null &&
result.Identity.IsAuthenticated)
{
return result.Identity;
}
return null;
}
public static async Task<ClaimsIdentity> GetIdentityFromPartialSignIn(this IOwinContext context)
{
if (context == null) throw new ArgumentNullException("context");
return await context.GetIdentityFrom(Constants.PartialSignInAuthenticationType);
}
public static async Task<bool?> GetPartialLoginRememberMeAsync(this IOwinContext context)
{
if (context == null) throw new ArgumentNullException("context");
var result = await context.Authentication.AuthenticateAsync(Constants.PartialSignInAuthenticationType);
if (result == null || result.Identity == null || result.Identity.IsAuthenticated == false)
{
throw new Exception("No partial login");
}
if (result.Properties.Dictionary.ContainsKey(Constants.Authentication.PartialLoginRememberMe))
{
return "true".Equals(result.Properties.Dictionary[Constants.Authentication.PartialLoginRememberMe]);
}
return null;
}
public static async Task<ClaimsIdentity> GetIdentityFromExternalSignIn(this IOwinContext context)
{
if (context == null) throw new ArgumentNullException("context");
return await context.GetIdentityFrom(Constants.ExternalAuthenticationType);
}
public static async Task<string> GetSignInIdFromExternalProvider(this IOwinContext context)
{
if (context == null) throw new ArgumentNullException("context");
var result = await context.GetAuthenticationFrom(Constants.ExternalAuthenticationType);
if (result != null)
{
string val;
if (result.Properties.Dictionary.TryGetValue(Constants.Authentication.SigninId, out val))
{
return val;
}
}
return null;
}
public static async Task<ClaimsIdentity> GetIdentityFromExternalProvider(this IOwinContext context)
{
if (context == null) throw new ArgumentNullException("context");
var id = await context.GetIdentityFromExternalSignIn();
if (id != null)
{
// this is mapping from the external IdP's issuer to the name of the
// katana middleware that's registered in startup
var result = await context.GetAuthenticationFrom(Constants.ExternalAuthenticationType);
if (!result.Properties.Dictionary.Keys.Contains(Constants.Authentication.KatanaAuthenticationType))
{
throw new InvalidOperationException("Missing KatanaAuthenticationType");
}
var provider = result.Properties.Dictionary[Constants.Authentication.KatanaAuthenticationType];
var newClaims = id.Claims.Select(x => new Claim(x.Type, x.Value, x.ValueType, provider));
id = new ClaimsIdentity(newClaims, id.AuthenticationType);
}
return id;
}
public static string GetCspReportUrl(this IOwinContext context)
{
if (context == null) throw new ArgumentNullException("context");
return context.Environment.GetIdentityServerBaseUrl() + Constants.RoutePaths.CspReport;
}
public static string GetPartialLoginRestartUrl(this IOwinContext context, string signinId)
{
if (context == null) throw new ArgumentNullException("context");
if (String.IsNullOrWhiteSpace(signinId)) throw new ArgumentNullException("signinId");
return context.Environment.GetIdentityServerBaseUrl() + Constants.RoutePaths.Login + "?signin=" + signinId;
}
public static string GetPartialLoginResumeUrl(this IOwinContext context, string resumeId)
{
if (context == null) throw new ArgumentNullException("context");
if (String.IsNullOrWhiteSpace(resumeId)) throw new ArgumentNullException("resumeId");
return context.Environment.GetIdentityServerBaseUrl() + Constants.RoutePaths.ResumeLoginFromRedirect + "?resume=" + resumeId;
}
public static string GetPermissionsPageUrl(this IOwinContext context)
{
if (context == null) throw new ArgumentNullException("context");
return context.Environment.GetIdentityServerBaseUrl() + Constants.RoutePaths.ClientPermissions;
}
public static string GetExternalProviderLoginUrl(this IOwinContext context, string provider, string signinId)
{
if (context == null) throw new ArgumentNullException("context");
if (String.IsNullOrWhiteSpace(provider)) throw new ArgumentNullException("provider");
if (String.IsNullOrWhiteSpace(signinId)) throw new ArgumentNullException("signinId");
return context.Environment.GetIdentityServerBaseUrl() + Constants.RoutePaths.LoginExternal + "?provider=" + provider + "&signin=" + signinId;
}
public static string GetIdentityServerHost(this IOwinContext context)
{
if (context == null) throw new ArgumentNullException("context");
return context.Environment.GetIdentityServerHost();
}
public static string GetIdentityServerBasePath(this IOwinContext context)
{
if (context == null) throw new ArgumentNullException("context");
return context.Environment.GetIdentityServerBasePath();
}
public static string GetIdentityServerBaseUrl(this IOwinContext context)
{
if (context == null) throw new ArgumentNullException("context");
return context.Environment.GetIdentityServerBaseUrl();
}
public static string GetIdentityServerIssuerUri(this IOwinContext context)
{
if (context == null) throw new ArgumentNullException("context");
return context.Environment.GetIdentityServerIssuerUri();
}
public static string GetIdentityServerLogoutUrl(this IOwinContext context)
{
if (context == null) throw new ArgumentNullException("context");
return context.Environment.GetIdentityServerLogoutUrl();
}
public static string GetCurrentUserDisplayName(this IOwinContext context)
{
if (context == null) throw new ArgumentNullException("context");
if (context.Authentication.User != null &&
context.Authentication.User.Identity != null)
{
return context.Authentication.User.Identity.Name;
}
return null;
}
public static string CreateSignInRequest(this IOwinContext context, SignInMessage message)
{
if (context == null) throw new ArgumentNullException("context");
return context.Environment.CreateSignInRequest(message);
}
public static bool IsFormData(this IOwinRequest request)
{
if (request == null) throw new ArgumentNullException("request");
return request.Method == "POST" &&
String.Equals(request.ContentType, "application/x-www-form-urlencoded", StringComparison.OrdinalIgnoreCase);
}
public async static Task<IFormCollection> ReadRequestFormAsync(this IOwinContext context)
{
if (context == null) throw new ArgumentNullException("context");
// hack to clear a possible cached type from Katana in environment
context.Environment.Remove("Microsoft.Owin.Form#collection");
if (!context.Request.Body.CanSeek)
{
var copy = new MemoryStream();
await context.Request.Body.CopyToAsync(copy);
copy.Seek(0L, SeekOrigin.Begin);
context.Request.Body = copy;
}
context.Request.Body.Seek(0L, SeekOrigin.Begin);
var form = await context.Request.ReadFormAsync();
context.Request.Body.Seek(0L, SeekOrigin.Begin);
// hack to prevent caching of an internalized type from Katana in environment
context.Environment.Remove("Microsoft.Owin.Form#collection");
return form;
}
public async static Task<string> ReadBodyAsStringAsync(this IOwinRequest request)
{
if (request == null) throw new ArgumentNullException("request");
if (!request.Body.CanSeek)
{
var copy = new MemoryStream();
await request.Body.CopyToAsync(copy);
copy.Seek(0L, SeekOrigin.Begin);
request.Body = copy;
}
request.Body.Seek(0L, SeekOrigin.Begin);
string body = null;
using (var reader = new StreamReader(request.Body, Encoding.UTF8, true, 4096, true))
{
body = await reader.ReadToEndAsync();
}
request.Body.Seek(0L, SeekOrigin.Begin);
return body;
}
public async static Task<string> ReadBodyAsStringAsync(this IOwinResponse response)
{
if (response == null) throw new ArgumentNullException("response");
if (response.Body == null) return String.Empty;
if (response.Body.CanRead == false) return "can't read response body";
if (!response.Body.CanSeek)
{
var copy = new MemoryStream();
await response.Body.CopyToAsync(copy);
copy.Seek(0L, SeekOrigin.Begin);
response.Body = copy;
}
response.Body.Seek(0L, SeekOrigin.Begin);
string body = null;
using (var reader = new StreamReader(response.Body, Encoding.UTF8, true, 4096, true))
{
body = await reader.ReadToEndAsync();
}
response.Body.Seek(0L, SeekOrigin.Begin);
return body;
}
public async static Task<NameValueCollection> ReadRequestFormAsNameValueCollectionAsync(this IOwinContext context)
{
if (context == null) throw new ArgumentNullException("context");
var form = await context.ReadRequestFormAsync();
NameValueCollection nv = new NameValueCollection();
foreach (var item in form)
{
if (item.Value.Any())
{
nv.Add(item.Key, item.Value[0]);
}
}
return nv;
}
const string SignOutMessageCookieIdtoRemove = "ids:SignOutMessageCookieIdtoRemove";
public static void QueueRemovalOfSignOutMessageCookie(this IOwinContext context, string id)
{
if (context == null) throw new ArgumentNullException("context");
if (id != null)
{
context.Environment[SignOutMessageCookieIdtoRemove] = id;
}
}
public static void ProcessRemovalOfSignOutMessageCookie(this IOwinContext context, MessageCookie<SignOutMessage> signOutMessageCookie)
{
if (context == null) throw new ArgumentNullException("context");
if (signOutMessageCookie == null) throw new ArgumentNullException("signOutMessageCookie");
if (context.Response.StatusCode == 200 && context.Environment.ContainsKey(SignOutMessageCookieIdtoRemove))
{
signOutMessageCookie.Clear((string)context.Environment[SignOutMessageCookieIdtoRemove]);
}
}
const string QueueRenderLoggedOutPageFlag = "ids:QueueRenderLoggedOutPage";
public static void QueueRenderLoggedOutPage(this IOwinContext context, string signOutMessageId)
{
if (context == null) throw new ArgumentNullException("context");
context.Environment[QueueRenderLoggedOutPageFlag] = signOutMessageId;
}
public static bool ShouldRenderLoggedOutPage(this IOwinContext context)
{
if (context == null) throw new ArgumentNullException("context");
return context.Environment.ContainsKey(QueueRenderLoggedOutPageFlag);
}
public static void PrepareContextForLoggedOutPage(this IOwinContext context)
{
if (context == null) throw new ArgumentNullException("context");
context.Request.Method = "POST";
context.Request.ContentType = "application/x-www-form-urlencoded";
context.Request.Path = new PathString("/" + Constants.RoutePaths.Logout);
var signOutId = context.Environment[QueueRenderLoggedOutPageFlag];
if (signOutId != null)
{
context.Request.QueryString = new QueryString("id", (string)context.Environment[QueueRenderLoggedOutPageFlag]);
}
context.SetSuppressAntiForgeryCheck();
}
const string SuppressAntiForgeryCheck = "ids:SuppressAntiForgeryCheck";
public static void SetSuppressAntiForgeryCheck(this IOwinContext context)
{
if (context == null) throw new ArgumentNullException("context");
context.Environment[SuppressAntiForgeryCheck] = true;
}
public static bool GetSuppressAntiForgeryCheck(this IOwinContext context)
{
if (context == null) throw new ArgumentNullException("context");
return context.Environment.ContainsKey(SuppressAntiForgeryCheck) && true.Equals(context.Environment[SuppressAntiForgeryCheck]);
}
public static void ClearAuthenticationCookies(this IOwinContext context)
{
if (context == null) throw new ArgumentNullException("context");
context.Authentication.SignOut(
Constants.PrimaryAuthenticationType,
Constants.ExternalAuthenticationType,
Constants.PartialSignInAuthenticationType);
}
public static void SignOutOfExternalIdP(this IOwinContext context, string signOutId)
{
if (context == null) throw new ArgumentNullException("context");
// look for idp claim other than IdSvr
// if present, then signout of it
var user = context.Authentication.User;
if (user != null && user.Identity != null && user.Identity.IsAuthenticated)
{
var idp = user.GetIdentityProvider();
if (idp != Constants.BuiltInIdentityProvider)
{
var authProps = new AuthenticationProperties();
var options = context.ResolveDependency<IdentityServerOptions>();
if (options.AuthenticationOptions.EnableAutoCallbackForFederatedSignout)
{
authProps.RedirectUri = context.Environment.GetIdentityServerLogoutUrl().EnsureTrailingSlash();
if (signOutId != null)
{
authProps.RedirectUri = authProps.RedirectUri.AddQueryString(Constants.Authentication.SignoutId + "=" + signOutId);
}
}
context.Authentication.SignOut(authProps, idp);
}
}
}
public static async Task CallUserServiceSignOutAsync(this IOwinContext context, string clientId = null)
{
if (context == null) throw new ArgumentNullException("context");
var result = await context.Authentication.AuthenticateAsync(Constants.PrimaryAuthenticationType);
if (result != null)
{
var user = result.Identity;
if (user != null && user.IsAuthenticated)
{
var signOutContext = new SignOutContext
{
Subject = new ClaimsPrincipal(user),
ClientId = clientId
};
var userService = context.ResolveDependency<IUserService>();
await userService.SignOutAsync(signOutContext);
}
}
}
}
}