Versatile OpenID Connect stack for ASP.NET Core and Microsoft.Owin (compatible with ASP.NET 4.6.1)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

207 lines
8.1 KiB

/*
* 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;
using OpenIddict.MongoDb;
using OpenIddict.MongoDb.Models;
namespace Microsoft.Extensions.DependencyInjection;
/// <summary>
/// Exposes the necessary methods required to configure the OpenIddict MongoDB services.
/// </summary>
public sealed class OpenIddictMongoDbBuilder
{
/// <summary>
/// Initializes a new instance of <see cref="OpenIddictMongoDbBuilder"/>.
/// </summary>
/// <param name="services">The services collection.</param>
public OpenIddictMongoDbBuilder(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 MongoDB 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="OpenIddictMongoDbBuilder"/> instance.</returns>
public OpenIddictMongoDbBuilder Configure(Action<OpenIddictMongoDbOptions> configuration)
{
if (configuration is null)
{
throw new ArgumentNullException(nameof(configuration));
}
Services.Configure(configuration);
return this;
}
/// <summary>
/// Configures OpenIddict to use the specified entity as the default application entity.
/// </summary>
/// <returns>The <see cref="OpenIddictMongoDbBuilder"/> instance.</returns>
public OpenIddictMongoDbBuilder ReplaceDefaultApplicationEntity<
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TApplication>()
where TApplication : OpenIddictMongoDbApplication
{
Services.Replace(ServiceDescriptor.Scoped<IOpenIddictApplicationManager>(static provider =>
provider.GetRequiredService<OpenIddictApplicationManager<TApplication>>()));
Services.Replace(ServiceDescriptor.Scoped<
IOpenIddictApplicationStore<TApplication>, OpenIddictMongoDbApplicationStore<TApplication>>());
return this;
}
/// <summary>
/// Configures OpenIddict to use the specified entity as the default authorization entity.
/// </summary>
/// <returns>The <see cref="OpenIddictMongoDbBuilder"/> instance.</returns>
public OpenIddictMongoDbBuilder ReplaceDefaultAuthorizationEntity<
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TAuthorization>()
where TAuthorization : OpenIddictMongoDbAuthorization
{
Services.Replace(ServiceDescriptor.Scoped<IOpenIddictAuthorizationManager>(static provider =>
provider.GetRequiredService<OpenIddictAuthorizationManager<TAuthorization>>()));
Services.Replace(ServiceDescriptor.Scoped<
IOpenIddictAuthorizationStore<TAuthorization>, OpenIddictMongoDbAuthorizationStore<TAuthorization>>());
return this;
}
/// <summary>
/// Configures OpenIddict to use the specified entity as the default scope entity.
/// </summary>
/// <returns>The <see cref="OpenIddictMongoDbBuilder"/> instance.</returns>
public OpenIddictMongoDbBuilder ReplaceDefaultScopeEntity<
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TScope>()
where TScope : OpenIddictMongoDbScope
{
Services.Replace(ServiceDescriptor.Scoped<IOpenIddictScopeManager>(static provider =>
provider.GetRequiredService<OpenIddictScopeManager<TScope>>()));
Services.Replace(ServiceDescriptor.Scoped<
IOpenIddictScopeStore<TScope>, OpenIddictMongoDbScopeStore<TScope>>());
return this;
}
/// <summary>
/// Configures OpenIddict to use the specified entity as the default token entity.
/// </summary>
/// <returns>The <see cref="OpenIddictMongoDbBuilder"/> instance.</returns>
public OpenIddictMongoDbBuilder ReplaceDefaultTokenEntity<
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TToken>()
where TToken : OpenIddictMongoDbToken
{
Services.Replace(ServiceDescriptor.Scoped<IOpenIddictTokenManager>(static provider =>
provider.GetRequiredService<OpenIddictTokenManager<TToken>>()));
Services.Replace(ServiceDescriptor.Scoped<
IOpenIddictTokenStore<TToken>, OpenIddictMongoDbTokenStore<TToken>>());
return this;
}
/// <summary>
/// Replaces the default applications collection name (by default, openiddict.applications).
/// </summary>
/// <param name="name">The collection name</param>
/// <returns>The <see cref="OpenIddictMongoDbBuilder"/> instance.</returns>
public OpenIddictMongoDbBuilder SetApplicationsCollectionName(string name)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentException(SR.GetResourceString(SR.ID0261), nameof(name));
}
return Configure(options => options.ApplicationsCollectionName = name);
}
/// <summary>
/// Replaces the default authorizations collection name (by default, openiddict.authorizations).
/// </summary>
/// <param name="name">The collection name</param>
/// <returns>The <see cref="OpenIddictMongoDbBuilder"/> instance.</returns>
public OpenIddictMongoDbBuilder SetAuthorizationsCollectionName(string name)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentException(SR.GetResourceString(SR.ID0261), nameof(name));
}
return Configure(options => options.AuthorizationsCollectionName = name);
}
/// <summary>
/// Replaces the default scopes collection name (by default, openiddict.scopes).
/// </summary>
/// <param name="name">The collection name</param>
/// <returns>The <see cref="OpenIddictMongoDbBuilder"/> instance.</returns>
public OpenIddictMongoDbBuilder SetScopesCollectionName(string name)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentException(SR.GetResourceString(SR.ID0261), nameof(name));
}
return Configure(options => options.ScopesCollectionName = name);
}
/// <summary>
/// Replaces the default tokens collection name (by default, openiddict.tokens).
/// </summary>
/// <param name="name">The collection name</param>
/// <returns>The <see cref="OpenIddictMongoDbBuilder"/> instance.</returns>
public OpenIddictMongoDbBuilder SetTokensCollectionName(string name)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentException(SR.GetResourceString(SR.ID0261), nameof(name));
}
return Configure(options => options.TokensCollectionName = name);
}
/// <summary>
/// Configures the MongoDB stores to use the specified database
/// instead of retrieving it from the dependency injection container.
/// </summary>
/// <param name="database">The <see cref="IMongoDatabase"/>.</param>
/// <returns>The <see cref="OpenIddictMongoDbBuilder"/> instance.</returns>
public OpenIddictMongoDbBuilder UseDatabase(IMongoDatabase database)
{
if (database is null)
{
throw new ArgumentNullException(nameof(database));
}
return Configure(options => options.Database = database);
}
/// <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();
}