/*
* 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;
using Microsoft.Extensions.DependencyInjection.Extensions;
using OpenIddict.MongoDb;
using OpenIddict.MongoDb.Models;
namespace Microsoft.Extensions.DependencyInjection
{
///
/// Exposes extensions allowing to register the OpenIddict MongoDB services.
///
public static class OpenIddictMongoDbExtensions
{
///
/// Registers the MongoDB stores services in the DI container and
/// configures OpenIddict to use the MongoDB entities by default.
///
/// The services builder used by OpenIddict to register new services.
/// This extension can be safely called multiple times.
/// The .
public static OpenIddictMongoDbBuilder UseMongoDb(this OpenIddictCoreBuilder builder)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
// Note: Mongo uses simple binary comparison checks by default so the additional
// query filtering applied by the default OpenIddict managers can be safely disabled.
builder.DisableAdditionalFiltering();
builder.SetDefaultApplicationEntity()
.SetDefaultAuthorizationEntity()
.SetDefaultScopeEntity()
.SetDefaultTokenEntity();
// Note: the Mongo stores/resolvers don't depend on scoped/transient services and thus
// can be safely registered as singleton services and shared/reused across requests.
builder.ReplaceApplicationStoreResolver(ServiceLifetime.Singleton)
.ReplaceAuthorizationStoreResolver(ServiceLifetime.Singleton)
.ReplaceScopeStoreResolver(ServiceLifetime.Singleton)
.ReplaceTokenStoreResolver(ServiceLifetime.Singleton);
builder.Services.TryAddSingleton(typeof(OpenIddictMongoDbApplicationStore<>));
builder.Services.TryAddSingleton(typeof(OpenIddictMongoDbAuthorizationStore<>));
builder.Services.TryAddSingleton(typeof(OpenIddictMongoDbScopeStore<>));
builder.Services.TryAddSingleton(typeof(OpenIddictMongoDbTokenStore<>));
builder.Services.TryAddSingleton();
return new OpenIddictMongoDbBuilder(builder.Services);
}
///
/// Registers the MongoDB stores services in the DI container and
/// configures OpenIddict to use the MongoDB entities by default.
///
/// The services builder used by OpenIddict to register new services.
/// The configuration delegate used to configure the MongoDB services.
/// This extension can be safely called multiple times.
/// The .
public static OpenIddictCoreBuilder UseMongoDb(
this OpenIddictCoreBuilder builder, Action configuration)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
if (configuration is null)
{
throw new ArgumentNullException(nameof(configuration));
}
configuration(builder.UseMongoDb());
return builder;
}
}
}