public TenantDataProviderImpl(IServiceProvider serviceProvider, ILogger <TenantDataProviderImpl> logger)
        {
            var scopeFactory = serviceProvider.GetRequiredService <IServiceScopeFactory>();

            var configuration = serviceProvider.GetRequiredService <IConfiguration>();

            int    httpHealthCheckPort     = configuration.GetValue <int>("WebServer:HttpHealthCheckPort");
            string tenantHealthCheckTenant = configuration["Identity:Tenants:HealthCheckTenant"];

            if (httpHealthCheckPort > 0 &&
                !string.IsNullOrEmpty(tenantHealthCheckTenant))
            {
                HealthCheckPort       = httpHealthCheckPort;
                HealthCheckTenantHost = tenantHealthCheckTenant;
            }

            var standardSamlCertificate = ReadStandardSamlCertificate(configuration);

            using (var scope = scopeFactory.CreateScope())
            {
                using (var dbContext = scope.ServiceProvider.GetRequiredService <SqlServerTenantDbContext>())
                {
                    IQueryable <DeveloperModel> query = dbContext.Developers;

                    query = query.Include(developerModel => developerModel.Tenants);

                    List <DeveloperModel> developers = query.ToList();

                    _developerMappings       = new Dictionary <string, DeveloperWrapper>();
                    _developerMappingsByUuid = new Dictionary <long, DeveloperWrapper>();

                    Developers = new List <IDeveloperInfo>();
                    Tenants    = new List <ITenantInfo>();

                    developers.ForEach(developer =>
                    {
                        var developerWrapper = new DeveloperWrapper(developer, standardSamlCertificate);

                        _developerMappings.Add(developer.HostPattern, developerWrapper);
                        _developerMappingsByUuid.Add(developer.Uuid, developerWrapper);

                        Tenants.AddRange(developerWrapper.TenantInfos);

                        X509Certificate2 developerCertificate = null;

                        if (!string.IsNullOrEmpty(developer.Certificate))
                        {
                            if (string.IsNullOrEmpty(developer.CertificatePassword))
                            {
                                throw new Exception("Developer in tenant database has certificate set, but no certificate password is present");
                            }

                            developerCertificate = new X509Certificate2(GetCertificateBytesFromPEM(developer.Certificate), developer.CertificatePassword);
                        }

                        if (string.IsNullOrEmpty(developer.Authority))
                        {
                            throw new Exception("The developer authority is empty");
                        }

                        if (string.IsNullOrEmpty(developer.Audience))
                        {
                            throw new Exception("The developer audience is empty");
                        }

                        if (string.IsNullOrEmpty(developer.AuthCookieDomain))
                        {
                            throw new Exception("The developer auth cookie domain is empty");
                        }

                        if (string.IsNullOrEmpty(developer.Name))
                        {
                            throw new Exception("The developer name is empty");
                        }

                        if (string.IsNullOrEmpty(developer.LogoSvgUrl))
                        {
                            throw new Exception("The developer logo SVG URL is empty");
                        }

                        if (string.IsNullOrEmpty(developer.LogoPngUrl))
                        {
                            throw new Exception("The developer logo PNG URL is empty");
                        }

                        if (string.IsNullOrEmpty(developer.IconIcoUrl))
                        {
                            throw new Exception("The developer icon ICO URL is empty");
                        }

                        // storage is optional

                        string storageImplementation = developer.StorageImplementation;
                        if (!string.IsNullOrEmpty(storageImplementation))
                        {
                            if (!storageImplementation.Equals(StorageConstants.StorageImplementationAzure) && !storageImplementation.Equals(StorageConstants.StorageImplementationGoogleCloud))
                            {
                                throw new Exception("The developer storage implementation specification is invalid");
                            }

                            if (string.IsNullOrEmpty(developer.StorageConnectionString))
                            {
                                throw new Exception("The developer storage connection string is empty");
                            }
                        }

                        if (string.IsNullOrEmpty(developer.SupportEmail))
                        {
                            throw new Exception("The developer support email is empty");
                        }

                        if (string.IsNullOrEmpty(developer.NoreplyEmail))
                        {
                            throw new Exception("The developer noreply email is empty");
                        }

                        if (string.IsNullOrEmpty(developer.ProductName))
                        {
                            throw new Exception("The developer product name is empty");
                        }

                        var developerInfo = new DeveloperInfoImpl()
                        {
                            DeveloperUuid           = developer.Uuid,
                            Authority               = developer.Authority,
                            Audience                = developer.Audience,
                            Certificate             = developerCertificate,
                            AuthCookieDomain        = developer.AuthCookieDomain,
                            Name                    = developer.Name,
                            LogoSvgUrl              = developer.LogoSvgUrl,
                            LogoPngUrl              = developer.LogoPngUrl,
                            IconIcoUrl              = developer.IconIcoUrl,
                            StorageImplementation   = developer.StorageImplementation,
                            StorageConnectionString = developer.StorageConnectionString,
                            PrimaryColor            = developer.PrimaryColor,
                            SecondaryColor          = developer.SecondaryColor,
                            TextOnPrimaryColor      = developer.TextOnPrimaryColor,
                            TextOnSecondaryColor    = developer.TextOnSecondaryColor,
                            SupportEmail            = developer.SupportEmail,
                            NoreplyEmail            = developer.NoreplyEmail,
                            ProductName             = developer.ProductName
                        };

                        Developers.Add(developerInfo);
                    });
                }
            }

            _logger = logger;
        }
