Repository navigation
Expand file tree
/
Copy pathTestServerBuilder.cs
More file actions
132 lines (118 loc) · 5.77 KB
/
Copy pathTestServerBuilder.cs
File metadata and controls
132 lines (118 loc) · 5.77 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
// 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.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Authentication;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Protocols;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
namespace Microsoft.AspNetCore.Authentication.Tests.OpenIdConnect
{
internal class TestServerBuilder
{
public static readonly string DefaultAuthority = @"https://login.microsoftonline.com/common";
public static readonly string TestHost = @"https://example.com";
public static readonly string Challenge = "/challenge";
public static readonly string ChallengeWithOutContext = "/challengeWithOutContext";
public static readonly string ChallengeWithProperties = "/challengeWithProperties";
public static readonly string Signin = "/signin";
public static readonly string Signout = "/signout";
public static OpenIdConnectOptions CreateOpenIdConnectOptions() =>
new OpenIdConnectOptions
{
Authority = DefaultAuthority,
ClientId = Guid.NewGuid().ToString(),
Configuration = CreateDefaultOpenIdConnectConfiguration()
};
public static OpenIdConnectOptions CreateOpenIdConnectOptions(Action<OpenIdConnectOptions> update)
{
var options = CreateOpenIdConnectOptions();
if (update != null)
{
update(options);
}
return options;
}
public static OpenIdConnectConfiguration CreateDefaultOpenIdConnectConfiguration() =>
new OpenIdConnectConfiguration()
{
AuthorizationEndpoint = DefaultAuthority + "/oauth2/authorize",
EndSessionEndpoint = DefaultAuthority + "/oauth2/endsessionendpoint",
TokenEndpoint = DefaultAuthority + "/oauth2/token"
};
public static IConfigurationManager<OpenIdConnectConfiguration> CreateDefaultOpenIdConnectConfigurationManager() =>
new StaticConfigurationManager<OpenIdConnectConfiguration>(CreateDefaultOpenIdConnectConfiguration());
public static TestServer CreateServer(OpenIdConnectOptions options)
{
return CreateServer(options, handler: null, properties: null);
}
public static TestServer CreateServer(
OpenIdConnectOptions options,
Func<HttpContext, Task> handler,
AuthenticationProperties properties)
{
var builder = new WebHostBuilder()
.Configure(app =>
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationScheme = CookieAuthenticationDefaults.AuthenticationScheme
});
app.UseOpenIdConnectAuthentication(options);
app.Use(async (context, next) =>
{
var req = context.Request;
var res = context.Response;
if (req.Path == new PathString(Challenge))
{
await context.Authentication.ChallengeAsync(OpenIdConnectDefaults.AuthenticationScheme);
}
else if (req.Path == new PathString(ChallengeWithProperties))
{
await context.Authentication.ChallengeAsync(OpenIdConnectDefaults.AuthenticationScheme, properties);
}
else if (req.Path == new PathString(ChallengeWithOutContext))
{
res.StatusCode = 401;
}
else if (req.Path == new PathString(Signin))
{
// REVIEW: this used to just be res.SignIn()
await context.Authentication.SignInAsync(OpenIdConnectDefaults.AuthenticationScheme, new ClaimsPrincipal());
}
else if (req.Path == new PathString(Signout))
{
await context.Authentication.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme);
}
else if (req.Path == new PathString("/signout_with_specific_redirect_uri"))
{
await context.Authentication.SignOutAsync(
OpenIdConnectDefaults.AuthenticationScheme,
new AuthenticationProperties() { RedirectUri = "http://www.example.com/specific_redirect_uri" });
}
else if (handler != null)
{
await handler(context);
}
else
{
await next();
}
});
})
.ConfigureServices(services =>
{
services.AddAuthentication();
services.Configure<SharedAuthenticationOptions>(authOptions => authOptions.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme);
});
return new TestServer(builder);
}
}
}