-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathServiceCollectionExtensions.cs
More file actions
207 lines (182 loc) · 9.57 KB
/
ServiceCollectionExtensions.cs
File metadata and controls
207 lines (182 loc) · 9.57 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
// 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 Duende.AccessTokenManagement.DPoP;
using Duende.AccessTokenManagement.DPoP.Internal;
using Duende.AccessTokenManagement.Internal;
using Duende.AccessTokenManagement.OTel;
using Microsoft.Extensions.Caching.Hybrid;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Duende.AccessTokenManagement;
/// <summary>
/// Extension methods for IServiceCollection to register the client credentials token management services
/// </summary>
public static class ServiceCollectionExtensions
{
/// <summary>
/// Adds all necessary services for client credentials token management
/// </summary>
/// <param name="services"></param>
/// <param name="options"></param>
/// <returns></returns>
public static ClientCredentialsTokenManagementBuilder AddClientCredentialsTokenManagement(
this IServiceCollection services,
Action<ClientCredentialsTokenManagementOptions> options)
{
services.Configure(options);
return services.AddClientCredentialsTokenManagement();
}
/// <summary>
/// Adds all necessary services for client credentials token management
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
public static ClientCredentialsTokenManagementBuilder AddClientCredentialsTokenManagement(
this IServiceCollection services)
{
services.TryAddTransient<IClientCredentialsTokenManager, ClientCredentialsTokenManager>();
services.TryAddSingleton<IClientCredentialsCacheDurationStore, ClientCredentialsCacheDurationStore>();
services.AddHybridCache();
// Add a default serializer for ClientCredentialsToken
services
.TryAddSingleton<IHybridCacheSerializer<ClientCredentialsToken>,
AotTrimCompatibleClientCredentialsTokenSerializer>();
// By default, resolve the default hybrid cache for the DefaultClientCredentialsTokenManager
// without key. If desired, a consumers can register the distributed cache with a key
services.TryAddKeyedSingleton<HybridCache>(ServiceProviderKeys.ClientCredentialsTokenCache,
(sp, _) => sp.GetRequiredService<HybridCache>());
services.TryAddTransient<IClientCredentialsTokenEndpoint, ClientCredentialsTokenClient>();
services.TryAddTransient<IClientAssertionService, NoOpClientAssertionService>();
services.TryAddTransient<IDPoPProofService, DefaultDPoPProofService>();
services.TryAddTransient<IDPoPKeyStore, DefaultDPoPKeyStore>();
// By default, resolve the default hybrid cache for the HybridDPoPNonceStore
// without key. If desired, a consumers can register the distributed cache with a key
services.TryAddKeyedSingleton<HybridCache>(ServiceProviderKeys.DPoPNonceStore,
(sp, _) => sp.GetRequiredService<HybridCache>());
services.AddTransient<IDPoPNonceStore, HybridDPoPNonceStore>();
services.TryAddSingleton(TimeProvider.System);
services.AddHttpClient(ClientCredentialsTokenManagementDefaults.BackChannelHttpClientName);
services.TryAddTransient<IClientCredentialsCacheKeyGenerator, DefaultClientCredentialsCacheKeyGenerator>();
services.TryAddTransient<IDPoPNonceStoreKeyGenerator, DPoPNonceStoreKeyGenerator>();
services.AddSingleton<AccessTokenManagementMetrics>();
services.TryAddSingleton<IValidateOptions<ClientCredentialsClient>, ClientCredentialsClient.Validator>();
return new ClientCredentialsTokenManagementBuilder(services);
}
/// <summary>
/// Adds a named HTTP client for the factory that automatically sends a client access token
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="httpClientName">The name of the client.</param>
/// <param name="clientName">The name of the token client.</param>
/// <param name="configureClient">A delegate that is used to configure a <see cref="HttpClient"/>.</param>
/// <returns></returns>
public static IHttpClientBuilder AddClientCredentialsHttpClient(
this IServiceCollection services,
string httpClientName,
ClientCredentialsClientName clientName,
Action<HttpClient>? configureClient = null)
{
if (configureClient != null)
{
return services.AddHttpClient(httpClientName, configureClient)
.AddDefaultAccessTokenResiliency()
.AddClientCredentialsTokenHandler(clientName);
}
return services.AddHttpClient(httpClientName)
.AddDefaultAccessTokenResiliency()
.AddClientCredentialsTokenHandler(clientName);
}
/// <summary>
/// Adds a named HTTP client for the factory that automatically sends a client access token
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="httpClientName">The name of the client.</param>
/// <param name="clientName">The name of the token client.</param>
/// <param name="configureClient">Additional configuration with service provider instance.</param>
/// <returns></returns>
public static IHttpClientBuilder AddClientCredentialsHttpClient(
this IServiceCollection services,
string httpClientName,
ClientCredentialsClientName clientName,
Action<IServiceProvider, HttpClient> configureClient) =>
services.AddHttpClient(httpClientName, configureClient)
.AddDefaultAccessTokenResiliency()
.AddClientCredentialsTokenHandler(clientName);
public static IHttpClientBuilder AddDefaultAccessTokenResiliency(this IHttpClientBuilder httpClientBuilder)
{
httpClientBuilder.AddResilienceHandler("Duende",
(builder, context) => builder.AddDefaultAccessTokenHandlingResiliency(context));
return httpClientBuilder;
}
/// <summary>
/// Adds the client access token handler to an HttpClient
/// </summary>
/// <param name="httpClientBuilder"></param>
/// <param name="clientName"></param>
/// <returns></returns>
public static IHttpClientBuilder AddClientCredentialsTokenHandler(
this IHttpClientBuilder httpClientBuilder,
ClientCredentialsClientName clientName) =>
AddClientCredentialsTokenHandlerInternal(httpClientBuilder, tokenRequestCustomizerFactory: null, clientName);
/// <summary>
/// Adds the client access token handler to an HttpClient
/// </summary>
/// <param name="httpClientBuilder"></param>
/// <param name="tokenRequestCustomizer"></param>
/// <param name="clientName"></param>
/// <returns></returns>
public static IHttpClientBuilder AddClientCredentialsTokenHandler(
this IHttpClientBuilder httpClientBuilder,
ITokenRequestCustomizer tokenRequestCustomizer,
ClientCredentialsClientName clientName)
{
ArgumentNullException.ThrowIfNull(tokenRequestCustomizer);
return AddClientCredentialsTokenHandlerInternal(httpClientBuilder, _ => tokenRequestCustomizer, clientName);
}
/// <summary>
/// Adds the client access token handler to an HttpClient
/// </summary>
/// <param name="httpClientBuilder"></param>
/// <param name="tokenRequestCustomizerFactory"></param>
/// <param name="clientName"></param>
/// <returns></returns>
public static IHttpClientBuilder AddClientCredentialsTokenHandler(
this IHttpClientBuilder httpClientBuilder,
Func<IServiceProvider, ITokenRequestCustomizer> tokenRequestCustomizerFactory,
ClientCredentialsClientName clientName)
{
ArgumentNullException.ThrowIfNull(tokenRequestCustomizerFactory);
return AddClientCredentialsTokenHandlerInternal(httpClientBuilder, tokenRequestCustomizerFactory, clientName);
}
private static IHttpClientBuilder AddClientCredentialsTokenHandlerInternal(
IHttpClientBuilder httpClientBuilder,
Func<IServiceProvider, ITokenRequestCustomizer>? tokenRequestCustomizerFactory,
ClientCredentialsClientName clientName) => httpClientBuilder
.AddHttpMessageHandler(provider =>
{
var accessTokenManagementService = provider.GetRequiredService<IClientCredentialsTokenManager>();
var tokenRequestCustomizer = tokenRequestCustomizerFactory?.Invoke(provider) ??
provider.GetService<ITokenRequestCustomizer>();
var retriever =
new ClientCredentialsTokenRetriever(accessTokenManagementService, clientName,
tokenRequestCustomizer);
var accessTokenHandler = provider.BuildAccessTokenRequestHandler(retriever);
return accessTokenHandler;
});
internal static AccessTokenRequestHandler BuildAccessTokenRequestHandler(
this IServiceProvider provider,
AccessTokenRequestHandler.ITokenRetriever retriever)
{
var logger = provider.GetRequiredService<ILogger<AccessTokenRequestHandler>>();
var dPoPProofService = provider.GetRequiredService<IDPoPProofService>();
var dPoPNonceStore = provider.GetRequiredService<IDPoPNonceStore>();
var accessTokenHandler = new AccessTokenRequestHandler(
tokenRetriever: retriever,
dPoPNonceStore: dPoPNonceStore,
dPoPProofService: dPoPProofService,
logger: logger);
return accessTokenHandler;
}
}