2018-03-21 17:57:43 +01:00
|
|
|
|
using System;
|
2019-07-11 21:03:17 +02:00
|
|
|
|
using System.Globalization;
|
2021-02-04 19:54:21 +01:00
|
|
|
|
using Bit.Core.Context;
|
2018-03-21 19:26:49 +01:00
|
|
|
|
using Bit.Core.Identity;
|
2021-02-22 22:35:16 +01:00
|
|
|
|
using Bit.Core.Settings;
|
2018-03-21 19:26:49 +01:00
|
|
|
|
using Bit.Core.Utilities;
|
2022-01-11 10:40:51 +01:00
|
|
|
|
using Bit.SharedWeb.Utilities;
|
2018-03-21 17:57:43 +01:00
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2019-01-17 22:07:24 +01:00
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
2018-03-21 17:57:43 +01:00
|
|
|
|
using Microsoft.AspNetCore.Routing;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2020-01-10 14:33:13 +01:00
|
|
|
|
using Microsoft.Extensions.Hosting;
|
2018-03-21 19:26:49 +01:00
|
|
|
|
using Stripe;
|
2018-03-21 17:57:43 +01:00
|
|
|
|
|
2021-06-30 09:35:26 +02:00
|
|
|
|
#if !OSS
|
|
|
|
|
using Bit.CommCore.Utilities;
|
|
|
|
|
#endif
|
|
|
|
|
|
2018-03-21 17:57:43 +01:00
|
|
|
|
namespace Bit.Admin
|
|
|
|
|
{
|
|
|
|
|
public class Startup
|
|
|
|
|
{
|
2020-01-10 14:33:13 +01:00
|
|
|
|
public Startup(IWebHostEnvironment env, IConfiguration configuration)
|
2018-03-21 17:57:43 +01:00
|
|
|
|
{
|
2019-07-11 21:03:17 +02:00
|
|
|
|
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US");
|
2018-03-21 17:57:43 +01:00
|
|
|
|
Configuration = configuration;
|
2018-03-28 03:37:35 +02:00
|
|
|
|
Environment = env;
|
2018-03-21 17:57:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-28 03:37:35 +02:00
|
|
|
|
public IConfiguration Configuration { get; private set; }
|
2020-01-10 14:33:13 +01:00
|
|
|
|
public IWebHostEnvironment Environment { get; set; }
|
2018-03-21 17:57:43 +01:00
|
|
|
|
|
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
|
{
|
2018-03-21 19:26:49 +01:00
|
|
|
|
// Options
|
|
|
|
|
services.AddOptions();
|
|
|
|
|
|
|
|
|
|
// Settings
|
2022-03-21 23:13:00 +01:00
|
|
|
|
var globalSettings = services.AddGlobalSettingsServices(Configuration, Environment);
|
2018-03-21 19:26:49 +01:00
|
|
|
|
services.Configure<AdminSettings>(Configuration.GetSection("AdminSettings"));
|
|
|
|
|
|
2018-03-28 03:37:35 +02:00
|
|
|
|
// Data Protection
|
|
|
|
|
services.AddCustomDataProtectionServices(Environment, globalSettings);
|
|
|
|
|
|
2018-03-21 19:26:49 +01:00
|
|
|
|
// Stripe Billing
|
2021-11-29 01:01:51 +01:00
|
|
|
|
StripeConfiguration.ApiKey = globalSettings.Stripe.ApiKey;
|
|
|
|
|
StripeConfiguration.MaxNetworkRetries = globalSettings.Stripe.MaxNetworkRetries;
|
2018-03-21 19:26:49 +01:00
|
|
|
|
|
|
|
|
|
// Repositories
|
|
|
|
|
services.AddSqlServerRepositories(globalSettings);
|
|
|
|
|
|
|
|
|
|
// Context
|
2021-02-04 19:54:21 +01:00
|
|
|
|
services.AddScoped<ICurrentContext, CurrentContext>();
|
2018-03-21 19:26:49 +01:00
|
|
|
|
|
|
|
|
|
// Identity
|
|
|
|
|
services.AddPasswordlessIdentityServices<ReadOnlyEnvIdentityUserStore>(globalSettings);
|
2019-01-17 22:07:24 +01:00
|
|
|
|
services.Configure<SecurityStampValidatorOptions>(options =>
|
|
|
|
|
{
|
|
|
|
|
options.ValidationInterval = TimeSpan.FromMinutes(5);
|
|
|
|
|
});
|
2020-03-27 19:36:37 +01:00
|
|
|
|
if (globalSettings.SelfHosted)
|
2018-03-28 16:38:01 +02:00
|
|
|
|
{
|
|
|
|
|
services.ConfigureApplicationCookie(options =>
|
|
|
|
|
{
|
|
|
|
|
options.Cookie.Path = "/admin";
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-03-21 19:26:49 +01:00
|
|
|
|
|
|
|
|
|
// Services
|
2022-05-10 23:12:09 +02:00
|
|
|
|
services.AddBaseServices(globalSettings);
|
2018-03-21 19:26:49 +01:00
|
|
|
|
services.AddDefaultServices(globalSettings);
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-30 09:35:26 +02:00
|
|
|
|
#if OSS
|
|
|
|
|
services.AddOosServices();
|
|
|
|
|
#else
|
|
|
|
|
services.AddCommCoreServices();
|
|
|
|
|
#endif
|
2018-03-21 19:26:49 +01:00
|
|
|
|
|
|
|
|
|
// Mvc
|
|
|
|
|
services.AddMvc(config =>
|
|
|
|
|
{
|
|
|
|
|
config.Filters.Add(new LoggingExceptionHandlerFilterAttribute());
|
|
|
|
|
});
|
2018-03-21 17:57:43 +01:00
|
|
|
|
services.Configure<RouteOptions>(options => options.LowercaseUrls = true);
|
2018-10-09 16:12:27 +02:00
|
|
|
|
|
|
|
|
|
// Jobs service
|
2019-11-04 13:43:15 +01:00
|
|
|
|
Jobs.JobsHostedService.AddJobsServices(services, globalSettings.SelfHosted);
|
2018-10-09 16:12:27 +02:00
|
|
|
|
services.AddHostedService<Jobs.JobsHostedService>();
|
2020-03-27 19:36:37 +01:00
|
|
|
|
if (globalSettings.SelfHosted)
|
2019-03-25 18:21:05 +01:00
|
|
|
|
{
|
|
|
|
|
services.AddHostedService<HostedServices.DatabaseMigrationHostedService>();
|
|
|
|
|
}
|
|
|
|
|
else
|
2019-03-05 05:41:46 +01:00
|
|
|
|
{
|
2020-03-27 19:36:37 +01:00
|
|
|
|
if (CoreHelpers.SettingHasValue(globalSettings.Storage.ConnectionString))
|
2019-03-18 21:23:37 +01:00
|
|
|
|
{
|
|
|
|
|
services.AddHostedService<HostedServices.AzureQueueBlockIpHostedService>();
|
|
|
|
|
}
|
2020-03-27 19:36:37 +01:00
|
|
|
|
else if (CoreHelpers.SettingHasValue(globalSettings.Amazon?.AccessKeySecret))
|
2019-03-18 21:23:37 +01:00
|
|
|
|
{
|
|
|
|
|
services.AddHostedService<HostedServices.AmazonSqsBlockIpHostedService>();
|
|
|
|
|
}
|
2021-05-17 16:43:02 +02:00
|
|
|
|
if (CoreHelpers.SettingHasValue(globalSettings.Mail.ConnectionString))
|
|
|
|
|
{
|
|
|
|
|
services.AddHostedService<HostedServices.AzureQueueMailHostedService>();
|
|
|
|
|
}
|
2019-03-05 05:41:46 +01:00
|
|
|
|
}
|
2018-03-21 17:57:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-21 19:26:49 +01:00
|
|
|
|
public void Configure(
|
|
|
|
|
IApplicationBuilder app,
|
2020-01-10 14:33:13 +01:00
|
|
|
|
IWebHostEnvironment env,
|
|
|
|
|
IHostApplicationLifetime appLifetime,
|
2019-07-23 22:38:49 +02:00
|
|
|
|
GlobalSettings globalSettings)
|
2018-03-21 17:57:43 +01:00
|
|
|
|
{
|
2019-07-23 22:38:49 +02:00
|
|
|
|
app.UseSerilog(env, appLifetime, globalSettings);
|
2018-05-21 19:31:47 +02:00
|
|
|
|
|
2021-11-09 17:37:14 +01:00
|
|
|
|
// Add general security headers
|
|
|
|
|
app.UseMiddleware<SecurityHeadersMiddleware>();
|
|
|
|
|
|
2020-03-27 19:36:37 +01:00
|
|
|
|
if (globalSettings.SelfHosted)
|
2018-03-21 17:57:43 +01:00
|
|
|
|
{
|
2018-03-24 13:39:55 +01:00
|
|
|
|
app.UsePathBase("/admin");
|
2019-04-26 15:52:54 +02:00
|
|
|
|
app.UseForwardedHeaders(globalSettings);
|
2018-03-21 17:57:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-27 19:36:37 +01:00
|
|
|
|
if (env.IsDevelopment())
|
2018-03-24 04:27:33 +01:00
|
|
|
|
{
|
2018-03-24 13:39:55 +01:00
|
|
|
|
app.UseDeveloperExceptionPage();
|
2018-03-24 04:27:33 +01:00
|
|
|
|
}
|
2021-08-10 18:28:00 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
app.UseExceptionHandler("/error");
|
|
|
|
|
}
|
2018-03-24 04:27:33 +01:00
|
|
|
|
|
2018-03-21 17:57:43 +01:00
|
|
|
|
app.UseStaticFiles();
|
2020-01-10 14:33:13 +01:00
|
|
|
|
app.UseRouting();
|
|
|
|
|
app.UseAuthentication();
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
app.UseEndpoints(endpoints => endpoints.MapDefaultControllerRoute());
|
2018-03-21 17:57:43 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|