1
0
mirror of https://github.com/bitwarden/server.git synced 2024-12-02 13:53:23 +01:00
bitwarden-server/src/Admin/Startup.cs

120 lines
3.9 KiB
C#
Raw Normal View History

2018-03-21 17:57:43 +01:00
using System;
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;
2018-06-10 05:57:26 +02:00
using Microsoft.AspNetCore.HttpOverrides;
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 Microsoft.Extensions.Logging;
using Serilog.Events;
using Stripe;
2018-03-21 17:57:43 +01:00
namespace Bit.Admin
{
public class Startup
{
public Startup(IHostingEnvironment env, IConfiguration configuration)
2018-03-21 17:57:43 +01:00
{
Configuration = configuration;
Environment = env;
2018-03-21 17:57:43 +01: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"));
// Data Protection
services.AddCustomDataProtectionServices(Environment, globalSettings);
2018-03-21 19:26:49 +01:00
// Stripe Billing
StripeConfiguration.SetApiKey(globalSettings.StripeApiKey);
// Repositories
services.AddSqlServerRepositories(globalSettings);
// Context
services.AddScoped<CurrentContext>();
// Identity
services.AddPasswordlessIdentityServices<ReadOnlyEnvIdentityUserStore>(globalSettings);
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);
// Jobs service
Jobs.JobsHostedService.AddJobsServices(services);
services.AddHostedService<Jobs.JobsHostedService>();
2019-03-05 05:41:46 +01:00
if(!globalSettings.SelfHosted)
{
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,
GlobalSettings globalSettings,
ILoggerFactory loggerFactory)
2018-03-21 17:57:43 +01:00
{
loggerFactory.AddSerilog(app, env, appLifetime, globalSettings, (e) => e.Level >= LogEventLevel.Error);
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");
2018-06-10 05:57:26 +02:00
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
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();
}
}
}