예제 #1
0
 public StoreRepository(ApplicationDbContextFactory contextFactory)
 {
     _ContextFactory = contextFactory ?? throw new ArgumentNullException(nameof(contextFactory));
 }
예제 #2
0
 public FinderController(ApplicationDbContextFactory dbFactory, UserManager <User> userManager)
 {
     _userManager = userManager;
     DbFactory    = dbFactory;
 }
 public MessagesDatabaseSeeder(ApplicationDbContextFactory contextFactory)
 {
     _dbContextFactory = contextFactory;
 }
 public SettingsRepository(ApplicationDbContextFactory contextFactory, EventAggregator eventAggregator)
 {
     _ContextFactory  = contextFactory;
     _EventAggregator = eventAggregator;
 }
예제 #5
0
        public static IServiceCollection AddBTCPayServer(this IServiceCollection services)
        {
            services.AddDbContext <ApplicationDbContext>((provider, o) =>
            {
                var factory = provider.GetRequiredService <ApplicationDbContextFactory>();
                factory.ConfigureBuilder(o);
            });
            services.TryAddSingleton <SettingsRepository>();
            services.TryAddSingleton <InvoicePaymentNotification>();
            services.TryAddSingleton <BTCPayServerOptions>(o => o.GetRequiredService <IOptions <BTCPayServerOptions> >().Value);
            services.TryAddSingleton <InvoiceRepository>(o =>
            {
                var opts      = o.GetRequiredService <BTCPayServerOptions>();
                var dbContext = o.GetRequiredService <ApplicationDbContextFactory>();
                var dbpath    = Path.Combine(opts.DataDir, "InvoiceDB");
                if (!Directory.Exists(dbpath))
                {
                    Directory.CreateDirectory(dbpath);
                }
                return(new InvoiceRepository(dbContext, dbpath));
            });
            services.AddSingleton <BTCPayServerEnvironment>();
            services.TryAddSingleton <TokenRepository>();
            services.TryAddSingleton <EventAggregator>();
            services.TryAddSingleton <ICoinAverageAuthenticator, BTCPayCoinAverageAuthenticator>();
            services.TryAddSingleton <ApplicationDbContextFactory>(o =>
            {
                var opts = o.GetRequiredService <BTCPayServerOptions>();
                ApplicationDbContextFactory dbContext = null;
                if (opts.PostgresConnectionString == null)
                {
                    var connStr = "Data Source=" + Path.Combine(opts.DataDir, "sqllite.db");
                    Logs.Configuration.LogInformation($"SQLite DB used ({connStr})");
                    dbContext = new ApplicationDbContextFactory(DatabaseType.Sqlite, connStr);
                }
                else
                {
                    Logs.Configuration.LogInformation($"Postgres DB used ({opts.PostgresConnectionString})");
                    dbContext = new ApplicationDbContextFactory(DatabaseType.Postgres, opts.PostgresConnectionString);
                }
                return(dbContext);
            });
            services.TryAddSingleton <Payments.Lightning.LightningClientFactory>();

            services.TryAddSingleton <BTCPayNetworkProvider>(o =>
            {
                var opts = o.GetRequiredService <BTCPayServerOptions>();
                return(opts.NetworkProvider);
            });

            services.TryAddSingleton <LanguageService>();
            services.TryAddSingleton <NBXplorerDashboard>();
            services.TryAddSingleton <StoreRepository>();
            services.TryAddSingleton <BTCPayWalletProvider>();
            services.TryAddSingleton <CurrencyNameTable>();
            services.TryAddSingleton <IFeeProviderFactory>(o => new NBXplorerFeeProviderFactory(o.GetRequiredService <ExplorerClientProvider>())
            {
                Fallback    = new FeeRate(100, 1),
                BlockTarget = 20
            });

            services.AddSingleton <Payments.IPaymentMethodHandler <DerivationStrategy>, Payments.Bitcoin.BitcoinLikePaymentHandler>();
            services.AddSingleton <IHostedService, Payments.Bitcoin.NBXplorerListener>();

            services.AddSingleton <Payments.IPaymentMethodHandler <Payments.Lightning.LightningSupportedPaymentMethod>, Payments.Lightning.LightningLikePaymentHandler>();
            services.AddSingleton <IHostedService, Payments.Lightning.LightningListener>();

            services.AddSingleton <IHostedService, NBXplorerWaiters>();
            services.AddSingleton <IHostedService, InvoiceNotificationManager>();
            services.AddSingleton <IHostedService, InvoiceWatcher>();
            services.AddSingleton <IHostedService, RatesHostedService>();

            services.TryAddSingleton <ExplorerClientProvider>();
            services.TryAddSingleton <Bitpay>(o =>
            {
                if (o.GetRequiredService <BTCPayServerOptions>().ChainType == ChainType.Main)
                {
                    return(new Bitpay(new Key(), new Uri("https://bitpay.com/")));
                }
                else
                {
                    return(new Bitpay(new Key(), new Uri("https://test.bitpay.com/")));
                }
            });
            services.TryAddSingleton <IRateProviderFactory, BTCPayRateProviderFactory>();

            services.TryAddScoped <IHttpContextAccessor, HttpContextAccessor>();
            services.TryAddSingleton <IAuthorizationHandler, OwnStoreHandler>();
            services.AddTransient <AccessTokenController>();
            services.AddTransient <InvoiceController>();
            // Add application services.
            services.AddTransient <IEmailSender, EmailSender>();

            services.AddAuthorization(o =>
            {
                o.AddPolicy(StorePolicies.CanAccessStores, builder =>
                {
                    builder.AddRequirements(new OwnStoreAuthorizationRequirement());
                });

                o.AddPolicy(StorePolicies.OwnStore, builder =>
                {
                    builder.AddRequirements(new OwnStoreAuthorizationRequirement(StoreRoles.Owner));
                });
            });

            // bundling

            services.AddBundles();
            services.AddTransient <BundleOptions>(provider =>
            {
                var opts   = provider.GetRequiredService <BTCPayServerOptions>();
                var bundle = new BundleOptions();
                bundle.UseMinifiedFiles = opts.BundleJsCss;
                bundle.AppendVersion    = true;
                return(bundle);
            });

            return(services);
        }
        /// <summary>Registers the type mappings with the Unity container.</summary>
        /// <param name="container">The unity container to configure.</param>
        public static void RegisterTypes(IUnityContainer container)
        {
            ApplicationDbContextFactory factory;
            switch (ChooseBookStoreFromConfig())
            {
                case BookStoreFlag.Datastore:
                    container.RegisterInstance<IBookStore>(
                        new DatastoreBookStore(ProjectId));
                    break;

                case BookStoreFlag.MySql:
                    factory = new ApplicationDbContextFactory();
                    container.RegisterType<ApplicationDbContext>(
                        new InjectionFactory((x) => factory.Create()));
                    container.RegisterType<IBookStore, DbBookStore>();
                    break;
            }

            container.RegisterInstance<ImageUploader>(
                new ImageUploader(
                  GetConfigVariable("GoogleCloudSamples:BucketName"),
                  "Bookshelf.NET-Step3"
                )
            );
        }
