-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathWhen_authentication_is_enabled.cs
More file actions
198 lines (171 loc) · 6.88 KB
/
Copy pathWhen_authentication_is_enabled.cs
File metadata and controls
198 lines (171 loc) · 6.88 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
namespace ServiceControl.Audit.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, API endpoints should require a valid JWT Bearer token
/// unless marked with [AllowAnonymous].
///
/// 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";
[SetUp]
public void ConfigureAuth()
{
// Start mock OIDC server
mockOidcServer = new MockOidcServer(audience: TestAudience);
mockOidcServer.Start();
configuration = new OpenIdConnectTestConfiguration(ServiceControlInstanceType.Audit)
.WithConfigurationValidationDisabled()
.WithAuthenticationEnabled()
.WithRoleBasedAuthorizationEnabled()
.WithAuthority(mockOidcServer.Authority)
.WithAudience(TestAudience)
.WithRequireHttpsMetadata(false);
}
[TearDown]
public void CleanupAuth()
{
configuration?.Dispose();
mockOidcServer?.Dispose();
}
[Test]
public async Task Should_reject_requests_without_bearer_token()
{
HttpResponseMessage response = null;
_ = await Define<Context>()
.Done(async ctx =>
{
// Use /api/messages which does NOT have [AllowAnonymous] so it should require authentication
response = await OpenIdConnectAssertions.SendRequestWithoutAuth(
HttpClient,
HttpMethod.Get,
"/api/messages");
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/messages",
"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 audit:message:view
// required by /api/messages. 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/messages",
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/messages",
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/messages",
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/messages",
wrongIssuerToken);
return response != null;
})
.Run();
OpenIdConnectAssertions.AssertUnauthorized(response);
}
[Test]
public async Task Should_allow_anonymous_access_to_root_endpoint()
{
HttpResponseMessage response = null;
_ = await Define<Context>()
.Done(async ctx =>
{
// The /api endpoint is marked [AllowAnonymous] for discovery
response = await OpenIdConnectAssertions.SendRequestWithoutAuth(
HttpClient,
HttpMethod.Get,
"/api");
return response != null;
})
.Run();
OpenIdConnectAssertions.AssertNoAuthenticationRequired(response);
}
class Context : ScenarioContext;
}
}