Repository navigation
Expand file tree
/
Copy pathOpenIdConnectConfigurationTests.cs
More file actions
156 lines (145 loc) · 5.97 KB
/
Copy pathOpenIdConnectConfigurationTests.cs
File metadata and controls
156 lines (145 loc) · 5.97 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// 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.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
namespace Microsoft.AspNetCore.Authentication.Test.OpenIdConnect
{
public class OpenIdConnectConfigurationTests
{
[Fact]
public async Task MetadataAddressIsGeneratedFromAuthorityWhenMissing()
{
var builder = new WebHostBuilder()
.ConfigureServices(services =>
{
services.AddAuthentication()
.AddCookie()
.AddOpenIdConnect(o =>
{
o.Authority = TestServerBuilder.DefaultAuthority;
o.ClientId = Guid.NewGuid().ToString();
o.SignInScheme = Guid.NewGuid().ToString();
});
})
.Configure(app =>
{
app.UseAuthentication();
app.Run(async context =>
{
var resolver = context.RequestServices.GetRequiredService<IAuthenticationHandlerProvider>();
var handler = await resolver.GetHandlerAsync(context, OpenIdConnectDefaults.AuthenticationScheme) as OpenIdConnectHandler;
Assert.Equal($"{TestServerBuilder.DefaultAuthority}/.well-known/openid-configuration", handler.Options.MetadataAddress);
});
});
var server = new TestServer(builder);
var transaction = await server.SendAsync(@"https://example.com");
Assert.Equal(HttpStatusCode.OK, transaction.Response.StatusCode);
}
[Fact]
public Task ThrowsWhenSignInSchemeIsSetToSelf()
{
return TestConfigurationException<InvalidOperationException>(
o =>
{
o.SignInScheme = OpenIdConnectDefaults.AuthenticationScheme;
o.Authority = TestServerBuilder.DefaultAuthority;
o.ClientId = "Test Id";
o.ClientSecret = "Test Secret";
},
ex => Assert.Contains("cannot be set to itself", ex.Message));
}
[Fact]
public Task ThrowsWhenClientIdIsMissing()
{
return TestConfigurationException<ArgumentException>(
o =>
{
o.SignInScheme = "TestScheme";
o.Authority = TestServerBuilder.DefaultAuthority;
},
ex => Assert.Equal("ClientId", ex.ParamName));
}
[Fact]
public Task ThrowsWhenAuthorityIsMissing()
{
return TestConfigurationException<InvalidOperationException>(
o =>
{
o.SignInScheme = "TestScheme";
o.ClientId = "Test Id";
o.CallbackPath = "/";
},
ex => Assert.Equal("Provide Authority, MetadataAddress, Configuration, or ConfigurationManager to OpenIdConnectOptions", ex.Message)
);
}
[Fact]
public Task ThrowsWhenAuthorityIsNotHttps()
{
return TestConfigurationException<InvalidOperationException>(
o =>
{
o.SignInScheme = "TestScheme";
o.ClientId = "Test Id";
o.MetadataAddress = "http://example.com";
o.CallbackPath = "/";
},
ex => Assert.Equal("The MetadataAddress or Authority must use HTTPS unless disabled for development by setting RequireHttpsMetadata=false.", ex.Message)
);
}
[Fact]
public Task ThrowsWhenMetadataAddressIsNotHttps()
{
return TestConfigurationException<InvalidOperationException>(
o =>
{
o.SignInScheme = "TestScheme";
o.ClientId = "Test Id";
o.MetadataAddress = "http://example.com";
o.CallbackPath = "/";
},
ex => Assert.Equal("The MetadataAddress or Authority must use HTTPS unless disabled for development by setting RequireHttpsMetadata=false.", ex.Message)
);
}
[Fact]
public Task ThrowsWhenMaxAgeIsNegative()
{
return TestConfigurationException<ArgumentOutOfRangeException>(
o =>
{
o.SignInScheme = "TestScheme";
o.ClientId = "Test Id";
o.Authority = TestServerBuilder.DefaultAuthority;
o.MaxAge = TimeSpan.FromSeconds(-1);
},
ex => Assert.StartsWith("The value must not be a negative TimeSpan.", ex.Message)
);
}
private TestServer BuildTestServer(Action<OpenIdConnectOptions> options)
{
var builder = new WebHostBuilder()
.ConfigureServices(services =>
{
services.AddAuthentication()
.AddCookie()
.AddOpenIdConnect(options);
})
.Configure(app => app.UseAuthentication());
return new TestServer(builder);
}
private async Task TestConfigurationException<T>(
Action<OpenIdConnectOptions> options,
Action<T> verifyException)
where T : Exception
{
var exception = await Assert.ThrowsAsync<T>(() => BuildTestServer(options).SendAsync(@"https://example.com"));
verifyException(exception);
}
}
}