예제 #7
0
 public APIKeyRepository(ApplicationDbContextFactory applicationDbContextFactory)
 {
     _applicationDbContextFactory = applicationDbContextFactory;
 }
예제 #8
0
 public Fido2Service(ApplicationDbContextFactory contextFactory, IFido2 fido2)
 {
     _contextFactory = contextFactory;
     _fido2          = fido2;
 }
예제 #9
0
 public ScheduleDataService(ApplicationDbContextFactory dbContextFactory, NonQueryDataService <Schedule> scheduleNonQueryDataService, NonQueryDataService <Reservation> reservationNonQueryDataService)
 {
     _dbContextFactory               = dbContextFactory;
     _scheduleNonQueryDataService    = scheduleNonQueryDataService;
     _reservationNonQueryDataService = reservationNonQueryDataService;
 }
예제 #10
0
 public NotificationManager(ApplicationDbContextFactory factory, UserManager <ApplicationUser> userManager, IMemoryCache memoryCache)
 {
     _factory     = factory;
     _userManager = userManager;
     _memoryCache = memoryCache;
 }
예제 #11
0
 public NonQueryDataService(ApplicationDbContextFactory contextFactory)
 {
     _contextFactory = contextFactory;
 }
 public DocumentStatusController(ApplicationDbContextFactory applicationDbContextFactory)
 {
     this.applicationDbContextFactory = applicationDbContextFactory;
 }
예제 #13
0
 public RespostaController()
 {
     dbContext = ApplicationDbContextFactory.Create();
 }
 public TokenRepository(ApplicationDbContextFactory dbFactory)
 {
     if (dbFactory == null)
         throw new ArgumentNullException(nameof(dbFactory));
     _Factory = dbFactory;
 }
