2015-12-09 04:57:38 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Security.Claims;
|
2016-05-20 01:10:24 +02:00
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
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;
|
2016-07-14 00:37:14 +02:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Formatters;
|
|
|
|
|
using Microsoft.Net.Http.Headers;
|
2016-07-14 01:24:26 +02:00
|
|
|
|
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;
|
2017-05-06 02:57:33 +02:00
|
|
|
|
using Bit.Core.Utilities;
|
2017-06-07 05:19:42 +02:00
|
|
|
|
using IdentityModel;
|
2017-10-06 17:38:47 +02:00
|
|
|
|
using IdentityServer4.AccessTokenValidation;
|
2017-10-25 06:45:11 +02:00
|
|
|
|
using jsreport.AspNetCore;
|
2017-12-04 16:59:07 +01:00
|
|
|
|
using Bit.Core.IdentityServer;
|
2015-12-09 04:57:38 +01:00
|
|
|
|
|
|
|
|
|
namespace Bit.Api
|
|
|
|
|
{
|
|
|
|
|
public class Startup
|
|
|
|
|
{
|
2017-10-19 06:08:09 +02:00
|
|
|
|
public Startup(IHostingEnvironment env, IConfiguration configuration)
|
2015-12-09 04:57:38 +01:00
|
|
|
|
{
|
2017-10-19 06:08:09 +02:00
|
|
|
|
Configuration = configuration;
|
2017-01-13 03:07:25 +01:00
|
|
|
|
Environment = env;
|
2015-12-09 04:57:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-19 06:08:09 +02:00
|
|
|
|
public IConfiguration Configuration { get; private set; }
|
2017-01-13 03:07:25 +01:00
|
|
|
|
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
|
2017-05-06 02:57:33 +02:00
|
|
|
|
var globalSettings = services.AddGlobalSettingsServices(Configuration);
|
2017-10-25 16:59:30 +02:00
|
|
|
|
services.Configure<ApiSettings>(Configuration.GetSection("apiSettings"));
|
2017-08-15 22:33:38 +02:00
|
|
|
|
if(!globalSettings.SelfHosted)
|
|
|
|
|
{
|
|
|
|
|
services.Configure<IpRateLimitOptions>(Configuration.GetSection("IpRateLimitOptions"));
|
|
|
|
|
services.Configure<IpRateLimitPolicies>(Configuration.GetSection("IpRateLimitPolicies"));
|
|
|
|
|
}
|
2015-12-09 04:57:38 +01:00
|
|
|
|
|
2017-03-24 03:02:55 +01:00
|
|
|
|
// Data Protection
|
2017-05-06 02:57:33 +02:00
|
|
|
|
services.AddCustomDataProtectionServices(Environment, globalSettings);
|
2017-03-24 03:02:55 +01:00
|
|
|
|
|
2017-04-04 16:13:16 +02:00
|
|
|
|
// Stripe Billing
|
|
|
|
|
StripeConfiguration.SetApiKey(globalSettings.StripeApiKey);
|
|
|
|
|
|
2015-12-09 04:57:38 +01:00
|
|
|
|
// Repositories
|
2017-12-01 15:22:04 +01:00
|
|
|
|
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();
|
|
|
|
|
|
2017-08-15 22:33:38 +02:00
|
|
|
|
if(!globalSettings.SelfHosted)
|
|
|
|
|
{
|
|
|
|
|
// Rate limiting
|
|
|
|
|
services.AddSingleton<IIpPolicyStore, MemoryCacheIpPolicyStore>();
|
|
|
|
|
services.AddSingleton<IRateLimitCounterStore, MemoryCacheRateLimitCounterStore>();
|
|
|
|
|
}
|
2016-11-13 00:43:32 +01:00
|
|
|
|
|
2015-12-09 04:57:38 +01:00
|
|
|
|
// Identity
|
2017-05-06 02:57:33 +02:00
|
|
|
|
services.AddCustomIdentityServices(globalSettings);
|
2015-12-09 04:57:38 +01:00
|
|
|
|
|
2017-10-06 17:38:47 +02:00
|
|
|
|
services
|
|
|
|
|
.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
|
|
|
|
|
.AddIdentityServerAuthentication(options =>
|
|
|
|
|
{
|
|
|
|
|
options.Authority = globalSettings.BaseServiceUri.InternalIdentity;
|
|
|
|
|
options.RequireHttpsMetadata = !Environment.IsDevelopment() &&
|
|
|
|
|
globalSettings.BaseServiceUri.InternalIdentity.StartsWith("https");
|
|
|
|
|
options.NameClaimType = ClaimTypes.Email;
|
2017-10-17 14:54:49 +02:00
|
|
|
|
options.TokenRetriever = TokenRetrieval.FromAuthorizationHeaderOrQueryString(
|
|
|
|
|
new string[] { "Bearer", "Bearer3" });
|
2017-10-06 20:02:28 +02:00
|
|
|
|
options.SupportedTokens = SupportedTokens.Jwt;
|
2017-10-06 17:38:47 +02:00
|
|
|
|
});
|
|
|
|
|
|
2015-12-31 00:49:43 +01:00
|
|
|
|
services.AddAuthorization(config =>
|
2015-12-09 04:57:38 +01:00
|
|
|
|
{
|
2017-01-11 06:34:16 +01:00
|
|
|
|
config.AddPolicy("Application", policy =>
|
|
|
|
|
{
|
|
|
|
|
policy.RequireAuthenticatedUser();
|
2017-06-07 05:19:42 +02:00
|
|
|
|
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");
|
2017-01-11 06:34:16 +01:00
|
|
|
|
});
|
2017-08-10 20:39:11 +02:00
|
|
|
|
config.AddPolicy("Push", policy =>
|
|
|
|
|
{
|
|
|
|
|
policy.RequireAuthenticatedUser();
|
|
|
|
|
policy.RequireClaim(JwtClaimTypes.Scope, "api.push");
|
|
|
|
|
});
|
2017-08-30 17:23:55 +02:00
|
|
|
|
config.AddPolicy("Licensing", policy =>
|
|
|
|
|
{
|
|
|
|
|
policy.RequireAuthenticatedUser();
|
|
|
|
|
policy.RequireClaim(JwtClaimTypes.Scope, "api.licensing");
|
|
|
|
|
});
|
2015-12-09 04:57:38 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
services.AddScoped<AuthenticatorTokenProvider>();
|
|
|
|
|
|
|
|
|
|
// Services
|
2017-04-26 22:13:24 +02:00
|
|
|
|
services.AddBaseServices();
|
2017-08-07 22:31:00 +02:00
|
|
|
|
services.AddDefaultServices(globalSettings);
|
2015-12-09 04:57:38 +01:00
|
|
|
|
|
|
|
|
|
// Cors
|
2015-12-31 00:49:43 +01:00
|
|
|
|
services.AddCors(config =>
|
|
|
|
|
{
|
2016-07-14 00:37:14 +02:00
|
|
|
|
config.AddPolicy("All", policy =>
|
|
|
|
|
policy.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().SetPreflightMaxAge(TimeSpan.FromDays(1)));
|
2015-12-31 00:49:43 +01:00
|
|
|
|
});
|
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
|
|
|
|
{
|
2015-12-31 00:49:43 +01:00
|
|
|
|
config.Filters.Add(new ExceptionHandlerFilterAttribute());
|
|
|
|
|
config.Filters.Add(new ModelStateValidationFilterAttribute());
|
2016-07-14 00:42:57 +02:00
|
|
|
|
|
2016-07-14 00:37:14 +02:00
|
|
|
|
// Allow JSON of content type "text/plain" to avoid cors preflight
|
2016-07-14 00:42:57 +02:00
|
|
|
|
var textPlainMediaType = MediaTypeHeaderValue.Parse("text/plain");
|
|
|
|
|
foreach(var jsonFormatter in config.InputFormatters.OfType<JsonInputFormatter>())
|
|
|
|
|
{
|
|
|
|
|
jsonFormatter.SupportedMediaTypes.Add(textPlainMediaType);
|
|
|
|
|
}
|
2017-01-29 04:23:25 +01:00
|
|
|
|
}).AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
|
2017-10-25 06:45:11 +02:00
|
|
|
|
|
|
|
|
|
// PDF generation
|
|
|
|
|
if(!globalSettings.SelfHosted)
|
|
|
|
|
{
|
2017-10-25 16:59:30 +02:00
|
|
|
|
services.AddJsReport(new jsreport.Local.LocalReporting()
|
2017-10-25 06:45:11 +02:00
|
|
|
|
.UseBinary(jsreport.Binary.JsReportBinary.GetBinary())
|
|
|
|
|
.AsUtility()
|
|
|
|
|
.Create());
|
|
|
|
|
}
|
2015-12-09 04:57:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-07 05:45:33 +01:00
|
|
|
|
public void Configure(
|
|
|
|
|
IApplicationBuilder app,
|
|
|
|
|
IHostingEnvironment env,
|
|
|
|
|
ILoggerFactory loggerFactory,
|
2017-01-15 05:24:02 +01:00
|
|
|
|
IApplicationLifetime appLifetime,
|
2016-02-07 05:45:33 +01:00
|
|
|
|
GlobalSettings globalSettings)
|
2015-12-09 04:57:38 +01:00
|
|
|
|
{
|
2017-10-23 15:11:25 +02:00
|
|
|
|
loggerFactory.AddSerilog(env, appLifetime, globalSettings, (e) =>
|
|
|
|
|
{
|
|
|
|
|
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(context.Contains(typeof(IpRateLimitMiddleware).FullName) && e.Level == LogEventLevel.Information)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-27 15:24:09 +02:00
|
|
|
|
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
|
|
|
|
|
2017-08-15 22:33:38 +02:00
|
|
|
|
if(!globalSettings.SelfHosted)
|
|
|
|
|
{
|
|
|
|
|
// Rate limiting
|
|
|
|
|
app.UseMiddleware<CustomIpRateLimitMiddleware>();
|
|
|
|
|
}
|
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
|
|
|
|
|
app.UseCors("All");
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
2015-12-09 04:57:38 +01:00
|
|
|
|
}
|
|
|
|
|
}
|