Skip to content

Commit 365fc94

Browse files
simplified wireup so only using IKeyedDependency
1 parent 81ebca4 commit 365fc94

File tree

6 files changed

+13
-191
lines changed

6 files changed

+13
-191
lines changed

access-token-management/src/AccessTokenManagement/ClientCredentialsTokenManagementServiceCollectionExtensions.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,21 @@ public static ClientCredentialsTokenManagementBuilder AddClientCredentialsTokenM
3939
services.TryAddSingleton<ITokenRequestSynchronization, TokenRequestSynchronization>();
4040

4141
services.TryAddTransient<IClientCredentialsTokenManagementService, ClientCredentialsTokenManagementService>();
42-
services.TryAddKeyedTransient<OptionallyKeyedDependency<IDistributedCache>>(nameof(DistributedClientCredentialsTokenCache));
42+
43+
// By default, resolve the distributed cache for the DistributedClientCredentialsTokenCache
44+
// without key. If desired, a consumers can register the distributed cache with a key
45+
services.TryAddKeyedTransient<IDistributedCache>(nameof(DistributedClientCredentialsTokenCache), (sp, _) => sp.GetRequiredService<IDistributedCache>());
4346
services.TryAddTransient<IClientCredentialsTokenCache, DistributedClientCredentialsTokenCache>();
4447
services.TryAddTransient<IClientCredentialsTokenEndpointService, ClientCredentialsTokenEndpointService>();
4548
services.TryAddTransient<IClientAssertionService, DefaultClientAssertionService>();
4649

4750
services.TryAddTransient<IDPoPProofService, DefaultDPoPProofService>();
4851
services.TryAddTransient<IDPoPKeyStore, DefaultDPoPKeyStore>();
4952

50-
services.TryAddKeyedTransient<OptionallyKeyedDependency<IDistributedCache>>(nameof(DistributedDPoPNonceStore));
53+
// ** DistributedDPoPNonceStore **
54+
// By default, resolve the distributed cache for the DistributedClientCredentialsTokenCache
55+
// without key. If desired, a consumers can register the distributed cache with a key
56+
services.TryAddKeyedTransient<IDistributedCache>(nameof(DistributedDPoPNonceStore), (sp, _) => sp.GetRequiredService<IDistributedCache>());
5157
services.TryAddTransient<IDPoPNonceStore, DistributedDPoPNonceStore>();
5258

5359
services.AddHttpClient(ClientCredentialsTokenManagementDefaults.BackChannelHttpClientName);