예제 #15
0
        /// <summary>
        /// Expression converting Advertisment to ViewModel
        /// </summary>

        /*private readonly Expression<Func<Advertisment, AdvertismentViewModel>> _toModel =
         *  a => new AdvertismentViewModel()
         *  {
         *      Id = a.Id,
         *      Since = a.Since,
         *      Details = a.Details,
         *      Location = a.Location,
         *      Until = a.Until
         *  };*/

        public AdvertismentController(ApplicationDbContextFactory dbFactory)
        {
            DbFactory = dbFactory;
        }
예제 #16
0
 public TokenRepository(ApplicationDbContextFactory dbFactory)
 {
     ArgumentNullException.ThrowIfNull(dbFactory);
     _Factory = dbFactory;
 }
 public InMemoryDatabaseFactory()
 {
     this.Context = ApplicationDbContextFactory.CreateInMemoryDatabase();
 }
예제 #18
0
        public static IServiceCollection AddBTCPayServer(this IServiceCollection services, IConfiguration configuration)
        {
            services.AddDbContext <ApplicationDbContext>((provider, o) =>
            {
                var factory = provider.GetRequiredService <ApplicationDbContextFactory>();
                factory.ConfigureBuilder(o);
                o.UseOpenIddict <BTCPayOpenIdClient, BTCPayOpenIdAuthorization, OpenIddictScope <string>, BTCPayOpenIdToken, string>();
            });
            services.AddHttpClient();
            services.AddHttpClient(nameof(ExplorerClientProvider), httpClient =>
            {
                httpClient.Timeout = Timeout.InfiniteTimeSpan;
            });
            services.TryAddSingleton <SettingsRepository>();
            services.TryAddSingleton <TorServices>();
            services.TryAddSingleton <SocketFactory>();
            services.TryAddSingleton <LightningClientFactoryService>();
            services.TryAddSingleton <InvoicePaymentNotification>();
            services.TryAddSingleton <BTCPayServerOptions>(o =>
                                                           o.GetRequiredService <IOptions <BTCPayServerOptions> >().Value);
            services.AddStartupTask <MigrationStartupTask>();
            services.TryAddSingleton <InvoiceRepository>(o =>
            {
                var opts      = o.GetRequiredService <BTCPayServerOptions>();
                var dbContext = o.GetRequiredService <ApplicationDbContextFactory>();
                var dbpath    = Path.Combine(opts.DataDir, "InvoiceDB");
                if (!Directory.Exists(dbpath))
                {
                    Directory.CreateDirectory(dbpath);
                }
                return(new InvoiceRepository(dbContext, dbpath, o.GetRequiredService <BTCPayNetworkProvider>()));
            });
            services.AddSingleton <BTCPayServerEnvironment>();
            services.TryAddSingleton <TokenRepository>();
            services.TryAddSingleton <WalletRepository>();
            services.TryAddSingleton <EventAggregator>();
            services.TryAddSingleton <PaymentRequestService>();
            services.TryAddSingleton <U2FService>();
            services.TryAddSingleton <CoinAverageSettings>();
            services.TryAddSingleton <ApplicationDbContextFactory>(o =>
            {
                var opts = o.GetRequiredService <BTCPayServerOptions>();
                ApplicationDbContextFactory dbContext = null;
                if (!String.IsNullOrEmpty(opts.PostgresConnectionString))
                {
                    Logs.Configuration.LogInformation($"Postgres DB used ({opts.PostgresConnectionString})");
                    dbContext = new ApplicationDbContextFactory(DatabaseType.Postgres, opts.PostgresConnectionString);
                }
                else if (!String.IsNullOrEmpty(opts.MySQLConnectionString))
                {
                    Logs.Configuration.LogInformation($"MySQL DB used ({opts.MySQLConnectionString})");
                    Logs.Configuration.LogWarning("MySQL is not widely tested and should be considered experimental, we advise you to use postgres instead.");
                    dbContext = new ApplicationDbContextFactory(DatabaseType.MySQL, opts.MySQLConnectionString);
                }
                else
                {
                    var connStr = "Data Source=" + Path.Combine(opts.DataDir, "sqllite.db");
                    Logs.Configuration.LogInformation($"SQLite DB used ({connStr})");
                    Logs.Configuration.LogWarning("MySQL is not widely tested and should be considered experimental, we advise you to use postgres instead.");
                    dbContext = new ApplicationDbContextFactory(DatabaseType.Sqlite, connStr);
                }

                return(dbContext);
            });

            services.TryAddSingleton <BTCPayNetworkProvider>(o =>
            {
                var opts = o.GetRequiredService <BTCPayServerOptions>();
                return(opts.NetworkProvider);
            });

            services.TryAddSingleton <AppService>();
            services.TryAddTransient <Safe>();
            services.TryAddSingleton <Ganss.XSS.HtmlSanitizer>(o =>
            {
                var htmlSanitizer = new Ganss.XSS.HtmlSanitizer();


                htmlSanitizer.RemovingAtRule += (sender, args) =>
                {
                };
                htmlSanitizer.RemovingTag += (sender, args) =>
                {
                    if (args.Tag.TagName.Equals("img", StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (!args.Tag.ClassList.Contains("img-fluid"))
                        {
                            args.Tag.ClassList.Add("img-fluid");
                        }

                        args.Cancel = true;
                    }
                };

                htmlSanitizer.RemovingAttribute += (sender, args) =>
                {
                    if (args.Tag.TagName.Equals("img", StringComparison.InvariantCultureIgnoreCase) &&
                        args.Attribute.Name.Equals("src", StringComparison.InvariantCultureIgnoreCase) &&
                        args.Reason == Ganss.XSS.RemoveReason.NotAllowedUrlValue)
                    {
                        args.Cancel = true;
                    }
                };
                htmlSanitizer.RemovingStyle += (sender, args) => { args.Cancel = true; };
                htmlSanitizer.AllowedAttributes.Add("class");
                htmlSanitizer.AllowedTags.Add("iframe");
                htmlSanitizer.AllowedTags.Remove("img");
                htmlSanitizer.AllowedAttributes.Add("webkitallowfullscreen");
                htmlSanitizer.AllowedAttributes.Add("allowfullscreen");
                return(htmlSanitizer);
            });

            services.TryAddSingleton <LightningConfigurationProvider>();
            services.TryAddSingleton <LanguageService>();
            services.TryAddSingleton <NBXplorerDashboard>();
            services.TryAddSingleton <StoreRepository>();
            services.TryAddSingleton <PaymentRequestRepository>();
            services.TryAddSingleton <BTCPayWalletProvider>();
            services.TryAddSingleton <CurrencyNameTable>();
            services.TryAddSingleton <IFeeProviderFactory>(o => new NBXplorerFeeProviderFactory(o.GetRequiredService <ExplorerClientProvider>())
            {
                Fallback    = new FeeRate(100L, 1),
                BlockTarget = 20
            });

            services.AddSingleton <CssThemeManager>();
            services.Configure <MvcOptions>((o) => {
                o.Filters.Add(new ContentSecurityPolicyCssThemeManager());
                o.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(WalletId)));
                o.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(DerivationStrategyBase)));
            });
            services.AddSingleton <IHostedService, CssThemeManagerHostedService>();

            services.AddSingleton <HostedServices.CheckConfigurationHostedService>();
            services.AddSingleton <IHostedService, HostedServices.CheckConfigurationHostedService>(o => o.GetRequiredService <CheckConfigurationHostedService>());

            services.AddSingleton <BitcoinLikePaymentHandler>();
            services.AddSingleton <IPaymentMethodHandler>(provider => provider.GetService <BitcoinLikePaymentHandler>());
            services.AddSingleton <IHostedService, NBXplorerListener>();

            services.AddSingleton <LightningLikePaymentHandler>();
            services.AddSingleton <IPaymentMethodHandler>(provider => provider.GetService <LightningLikePaymentHandler>());
            services.AddSingleton <IHostedService, LightningListener>();

            services.AddSingleton <PaymentMethodHandlerDictionary>();

            services.AddSingleton <ChangellyClientProvider>();

            services.AddSingleton <IHostedService, NBXplorerWaiters>();
            services.AddSingleton <IHostedService, InvoiceNotificationManager>();
            services.AddSingleton <IHostedService, InvoiceWatcher>();
            services.AddSingleton <IHostedService, RatesHostedService>();
            services.AddSingleton <IHostedService, BackgroundJobSchedulerHostedService>();
            services.AddSingleton <IHostedService, AppHubStreamer>();
            services.AddSingleton <IHostedService, AppInventoryUpdaterHostedService>();
            services.AddSingleton <IHostedService, DynamicDnsHostedService>();
            services.AddSingleton <IHostedService, TorServicesHostedService>();
            services.AddSingleton <IHostedService, PaymentRequestStreamer>();
            services.AddSingleton <IBackgroundJobClient, BackgroundJobClient>();
            services.AddTransient <IConfigureOptions <MvcOptions>, BTCPayClaimsFilter>();

            services.TryAddSingleton <ExplorerClientProvider>();
            services.TryAddSingleton <Bitpay>(o =>
            {
                if (o.GetRequiredService <BTCPayServerOptions>().NetworkType == NetworkType.Mainnet)
                {
                    return(new Bitpay(new Key(), new Uri("https://bitpay.com/")));
                }
                else
                {
                    return(new Bitpay(new Key(), new Uri("https://test.bitpay.com/")));
                }
            });
            services.TryAddSingleton <RateProviderFactory>();
            services.TryAddSingleton <RateFetcher>();

            services.TryAddScoped <IHttpContextAccessor, HttpContextAccessor>();
            services.AddTransient <AccessTokenController>();
            services.AddTransient <InvoiceController>();
            services.AddTransient <AppsPublicController>();
            services.AddTransient <PaymentRequestController>();
            // Add application services.
            services.AddSingleton <EmailSenderFactory>();
            // bundling

            services.AddAuthorization(o => Policies.AddBTCPayPolicies(o));
            services.AddBtcPayServerAuthenticationSchemes(configuration);

            services.AddSingleton <IBundleProvider, ResourceBundleProvider>();
            services.AddTransient <BundleOptions>(provider =>
            {
                var opts             = provider.GetRequiredService <BTCPayServerOptions>();
                var bundle           = new BundleOptions();
                bundle.UseBundles    = opts.BundleJsCss;
                bundle.AppendVersion = true;
                return(bundle);
            });

            services.AddCors(options =>
            {
                options.AddPolicy(CorsPolicies.All, p => p.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());
            });

            var rateLimits = new RateLimitService();

            rateLimits.SetZone($"zone={ZoneLimits.Login} rate=5r/min burst=3 nodelay");
            services.AddSingleton(rateLimits);
            return(services);
        }
 public DbMigrationsHostedService(InvoiceRepository invoiceRepository, SettingsRepository settingsRepository, ApplicationDbContextFactory dbContextFactory, DataDirectories datadirs)
 {
     _invoiceRepository  = invoiceRepository;
     _settingsRepository = settingsRepository;
     _dbContextFactory   = dbContextFactory;
     _datadirs           = datadirs;
 }
