-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathWhen_authentication_is_enabled.cs
More file actions
210 lines (183 loc) · 7.59 KB
/
Copy pathWhen_authentication_is_enabled.cs
File metadata and controls
210 lines (183 loc) · 7.59 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
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
namespace ServiceControl.AcceptanceTests.Security.OpenIdConnect
{
using System.Net.Http;
using System.Security.Claims;
using System.Threading.Tasks;
using AcceptanceTesting;
using AcceptanceTesting.OpenIdConnect;
using NServiceBus.AcceptanceTesting;
using NUnit.Framework;
/// <summary>
/// Authentication Enabled
/// When Authentication.Enabled is true, the authentication configuration endpoint should
/// return the configured OIDC settings so that clients (like ServicePulse) can discover
/// how to authenticate.
///
/// This test uses a mock OIDC server to provide discovery endpoints and signing keys,
/// allowing full testing of the JWT Bearer authentication flow.
/// </summary>
class When_authentication_is_enabled : AcceptanceTest
{
OpenIdConnectTestConfiguration configuration;
MockOidcServer mockOidcServer;
const string TestAudience = "api://test-audience";
const string TestClientId = "test-client-id";
const string TestApiScopes = "api://test-audience/.default";
[SetUp]
public void ConfigureAuth()
{
// Start mock OIDC server
mockOidcServer = new MockOidcServer(audience: TestAudience);
mockOidcServer.Start();
configuration = new OpenIdConnectTestConfiguration(ServiceControlInstanceType.Primary)
.WithConfigurationValidationDisabled()
.WithAuthenticationEnabled()
.WithRoleBasedAuthorizationEnabled()
.WithAuthority(mockOidcServer.Authority)
.WithAudience(TestAudience)
.WithServicePulseClientId(TestClientId)
.WithServicePulseApiScopes(TestApiScopes)
.WithRequireHttpsMetadata(false);
}
[TearDown]
public void CleanupAuth()
{
configuration?.Dispose();
mockOidcServer?.Dispose();
}
[Test]
public async Task Should_return_authentication_configuration_with_enabled_true()
{
HttpResponseMessage response = null;
_ = await Define<Context>()
.Done(async ctx =>
{
// The authentication/configuration endpoint is marked [AllowAnonymous]
// so it should be accessible without authentication
response = await OpenIdConnectAssertions.SendRequestWithoutAuth(
HttpClient,
HttpMethod.Get,
"/api/authentication/configuration");
return response != null;
})
.Run();
await OpenIdConnectAssertions.AssertAuthConfigurationResponse(
response,
expectedEnabled: true,
expectedClientId: TestClientId,
expectedAudience: TestAudience,
expectedApiScopes: TestApiScopes);
}
[Test]
public async Task Should_reject_requests_without_bearer_token()
{
HttpResponseMessage response = null;
_ = await Define<Context>()
.Done(async ctx =>
{
// Use /api/errors which does NOT have [AllowAnonymous] so it should require authentication
// Note: /api is marked [AllowAnonymous] for server-to-server configuration fetching
response = await OpenIdConnectAssertions.SendRequestWithoutAuth(
HttpClient,
HttpMethod.Get,
"/api/errors");
return response != null;
})
.Run();
OpenIdConnectAssertions.AssertUnauthorized(response);
}
[Test]
public async Task Should_reject_requests_with_invalid_bearer_token()
{
HttpResponseMessage response = null;
_ = await Define<Context>()
.Done(async ctx =>
{
response = await OpenIdConnectAssertions.SendRequestWithBearerToken(
HttpClient,
HttpMethod.Get,
"/api/errors",
"invalid-token-value");
return response != null;
})
.Run();
OpenIdConnectAssertions.AssertUnauthorized(response);
}
[Test]
public async Task Should_accept_requests_with_valid_bearer_token()
{
HttpResponseMessage response = null;
_ = await Define<Context>()
.Done(async ctx =>
{
// The "reader" role grants every *:*:view permission, including error:messages:view
// required by /api/errors. Without a role-bearing claim the request would be 403.
var validToken = mockOidcServer.GenerateToken(
additionalClaims: new[] { new Claim("roles", "reader") });
response = await OpenIdConnectAssertions.SendRequestWithBearerToken(
HttpClient,
HttpMethod.Get,
"/api/errors",
validToken);
return response != null;
})
.Run();
OpenIdConnectAssertions.AssertAuthenticated(response);
}
[Test]
public async Task Should_reject_requests_with_expired_token()
{
HttpResponseMessage response = null;
_ = await Define<Context>()
.Done(async ctx =>
{
var expiredToken = mockOidcServer.GenerateExpiredToken();
response = await OpenIdConnectAssertions.SendRequestWithBearerToken(
HttpClient,
HttpMethod.Get,
"/api/errors",
expiredToken);
return response != null;
})
.Run();
OpenIdConnectAssertions.AssertUnauthorized(response);
}
[Test]
public async Task Should_reject_requests_with_wrong_audience()
{
HttpResponseMessage response = null;
_ = await Define<Context>()
.Done(async ctx =>
{
var wrongAudienceToken = mockOidcServer.GenerateTokenWithWrongAudience();
response = await OpenIdConnectAssertions.SendRequestWithBearerToken(
HttpClient,
HttpMethod.Get,
"/api/errors",
wrongAudienceToken);
return response != null;
})
.Run();
OpenIdConnectAssertions.AssertUnauthorized(response);
}
[Test]
public async Task Should_reject_requests_with_wrong_issuer()
{
HttpResponseMessage response = null;
_ = await Define<Context>()
.Done(async ctx =>
{
var wrongIssuerToken = mockOidcServer.GenerateTokenWithWrongIssuer();
response = await OpenIdConnectAssertions.SendRequestWithBearerToken(
HttpClient,
HttpMethod.Get,
"/api/errors",
wrongIssuerToken);
return response != null;
})
.Run();
OpenIdConnectAssertions.AssertUnauthorized(response);
}
class Context : ScenarioContext;
}
}