access-token-management/src/AccessTokenManagement/DistributedClientCredentialsTokenCache.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ namespace Duende.AccessTokenManagement;
1313
/// Client access token cache using IDistributedCache
1414
/// </summary>
1515
public class DistributedClientCredentialsTokenCache(
16-
[FromKeyedServices(nameof(DistributedClientCredentialsTokenCache))] OptionallyKeyedDependency<IDistributedCache> cache,
16+
[FromKeyedServices(nameof(DistributedClientCredentialsTokenCache))] IDistributedCache cache,
1717
ITokenRequestSynchronization synchronization,
1818
IOptions<ClientCredentialsTokenManagementOptions> options,
1919
ILogger<DistributedClientCredentialsTokenCache> logger
2020
)
2121
: IClientCredentialsTokenCache
2222
{
23-
private readonly IDistributedCache _cache = cache.Dependency;
23+
private readonly IDistributedCache _cache = cache;
2424
private readonly ITokenRequestSynchronization _synchronization = synchronization;
2525
private readonly ILogger<DistributedClientCredentialsTokenCache> _logger = logger;
2626
private readonly ClientCredentialsTokenManagementOptions _options = options.Value;

access-token-management/src/AccessTokenManagement/DistributedDPoPNonceStore.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ public class DistributedDPoPNonceStore : IDPoPNonceStore
2424
/// <param name="cache"></param>
2525
/// <param name="logger"></param>
2626
public DistributedDPoPNonceStore(
27-
[FromKeyedServices(nameof(DistributedDPoPNonceStore))]OptionallyKeyedDependency<IDistributedCache> cache,
27+
[FromKeyedServices(nameof(DistributedDPoPNonceStore))] IDistributedCache cache,
2828
ILogger<DistributedDPoPNonceStore> logger)
2929
{
30-
_cache = cache.Dependency;
30+
_cache = cache;
3131
_logger = logger;
3232
}
3333

access-token-management/src/AccessTokenManagement/OptionallyKeyedDependency.cs

Lines changed: 0 additions & 40 deletions
This file was deleted.

access-token-management/test/AccessTokenManagement.Tests/BackChannelClientTests.cs

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ public async Task Can_use_custom_cache_implementation()
306306

307307
services.AddDistributedMemoryCache();
308308
var replacementCache = new FakeCache();
309-
services.AddKeyedSingleton<IDistributedCache>(OptionallyKeyedDependency.Duende, replacementCache);
309+
services.AddKeyedSingleton<IDistributedCache>(nameof(DistributedClientCredentialsTokenCache), replacementCache);
310310

311311
services.AddClientCredentialsTokenManagement()
312312
.AddClient("test", client =>
@@ -336,61 +336,6 @@ public async Task Can_use_custom_cache_implementation()
336336
replacementCache.SetCount.ShouldBe(1);
337337
}
338338

339-
[Fact]
340-
public async Task Can_use_custom_cache_implementation_only_for_DistributedClientCredentialsTokenCache()
341-
{
342-
var services = new ServiceCollection();
343-
344-
services.AddDistributedMemoryCache();
345-
var replacementCache = new FakeCache();
346-
347-
// register the cache, but using a different key (not default 'duende')
348-
services.AddKeyedSingleton<IDistributedCache>("different", replacementCache)
349-
350-
// Now register a custom OptionallyKeyedDependency that uses the different key
351-
.AddKeyedTransient<OptionallyKeyedDependency<IDistributedCache>,
352-
CustomOptionallyKeyedDependency<IDistributedCache>>(nameof(DistributedClientCredentialsTokenCache));
353-
354-
services.AddClientCredentialsTokenManagement()
355-
.AddClient("test", client =>
356-
{
357-
client.TokenEndpoint = "https://as";
358-
client.ClientId = "id";
359-
360-
client.HttpClientName = "custom";
361-
});
362-
363-
var mockHttp = new MockHttpMessageHandler();
364-
mockHttp.When("https://as/*")
365-
.Respond(HttpStatusCode.OK, JsonContent.Create(new TokenResponse()));
366-
367-
services.AddHttpClient("custom")
368-
.ConfigurePrimaryHttpMessageHandler(() => mockHttp);
369-
370-
var provider = services.BuildServiceProvider();
371-
var sut = provider.GetRequiredService<IClientCredentialsTokenManagementService>();
372-
373-
var token = await sut.GetAccessTokenAsync("test");
374-
375-
token.Error.ShouldBeNull();
376-
377-
// Verify we actually used the cache
378-
replacementCache.GetCount.ShouldBe(1);
379-
replacementCache.SetCount.ShouldBe(1);
380-
}
381-
382-
/// <summary>
383-
/// You can change the key of a dependency by deriving from OptionallyKeyedDependency.
384-
/// </summary>
385-
/// <typeparam name="T"></typeparam>
386-
public class CustomOptionallyKeyedDependency<T>(
387-
T defaultDependency,
388-
389-
// Here the key is overwritten
390-
[FromKeyedServices(("different"))] T? keyedDependency = default(T?))
391-
: OptionallyKeyedDependency<T>(defaultDependency, keyedDependency)
392-
where T : class;
393-
394339
public class FakeCache : IDistributedCache
395340
{
396341
public int GetCount = 0;

access-token-management/test/AccessTokenManagement.Tests/OptionallyNamedDependencyTest.cs

Lines changed: 0 additions & 89 deletions
This file was deleted.

0 commit comments

Comments
 (0)