예제 #20
0
        public static IServiceCollection AddBTCPayServer(this IServiceCollection services, IConfiguration configuration)
        {
            services.AddSingleton <MvcNewtonsoftJsonOptions>(o => o.GetRequiredService <IOptions <MvcNewtonsoftJsonOptions> >().Value);
            services.AddDbContext <ApplicationDbContext>((provider, o) =>
            {
                var factory = provider.GetRequiredService <ApplicationDbContextFactory>();
                factory.ConfigureBuilder(o);
            });
            services.AddHttpClient();
            services.AddHttpClient(nameof(ExplorerClientProvider), httpClient =>
            {
                httpClient.Timeout = Timeout.InfiniteTimeSpan;
            });

            services.AddSingleton <BTCPayNetworkJsonSerializerSettings>();
            services.RegisterJsonConverter(n => new ClaimDestinationJsonConverter(n));

            services.AddPayJoinServices();
#if ALTCOINS
            services.AddMoneroLike();
            services.AddEthereumLike();
#endif
            services.TryAddSingleton <SettingsRepository>();
            services.TryAddSingleton <ISettingsRepository>(provider => provider.GetService <SettingsRepository>());
            services.TryAddSingleton <LabelFactory>();
            services.TryAddSingleton <TorServices>();
            services.TryAddSingleton <SocketFactory>();
            services.TryAddSingleton <LightningClientFactoryService>();
            services.TryAddSingleton <InvoicePaymentNotification>();
            services.TryAddSingleton <BTCPayServerOptions>(o =>
                                                           o.GetRequiredService <IOptions <BTCPayServerOptions> >().Value);
            services.AddStartupTask <MigrationStartupTask>();
            services.AddStartupTask <BlockExplorerLinkStartupTask>();
            services.TryAddSingleton <InvoiceRepository>(o =>
            {
                var opts      = o.GetRequiredService <BTCPayServerOptions>();
                var dbContext = o.GetRequiredService <ApplicationDbContextFactory>();
                var dbpath    = Path.Combine(opts.DataDir, "InvoiceDB");
                if (!Directory.Exists(dbpath))
                {
                    Directory.CreateDirectory(dbpath);
                }
                return(new InvoiceRepository(dbContext, dbpath, o.GetRequiredService <BTCPayNetworkProvider>(), o.GetService <EventAggregator>()));
            });
            services.AddSingleton <BTCPayServerEnvironment>();
            services.TryAddSingleton <TokenRepository>();
            services.TryAddSingleton <WalletRepository>();
            services.TryAddSingleton <EventAggregator>();
            services.TryAddSingleton <PaymentRequestService>();
            services.TryAddSingleton <U2FService>();
            services.TryAddSingleton <ApplicationDbContextFactory>(o =>
            {
                var opts = o.GetRequiredService <BTCPayServerOptions>();
                ApplicationDbContextFactory dbContext = null;
                if (!string.IsNullOrEmpty(opts.PostgresConnectionString))
                {
                    Logs.Configuration.LogInformation($"Postgres DB used");
                    dbContext = new ApplicationDbContextFactory(DatabaseType.Postgres, opts.PostgresConnectionString);
                }
                else if (!string.IsNullOrEmpty(opts.MySQLConnectionString))
                {
                    Logs.Configuration.LogInformation($"MySQL DB used");
                    Logs.Configuration.LogWarning("MySQL is not widely tested and should be considered experimental, we advise you to use postgres instead.");
                    dbContext = new ApplicationDbContextFactory(DatabaseType.MySQL, opts.MySQLConnectionString);
                }
                else if (!string.IsNullOrEmpty(opts.SQLiteFileName))
                {
                    var connStr = "Data Source=" + (Path.IsPathRooted(opts.SQLiteFileName)
                        ? opts.SQLiteFileName
                        : Path.Combine(opts.DataDir, opts.SQLiteFileName));
                    Logs.Configuration.LogInformation($"SQLite DB used");
                    Logs.Configuration.LogWarning("SQLite is not widely tested and should be considered experimental, we advise you to use postgres instead.");
                    dbContext = new ApplicationDbContextFactory(DatabaseType.Sqlite, connStr);
                }
                else
                {
                    throw new ConfigException("No database option was configured.");
                }

                return(dbContext);
            });

            services.TryAddSingleton <BTCPayNetworkProvider>(o =>
            {
                var opts = o.GetRequiredService <BTCPayServerOptions>();
                return(opts.NetworkProvider);
            });

            services.TryAddSingleton <AppService>();
            services.AddSingleton <PluginService>();
            services.TryAddTransient <Safe>();
            services.TryAddSingleton <Ganss.XSS.HtmlSanitizer>(o =>
            {
                var htmlSanitizer = new Ganss.XSS.HtmlSanitizer();


                htmlSanitizer.RemovingAtRule += (sender, args) =>
                {
                };
                htmlSanitizer.RemovingTag += (sender, args) =>
                {
                    if (args.Tag.TagName.Equals("img", StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (!args.Tag.ClassList.Contains("img-fluid"))
                        {
                            args.Tag.ClassList.Add("img-fluid");
                        }

                        args.Cancel = true;
                    }
                };

                htmlSanitizer.RemovingAttribute += (sender, args) =>
                {
                    if (args.Tag.TagName.Equals("img", StringComparison.InvariantCultureIgnoreCase) &&
                        args.Attribute.Name.Equals("src", StringComparison.InvariantCultureIgnoreCase) &&
                        args.Reason == Ganss.XSS.RemoveReason.NotAllowedUrlValue)
                    {
                        args.Cancel = true;
                    }
                };
                htmlSanitizer.RemovingStyle += (sender, args) => { args.Cancel = true; };
                htmlSanitizer.AllowedAttributes.Add("class");
                htmlSanitizer.AllowedTags.Add("iframe");
                htmlSanitizer.AllowedTags.Add("style");
                htmlSanitizer.AllowedTags.Remove("img");
                htmlSanitizer.AllowedAttributes.Add("webkitallowfullscreen");
                htmlSanitizer.AllowedAttributes.Add("allowfullscreen");
                return(htmlSanitizer);
            });

            services.TryAddSingleton <LightningConfigurationProvider>();
            services.TryAddSingleton <LanguageService>();
            services.TryAddSingleton <NBXplorerDashboard>();
            services.TryAddSingleton <ISyncSummaryProvider, NBXSyncSummaryProvider>();
            services.TryAddSingleton <StoreRepository>();
            services.TryAddSingleton <PaymentRequestRepository>();
            services.TryAddSingleton <BTCPayWalletProvider>();
            services.TryAddSingleton <WalletReceiveStateService>();
            services.TryAddSingleton <CurrencyNameTable>(CurrencyNameTable.Instance);
            services.TryAddSingleton <IFeeProviderFactory>(o => new NBXplorerFeeProviderFactory(o.GetRequiredService <ExplorerClientProvider>())
            {
                Fallback = new FeeRate(100L, 1)
            });

            services.AddSingleton <CssThemeManager>();
            services.Configure <MvcOptions>((o) =>
            {
                o.Filters.Add(new ContentSecurityPolicyCssThemeManager());
                o.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(WalletId)));
                o.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(DerivationStrategyBase)));
            });
            services.AddSingleton <IHostedService, CssThemeManagerHostedService>();

            services.AddSingleton <HostedServices.CheckConfigurationHostedService>();
            services.AddSingleton <IHostedService, HostedServices.CheckConfigurationHostedService>(o => o.GetRequiredService <CheckConfigurationHostedService>());

            services.AddSingleton <HostedServices.PullPaymentHostedService>();
            services.AddSingleton <IHostedService, HostedServices.PullPaymentHostedService>(o => o.GetRequiredService <PullPaymentHostedService>());

            services.AddSingleton <BitcoinLikePaymentHandler>();
            services.AddSingleton <IPaymentMethodHandler>(provider => provider.GetService <BitcoinLikePaymentHandler>());
            services.AddSingleton <IHostedService, NBXplorerListener>();

            services.AddSingleton <LightningLikePaymentHandler>();
            services.AddSingleton <IPaymentMethodHandler>(provider => provider.GetService <LightningLikePaymentHandler>());
            services.AddSingleton <IHostedService, LightningListener>();

            services.AddSingleton <PaymentMethodHandlerDictionary>();

            services.AddSingleton <NotificationManager>();
            services.AddScoped <NotificationSender>();

            services.AddSingleton <IHostedService, NBXplorerWaiters>();
            services.AddSingleton <IHostedService, InvoiceEventSaverService>();
            services.AddSingleton <IHostedService, InvoiceNotificationManager>();
            services.AddSingleton <IHostedService, InvoiceWatcher>();
            services.AddSingleton <IHostedService, RatesHostedService>();
            services.AddSingleton <IHostedService, BackgroundJobSchedulerHostedService>();
            services.AddSingleton <IHostedService, AppHubStreamer>();
            services.AddSingleton <IHostedService, AppInventoryUpdaterHostedService>();
            services.AddSingleton <IHostedService, TransactionLabelMarkerHostedService>();
            services.AddSingleton <IHostedService, UserEventHostedService>();
            services.AddSingleton <IHostedService, DynamicDnsHostedService>();
            services.AddSingleton <IHostedService, TorServicesHostedService>();
            services.AddSingleton <IHostedService, PaymentRequestStreamer>();
            services.AddSingleton <IHostedService, WalletReceiveCacheUpdater>();
            services.AddSingleton <IBackgroundJobClient, BackgroundJobClient>();
            services.AddScoped <IAuthorizationHandler, CookieAuthorizationHandler>();
            services.AddScoped <IAuthorizationHandler, BitpayAuthorizationHandler>();

            services.AddSingleton <IVersionFetcher, GithubVersionFetcher>();
            services.AddSingleton <IHostedService, NewVersionCheckerHostedService>();
            services.AddSingleton <INotificationHandler, NewVersionNotification.Handler>();

            services.AddSingleton <INotificationHandler, InvoiceEventNotification.Handler>();
            services.AddSingleton <INotificationHandler, PayoutNotification.Handler>();

            services.AddShopify();
