using System; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Configuration; using Bit.Core; using Bit.Core.Utilities; using Serilog.Events; using AspNetCoreRateLimit; namespace Bit.Identity { public class Startup { public Startup(IHostingEnvironment env, IConfiguration configuration) { Configuration = configuration; Environment = env; } public IConfiguration Configuration { get; private set; } public IHostingEnvironment Environment { get; set; } public void ConfigureServices(IServiceCollection services) { // Options services.AddOptions(); // Settings var globalSettings = services.AddGlobalSettingsServices(Configuration); if(!globalSettings.SelfHosted) { services.Configure(Configuration.GetSection("IpRateLimitOptions")); services.Configure(Configuration.GetSection("IpRateLimitPolicies")); } // Data Protection services.AddCustomDataProtectionServices(Environment, globalSettings); // Repositories services.AddSqlServerRepositories(globalSettings); // Context services.AddScoped(); // Caching services.AddMemoryCache(); if(!globalSettings.SelfHosted) { // Rate limiting services.AddSingleton(); services.AddSingleton(); } // IdentityServer services.AddCustomIdentityServerServices(Environment, globalSettings); // Identity services.AddCustomIdentityServices(globalSettings); // Services services.AddBaseServices(); services.AddDefaultServices(globalSettings); } public void Configure( IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifetime, GlobalSettings globalSettings) { loggerFactory.AddSerilog(env, appLifetime, globalSettings, (e) => { var context = e.Properties["SourceContext"].ToString(); if(context.Contains("IdentityServer4.Validation.TokenValidator") || context.Contains("IdentityServer4.Validation.TokenRequestValidator")) { return e.Level > LogEventLevel.Error; } if(context.Contains(typeof(IpRateLimitMiddleware).FullName) && e.Level == LogEventLevel.Information) { return true; } return e.Level >= LogEventLevel.Error; }); // Default Middleware app.UseDefaultMiddleware(env); if(!globalSettings.SelfHosted) { // Rate limiting app.UseMiddleware(); } // Add current context app.UseMiddleware(); // Add IdentityServer to the request pipeline. app.UseIdentityServer(); } } }