1
0
mirror of https://github.com/bitwarden/server.git synced 2024-12-12 15:26:48 +01:00
bitwarden-server/src/Api/Startup.cs

213 lines
7.6 KiB
C#
Raw Normal View History

using Microsoft.AspNetCore.Builder;
2016-05-20 01:10:24 +02:00
using Microsoft.AspNetCore.Hosting;
2015-12-09 04:57:38 +01:00
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
2016-05-20 01:10:24 +02:00
using Microsoft.IdentityModel.Tokens;
2015-12-09 04:57:38 +01:00
using Bit.Api.Utilities;
using Bit.Core;
using Bit.Core.Identity;
using Newtonsoft.Json.Serialization;
2016-11-13 00:43:32 +01:00
using AspNetCoreRateLimit;
2017-01-15 05:24:02 +01:00
using Serilog.Events;
2017-04-04 16:13:16 +02:00
using Stripe;
using Bit.Core.Utilities;
using IdentityModel;
2018-05-22 03:24:35 +02:00
using Microsoft.AspNetCore.HttpOverrides;
2015-12-09 04:57:38 +01:00
namespace Bit.Api
{
public class Startup
{
public Startup(IHostingEnvironment env, IConfiguration configuration)
2015-12-09 04:57:38 +01:00
{
Configuration = configuration;
Environment = env;
2015-12-09 04:57:38 +01:00
}
public IConfiguration Configuration { get; private set; }
public IHostingEnvironment Environment { get; set; }
2015-12-09 04:57:38 +01:00
public void ConfigureServices(IServiceCollection services)
{
2016-05-20 01:10:24 +02:00
var provider = services.BuildServiceProvider();
2015-12-09 04:57:38 +01:00
// Options
services.AddOptions();
// Settings
var globalSettings = services.AddGlobalSettingsServices(Configuration);
if(!globalSettings.SelfHosted)
{
services.Configure<IpRateLimitOptions>(Configuration.GetSection("IpRateLimitOptions"));
services.Configure<IpRateLimitPolicies>(Configuration.GetSection("IpRateLimitPolicies"));
}
2015-12-09 04:57:38 +01:00
// Data Protection
services.AddCustomDataProtectionServices(Environment, globalSettings);
2017-04-04 16:13:16 +02:00
// Stripe Billing
StripeConfiguration.SetApiKey(globalSettings.StripeApiKey);
2015-12-09 04:57:38 +01:00
// Repositories
services.AddSqlServerRepositories(globalSettings);
2015-12-09 04:57:38 +01:00
// Context
services.AddScoped<CurrentContext>();
2016-11-13 00:43:32 +01:00
// Caching
services.AddMemoryCache();
if(!globalSettings.SelfHosted)
{
// Rate limiting
services.AddSingleton<IIpPolicyStore, MemoryCacheIpPolicyStore>();
services.AddSingleton<IRateLimitCounterStore, MemoryCacheRateLimitCounterStore>();
2019-02-22 04:43:37 +01:00
// BitPay
services.AddSingleton<BitPayClient>();
}
2016-11-13 00:43:32 +01:00
2015-12-09 04:57:38 +01:00
// Identity
services.AddCustomIdentityServices(globalSettings);
services.AddIdentityAuthenticationServices(globalSettings, Environment, config =>
2015-12-09 04:57:38 +01:00
{
config.AddPolicy("Application", policy =>
{
policy.RequireAuthenticatedUser();
policy.RequireClaim(JwtClaimTypes.AuthenticationMethod, "Application");
2017-06-26 15:09:30 +02:00
policy.RequireClaim(JwtClaimTypes.Scope, "api");
});
config.AddPolicy("Web", policy =>
{
policy.RequireAuthenticatedUser();
policy.RequireClaim(JwtClaimTypes.AuthenticationMethod, "Application");
policy.RequireClaim(JwtClaimTypes.Scope, "api");
policy.RequireClaim(JwtClaimTypes.ClientId, "web");
});
config.AddPolicy("Push", policy =>
{
policy.RequireAuthenticatedUser();
policy.RequireClaim(JwtClaimTypes.Scope, "api.push");
});
config.AddPolicy("Licensing", policy =>
{
policy.RequireAuthenticatedUser();
policy.RequireClaim(JwtClaimTypes.Scope, "api.licensing");
});
2019-02-26 23:01:33 +01:00
config.AddPolicy("Organization", policy =>
{
policy.RequireAuthenticatedUser();
policy.RequireClaim(JwtClaimTypes.Scope, "api.organization");
});
2015-12-09 04:57:38 +01:00
});
services.AddScoped<AuthenticatorTokenProvider>();
// Services
services.AddBaseServices();
2017-08-07 22:31:00 +02:00
services.AddDefaultServices(globalSettings);
2015-12-09 04:57:38 +01:00
// MVC
2015-12-31 00:49:43 +01:00
services.AddMvc(config =>
2015-12-09 04:57:38 +01:00
{
2019-02-28 20:20:14 +01:00
config.Conventions.Add(new ApiExplorerGroupConvention());
2019-03-01 02:50:40 +01:00
config.Conventions.Add(new PublicApiControllersModelConvention());
}).AddJsonOptions(options =>
{
if(Environment.IsProduction() && Configuration["swaggerGen"] != "true")
2019-03-01 02:50:40 +01:00
{
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
}
});
2017-10-25 06:45:11 +02:00
2019-02-28 20:20:14 +01:00
services.AddSwagger(globalSettings);
if(globalSettings.SelfHosted)
{
// Jobs service
Jobs.JobsHostedService.AddJobsServices(services);
services.AddHostedService<Jobs.JobsHostedService>();
}
2015-12-09 04:57:38 +01:00
}
public void Configure(
IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory,
2017-01-15 05:24:02 +01:00
IApplicationLifetime appLifetime,
GlobalSettings globalSettings)
2015-12-09 04:57:38 +01:00
{
2018-03-23 18:33:31 +01:00
loggerFactory.AddSerilog(app, env, appLifetime, globalSettings, (e) =>
2017-10-23 15:11:25 +02:00
{
var context = e.Properties["SourceContext"].ToString();
if(e.Exception != null && (e.Exception.GetType() == typeof(SecurityTokenValidationException) ||
e.Exception.Message == "Bad security stamp."))
2017-01-15 05:24:02 +01:00
{
2017-10-23 15:11:25 +02:00
return false;
}
if(e.Level == LogEventLevel.Information && context.Contains(typeof(IpRateLimitMiddleware).FullName))
2017-10-23 15:11:25 +02:00
{
return true;
}
if(context.Contains("IdentityServer4.Validation.TokenValidator") ||
context.Contains("IdentityServer4.Validation.TokenRequestValidator"))
{
return e.Level > LogEventLevel.Error;
}
2017-10-23 15:11:25 +02:00
return e.Level >= LogEventLevel.Error;
});
2017-01-15 05:24:02 +01:00
2017-08-25 14:57:43 +02:00
// Default Middleware
app.UseDefaultMiddleware(env);
2017-07-21 18:53:26 +02:00
if(!globalSettings.SelfHosted)
{
// Rate limiting
app.UseMiddleware<CustomIpRateLimitMiddleware>();
}
2018-05-22 03:24:35 +02:00
else
{
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
}
2016-11-13 00:43:32 +01:00
2015-12-09 04:57:38 +01:00
// Add static files to the request pipeline.
app.UseStaticFiles();
// Add Cors
2017-12-05 03:44:02 +01:00
app.UseCors(policy => policy.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials());
2015-12-09 04:57:38 +01:00
2017-10-06 20:02:28 +02:00
// Add authentication to the request pipeline.
app.UseAuthentication();
2017-10-09 22:21:49 +02:00
// Add current context
app.UseMiddleware<CurrentContextMiddleware>();
2017-04-19 22:01:34 +02:00
// Add MVC to the request pipeline.
app.UseMvc();
2019-03-08 21:48:34 +01:00
if(Environment.IsDevelopment() || globalSettings.SelfHosted)
2019-02-28 20:20:14 +01:00
{
app.UseSwagger(config =>
{
config.RouteTemplate = "specs/{documentName}/swagger.json";
});
app.UseSwaggerUI(config =>
{
2019-02-28 20:25:47 +01:00
config.DocumentTitle = "Bitwarden API Documentation";
2019-02-28 20:20:14 +01:00
config.RoutePrefix = "docs";
2019-03-08 21:48:34 +01:00
config.SwaggerEndpoint($"{globalSettings.BaseServiceUri.Api}/specs/public/swagger.json",
"Bitwarden Public API");
2019-02-28 20:20:14 +01:00
config.OAuthClientId("accountType.id");
config.OAuthClientSecret("secretKey");
});
}
2017-04-19 22:01:34 +02:00
}
2015-12-09 04:57:38 +01:00
}
}