Пример #1
0
        /// <summary>Configures HTTP message signature verification to use a MongoDB <see cref="IClientStore"/>.</summary>
        /// <param name="builder">The <see cref="IHttpMessageSigningVerificationBuilder" /> that is used to configure verification.</param>
        /// <param name="clientStoreSettingsFactory">The factory that creates the settings for the Mongo connection.</param>
        /// <returns>The <see cref="IHttpMessageSigningVerificationBuilder" /> that can be used to continue configuring the verification settings.</returns>
        public static IHttpMessageSigningVerificationBuilder UseMongoDbClientStore(
            this IHttpMessageSigningVerificationBuilder builder,
            Func <IServiceProvider, MongoDbClientStoreSettings> clientStoreSettingsFactory)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }
            if (clientStoreSettingsFactory == null)
            {
                throw new ArgumentNullException(nameof(clientStoreSettingsFactory));
            }

            builder.Services
            // Services
            .AddMemoryCache()
            .AddSingleton <ISystemClock, RealSystemClock>()
            .AddSingleton <IDelayer, Delayer>()
            .AddSingleton <IBackgroundTaskStarter, BackgroundTaskStarter>()
            .AddSingleton(prov => {
                var settings = clientStoreSettingsFactory(prov);
                if (settings == null)
                {
                    throw new ValidationException($"Invalid {nameof(MongoDbClientStoreSettings)} were specified.");
                }
                settings.Validate();
                return(settings);
            })
            .AddSingleton <IMongoDatabaseClientProvider>(prov => {
                var mongoSettings = prov.GetRequiredService <MongoDbClientStoreSettings>();
                return(new MongoDatabaseClientProvider(mongoSettings.ConnectionString));
            })

            // ClientStore Migrations
            .AddSingleton <IClientStoreBaseliner, ClientStoreBaseliner>()
            .AddSingleton <ISemaphoreFactory, SemaphoreFactory>()
            .AddSingleton <IClientStoreMigrator>(prov =>
                                                 new OnlyOnceClientStoreMigrator(
                                                     new ClientStoreMigrator(
                                                         prov.GetRequiredService <IEnumerable <IClientStoreMigrationStep> >(),
                                                         prov.GetRequiredService <IClientStoreBaseliner>()),
                                                     prov.GetRequiredService <IClientStoreBaseliner>(),
                                                     prov.GetRequiredService <ISemaphoreFactory>()))
            .AddSingleton <IClientStoreMigrationStep, AddEncryptionSupportToClientsMigrationStep>();

            return(builder
                   // The actual store
                   .UseClientStore(prov => {
                var mongoSettings = prov.GetRequiredService <MongoDbClientStoreSettings>();
                return new CachingMongoDbClientStore(
                    new MongoDbClientStore(
                        prov.GetRequiredService <IMongoDatabaseClientProvider>(),
                        mongoSettings.CollectionName,
                        mongoSettings.SharedSecretEncryptionKey,
                        prov.GetRequiredService <IClientStoreMigrator>()),
                    prov.GetRequiredService <IMemoryCache>(),
                    mongoSettings.ClientCacheEntryExpiration,
                    prov.GetRequiredService <IBackgroundTaskStarter>());
            }));
        }
        /// <summary>Configures HTTP message signature verification to use a SQL Server <see cref="IClientStore"/>.</summary>
        /// <param name="builder">The <see cref="IHttpMessageSigningVerificationBuilder" /> that is used to configure verification.</param>
        /// <param name="clientStoreSettingsFactory">The factory that creates the settings for the SQL Server connection.</param>
        /// <returns>The <see cref="IHttpMessageSigningVerificationBuilder" /> that can be used to continue configuring the verification settings.</returns>
        public static IHttpMessageSigningVerificationBuilder UseSqlServerClientStore(
            this IHttpMessageSigningVerificationBuilder builder,
            Func <IServiceProvider, SqlServerClientStoreSettings> clientStoreSettingsFactory)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }
            if (clientStoreSettingsFactory == null)
            {
                throw new ArgumentNullException(nameof(clientStoreSettingsFactory));
            }

            builder.Services
            .AddMemoryCache()
            .AddSingleton <ISignatureAlgorithmConverter, SignatureAlgorithmConverter>()
            .AddSingleton(prov => {
                var settings = clientStoreSettingsFactory(prov);
                if (settings == null)
                {
                    throw new ValidationException($"Invalid {nameof(SqlServerClientStoreSettings)} were specified.");
                }
                settings.Validate();
                return(settings);
            });

            return(builder
                   // The actual store
                   .UseClientStore(prov => {
                var sqlSettings = prov.GetRequiredService <SqlServerClientStoreSettings>();
                var decorator = prov.GetRequiredService <ICachingClientStoreDecorator>();
                var store = new SqlServerClientStore(sqlSettings, prov.GetRequiredService <ISignatureAlgorithmConverter>());
                return decorator.DecorateWithCaching(store, sqlSettings.ClientCacheEntryExpiration);
            }));
        }
        /// <summary>Configures HTTP message signature verification to use a file system-backed <see cref="IClientStore"/>.</summary>
        /// <param name="builder">The <see cref="IHttpMessageSigningVerificationBuilder" /> that is used to configure verification.</param>
        /// <param name="clientStoreSettingsFactory">The factory that creates the settings for the file.</param>
        /// <returns>The <see cref="IHttpMessageSigningVerificationBuilder" /> that can be used to continue configuring the verification settings.</returns>
        public static IHttpMessageSigningVerificationBuilder UseFileSystemClientStore(
            this IHttpMessageSigningVerificationBuilder builder,
            Func <IServiceProvider, FileSystemClientStoreSettings> clientStoreSettingsFactory)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }
            if (clientStoreSettingsFactory == null)
            {
                throw new ArgumentNullException(nameof(clientStoreSettingsFactory));
            }

            builder.Services
            .AddMemoryCache()
            .AddSingleton <IFileReader, FileReader>()
            .AddSingleton <IFileWriter, FileWriter>()
            .AddSingleton <IClientDataRecordSerializer, ClientDataRecordSerializer>()
            .AddSingleton <ILockFactory, LockFactory>()
            .AddSingleton <ISignatureAlgorithmDataRecordConverter, SignatureAlgorithmDataRecordConverter>()
            .AddSingleton(prov => {
                var settings = clientStoreSettingsFactory(prov);
                if (settings == null)
                {
                    throw new ValidationException($"Invalid {nameof(FileSystemClientStoreSettings)} were specified.");
                }
                settings.Validate();
                return(settings);
            });

            return(builder
                   // The actual store
                   .UseClientStore(prov => {
                var settings = prov.GetRequiredService <FileSystemClientStoreSettings>();
                var decorator = prov.GetRequiredService <ICachingClientStoreDecorator>();
                var store = new LockingClientStore(
                    new FileSystemClientStore(
                        new ClientsFileManager(
                            prov.GetRequiredService <IFileReader>(),
                            prov.GetRequiredService <IFileWriter>(),
                            settings.FilePath,
                            prov.GetRequiredService <IClientDataRecordSerializer>()),
                        prov.GetRequiredService <ISignatureAlgorithmDataRecordConverter>(),
                        settings.SharedSecretEncryptionKey),
                    prov.GetRequiredService <ILockFactory>());
                return decorator.DecorateWithCaching(store, settings.ClientCacheEntryExpiration);
            }));
        }