Esempio n. 1
0
        public Task StartAsync(CancellationToken cancellationToken)
        {
            // Load Open Banking Connector configuration options
            ObcConfiguration obcConfig = _obcConfigurationProvider.GetObcConfiguration();

            // Ensure DB exists
            using IServiceScope scope   = _serviceScopeFactory.CreateScope();
            using BaseDbContext context = scope.ServiceProvider.GetRequiredService <BaseDbContext>();
            if (obcConfig.EnsureDbCreated == "true")
            {
                // Create DB if configured to do so and DB doesn't exist
                context.Database.EnsureCreated();
            }
            else
            {
                // Throw exception if DB doesn't exist
                IRelationalDatabaseCreator creator = context.Database.GetService <IRelationalDatabaseCreator>();
                if (!creator.Exists())
                {
                    throw new ApplicationException(
                              "No database found. Run 'dotnet ef database update' in OpenBanking.WebApp.Connector.Sample root folder to create test DB.");
                }
            }

            return(Task.CompletedTask);
        }
Esempio n. 2
0
        public static IServiceCollection AddOpenBankingConnector(
            this IServiceCollection services,
            IConfiguration configuration,
            bool loadSecretsFromConfig)
        {
            const string httpClientName = "OBC";

            services.AddHttpClient(httpClientName)
            .ConfigurePrimaryHttpMessageHandler(
                sp =>
            {
                ISoftwareStatementProfileService profileService =
                    sp.GetRequiredService <ISoftwareStatementProfileService>();

                // This can be removed in .NET 5 when SoftwareStatementProfileHostedService
                // and SocketsHttpHandler used.
                profileService.SetSoftwareStatementProfileFromSecretsSync();

                HttpMessageHandler handler = new HttpRequestBuilder()
                                             .SetClientCertificates(profileService.GetCertificates())
                                             .CreateMessageHandler();

                return(handler);
            });

            services.AddSingleton <IObcConfigurationProvider>(
                sp => new NetGenericHostObcConfigurationProvider(configuration));
            services.AddSingleton <IInstrumentationClient, LoggerInstrumentationClient>();
            if (loadSecretsFromConfig)
            {
                services.AddSingleton(
                    sp =>
                {
                    IObcConfigurationProvider configProvider = sp.GetService <IObcConfigurationProvider>();

                    KeySecretBuilder builder = new KeySecretBuilder();
                    return(builder.GetKeySecretProvider(
                               config: configuration,
                               obcConfig: configProvider.GetObcConfiguration()));
                });
            }
            else
            {
                services.AddSingleton <IKeySecretProvider, MemoryKeySecretProvider>();
            }

            services.AddSingleton(x => (IKeySecretReadOnlyProvider)x.GetRequiredService <IKeySecretProvider>());
            services.AddSingleton <IApiClient>(
                sp =>
            {
                IHttpClientFactory hcf = sp.GetService <IHttpClientFactory>();

                HttpClient client = hcf.CreateClient(httpClientName);

                return(new ApiClient(instrumentation: sp.GetService <IInstrumentationClient>(), httpClient: client));
            });
            services.AddSingleton <ICertificateReader, PemParsingCertificateReader>();
            services.AddSingleton <IEntityMapper, EntityMapper>();

            // Configure DB
            switch (configuration["DbProvider"])
            {
            case "Sqlite":
                services
                // See e.g. https://jasonwatmore.com/post/2020/01/03/aspnet-core-ef-core-migrations-for-multiple-databases-sqlite-and-sql-server
                .AddDbContext <BaseDbContext, SqliteDbContext>(
                    options =>
                {
                    string connectionString = configuration.GetConnectionString("SqliteDbContext");
                    options.UseSqlite(connectionString);
                });
                break;

            default:
                throw new ArgumentException(message: "Unknown DB provider", paramName: configuration["DbProvider"]);
            }

            services.AddScoped <IDbMultiEntityMethods,
                                DbMultiEntityMethods>();
            services.AddScoped <IDbEntityRepository <BankClientProfile>,
                                DbEntityRepository <BankClientProfile> >();
            services.AddScoped <IDbEntityRepository <ApiProfile>,
                                DbEntityRepository <ApiProfile> >();
            services.AddScoped <IDbEntityRepository <DomesticConsent>,
                                DbEntityRepository <DomesticConsent> >();
            services.AddSingleton <IKeySecretReadRepository <ActiveSoftwareStatementProfiles>,
                                   KeySecretReadRepository <ActiveSoftwareStatementProfiles> >();
            services.AddSingleton <IKeySecretWriteRepository <ActiveSoftwareStatementProfiles>,
                                   KeySecretWriteRepository <ActiveSoftwareStatementProfiles> >();
            services.AddSingleton <IKeySecretMultiItemWriteRepository <SoftwareStatementProfile>,
                                   KeySecretMultiItemWriteRepository <SoftwareStatementProfile> >();
            services.AddSingleton <IKeySecretMultiItemReadRepository <SoftwareStatementProfile>,
                                   KeySecretMultiItemReadRepository <SoftwareStatementProfile> >();
            services.AddSingleton <ISoftwareStatementProfileService, SoftwareStatementProfileService>();
            services.AddScoped <IOpenBankingRequestBuilder, RequestBuilder>();

            // Startup tasks
            services.AddHostedService <StartupTasksHostedService>();

            // This can be enabled in .NET 5 when SoftwareStatementProfileHostedService
            // and SocketsHttpHandler used.
            // services.AddHostedService<SoftwareStatementProfileHostedService>();

            return(services);
        }