2018-03-21 17:57:43 +01:00
|
|
|
|
using System;
|
2019-07-11 21:03:17 +02:00
|
|
|
|
using System.Globalization;
|
2018-03-21 19:26:49 +01:00
|
|
|
|
using Bit.Core;
|
|
|
|
|
using Bit.Core.Identity;
|
|
|
|
|
using Bit.Core.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;
|
2018-03-21 19:26:49 +01:00
|
|
|
|
using Stripe;
|
2018-03-21 17:57:43 +01:00
|
|
|
|
|
|
|
|
|
namespace Bit.Admin
|
|
|
|
|
{
|
|
|
|
|
public class Startup
|
|
|
|
|
{
|
2018-03-28 03:37:35 +02:00
|
|
|
|
public Startup(IHostingEnvironment 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; }
|
|
|
|
|
public IHostingEnvironment 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
|
|
|
|
|
var globalSettings = services.AddGlobalSettingsServices(Configuration);
|
|
|
|
|
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
|
2019-08-08 23:36:41 +02:00
|
|
|
|
StripeConfiguration.ApiKey = globalSettings.StripeApiKey;
|
2018-03-21 19:26:49 +01:00
|
|
|
|
|
|
|
|
|
// Repositories
|
|
|
|
|
services.AddSqlServerRepositories(globalSettings);
|
|
|
|
|
|
|
|
|
|
// Context
|
|
|
|
|
services.AddScoped<CurrentContext>();
|
|
|
|
|
|
|
|
|
|
// Identity
|
|
|
|
|
services.AddPasswordlessIdentityServices<ReadOnlyEnvIdentityUserStore>(globalSettings);
|
2019-01-17 22:07:24 +01:00
|
|
|
|
services.Configure<SecurityStampValidatorOptions>(options =>
|
|
|
|
|
{
|
|
|
|
|
options.ValidationInterval = TimeSpan.FromMinutes(5);
|
|
|
|
|
});
|
2018-03-28 16:38:01 +02:00
|
|
|
|
if(globalSettings.SelfHosted)
|
|
|
|
|
{
|
|
|
|
|
services.ConfigureApplicationCookie(options =>
|
|
|
|
|
{
|
|
|
|
|
options.Cookie.Path = "/admin";
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-03-21 19:26:49 +01:00
|
|
|
|
|
|
|
|
|
// Services
|
|
|
|
|
services.AddBaseServices();
|
|
|
|
|
services.AddDefaultServices(globalSettings);
|
|
|
|
|
|
|
|
|
|
// 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>();
|
2019-03-25 18:21:05 +01:00
|
|
|
|
if(globalSettings.SelfHosted)
|
|
|
|
|
{
|
|
|
|
|
services.AddHostedService<HostedServices.DatabaseMigrationHostedService>();
|
|
|
|
|
}
|
|
|
|
|
else
|
2019-03-05 05:41:46 +01:00
|
|
|
|
{
|
2019-03-18 21:23:37 +01:00
|
|
|
|
if(CoreHelpers.SettingHasValue(globalSettings.Storage.ConnectionString))
|
|
|
|
|
{
|
|
|
|
|
services.AddHostedService<HostedServices.AzureQueueBlockIpHostedService>();
|
|
|
|
|
}
|
|
|
|
|
else if(CoreHelpers.SettingHasValue(globalSettings.Amazon?.AccessKeySecret))
|
|
|
|
|
{
|
|
|
|
|
services.AddHostedService<HostedServices.AmazonSqsBlockIpHostedService>();
|
|
|
|
|
}
|
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,
|
|
|
|
|
IHostingEnvironment env,
|
|
|
|
|
IApplicationLifetime 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
|
|
|
|
|
2018-03-24 13:39:55 +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
|
|
|
|
}
|
|
|
|
|
|
2018-03-24 13:39:55 +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
|
|
|
|
}
|
|
|
|
|
|
2018-03-21 19:26:49 +01:00
|
|
|
|
app.UseAuthentication();
|
2018-03-21 17:57:43 +01:00
|
|
|
|
app.UseStaticFiles();
|
|
|
|
|
app.UseMvcWithDefaultRoute();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|