This repository has been archived by the owner on Mar 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathIApplicationBuilderExtensions.cs
98 lines (79 loc) · 3.36 KB
/
IApplicationBuilderExtensions.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
#if NETCOREAPP2_1
using System.Collections.Generic;
using System.Security.Claims;
using System.Linq;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
namespace RimDev.Stuntman.Core
{
public static class IApplicationBuilderExtensions
{
/// <summary>
/// Use Stuntman on this application.
/// </summary>
public static void UseStuntman(this IApplicationBuilder app, StuntmanOptions options)
{
app.UseAuthentication();
if (options.AllowCookieAuthentication)
{
app.Map(options.SignInUri, signin =>
{
signin.Use(async (context, next) =>
{
var claims = new List<Claim>();
var overrideUserId = context.Request.Query[Constants.StuntmanOptions.OverrideQueryStringKey];
if (string.IsNullOrWhiteSpace(overrideUserId))
{
await next.Invoke();
IAppBuilderShared.ShowLoginUI(context, options);
}
else
{
var user = options.Users
.Where(x => x.Id == overrideUserId)
.FirstOrDefault();
if (user == null)
{
context.Response.StatusCode = 404;
await context.Response.WriteAsync(
$"options provided does not include the requested '{overrideUserId}' user.");
return;
}
claims.Add(new Claim(ClaimTypes.Name, user.Name));
claims.AddRange(user.Claims);
var identity = new ClaimsIdentity(claims, Constants.StuntmanAuthenticationType, user.NameClaimType, user.RoleClaimType);
await context.SignInAsync(Constants.StuntmanAuthenticationType, new ClaimsPrincipal(identity));
await next.Invoke();
}
});
IAppBuilderShared.RedirectToReturnUrl(signin);
});
app.Map(options.SignOutUri, signout =>
{
signout.Use(async (context, next) =>
{
await context.SignOutAsync(Constants.StuntmanAuthenticationType);
await next.Invoke();
});
IAppBuilderShared.RedirectToReturnUrl(signout);
});
}
if (options.ServerEnabled)
{
app.Map(options.ServerUri, server =>
{
server.Use(async (context, next) =>
{
var response = new StuntmanServerResponse { Users = options.Users };
var json = JsonConvert.SerializeObject(response);
context.Response.ContentType = "application/json";
await context.Response.WriteAsync(json);
});
});
}
}
}
}
#endif