forked from openiddict/openiddict-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOpenIddictCoreBuilder.cs
More file actions
469 lines (407 loc) · 20.3 KB
/
OpenIddictCoreBuilder.cs
File metadata and controls
469 lines (407 loc) · 20.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
/*
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
* See https://github.com/openiddict/openiddict-core for more information concerning
* the license and the contributors participating to this project.
*/
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.DependencyInjection.Extensions;
using OpenIddict.Core;
namespace Microsoft.Extensions.DependencyInjection;
/// <summary>
/// Exposes the necessary methods required to configure the OpenIddict core services.
/// </summary>
public sealed class OpenIddictCoreBuilder
{
/// <summary>
/// Initializes a new instance of <see cref="OpenIddictCoreBuilder"/>.
/// </summary>
/// <param name="services">The services collection.</param>
public OpenIddictCoreBuilder(IServiceCollection services)
=> Services = services ?? throw new ArgumentNullException(nameof(services));
/// <summary>
/// Gets the services collection.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public IServiceCollection Services { get; }
/// <summary>
/// Amends the default OpenIddict core configuration.
/// </summary>
/// <param name="configuration">The delegate used to configure the OpenIddict options.</param>
/// <remarks>This extension can be safely called multiple times.</remarks>
/// <returns>The <see cref="OpenIddictCoreBuilder"/> instance.</returns>
public OpenIddictCoreBuilder Configure(Action<OpenIddictCoreOptions> configuration)
{
if (configuration is null)
{
throw new ArgumentNullException(nameof(configuration));
}
Services.Configure(configuration);
return this;
}
/// <summary>
/// Replaces the application manager by the specified type.
/// </summary>
/// <typeparam name="TApplication">The type of the entity.</typeparam>
/// <typeparam name="TManager">The type of the manager.</typeparam>
/// <returns>The <see cref="OpenIddictCoreBuilder"/> instance.</returns>
public OpenIddictCoreBuilder ReplaceApplicationManager<
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TApplication,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TManager>()
where TApplication : class
where TManager : OpenIddictApplicationManager<TApplication>
{
Services.Replace(ServiceDescriptor.Scoped<OpenIddictApplicationManager<TApplication>, TManager>());
return this;
}
/// <summary>
/// Replaces the application manager by the specified type.
/// </summary>
/// <remarks>
/// Note: the specified type MUST be an open generic type definition containing exactly one generic argument.
/// </remarks>
/// <param name="type">The type of the manager.</param>
/// <returns>The <see cref="OpenIddictCoreBuilder"/> instance.</returns>
public OpenIddictCoreBuilder ReplaceApplicationManager(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type type)
{
if (!type.IsGenericTypeDefinition || type.GetGenericArguments() is not { Length: 1 })
{
throw new ArgumentException(SR.GetResourceString(SR.ID0232), nameof(type));
}
Services.Replace(ServiceDescriptor.Scoped(typeof(OpenIddictApplicationManager<>), type));
return this;
}
/// <summary>
/// Replaces the application store by the specified type.
/// </summary>
/// <typeparam name="TApplication">The type of the entity.</typeparam>
/// <typeparam name="TStore">The type of the store.</typeparam>
/// <param name="lifetime">The lifetime of the store.</param>
/// <returns>The <see cref="OpenIddictCoreBuilder"/> instance.</returns>
public OpenIddictCoreBuilder ReplaceApplicationStore<
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TApplication,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TStore>(
ServiceLifetime lifetime = ServiceLifetime.Scoped)
where TApplication : class
where TStore : IOpenIddictApplicationStore<TApplication>
{
Services.Replace(ServiceDescriptor.Describe(typeof(IOpenIddictApplicationStore<TApplication>), typeof(TStore), lifetime));
return this;
}
/// <summary>
/// Replaces the application store by the specified type.
/// </summary>
/// <remarks>
/// Note: the specified type MUST be an open generic type definition containing exactly one generic argument.
/// </remarks>
/// <param name="type">The type of the store.</param>
/// <param name="lifetime">The lifetime of the store.</param>
/// <returns>The <see cref="OpenIddictCoreBuilder"/> instance.</returns>
public OpenIddictCoreBuilder ReplaceApplicationStore(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type type,
ServiceLifetime lifetime = ServiceLifetime.Scoped)
{
if (!type.IsGenericTypeDefinition || type.GetGenericArguments() is not { Length: 1 })
{
throw new ArgumentException(SR.GetResourceString(SR.ID0232), nameof(type));
}
Services.Replace(ServiceDescriptor.Describe(typeof(IOpenIddictApplicationStore<>), type, lifetime));
return this;
}
/// <summary>
/// Replaces the authorization manager by the specified type.
/// </summary>
/// <typeparam name="TAuthorization">The type of the entity.</typeparam>
/// <typeparam name="TManager">The type of the manager.</typeparam>
/// <returns>The <see cref="OpenIddictCoreBuilder"/> instance.</returns>
public OpenIddictCoreBuilder ReplaceAuthorizationManager<
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TAuthorization,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TManager>()
where TAuthorization : class
where TManager : OpenIddictAuthorizationManager<TAuthorization>
{
Services.Replace(ServiceDescriptor.Scoped<OpenIddictAuthorizationManager<TAuthorization>, TManager>());
return this;
}
/// <summary>
/// Replaces the authorization manager by the specified type.
/// </summary>
/// <remarks>
/// Note: the specified type MUST be an open generic type definition containing exactly one generic argument.
/// </remarks>
/// <param name="type">The type of the manager.</param>
/// <returns>The <see cref="OpenIddictCoreBuilder"/> instance.</returns>
public OpenIddictCoreBuilder ReplaceAuthorizationManager(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type type)
{
if (!type.IsGenericTypeDefinition || type.GetGenericArguments() is not { Length: 1 })
{
throw new ArgumentException(SR.GetResourceString(SR.ID0232), nameof(type));
}
Services.Replace(ServiceDescriptor.Scoped(typeof(OpenIddictAuthorizationManager<>), type));
return this;
}
/// <summary>
/// Replaces the authorization store by the specified type.
/// </summary>
/// <typeparam name="TAuthorization">The type of the entity.</typeparam>
/// <typeparam name="TStore">The type of the store.</typeparam>
/// <param name="lifetime">The lifetime of the store.</param>
/// <returns>The <see cref="OpenIddictCoreBuilder"/> instance.</returns>
public OpenIddictCoreBuilder ReplaceAuthorizationStore<
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TAuthorization,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TStore>(
ServiceLifetime lifetime = ServiceLifetime.Scoped)
where TAuthorization : class
where TStore : IOpenIddictAuthorizationStore<TAuthorization>
{
Services.Replace(ServiceDescriptor.Describe(typeof(IOpenIddictAuthorizationStore<TAuthorization>), typeof(TStore), lifetime));
return this;
}
/// <summary>
/// Replaces the authorization store by the specified type.
/// </summary>
/// <remarks>
/// Note: the specified type MUST be an open generic type definition containing exactly one generic argument.
/// </remarks>
/// <param name="type">The type of the store.</param>
/// <param name="lifetime">The lifetime of the store.</param>
/// <returns>The <see cref="OpenIddictCoreBuilder"/> instance.</returns>
public OpenIddictCoreBuilder ReplaceAuthorizationStore(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type type,
ServiceLifetime lifetime = ServiceLifetime.Scoped)
{
if (!type.IsGenericTypeDefinition || type.GetGenericArguments() is not { Length: 1 })
{
throw new ArgumentException(SR.GetResourceString(SR.ID0232), nameof(type));
}
Services.Replace(ServiceDescriptor.Describe(typeof(IOpenIddictAuthorizationStore<>), type, lifetime));
return this;
}
/// <summary>
/// Replaces the scope manager by the specified type.
/// </summary>
/// <typeparam name="TScope">The type of the entity.</typeparam>
/// <typeparam name="TManager">The type of the manager.</typeparam>
/// <returns>The <see cref="OpenIddictCoreBuilder"/> instance.</returns>
public OpenIddictCoreBuilder ReplaceScopeManager<
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TScope,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TManager>()
where TScope : class
where TManager : OpenIddictScopeManager<TScope>
{
Services.Replace(ServiceDescriptor.Scoped<OpenIddictScopeManager<TScope>, TManager>());
return this;
}
/// <summary>
/// Replaces the scope manager by the specified type.
/// </summary>
/// <remarks>
/// Note: the specified type MUST be an open generic type definition containing exactly one generic argument.
/// </remarks>
/// <param name="type">The type of the manager.</param>
/// <returns>The <see cref="OpenIddictCoreBuilder"/> instance.</returns>
public OpenIddictCoreBuilder ReplaceScopeManager(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type type)
{
if (!type.IsGenericTypeDefinition || type.GetGenericArguments() is not { Length: 1 })
{
throw new ArgumentException(SR.GetResourceString(SR.ID0232), nameof(type));
}
Services.Replace(ServiceDescriptor.Scoped(typeof(OpenIddictScopeManager<>), type));
return this;
}
/// <summary>
/// Replaces the scope store by the specified type.
/// </summary>
/// <typeparam name="TScope">The type of the entity.</typeparam>
/// <typeparam name="TStore">The type of the store.</typeparam>
/// <param name="lifetime">The lifetime of the store.</param>
/// <returns>The <see cref="OpenIddictCoreBuilder"/> instance.</returns>
public OpenIddictCoreBuilder ReplaceScopeStore<
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TScope,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TStore>(
ServiceLifetime lifetime = ServiceLifetime.Scoped)
where TScope : class
where TStore : IOpenIddictScopeStore<TScope>
{
Services.Replace(ServiceDescriptor.Describe(typeof(IOpenIddictScopeStore<TScope>), typeof(TStore), lifetime));
return this;
}
/// <summary>
/// Replaces the scope store by the specified type.
/// </summary>
/// <remarks>
/// Note: the specified type MUST be an open generic type definition containing exactly one generic argument.
/// </remarks>
/// <param name="type">The type of the store.</param>
/// <param name="lifetime">The lifetime of the store.</param>
/// <returns>The <see cref="OpenIddictCoreBuilder"/> instance.</returns>
public OpenIddictCoreBuilder ReplaceScopeStore(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type type,
ServiceLifetime lifetime = ServiceLifetime.Scoped)
{
if (!type.IsGenericTypeDefinition || type.GetGenericArguments() is not { Length: 1 })
{
throw new ArgumentException(SR.GetResourceString(SR.ID0232), nameof(type));
}
Services.Replace(ServiceDescriptor.Describe(typeof(IOpenIddictScopeStore<>), type, lifetime));
return this;
}
/// <summary>
/// Replaces the token manager by the specified type.
/// </summary>
/// <typeparam name="TToken">The type of the entity.</typeparam>
/// <typeparam name="TManager">The type of the manager.</typeparam>
/// <returns>The <see cref="OpenIddictCoreBuilder"/> instance.</returns>
public OpenIddictCoreBuilder ReplaceTokenManager<
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TToken,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TManager>()
where TToken : class
where TManager : OpenIddictTokenManager<TToken>
{
Services.Replace(ServiceDescriptor.Scoped<OpenIddictTokenManager<TToken>, TManager>());
return this;
}
/// <summary>
/// Replaces the token manager by the specified type.
/// </summary>
/// <remarks>
/// Note: the specified type MUST be an open generic type definition containing exactly one generic argument.
/// </remarks>
/// <param name="type">The type of the manager.</param>
/// <returns>The <see cref="OpenIddictCoreBuilder"/> instance.</returns>
public OpenIddictCoreBuilder ReplaceTokenManager(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type type)
{
if (!type.IsGenericTypeDefinition || type.GetGenericArguments() is not { Length: 1 })
{
throw new ArgumentException(SR.GetResourceString(SR.ID0232), nameof(type));
}
Services.Replace(ServiceDescriptor.Scoped(typeof(OpenIddictTokenManager<>), type));
return this;
}
/// <summary>
/// Replaces the token store by the specified type.
/// </summary>
/// <typeparam name="TToken">The type of the entity.</typeparam>
/// <typeparam name="TStore">The type of the store.</typeparam>
/// <param name="lifetime">The lifetime of the store.</param>
/// <returns>The <see cref="OpenIddictCoreBuilder"/> instance.</returns>
public OpenIddictCoreBuilder ReplaceTokenStore<
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TToken,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TStore>(
ServiceLifetime lifetime = ServiceLifetime.Scoped)
where TToken : class
where TStore : IOpenIddictTokenStore<TToken>
{
Services.Replace(ServiceDescriptor.Describe(typeof(IOpenIddictTokenStore<TToken>), typeof(TStore), lifetime));
return this;
}
/// <summary>
/// Replaces the token store by the specified type.
/// </summary>
/// <remarks>
/// Note: the specified type MUST be an open generic type definition containing exactly one generic argument.
/// </remarks>
/// <param name="type">The type of the store.</param>
/// <param name="lifetime">The lifetime of the store.</param>
/// <returns>The <see cref="OpenIddictCoreBuilder"/> instance.</returns>
public OpenIddictCoreBuilder ReplaceTokenStore(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type type,
ServiceLifetime lifetime = ServiceLifetime.Scoped)
{
if (!type.IsGenericTypeDefinition || type.GetGenericArguments() is not { Length: 1 })
{
throw new ArgumentException(SR.GetResourceString(SR.ID0232), nameof(type));
}
Services.Replace(ServiceDescriptor.Describe(typeof(IOpenIddictTokenStore<>), type, lifetime));
return this;
}
/// <summary>
/// Disables additional filtering so that the OpenIddict managers don't execute a second check
/// to ensure the results returned by the stores exactly match the specified query filters,
/// casing included. Additional filtering shouldn't be disabled except when the underlying
/// stores are guaranteed to execute case-sensitive filtering at the database level.
/// Disabling this feature MAY result in security vulnerabilities in the other cases.
/// </summary>
/// <returns>The <see cref="OpenIddictCoreBuilder"/> instance.</returns>
public OpenIddictCoreBuilder DisableAdditionalFiltering()
=> Configure(options => options.DisableAdditionalFiltering = true);
/// <summary>
/// Disables the scoped entity caching applied by the OpenIddict managers.
/// Disabling entity caching may have a noticeable impact on the performance
/// of your application and result in multiple queries being sent by the stores.
/// </summary>
/// <returns>The <see cref="OpenIddictCoreBuilder"/> instance.</returns>
public OpenIddictCoreBuilder DisableEntityCaching()
=> Configure(options => options.DisableEntityCaching = true);
/// <summary>
/// Configures OpenIddict to use the specified entity as the default application entity.
/// </summary>
/// <returns>The <see cref="OpenIddictCoreBuilder"/> instance.</returns>
public OpenIddictCoreBuilder SetDefaultApplicationEntity<
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TApplication>() where TApplication : class
{
Services.Replace(ServiceDescriptor.Scoped<IOpenIddictApplicationManager>(static provider =>
provider.GetRequiredService<OpenIddictApplicationManager<TApplication>>()));
return this;
}
/// <summary>
/// Configures OpenIddict to use the specified entity as the default authorization entity.
/// </summary>
/// <returns>The <see cref="OpenIddictCoreBuilder"/> instance.</returns>
public OpenIddictCoreBuilder SetDefaultAuthorizationEntity<
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TAuthorization>() where TAuthorization : class
{
Services.Replace(ServiceDescriptor.Scoped<IOpenIddictAuthorizationManager>(static provider =>
provider.GetRequiredService<OpenIddictAuthorizationManager<TAuthorization>>()));
return this;
}
/// <summary>
/// Configures OpenIddict to use the specified entity as the default scope entity.
/// </summary>
/// <returns>The <see cref="OpenIddictCoreBuilder"/> instance.</returns>
public OpenIddictCoreBuilder SetDefaultScopeEntity<
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TScope>() where TScope : class
{
Services.Replace(ServiceDescriptor.Scoped<IOpenIddictScopeManager>(static provider =>
provider.GetRequiredService<OpenIddictScopeManager<TScope>>()));
return this;
}
/// <summary>
/// Configures OpenIddict to use the specified entity as the default token entity.
/// </summary>
/// <returns>The <see cref="OpenIddictCoreBuilder"/> instance.</returns>
public OpenIddictCoreBuilder SetDefaultTokenEntity<
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TToken>() where TToken : class
{
Services.Replace(ServiceDescriptor.Scoped<IOpenIddictTokenManager>(static provider =>
provider.GetRequiredService<OpenIddictTokenManager<TToken>>()));
return this;
}
/// <summary>
/// Configures OpenIddict to use the specified entity cache limit,
/// after which the internal cache is automatically compacted.
/// </summary>
/// <param name="limit">The cache limit, in number of entries.</param>
/// <returns>The <see cref="OpenIddictCoreBuilder"/> instance.</returns>
public OpenIddictCoreBuilder SetEntityCacheLimit(int limit)
{
if (limit < 10)
{
throw new ArgumentException(SR.GetResourceString(SR.ID0233), nameof(limit));
}
return Configure(options => options.EntityCacheLimit = limit);
}
/// <inheritdoc/>
[EditorBrowsable(EditorBrowsableState.Never)]
public override bool Equals(object? obj) => base.Equals(obj);
/// <inheritdoc/>
[EditorBrowsable(EditorBrowsableState.Never)]
public override int GetHashCode() => base.GetHashCode();
/// <inheritdoc/>
[EditorBrowsable(EditorBrowsableState.Never)]
public override string? ToString() => base.ToString();
}