Ejemplo n.º 1
0
 public LockingClientStoreTests()
 {
     FakeFactory.Create(out _decorated, out _lockFactory);
     _lock = new AsyncReaderWriterLock();
     A.CallTo(() => _lockFactory.CreateLock())
     .Returns(_lock);
     _sut = new LockingClientStore(_decorated, _lockFactory);
 }
        /// <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);
            }));
        }