-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathBackchannelLogoutTests.cs
More file actions
596 lines (513 loc) · 26.5 KB
/
BackchannelLogoutTests.cs
File metadata and controls
596 lines (513 loc) · 26.5 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Auth0.AspNetCore.Authentication.Exceptions;
using Auth0.AspNetCore.Authentication.IntegrationTests.Builders;
using Auth0.AspNetCore.Authentication.IntegrationTests.Extensions;
using Auth0.AspNetCore.Authentication.IntegrationTests.Infrastructure;
using Auth0.AspNetCore.Authentication.IntegrationTests.Utils;
using FluentAssertions;
using Xunit;
namespace Auth0.AspNetCore.Authentication.IntegrationTests;
public class BackchannelLogoutTests
{
[Fact]
public async Task Should_Return_405_If_Not_Post()
{
var configuration = TestConfiguration.GetConfiguration();
var domain = configuration["Auth0:Domain"];
var clientId = configuration["Auth0:ClientId"];
var mockHandler = new OidcMockBuilder()
.MockOpenIdConfig()
.MockJwks()
.MockToken(() => new JwtTokenBuilder(1)
.WithIssuer($"https://{domain}/")
.WithAudience(clientId)
.Build())
.Build();
using var server = TestServerBuilder.CreateServer(opt =>
{
opt.Backchannel = new HttpClient(mockHandler.Object);
}, null, false, false, false, null, true);
using var client = server.CreateClient();
var res = await client.SendAsync($"{TestServerBuilder.Host}/backchannel-logout");
res.StatusCode.Should().Be((HttpStatusCode)405);
}
[Fact]
public async Task Should_return_400_when_not_form_urlencoded()
{
var configuration = TestConfiguration.GetConfiguration();
var domain = configuration["Auth0:Domain"];
var clientId = configuration["Auth0:ClientId"];
var mockHandler = new OidcMockBuilder()
.MockOpenIdConfig()
.MockJwks()
.MockToken(() => new JwtTokenBuilder(1)
.WithIssuer($"https://{domain}/")
.WithAudience(clientId)
.Build())
.Build();
using var server = TestServerBuilder.CreateServer(opt =>
{
opt.Backchannel = new HttpClient(mockHandler.Object);
}, null, false, false, false, null, true);
using var client = server.CreateClient();
var message = new HttpRequestMessage(HttpMethod.Post, $"{TestServerBuilder.Host}/backchannel-logout");
var response = await client.SendAsync(message);
var content = await response.Content.ReadAsStringAsync();
var error = ApiError.Parse(content);
response.StatusCode.Should().Be((HttpStatusCode)400);
error.Message.Should().Be("Only application/x-www-form-urlencoded is allowed.");
}
[Fact]
public async Task Should_return_400_when_no_logout_token()
{
var configuration = TestConfiguration.GetConfiguration();
var domain = configuration["Auth0:Domain"];
var clientId = configuration["Auth0:ClientId"];
var mockHandler = new OidcMockBuilder()
.MockOpenIdConfig()
.MockJwks()
.MockToken(() => new JwtTokenBuilder(1)
.WithIssuer($"https://{domain}/")
.WithAudience(clientId)
.Build())
.Build();
using var server = TestServerBuilder.CreateServer(opt =>
{
opt.Backchannel = new HttpClient(mockHandler.Object);
}, null, false, false, false, null, true);
using var client = server.CreateClient();
var formData = new Dictionary<string, string> { { "Foo", "Bar" } };
using var req = new HttpRequestMessage(HttpMethod.Post, $"{TestServerBuilder.Host}/backchannel-logout");
req.Content = new FormUrlEncodedContent(formData);
using var response = await client.SendAsync(req);
var content = await response.Content.ReadAsStringAsync();
var error = ApiError.Parse(content);
response.StatusCode.Should().Be((HttpStatusCode)400);
error.Message.Should().Be("Missing logout_token.");
}
[Fact]
public async Task Should_Validate_Signature_On_Backchannel_Logout()
{
var configuration = TestConfiguration.GetConfiguration();
var domain = configuration["Auth0:Domain"];
var clientId = configuration["Auth0:ClientId"];
var mockHandler = new OidcMockBuilder()
.MockOpenIdConfig()
.MockJwks()
.MockToken(() => new JwtTokenBuilder(1)
.WithIssuer($"https://{domain}/")
.WithAudience(clientId)
.Build())
.Build();
using var server = TestServerBuilder.CreateServer(opt =>
{
opt.Backchannel = new HttpClient(mockHandler.Object);
}, null, false, false, false, null, true);
using var client = server.CreateClient();
var logoutToken = new JwtTokenBuilder(1)
.WithIssuer($"https://{domain}/")
.WithAudience(clientId)
.WithClaim(JwtRegisteredClaimNames.Sid, "sid")
.WithClaim("events", "{ \"http://schemas.openid.net/event/backchannel-logout\": {} }" )
.SignWithRs256("Auth0.AspNetCore.Authentication.IntegrationTests.jwks2.json")
.Build();
var formData = new Dictionary<string, string> { { "logout_token", logoutToken } };
using var req = new HttpRequestMessage(HttpMethod.Post, $"{TestServerBuilder.Host}/backchannel-logout");
req.Content = new FormUrlEncodedContent(formData);
using var response = await client.SendAsync(req);
var content = await response.Content.ReadAsStringAsync();
var error = ApiError.Parse(content);
response.StatusCode.Should().Be((HttpStatusCode)400);
error.Message.Should().Contain("Signature validation failed.");
}
[Fact]
public async Task Should_Validate_Issuer_On_Backchannel_Logout()
{
var configuration = TestConfiguration.GetConfiguration();
var domain = configuration["Auth0:Domain"];
var clientId = configuration["Auth0:ClientId"];
var mockHandler = new OidcMockBuilder()
.MockOpenIdConfig()
.MockJwks()
.MockToken(() => new JwtTokenBuilder(1)
.WithIssuer($"https://{domain}/")
.WithAudience(clientId)
.Build())
.Build();
using var server = TestServerBuilder.CreateServer(opt =>
{
opt.Backchannel = new HttpClient(mockHandler.Object);
}, null, false, false, false, null, true);
using var client = server.CreateClient();
var logoutToken = new JwtTokenBuilder(1)
.WithIssuer($"https://bad_issuer/")
.WithAudience(clientId)
.WithClaim(JwtRegisteredClaimNames.Sid, "sid")
.WithClaim("events", "{ \"http://schemas.openid.net/event/backchannel-logout\": {} }" )
.Build();
var formData = new Dictionary<string, string> { { "logout_token", logoutToken } };
using var req = new HttpRequestMessage(HttpMethod.Post, $"{TestServerBuilder.Host}/backchannel-logout");
req.Content = new FormUrlEncodedContent(formData);
using var response = await client.SendAsync(req);
var content = await response.Content.ReadAsStringAsync();
var error = ApiError.Parse(content);
response.StatusCode.Should().Be((HttpStatusCode)400);
error.Message.Should().Contain("Issuer validation failed.");
}
[Fact]
public async Task Should_Validate_Audience_On_Backchannel_Logout()
{
var configuration = TestConfiguration.GetConfiguration();
var domain = configuration["Auth0:Domain"];
var clientId = configuration["Auth0:ClientId"];
var mockHandler = new OidcMockBuilder()
.MockOpenIdConfig()
.MockJwks()
.MockToken(() => new JwtTokenBuilder(1)
.WithIssuer($"https://{domain}/")
.WithAudience(clientId)
.Build())
.Build();
using var server = TestServerBuilder.CreateServer(opt =>
{
opt.Backchannel = new HttpClient(mockHandler.Object);
}, null, false, false, false, null, true);
using var client = server.CreateClient();
var logoutToken = new JwtTokenBuilder(1)
.WithIssuer($"https://{domain}/")
.WithAudience("bad_audience")
.WithClaim(JwtRegisteredClaimNames.Sid, "sid")
.WithClaim("events", "{ \"http://schemas.openid.net/event/backchannel-logout\": {} }" )
.Build();
var formData = new Dictionary<string, string> { { "logout_token", logoutToken } };
using var req = new HttpRequestMessage(HttpMethod.Post, $"{TestServerBuilder.Host}/backchannel-logout");
req.Content = new FormUrlEncodedContent(formData);
using var response = await client.SendAsync(req);
var content = await response.Content.ReadAsStringAsync();
var error = ApiError.Parse(content);
response.StatusCode.Should().Be((HttpStatusCode)400);
error.Message.Should().Contain("Audience validation failed.");
}
[Fact]
public async Task Should_Validate_Sid_On_Backchannel_Logout()
{
var configuration = TestConfiguration.GetConfiguration();
var domain = configuration["Auth0:Domain"];
var clientId = configuration["Auth0:ClientId"];
var mockHandler = new OidcMockBuilder()
.MockOpenIdConfig()
.MockJwks()
.MockToken(() => new JwtTokenBuilder(1)
.WithIssuer($"https://{domain}/")
.WithAudience(clientId)
.Build())
.Build();
using var server = TestServerBuilder.CreateServer(opt =>
{
opt.Backchannel = new HttpClient(mockHandler.Object);
}, null, false, false, false, null, true);
using var client = server.CreateClient();
var logoutToken = new JwtTokenBuilder(1)
.WithIssuer($"https://{domain}/")
.WithAudience(clientId)
.Build();
var formData = new Dictionary<string, string> { { "logout_token", logoutToken } };
using var req = new HttpRequestMessage(HttpMethod.Post, $"{TestServerBuilder.Host}/backchannel-logout");
req.Content = new FormUrlEncodedContent(formData);
using var response = await client.SendAsync(req);
var content = await response.Content.ReadAsStringAsync();
var error = ApiError.Parse(content);
response.StatusCode.Should().Be((HttpStatusCode)400);
error.Message.Should().Contain("Session Id (sid) claim must be a string present in the logout token.");
}
[Fact]
public async Task Should_Validate_Nonce_On_Backchannel_Logout()
{
var nonce = "test";
var configuration = TestConfiguration.GetConfiguration();
var domain = configuration["Auth0:Domain"];
var clientId = configuration["Auth0:ClientId"];
var mockHandler = new OidcMockBuilder()
.MockOpenIdConfig()
.MockJwks()
.MockToken(() => new JwtTokenBuilder(1)
.WithIssuer($"https://{domain}/")
.WithAudience(clientId)
.WithClaim(JwtRegisteredClaimNames.Nonce, nonce)
.Build())
.Build();
using var server = TestServerBuilder.CreateServer(opt =>
{
opt.Backchannel = new HttpClient(mockHandler.Object);
}, null, false, false, false, null, true);
using var client = server.CreateClient();
var logoutToken = new JwtTokenBuilder(1)
.WithIssuer($"https://{domain}/")
.WithAudience(clientId)
.WithClaim(JwtRegisteredClaimNames.Nonce, nonce)
.WithClaim(JwtRegisteredClaimNames.Sid, "sid")
.Build();
var formData = new Dictionary<string, string> { { "logout_token", logoutToken } };
using var req = new HttpRequestMessage(HttpMethod.Post, $"{TestServerBuilder.Host}/backchannel-logout");
req.Content = new FormUrlEncodedContent(formData);
using var response = await client.SendAsync(req);
var content = await response.Content.ReadAsStringAsync();
var error = ApiError.Parse(content);
response.StatusCode.Should().Be((HttpStatusCode)400);
error.Message.Should().Contain("Nonce (nonce) claim must not be present in the logout token.");
}
[Fact]
public async Task Should_Validate_Events_When_Missing_On_Backchannel_Logout()
{
var configuration = TestConfiguration.GetConfiguration();
var domain = configuration["Auth0:Domain"];
var clientId = configuration["Auth0:ClientId"];
var mockHandler = new OidcMockBuilder()
.MockOpenIdConfig()
.MockJwks()
.MockToken(() => new JwtTokenBuilder(1)
.WithIssuer($"https://{domain}/")
.WithAudience(clientId)
.Build())
.Build();
using var server = TestServerBuilder.CreateServer(opt =>
{
opt.Backchannel = new HttpClient(mockHandler.Object);
}, null, false, false, false, null, true);
using var client = server.CreateClient();
var logoutToken = new JwtTokenBuilder(1)
.WithIssuer($"https://{domain}/")
.WithAudience(clientId)
.WithClaim(JwtRegisteredClaimNames.Sid, "sid")
.Build();
var formData = new Dictionary<string, string> { { "logout_token", logoutToken } };
using var req = new HttpRequestMessage(HttpMethod.Post, $"{TestServerBuilder.Host}/backchannel-logout");
req.Content = new FormUrlEncodedContent(formData);
using var response = await client.SendAsync(req);
var content = await response.Content.ReadAsStringAsync();
var error = ApiError.Parse(content);
response.StatusCode.Should().Be((HttpStatusCode)400);
error.Message.Should().Contain("Events (events) claim must be present in the logout token.");
}
[Fact]
public async Task Should_Validate_Events_When_Missing_Property_Backchannel_Logout()
{
var configuration = TestConfiguration.GetConfiguration();
var domain = configuration["Auth0:Domain"];
var clientId = configuration["Auth0:ClientId"];
var mockHandler = new OidcMockBuilder()
.MockOpenIdConfig()
.MockJwks()
.MockToken(() => new JwtTokenBuilder(1)
.WithIssuer($"https://{domain}/")
.WithAudience(clientId)
.Build())
.Build();
using var server = TestServerBuilder.CreateServer(opt =>
{
opt.Backchannel = new HttpClient(mockHandler.Object);
}, null, false, false, false, null, true);
using var client = server.CreateClient();
var logoutToken = new JwtTokenBuilder(1)
.WithIssuer($"https://{domain}/")
.WithAudience(clientId)
.WithClaim(JwtRegisteredClaimNames.Sid, "sid")
.WithClaim("events", "{ \"foo\": {} }" )
.Build();
var formData = new Dictionary<string, string> { { "logout_token", logoutToken } };
using var req = new HttpRequestMessage(HttpMethod.Post, $"{TestServerBuilder.Host}/backchannel-logout");
req.Content = new FormUrlEncodedContent(formData);
using var response = await client.SendAsync(req);
var content = await response.Content.ReadAsStringAsync();
var error = ApiError.Parse(content);
response.StatusCode.Should().Be((HttpStatusCode)400);
error.Message.Should().Contain("Events (events) claim must contain a 'http://schemas.openid.net/event/backchannel-logout' property in the logout token.");
}
[Fact]
public async Task Should_Pass_Validation_On_Backchannel_Logout()
{
var configuration = TestConfiguration.GetConfiguration();
var domain = configuration["Auth0:Domain"];
var clientId = configuration["Auth0:ClientId"];
var mockHandler = new OidcMockBuilder()
.MockOpenIdConfig()
.MockJwks()
.MockToken(() => new JwtTokenBuilder(1)
.WithIssuer($"https://{domain}/")
.WithAudience(clientId)
.Build())
.Build();
using var server = TestServerBuilder.CreateServer(opt =>
{
opt.Backchannel = new HttpClient(mockHandler.Object);
}, null, false, false, false, null, true);
using var client = server.CreateClient();
var logoutToken = new JwtTokenBuilder(1)
.WithIssuer($"https://{domain}/")
.WithAudience(clientId)
.WithClaim(JwtRegisteredClaimNames.Sid, "sid")
.WithClaim("events", "{ \"http://schemas.openid.net/event/backchannel-logout\": {} }" )
.Build();
var formData = new Dictionary<string, string> { { "logout_token", logoutToken } };
using var req = new HttpRequestMessage(HttpMethod.Post, $"{TestServerBuilder.Host}/backchannel-logout");
req.Content = new FormUrlEncodedContent(formData);
using var response = await client.SendAsync(req);
response.StatusCode.Should().Be((HttpStatusCode)200);
}
[Fact]
public async Task Should_Logout_And_Clear_Cookie()
{
var nonce = "";
var configuration = TestConfiguration.GetConfiguration();
var domain = configuration["Auth0:Domain"];
var clientId = configuration["Auth0:ClientId"];
var mockHandler = new OidcMockBuilder()
.MockOpenIdConfig()
.MockJwks()
.MockToken(() => new JwtTokenBuilder(1)
.WithIssuer($"https://{domain}/")
.WithAudience(clientId)
.WithClaim(JwtRegisteredClaimNames.Sid, "sid")
// ReSharper disable once AccessToModifiedClosure
.WithClaim(JwtRegisteredClaimNames.Nonce, nonce)
.Build())
.Build();
using var server = TestServerBuilder.CreateServer(opt =>
{
opt.Backchannel = new HttpClient(mockHandler.Object);
}, null, false, true, false, null, true);
using var client = server.CreateClient();
var loginResponse = (await client.SendAsync($"{TestServerBuilder.Host}/{TestServerBuilder.Login}"));
var setCookie = Assert.Single(loginResponse.Headers, h => h.Key == "Set-Cookie");
var queryParameters = UriUtils.GetQueryParams(loginResponse.Headers.Location);
// Keep track of the nonce as we need to:
// - Send it to the `/oauth/token` endpoint
// - Include it in the generated ID Token
nonce = queryParameters["nonce"];
// Keep track of the state as we need to:
// - Send it to the `/oauth/token` endpoint
var state = queryParameters["state"];
var message = new HttpRequestMessage(HttpMethod.Get, $"{TestServerBuilder.Host}/{TestServerBuilder.Callback}?state={state}&nonce={nonce}&code=123");
// Pass along the Set-Cookies to ensure `Nonce` and `Correlation` cookies are set.
var callbackResponse = (await client.SendAsync(message, setCookie.Value));
var callbackCookies = callbackResponse.Headers.GetValues("Set-Cookie").ToList();
var protectedMessage = new HttpRequestMessage(HttpMethod.Get, $"{TestServerBuilder.Host}/{TestServerBuilder.Protected}");
var protectedResponse = await client.SendAsync(protectedMessage, callbackCookies);
// Accessing a protected endpoint before logging out should be OK.
protectedResponse.StatusCode.Should().Be(HttpStatusCode.OK);
protectedResponse.Headers.Location.Should().BeNull();
var logoutToken = new JwtTokenBuilder(1)
.WithIssuer($"https://{domain}/")
.WithAudience(clientId)
.WithClaim(JwtRegisteredClaimNames.Sid, "sid")
.WithClaim("events", "{ \"http://schemas.openid.net/event/backchannel-logout\": {} }" )
.Build();
var formData = new Dictionary<string, string> { { "logout_token", logoutToken } };
using var req = new HttpRequestMessage(HttpMethod.Post, $"{TestServerBuilder.Host}/backchannel-logout");
req.Content = new FormUrlEncodedContent(formData);
using var response = await client.SendAsync(req);
var protectedMessage2 = new HttpRequestMessage(HttpMethod.Get, $"{TestServerBuilder.Host}/{TestServerBuilder.Protected}");
var protectedResponse2 = await client.SendAsync(protectedMessage2, callbackCookies);
// Accessing a protected endpoint after logging out should redirect.
protectedResponse2.StatusCode.Should().Be(HttpStatusCode.Found);
protectedResponse2.Headers.Location.Should().NotBeNull();
protectedResponse2.Headers.Location!.AbsoluteUri.Should().Contain(TestServerBuilder.Login);
}
[Fact]
public async Task Should_Not_Logout_When_Sid_Doesnt_Match()
{
var nonce = "";
var configuration = TestConfiguration.GetConfiguration();
var domain = configuration["Auth0:Domain"];
var clientId = configuration["Auth0:ClientId"];
var mockHandler = new OidcMockBuilder()
.MockOpenIdConfig()
.MockJwks()
.MockToken(() => new JwtTokenBuilder(1)
.WithIssuer($"https://{domain}/")
.WithAudience(clientId)
// ReSharper disable once AccessToModifiedClosure
.WithClaim(JwtRegisteredClaimNames.Nonce, nonce)
.WithClaim(JwtRegisteredClaimNames.Sid, "sid2")
.Build())
.Build();
using var server = TestServerBuilder.CreateServer(opt =>
{
opt.Backchannel = new HttpClient(mockHandler.Object);
}, null, false, true, false, null, true);
using var client = server.CreateClient();
var loginResponse = (await client.SendAsync($"{TestServerBuilder.Host}/{TestServerBuilder.Login}"));
var setCookie = Assert.Single(loginResponse.Headers, h => h.Key == "Set-Cookie");
var queryParameters = UriUtils.GetQueryParams(loginResponse.Headers.Location);
// Keep track of the nonce as we need to:
// - Send it to the `/oauth/token` endpoint
// - Include it in the generated ID Token
nonce = queryParameters["nonce"];
// Keep track of the state as we need to:
// - Send it to the `/oauth/token` endpoint
var state = queryParameters["state"];
var message = new HttpRequestMessage(HttpMethod.Get, $"{TestServerBuilder.Host}/{TestServerBuilder.Callback}?state={state}&nonce={nonce}&code=123");
// Pass along the Set-Cookies to ensure `Nonce` and `Correlation` cookies are set.
var callbackResponse = (await client.SendAsync(message, setCookie.Value));
var callbackCookies = callbackResponse.Headers.GetValues("Set-Cookie").ToList();
var protectedMessage = new HttpRequestMessage(HttpMethod.Get, $"{TestServerBuilder.Host}/{TestServerBuilder.Protected}");
var protectedResponse = await client.SendAsync(protectedMessage, callbackCookies);
// Accessing a protected endpoint before logging out should be OK.
protectedResponse.StatusCode.Should().Be(HttpStatusCode.OK);
protectedResponse.Headers.Location.Should().BeNull();
var logoutToken = new JwtTokenBuilder(1)
.WithIssuer($"https://{domain}/")
.WithAudience(clientId)
.WithClaim(JwtRegisteredClaimNames.Sid, "sid")
.WithClaim("events", "{ \"http://schemas.openid.net/event/backchannel-logout\": {} }" )
.Build();
var formData = new Dictionary<string, string> { { "logout_token", logoutToken } };
using var req = new HttpRequestMessage(HttpMethod.Post, $"{TestServerBuilder.Host}/backchannel-logout");
req.Content = new FormUrlEncodedContent(formData);
using var response = await client.SendAsync(req);
var protectedMessage2 = new HttpRequestMessage(HttpMethod.Get, $"{TestServerBuilder.Host}/{TestServerBuilder.Protected}");
var protectedResponse2 = await client.SendAsync(protectedMessage2, callbackCookies);
// Accessing a protected endpoint after logging out should be still be OK when the SID didn't match.
protectedResponse2.StatusCode.Should().Be(HttpStatusCode.OK);
protectedResponse2.Headers.Location.Should().BeNull();
}
[Fact]
public async Task Should_Support_Custom_Authentication_Scheme()
{
var configuration = TestConfiguration.GetConfiguration();
var domain = configuration["Auth0:Domain"];
var clientId = configuration["Auth0:ClientId"];
var customScheme = "CustomScheme";
var mockHandler = new OidcMockBuilder()
.MockOpenIdConfig()
.MockJwks()
.MockToken(() => new JwtTokenBuilder(1)
.WithIssuer($"https://{domain}/")
.WithAudience(clientId)
.Build())
.Build();
// Create a server with a custom authentication scheme
using var server = TestServerBuilder.CreateServerWithCustomScheme(customScheme, opt =>
{
opt.Backchannel = new HttpClient(mockHandler.Object);
}, null, false, false, false, null, true);
using var client = server.CreateClient();
// Create a valid logout token
var logoutToken = new JwtTokenBuilder(1)
.WithIssuer($"https://{domain}/")
.WithAudience(clientId)
.WithClaim(JwtRegisteredClaimNames.Sid, "sid")
.WithClaim("events", "{ \"http://schemas.openid.net/event/backchannel-logout\": {} }" )
.Build();
var formData = new Dictionary<string, string> { { "logout_token", logoutToken } };
using var req = new HttpRequestMessage(HttpMethod.Post, $"{TestServerBuilder.Host}/backchannel-logout");
req.Content = new FormUrlEncodedContent(formData);
using var response = await client.SendAsync(req);
response.StatusCode.Should().Be((HttpStatusCode)200);
}
}