#if DEBUG
            services.AddSingleton <INotificationHandler, JunkNotification.Handler>();
#endif
            services.TryAddSingleton <ExplorerClientProvider>();
            services.TryAddSingleton <Bitpay>(o =>
            {
                if (o.GetRequiredService <BTCPayServerOptions>().NetworkType == NetworkType.Mainnet)
                {
                    return(new Bitpay(new Key(), new Uri("https://bitpay.com/")));
                }
                else
                {
                    return(new Bitpay(new Key(), new Uri("https://test.bitpay.com/")));
                }
            });
            services.TryAddSingleton <RateProviderFactory>();
            services.TryAddSingleton <RateFetcher>();

            services.TryAddScoped <IHttpContextAccessor, HttpContextAccessor>();
            services.AddTransient <AccessTokenController>();
            services.AddTransient <InvoiceController>();
            services.AddTransient <AppsPublicController>();
            services.AddTransient <PaymentRequestController>();
            // Add application services.
            services.AddSingleton <EmailSenderFactory>();

            services.AddAPIKeyAuthentication();
            services.AddBtcPayServerAuthenticationSchemes();
            services.AddAuthorization(o => o.AddBTCPayPolicies());
            // bundling
            services.AddSingleton <IBundleProvider, ResourceBundleProvider>();
            services.AddTransient <BundleOptions>(provider =>
            {
                var opts             = provider.GetRequiredService <BTCPayServerOptions>();
                var bundle           = new BundleOptions();
                bundle.UseBundles    = opts.BundleJsCss;
                bundle.AppendVersion = true;
                return(bundle);
            });

            services.AddCors(options =>
            {
                options.AddPolicy(CorsPolicies.All, p => p.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());
            });
            services.AddSingleton(provider =>
            {
                var btcPayEnv  = provider.GetService <BTCPayServerEnvironment>();
                var rateLimits = new RateLimitService();
                if (btcPayEnv.IsDeveloping)
                {
                    rateLimits.SetZone($"zone={ZoneLimits.Login} rate=1000r/min burst=100 nodelay");
                    rateLimits.SetZone($"zone={ZoneLimits.Register} rate=1000r/min burst=100 nodelay");
                    rateLimits.SetZone($"zone={ZoneLimits.PayJoin} rate=1000r/min burst=100 nodelay");
                    rateLimits.SetZone($"zone={ZoneLimits.Shopify} rate=1000r/min burst=100 nodelay");
                }
                else
                {
                    rateLimits.SetZone($"zone={ZoneLimits.Login} rate=5r/min burst=3 nodelay");
                    rateLimits.SetZone($"zone={ZoneLimits.Register} rate=2r/min burst=2 nodelay");
                    rateLimits.SetZone($"zone={ZoneLimits.PayJoin} rate=5r/min burst=3 nodelay");
                    rateLimits.SetZone($"zone={ZoneLimits.Shopify} rate=20r/min burst=3 nodelay");
                }
                return(rateLimits);
            });
            services.AddLogging(logBuilder =>
            {
                var debugLogFile = BTCPayServerOptions.GetDebugLog(configuration);
                if (!string.IsNullOrEmpty(debugLogFile))
                {
                    Serilog.Log.Logger = new LoggerConfiguration()
                                         .Enrich.FromLogContext()
                                         .MinimumLevel.Is(BTCPayServerOptions.GetDebugLogLevel(configuration))
                                         .WriteTo.File(debugLogFile, rollingInterval: RollingInterval.Day, fileSizeLimitBytes: MAX_DEBUG_LOG_FILE_SIZE, rollOnFileSizeLimit: true, retainedFileCountLimit: 1)
                                         .CreateLogger();
                    logBuilder.AddProvider(new Serilog.Extensions.Logging.SerilogLoggerProvider(Log.Logger));
                }
            });
            return(services);
        }
예제 #21
0
 public AppsHelper(ApplicationDbContextFactory contextFactory, CurrencyNameTable currencies)
 {
     _ContextFactory = contextFactory;
     _Currencies     = currencies;
 }
        /// <summary>Registers the type mappings with the Unity container.</summary>
        /// <param name="container">The unity container to configure.</param>
        public static void RegisterTypes(IUnityContainer container)
        {
            ApplicationDbContextFactory factory;
            switch (ChooseBookStoreFromConfig())
            {
                case BookStoreFlag.Datastore:
                    container.RegisterInstance<IBookStore>(
                        new DatastoreBookStore(ProjectId));
                    break;

                case BookStoreFlag.MySql:
                    factory = new ApplicationDbContextFactory();
                    container.RegisterType<ApplicationDbContext>(
                        new InjectionFactory((x) => factory.Create()));
                    container.RegisterType<IBookStore, DbBookStore>();
                    break;
            }
        }
 public PaymentRequestRepository(ApplicationDbContextFactory contextFactory, InvoiceRepository invoiceRepository)
 {
     _ContextFactory    = contextFactory;
     _InvoiceRepository = invoiceRepository;
 }