-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathClientTokenManagementTests.cs
More file actions
604 lines (455 loc) · 23.3 KB
/
ClientTokenManagementTests.cs
File metadata and controls
604 lines (455 loc) · 23.3 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
597
598
599
600
601
602
603
604
// Copyright (c) Duende Software. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
using System.Net;
using System.Text.Json;
using Duende.AccessTokenManagement.DPoP;
using Duende.AccessTokenManagement.Framework;
using Duende.IdentityModel;
using Duende.IdentityModel.Client;
using Microsoft.Extensions.Caching.Hybrid;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using RichardSzalay.MockHttp;
namespace Duende.AccessTokenManagement;
public class ClientTokenManagementTests
{
private readonly ServiceCollection _services = [];
private readonly MockHttpMessageHandler _mockHttp = new();
public ClientTokenManagementTests()
{
_services.AddSingleton<TimeProvider>(The.TimeProvider);
_mockHttp.Fallback.Respond(req => throw new InvalidOperationException("no handler for " + req.RequestUri));
}
private TestData The { get; } = new();
private TestDataBuilder Some => new(The);
[Fact]
public async Task Unknown_client_should_throw_exception()
{
_services.AddClientCredentialsTokenManagement();
var provider = _services.BuildServiceProvider();
var sut = provider.GetRequiredService<IClientCredentialsTokenManager>();
var action = async () => await sut.GetAccessTokenAsync(ClientCredentialsClientName.Parse("unknown"));
(await Should.ThrowAsync<OptionsValidationException>(action))
.Message.ShouldContain("No ClientId configured for client unknown");
}
[Fact]
public async Task Missing_client_id_throw_exception()
{
_services.AddClientCredentialsTokenManagement()
.AddClient("test", client =>
{
client.TokenEndpoint = new Uri("https://as/connect/token");
client.ClientId = null;
client.ClientSecret = ClientSecret.Parse("notnull");
});
var provider = _services.BuildServiceProvider();
var sut = provider.GetRequiredService<IClientCredentialsTokenManager>();
var action = async () => await sut.GetAccessTokenAsync(ClientCredentialsClientName.Parse("test"));
(await Should.ThrowAsync<OptionsValidationException>(action))
.Message.ShouldContain("ClientId");
}
[Fact]
public async Task Missing_client_secret_throw_exception()
{
var mockedRequest = _mockHttp.Expect("/connect/token")
.Respond(_ => Some.TokenHttpResponse());
_services.AddHttpClient(ClientCredentialsTokenManagementDefaults.BackChannelHttpClientName)
.ConfigurePrimaryHttpMessageHandler(() => _mockHttp);
_services.AddClientCredentialsTokenManagement()
.AddClient("test", client =>
{
client.TokenEndpoint = new Uri("https://as/connect/token");
client.ClientId = The.ClientId;
client.ClientSecret = null;
});
var provider = _services.BuildServiceProvider();
var sut = provider.GetRequiredService<IClientCredentialsTokenManager>();
var token = await sut.GetAccessTokenAsync(ClientCredentialsClientName.Parse("test")).GetToken();
_mockHttp.VerifyNoOutstandingExpectation();
token.ShouldBeEquivalentTo(Some.ClientCredentialsToken());
}
[Fact]
public async Task Missing_tokenEndpoint_throw_exception()
{
_services.AddClientCredentialsTokenManagement()
.AddClient("test", client =>
{
client.TokenEndpoint = null;
client.ClientId = ClientId.Parse("test");
client.ClientSecret = ClientSecret.Parse("notnull");
});
var provider = _services.BuildServiceProvider();
var sut = provider.GetRequiredService<IClientCredentialsTokenManager>();
var action = async () => await sut.GetAccessTokenAsync(ClientCredentialsClientName.Parse("test"));
(await Should.ThrowAsync<OptionsValidationException>(action))
.Message.ShouldContain("TokenEndpoint");
}
[Theory]
[InlineData(ClientCredentialStyle.AuthorizationHeader)]
[InlineData(ClientCredentialStyle.PostBody)]
public async Task Token_request_and_response_should_have_expected_values(ClientCredentialStyle style)
{
_services.AddClientCredentialsTokenManagement()
.AddClient("test", client => Some.ClientCredentialsClient(client,
resource: The.Resource,
style: style,
parameters: new()
{
["audience"] = "audience"
}));
var expectedRequestFormData = new Dictionary<string, string>
{
{ "scope", The.Scope.ToString() },
{ "resource", The.Resource.ToString() },
{ "audience", "audience" },
};
if (style == ClientCredentialStyle.PostBody)
{
expectedRequestFormData.Add("client_id", The.ClientId.ToString());
expectedRequestFormData.Add("client_secret", The.ClientSecret.ToString());
}
if (style == ClientCredentialStyle.PostBody)
{
_mockHttp.Expect("/connect/token")
.WithFormData(expectedRequestFormData)
.Respond(_ => Some.TokenHttpResponse());
}
else if (style == ClientCredentialStyle.AuthorizationHeader)
{
_mockHttp.Expect("/connect/token")
.WithFormData(expectedRequestFormData)
.WithHeaders("Authorization",
"Basic " + IdentityModel.Client.BasicAuthenticationOAuthHeaderValue.EncodeCredential(The.ClientId.ToString(), The.ClientSecret.ToString()))
.Respond(_ => Some.TokenHttpResponse(Some.Token()));
}
_services.AddHttpClient(ClientCredentialsTokenManagementDefaults.BackChannelHttpClientName)
.ConfigurePrimaryHttpMessageHandler(() => _mockHttp);
var provider = _services.BuildServiceProvider();
var sut = provider.GetRequiredService<IClientCredentialsTokenManager>();
var token = await sut.GetAccessTokenAsync(ClientCredentialsClientName.Parse("test")).GetToken();
_mockHttp.VerifyNoOutstandingExpectation();
token.ShouldBeEquivalentTo(Some.ClientCredentialsToken());
}
[Fact]
public async Task Explicit_expires_in_response_should_create_token_with_expiration()
{
_services.AddClientCredentialsTokenManagement()
.AddClient("test", client => Some.ClientCredentialsClient(client));
_mockHttp.Expect(The.TokenEndpoint.ToString())
.Respond(_ => Some.TokenHttpResponse(Some.Token() with
{
expires_in = 300
}));
_services.AddHttpClient(ClientCredentialsTokenManagementDefaults.BackChannelHttpClientName)
.ConfigurePrimaryHttpMessageHandler(() => _mockHttp);
var provider = _services.BuildServiceProvider();
var sut = provider.GetRequiredService<IClientCredentialsTokenManager>();
var token = await sut.GetAccessTokenAsync(ClientCredentialsClientName.Parse("test")).GetToken();
_mockHttp.VerifyNoOutstandingExpectation();
token.ShouldBeEquivalentTo(Some.ClientCredentialsToken() with
{
Expiration = The.CurrentDateTime.Add(TimeSpan.FromSeconds(300))
});
}
[Fact]
public async Task Missing_expires_in_response_should_create_long_lived_token()
{
_services.AddClientCredentialsTokenManagement()
.AddClient("test", client => Some.ClientCredentialsClient(client));
_mockHttp.Expect(The.TokenEndpoint.ToString())
.Respond(_ => Some.TokenHttpResponse(Some.Token() with
{
expires_in = null
}));
_services.AddHttpClient(ClientCredentialsTokenManagementDefaults.BackChannelHttpClientName)
.ConfigurePrimaryHttpMessageHandler(() => _mockHttp);
var provider = _services.BuildServiceProvider();
var sut = provider.GetRequiredService<IClientCredentialsTokenManager>();
var token = await sut.GetAccessTokenAsync(ClientCredentialsClientName.Parse("test")).GetToken();
_mockHttp.VerifyNoOutstandingExpectation();
token.ShouldBeEquivalentTo(Some.ClientCredentialsToken());
}
[Fact]
public async Task Request_parameters_should_take_precedence_over_configuration()
{
_services.AddClientCredentialsTokenManagement()
.AddClient("test", client => Some.ClientCredentialsClient(client,
resource: The.Resource,
parameters: new()
{
["audience"] = "audience"
}));
var request = new TokenRequestParameters
{
Scope = Scope.Parse("scope_per_request"),
Resource = Resource.Parse("resource_per_request"),
Parameters =
{
{ "audience", "audience_per_request" },
},
};
var expectedRequestFormData = new Dictionary<string, string>
{
{ "scope", "scope_per_request" },
{ "resource", "resource_per_request" },
{ "audience", "audience_per_request" },
};
_mockHttp.Expect("/connect/token")
.WithFormData(expectedRequestFormData)
.Respond(_ => Some.TokenHttpResponse(Some.Token() with
{
scope = "scope_per_request"
}));
_services.AddHttpClient(ClientCredentialsTokenManagementDefaults.BackChannelHttpClientName)
.ConfigurePrimaryHttpMessageHandler(() => _mockHttp);
var provider = _services.BuildServiceProvider();
var sut = provider.GetRequiredService<IClientCredentialsTokenManager>();
var token = await sut.GetAccessTokenAsync(ClientCredentialsClientName.Parse("test"), request).GetToken();
_mockHttp.VerifyNoOutstandingExpectation();
token.ShouldBeEquivalentTo(Some.ClientCredentialsToken() with
{
Scope = Scope.Parse("scope_per_request")
});
}
[Fact]
public async Task Request_assertions_should_be_sent_correctly()
{
_services.AddClientCredentialsTokenManagement()
.AddClient("test", client => Some.ClientCredentialsClient(client, resource: The.Resource));
var request = new TokenRequestParameters
{
Assertion = new()
{
Type = "type",
Value = "value"
}
};
var expectedRequestFormData = new Dictionary<string, string>
{
{ OidcConstants.TokenRequest.ClientAssertionType, "type" },
{ OidcConstants.TokenRequest.ClientAssertion, "value" },
};
_mockHttp.Expect("/connect/token")
.WithFormData(expectedRequestFormData)
.Respond(_ => Some.TokenHttpResponse());
_services.AddHttpClient(ClientCredentialsTokenManagementDefaults.BackChannelHttpClientName)
.ConfigurePrimaryHttpMessageHandler(() => _mockHttp);
var provider = _services.BuildServiceProvider();
var sut = provider.GetRequiredService<IClientCredentialsTokenManager>();
var token = await sut.GetAccessTokenAsync(ClientCredentialsClientName.Parse("test"), request).GetToken();
_mockHttp.VerifyNoOutstandingExpectation();
token.ShouldBeEquivalentTo(Some.ClientCredentialsToken());
}
[Fact]
public async Task Service_assertions_should_be_sent_correctly()
{
_services.AddClientCredentialsTokenManagement()
.AddClient("test", client => Some.ClientCredentialsClient(client, resource: The.Resource));
_services.AddTransient<IClientAssertionService>(_ =>
new TestClientAssertionService("test", "service_type", "service_value"));
var expectedRequestFormData = new Dictionary<string, string>
{
{ OidcConstants.TokenRequest.ClientAssertionType, "service_type" },
{ OidcConstants.TokenRequest.ClientAssertion, "service_value" },
};
_mockHttp.Expect("/connect/token")
.WithFormData(expectedRequestFormData)
.Respond(_ => Some.TokenHttpResponse());
_services.AddHttpClient(ClientCredentialsTokenManagementDefaults.BackChannelHttpClientName)
.ConfigurePrimaryHttpMessageHandler(() => _mockHttp);
var provider = _services.BuildServiceProvider();
var sut = provider.GetRequiredService<IClientCredentialsTokenManager>();
var token = await sut.GetAccessTokenAsync(ClientCredentialsClientName.Parse("test")).GetToken();
_mockHttp.VerifyNoOutstandingExpectation();
token.ShouldBeEquivalentTo(Some.ClientCredentialsToken());
}
[Fact]
public async Task Request_assertion_should_take_precedence_over_service_assertion()
{
_services.AddClientCredentialsTokenManagement()
.AddClient("test", client => Some.ClientCredentialsClient(client, resource: The.Resource));
_services.AddTransient<IClientAssertionService>(_ =>
new TestClientAssertionService("test", "service_type", "service_value"));
var request = new TokenRequestParameters
{
Assertion = new()
{
Type = "request_type",
Value = "request_value"
}
};
var expectedRequestFormData = new Dictionary<string, string>
{
{ OidcConstants.TokenRequest.ClientAssertionType, "request_type" },
{ OidcConstants.TokenRequest.ClientAssertion, "request_value" },
};
_mockHttp.Expect("/connect/token")
.WithFormData(expectedRequestFormData)
.Respond(_ => Some.TokenHttpResponse());
_services.AddHttpClient(ClientCredentialsTokenManagementDefaults.BackChannelHttpClientName)
.ConfigurePrimaryHttpMessageHandler(() => _mockHttp);
var provider = _services.BuildServiceProvider();
var sut = provider.GetRequiredService<IClientCredentialsTokenManager>();
var token = await sut.GetAccessTokenAsync(ClientCredentialsClientName.Parse("test"), request).GetToken();
_mockHttp.VerifyNoOutstandingExpectation();
token.ShouldBeEquivalentTo(Some.ClientCredentialsToken());
}
[Fact]
public async Task Service_should_hit_network_only_once_and_then_use_cache()
{
_services.AddClientCredentialsTokenManagement()
.AddClient("test", client => Some.ClientCredentialsClient(client));
var mockedRequest = _mockHttp.Expect("/connect/token")
.Respond(_ => Some.TokenHttpResponse());
_services.AddHttpClient(ClientCredentialsTokenManagementDefaults.BackChannelHttpClientName)
.ConfigurePrimaryHttpMessageHandler(() => _mockHttp);
var provider = _services.BuildServiceProvider();
var sut = provider.GetRequiredService<IClientCredentialsTokenManager>();
var token = await sut.GetAccessTokenAsync(ClientCredentialsClientName.Parse("test")).GetToken();
_mockHttp.VerifyNoOutstandingExpectation();
token.ShouldBeEquivalentTo(Some.ClientCredentialsToken());
// 2nd request
token = await sut.GetAccessTokenAsync(ClientCredentialsClientName.Parse("test")).GetToken();
token.ShouldBeEquivalentTo(Some.ClientCredentialsToken());
_mockHttp.GetMatchCount(mockedRequest).ShouldBe(1);
}
[Fact]
public async Task Service_should_hit_network_when_cache_throws_exception()
{
_services.AddClientCredentialsTokenManagement()
.AddClient("test", client => Some.ClientCredentialsClient(client));
// Get the cache to throw exceptions
var fakeHybridCache = new FakeHybridCache();
_services.AddSingleton<HybridCache>(fakeHybridCache);
fakeHybridCache.OnGetOrCreate = () => throw new InvalidOperationException("Cache error");
var mockedRequest = _mockHttp.Expect("/connect/token")
.Respond(_ => Some.TokenHttpResponse());
_services.AddHttpClient(ClientCredentialsTokenManagementDefaults.BackChannelHttpClientName)
.ConfigurePrimaryHttpMessageHandler(() => _mockHttp);
var provider = _services.BuildServiceProvider();
var sut = provider.GetRequiredService<IClientCredentialsTokenManager>();
await sut.GetAccessTokenAsync(ClientCredentialsClientName.Parse("test")).GetToken();
_mockHttp.GetMatchCount(mockedRequest).ShouldBe(1);
}
[Fact]
public async Task Service_should_always_hit_network_with_force_renewal()
{
_services.AddClientCredentialsTokenManagement()
.AddClient("test", client => Some.ClientCredentialsClient(client));
_mockHttp.Expect("/connect/token")
.Respond(_ => Some.TokenHttpResponse());
_services.AddHttpClient(ClientCredentialsTokenManagementDefaults.BackChannelHttpClientName)
.ConfigurePrimaryHttpMessageHandler(() => _mockHttp);
var provider = _services.BuildServiceProvider();
var sut = provider.GetRequiredService<IClientCredentialsTokenManager>();
var token = await sut.GetAccessTokenAsync(ClientCredentialsClientName.Parse("test")).GetToken();
_mockHttp.VerifyNoOutstandingExpectation();
token.AccessToken.ShouldBe(The.AccessToken);
token.AccessTokenType.ShouldNotBeNull().ShouldBe(The.TokenType);
// 2nd request
_mockHttp.Expect("/connect/token")
.Respond(_ => Some.TokenHttpResponse());
token = await sut.GetAccessTokenAsync(ClientCredentialsClientName.Parse("test"), new TokenRequestParameters { ForceTokenRenewal = true }).GetToken();
token.AccessToken.ShouldBe(The.AccessToken);
token.AccessTokenType.ShouldNotBeNull().ShouldBe(The.TokenType);
}
[Fact]
public async Task client_with_dpop_key_should_send_proof_token()
{
var proof = new TestDPoPProofService { ProofToken = "proof_token" };
_services.AddSingleton<IDPoPProofService>(proof);
_services.AddClientCredentialsTokenManagement()
.AddClient("test", client => Some.ClientCredentialsClient(
toConfigure: client,
jsonWebKey: The.JsonWebKey));
_mockHttp.Expect("/connect/token")
.With(m => m.Headers.Any(h => h.Key == "DPoP" && h.Value.FirstOrDefault() == "proof_token"))
.Respond(_ => Some.TokenHttpResponse());
_services.AddHttpClient(ClientCredentialsTokenManagementDefaults.BackChannelHttpClientName)
.ConfigurePrimaryHttpMessageHandler(() => _mockHttp);
var provider = _services.BuildServiceProvider();
var sut = provider.GetRequiredService<IClientCredentialsTokenManager>();
var token = await sut.GetAccessTokenAsync(ClientCredentialsClientName.Parse("test")).GetToken();
_mockHttp.VerifyNoOutstandingExpectation();
token.ShouldBeEquivalentTo(Some.ClientCredentialsToken() with
{
DPoPJsonWebKey = The.JsonWebKey
});
}
[Fact]
public async Task client_should_use_nonce_when_sending_dpop_proof()
{
var proof = new TestDPoPProofService { ProofToken = "proof_token", AppendNonce = true };
_services.AddSingleton<IDPoPProofService>(proof);
_services.AddClientCredentialsTokenManagement()
.AddClient("test", client => Some.ClientCredentialsClient(
toConfigure: client,
jsonWebKey: The.JsonWebKey));
_mockHttp.Expect(The.TokenEndpoint.ToString())
.With(m => m.Headers.Any(h => h.Key == "DPoP" && h.Value.FirstOrDefault() == "proof_token"))
.Respond(HttpStatusCode.BadRequest,
[new KeyValuePair<string, string>("DPoP-Nonce", "some_nonce")],
"application/json",
JsonSerializer.Serialize(new { error = "use_dpop_nonce" }));
_mockHttp.Expect(The.TokenEndpoint.ToString())
.With(m => m.Headers.Any(h => h.Key == "DPoP" && h.Value.First() == ("proof_tokensome_nonce")))
.Respond(_ => Some.TokenHttpResponse());
_services.AddHttpClient(ClientCredentialsTokenManagementDefaults.BackChannelHttpClientName)
.ConfigurePrimaryHttpMessageHandler(() => _mockHttp);
var provider = _services.BuildServiceProvider();
var sut = provider.GetRequiredService<IClientCredentialsTokenManager>();
var token = await sut.GetAccessTokenAsync(ClientCredentialsClientName.Parse("test")).GetToken();
_mockHttp.VerifyNoOutstandingExpectation();
token.ShouldBeEquivalentTo(Some.ClientCredentialsToken() with
{
DPoPJsonWebKey = The.JsonWebKey
});
}
[Fact]
public async Task Cache_auto_tuning_should_persist_across_transient_manager_instances()
{
var tokenExpiry = (int)TimeSpan.FromDays(7).TotalSeconds;
var fakeCache = new FakeHybridCache();
_services.AddSingleton<HybridCache>(fakeCache);
_services.AddClientCredentialsTokenManagement(options =>
{
options.UseCacheAutoTuning = true;
options.DefaultCacheLifetime = TimeSpan.FromSeconds(30);
options.LocalCacheExpiration = TimeSpan.FromMinutes(10);
options.CacheLifetimeBuffer = 60;
})
.AddClient("test", client => Some.ClientCredentialsClient(client));
_mockHttp.Expect("/connect/token")
.Respond(_ => Some.TokenHttpResponse(Some.Token() with
{
expires_in = tokenExpiry
}));
_services.AddHttpClient(ClientCredentialsTokenManagementDefaults.BackChannelHttpClientName)
.ConfigurePrimaryHttpMessageHandler(() => _mockHttp);
var services = _services.BuildServiceProvider();
// First request with first manager instance
var firstManager = services.GetRequiredService<IClientCredentialsTokenManager>();
var firstToken = await firstManager.GetAccessTokenAsync(ClientCredentialsClientName.Parse("test")).GetToken();
_mockHttp.VerifyNoOutstandingExpectation();
firstToken.Expiration.ShouldBe(The.CurrentDateTime.Add(TimeSpan.FromSeconds(tokenExpiry)));
// Get the cache expiration used for the first request
var firstRequestExpiration = fakeCache.LastOptions?.Expiration;
// The first request doesn't know the token lifetime yet, so it should use DefaultCacheLifetime (30 seconds)
firstRequestExpiration.ShouldBe(TimeSpan.FromSeconds(30));
_mockHttp.Expect("/connect/token")
.Respond(_ => Some.TokenHttpResponse(Some.Token() with
{
expires_in = tokenExpiry
}));
var secondManager = services.GetRequiredService<IClientCredentialsTokenManager>();
var secondToken = await secondManager.GetAccessTokenAsync(ClientCredentialsClientName.Parse("test")).GetToken();
_mockHttp.VerifyNoOutstandingExpectation();
secondToken.Expiration.ShouldBe(The.CurrentDateTime.Add(TimeSpan.FromSeconds(tokenExpiry)));
// Get the cache expiration used for the second request
var secondRequestExpiration = fakeCache.LastOptions?.Expiration;
// Expect the lifetime to be auto-tuned based on the first token's lifetime minus the buffer
var expectedExpiration = TimeSpan.FromSeconds(tokenExpiry) - TimeSpan.FromSeconds(60);
secondRequestExpiration.ShouldBe(expectedExpiration,
"Second request should use the auto-tuned cache duration learned from the first request");
}
}