Ejemplo n.º 2
0
        public TenantDataProviderImpl(IServiceProvider serviceProvider, ILogger <TenantDataProviderImpl> logger)
        {
            var scopeFactory = serviceProvider.GetRequiredService <IServiceScopeFactory>();

            using (var scope = scopeFactory.CreateScope())
            {
                using (var dbContext = scope.ServiceProvider.GetRequiredService <SqlServerTenantDbContext>())
                {
                    IQueryable <DeveloperModel> query = dbContext.Developers;

                    query = query.Include(developerModel => developerModel.Tenants);

                    List <DeveloperModel> developers = query.ToList();

                    _developerMappings       = new Dictionary <string, DeveloperWrapper>();
                    _developerMappingsByUuid = new Dictionary <long, DeveloperWrapper>();

                    Developers = new List <IDeveloperInfo>();
                    Tenants    = new List <ITenantInfo>();

                    developers.ForEach(developer =>
                    {
                        var developerWrapper = new DeveloperWrapper(developer);

                        _developerMappings.Add(developer.HostPattern, developerWrapper);
                        _developerMappingsByUuid.Add(developer.Uuid, developerWrapper);

                        Tenants.AddRange(developerWrapper.TenantInfos);

                        if (string.IsNullOrEmpty(developer.Authority))
                        {
                            throw new Exception("The developer authority is empty");
                        }

                        if (string.IsNullOrEmpty(developer.Audience))
                        {
                            throw new Exception("The developer audience is empty");
                        }

                        if (string.IsNullOrEmpty(developer.AuthCookieDomain))
                        {
                            throw new Exception("The developer auth cookie domain is empty");
                        }

                        if (string.IsNullOrEmpty(developer.Name))
                        {
                            throw new Exception("The developer name is empty");
                        }

                        if (string.IsNullOrEmpty(developer.LogoSvgUrl))
                        {
                            throw new Exception("The developer logo SVG URL is empty");
                        }

                        if (string.IsNullOrEmpty(developer.LogoPngUrl))
                        {
                            throw new Exception("The developer logo PNG URL is empty");
                        }

                        if (string.IsNullOrEmpty(developer.IconIcoUrl))
                        {
                            throw new Exception("The developer icon ICO URL is empty");
                        }

                        // storage is optional

                        string storageImplementation = developer.StorageImplementation;
                        if (!string.IsNullOrEmpty(storageImplementation))
                        {
                            if (!storageImplementation.Equals(StorageConstants.StorageImplementationAzure) && !storageImplementation.Equals(StorageConstants.StorageImplementationGoogleCloud))
                            {
                                throw new Exception("The developer storage implementation specification is invalid");
                            }

                            if (string.IsNullOrEmpty(developer.StorageConnectionString))
                            {
                                throw new Exception("The developer storage connection string is empty");
                            }
                        }

                        if (string.IsNullOrEmpty(developer.SupportEmail))
                        {
                            throw new Exception("The developer support email is empty");
                        }

                        if (string.IsNullOrEmpty(developer.NoreplyEmail))
                        {
                            throw new Exception("The developer noreply email is empty");
                        }

                        if (string.IsNullOrEmpty(developer.ProductName))
                        {
                            throw new Exception("The developer product name is empty");
                        }

                        var developerInfo = new DeveloperInfoImpl()
                        {
                            DeveloperUuid       = developer.Uuid,
                            Authority           = developer.Authority,
                            Audience            = developer.Audience,
                            Certificate         = developer.Certificate,
                            CertificatePassword = developer.CertificatePassword,
                            AuthCookieDomain    = developer.AuthCookieDomain,
                            Name                    = developer.Name,
                            LogoSvgUrl              = developer.LogoSvgUrl,
                            LogoPngUrl              = developer.LogoPngUrl,
                            IconIcoUrl              = developer.IconIcoUrl,
                            StorageImplementation   = developer.StorageImplementation,
                            StorageConnectionString = developer.StorageConnectionString,
                            PrimaryColor            = developer.PrimaryColor,
                            SecondaryColor          = developer.SecondaryColor,
                            TextOnPrimaryColor      = developer.TextOnPrimaryColor,
                            TextOnSecondaryColor    = developer.TextOnSecondaryColor,
                            SupportEmail            = developer.SupportEmail,
                            NoreplyEmail            = developer.NoreplyEmail,
                            ProductName             = developer.ProductName
                        };

                        Developers.Add(developerInfo);
                    });
                }
            }

            _logger = logger;
        }