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.Authentication.JwtBearer;
|
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
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.Extensions.Options;
|
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
2015-12-09 04:57:38 +01:00
|
|
|
|
using Bit.Api.Utilities;
|
|
|
|
|
using Bit.Core;
|
2017-03-09 03:45:08 +01:00
|
|
|
|
using Bit.Core.Models.Table;
|
2015-12-09 04:57:38 +01:00
|
|
|
|
using Bit.Core.Identity;
|
|
|
|
|
using Bit.Core.Repositories;
|
|
|
|
|
using Bit.Core.Services;
|
2017-01-12 05:20:54 +01:00
|
|
|
|
using SqlServerRepos = Bit.Core.Repositories.SqlServer;
|
2016-05-20 01:10:24 +02:00
|
|
|
|
using System.Text;
|
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;
|
|
|
|
|
using Bit.Api.Middleware;
|
2017-01-11 06:34:16 +01:00
|
|
|
|
using IdentityServer4.Validation;
|
|
|
|
|
using IdentityServer4.Services;
|
2017-01-12 05:20:54 +01:00
|
|
|
|
using IdentityServer4.Stores;
|
2017-01-13 00:35:26 +01:00
|
|
|
|
using Bit.Core.Utilities;
|
2017-01-15 05:24:02 +01:00
|
|
|
|
using Serilog;
|
|
|
|
|
using Serilog.Events;
|
2017-01-19 00:34:14 +01:00
|
|
|
|
using Bit.Api.IdentityServer;
|
2017-01-28 23:28:28 +01:00
|
|
|
|
using Bit.Core.Enums;
|
2017-03-24 03:02:55 +01:00
|
|
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
|
|
|
using Microsoft.WindowsAzure.Storage;
|
2017-04-04 16:13:16 +02:00
|
|
|
|
using Stripe;
|
2015-12-09 04:57:38 +01:00
|
|
|
|
|
|
|
|
|
namespace Bit.Api
|
|
|
|
|
{
|
|
|
|
|
public class Startup
|
|
|
|
|
{
|
|
|
|
|
public Startup(IHostingEnvironment env)
|
|
|
|
|
{
|
|
|
|
|
var builder = new ConfigurationBuilder()
|
2016-05-20 01:10:24 +02:00
|
|
|
|
.SetBasePath(env.ContentRootPath)
|
2015-12-09 04:57:38 +01:00
|
|
|
|
.AddJsonFile("settings.json")
|
|
|
|
|
.AddJsonFile($"settings.{env.EnvironmentName}.json", optional: true);
|
|
|
|
|
|
|
|
|
|
if(env.IsDevelopment())
|
|
|
|
|
{
|
2017-03-09 03:38:37 +01:00
|
|
|
|
builder.AddUserSecrets<Startup>();
|
2015-12-09 04:57:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
builder.AddEnvironmentVariables();
|
|
|
|
|
|
|
|
|
|
Configuration = builder.Build();
|
2017-01-13 03:07:25 +01:00
|
|
|
|
Environment = env;
|
2015-12-09 04:57:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IConfigurationRoot 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
|
2016-05-20 01:10:24 +02:00
|
|
|
|
var globalSettings = new GlobalSettings();
|
|
|
|
|
ConfigurationBinder.Bind(Configuration.GetSection("GlobalSettings"), globalSettings);
|
2015-12-09 04:57:38 +01:00
|
|
|
|
services.AddSingleton(s => globalSettings);
|
2016-11-13 00:43:32 +01:00
|
|
|
|
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
|
|
|
|
|
if(Environment.IsProduction())
|
|
|
|
|
{
|
|
|
|
|
var dataProtectionCert = CoreHelpers.GetCertificate(globalSettings.DataProtection.CertificateThumbprint);
|
|
|
|
|
var storageAccount = CloudStorageAccount.Parse(globalSettings.Storage.ConnectionString);
|
|
|
|
|
services.AddDataProtection()
|
|
|
|
|
.PersistKeysToAzureBlobStorage(storageAccount, "aspnet-dataprotection/keys.xml")
|
|
|
|
|
.ProtectKeysWithCertificate(dataProtectionCert);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-04 16:13:16 +02:00
|
|
|
|
// Stripe Billing
|
|
|
|
|
StripeConfiguration.SetApiKey(globalSettings.StripeApiKey);
|
|
|
|
|
|
2015-12-09 04:57:38 +01:00
|
|
|
|
// Repositories
|
2017-01-12 05:20:54 +01:00
|
|
|
|
services.AddSingleton<IUserRepository, SqlServerRepos.UserRepository>();
|
|
|
|
|
services.AddSingleton<ICipherRepository, SqlServerRepos.CipherRepository>();
|
|
|
|
|
services.AddSingleton<IDeviceRepository, SqlServerRepos.DeviceRepository>();
|
|
|
|
|
services.AddSingleton<IGrantRepository, SqlServerRepos.GrantRepository>();
|
2017-03-02 06:15:05 +01:00
|
|
|
|
services.AddSingleton<IOrganizationRepository, SqlServerRepos.OrganizationRepository>();
|
|
|
|
|
services.AddSingleton<IOrganizationUserRepository, SqlServerRepos.OrganizationUserRepository>();
|
2017-03-08 05:06:14 +01:00
|
|
|
|
services.AddSingleton<ISubvaultRepository, SqlServerRepos.SubvaultRepository>();
|
|
|
|
|
services.AddSingleton<ISubvaultUserRepository, SqlServerRepos.SubvaultUserRepository>();
|
2017-03-18 23:42:04 +01:00
|
|
|
|
services.AddSingleton<IFolderRepository, SqlServerRepos.FolderRepository>();
|
2017-03-24 03:02:55 +01:00
|
|
|
|
services.AddSingleton<ISubvaultCipherRepository, SqlServerRepos.SubvaultCipherRepository>();
|
2015-12-09 04:57:38 +01:00
|
|
|
|
|
|
|
|
|
// Context
|
|
|
|
|
services.AddScoped<CurrentContext>();
|
|
|
|
|
|
2016-11-13 00:43:32 +01:00
|
|
|
|
// Caching
|
|
|
|
|
services.AddMemoryCache();
|
|
|
|
|
|
|
|
|
|
// Rate limiting
|
|
|
|
|
services.AddSingleton<IIpPolicyStore, MemoryCacheIpPolicyStore>();
|
|
|
|
|
services.AddSingleton<IRateLimitCounterStore, MemoryCacheRateLimitCounterStore>();
|
|
|
|
|
|
2017-01-11 06:34:16 +01:00
|
|
|
|
// IdentityServer
|
2017-01-28 08:40:09 +01:00
|
|
|
|
var identityServerBuilder = services.AddIdentityServer(options =>
|
|
|
|
|
{
|
|
|
|
|
options.Endpoints.EnableAuthorizeEndpoint = false;
|
|
|
|
|
options.Endpoints.EnableIntrospectionEndpoint = false;
|
|
|
|
|
options.Endpoints.EnableEndSessionEndpoint = false;
|
|
|
|
|
options.Endpoints.EnableUserInfoEndpoint = false;
|
|
|
|
|
options.Endpoints.EnableCheckSessionEndpoint = false;
|
|
|
|
|
options.Endpoints.EnableTokenRevocationEndpoint = false;
|
|
|
|
|
})
|
2017-01-12 03:46:36 +01:00
|
|
|
|
.AddInMemoryApiResources(ApiResources.GetApiResources())
|
2017-01-11 06:34:16 +01:00
|
|
|
|
.AddInMemoryClients(Clients.GetClients());
|
2017-03-17 03:04:02 +01:00
|
|
|
|
services.AddTransient<ICorsPolicyService, AllowAllCorsPolicyService>();
|
2017-01-13 03:07:25 +01:00
|
|
|
|
|
|
|
|
|
if(Environment.IsProduction())
|
|
|
|
|
{
|
|
|
|
|
var identityServerCert = CoreHelpers.GetCertificate(globalSettings.IdentityServer.CertificateThumbprint);
|
|
|
|
|
identityServerBuilder.AddSigningCredential(identityServerCert);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
identityServerBuilder.AddTemporarySigningCredential();
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-26 04:31:14 +01:00
|
|
|
|
services.AddScoped<IResourceOwnerPasswordValidator, ResourceOwnerPasswordValidator>();
|
|
|
|
|
services.AddScoped<IProfileService, ProfileService>();
|
2017-01-12 05:20:54 +01:00
|
|
|
|
services.AddSingleton<IPersistedGrantStore, PersistedGrantStore>();
|
2017-01-11 06:34:16 +01:00
|
|
|
|
|
2015-12-09 04:57:38 +01:00
|
|
|
|
// Identity
|
|
|
|
|
services.AddTransient<ILookupNormalizer, LowerInvariantLookupNormalizer>();
|
|
|
|
|
services.AddJwtBearerIdentity(options =>
|
|
|
|
|
{
|
|
|
|
|
options.User = new UserOptions
|
|
|
|
|
{
|
|
|
|
|
RequireUniqueEmail = true,
|
|
|
|
|
AllowedUserNameCharacters = null // all
|
|
|
|
|
};
|
|
|
|
|
options.Password = new PasswordOptions
|
|
|
|
|
{
|
|
|
|
|
RequireDigit = false,
|
|
|
|
|
RequireLowercase = false,
|
|
|
|
|
RequiredLength = 8,
|
2016-05-20 01:10:24 +02:00
|
|
|
|
RequireNonAlphanumeric = false,
|
2015-12-09 04:57:38 +01:00
|
|
|
|
RequireUppercase = false
|
|
|
|
|
};
|
|
|
|
|
options.ClaimsIdentity = new ClaimsIdentityOptions
|
|
|
|
|
{
|
|
|
|
|
SecurityStampClaimType = "securitystamp",
|
|
|
|
|
UserNameClaimType = ClaimTypes.Email
|
|
|
|
|
};
|
|
|
|
|
options.Tokens.ChangeEmailTokenProvider = TokenOptions.DefaultEmailProvider;
|
|
|
|
|
}, jwtBearerOptions =>
|
|
|
|
|
{
|
|
|
|
|
jwtBearerOptions.Audience = "bitwarden";
|
|
|
|
|
jwtBearerOptions.Issuer = "bitwarden";
|
|
|
|
|
jwtBearerOptions.TokenLifetime = TimeSpan.FromDays(10 * 365);
|
|
|
|
|
jwtBearerOptions.TwoFactorTokenLifetime = TimeSpan.FromMinutes(10);
|
2016-05-20 01:10:24 +02:00
|
|
|
|
var keyBytes = Encoding.ASCII.GetBytes(globalSettings.JwtSigningKey);
|
|
|
|
|
jwtBearerOptions.SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(keyBytes), SecurityAlgorithms.HmacSha256);
|
2015-12-09 04:57:38 +01:00
|
|
|
|
})
|
|
|
|
|
.AddUserStore<UserStore>()
|
|
|
|
|
.AddRoleStore<RoleStore>()
|
2017-01-28 23:28:28 +01:00
|
|
|
|
.AddTokenProvider<AuthenticatorTokenProvider>(TwoFactorProviderType.Authenticator.ToString())
|
2015-12-09 04:57:38 +01:00
|
|
|
|
.AddTokenProvider<EmailTokenProvider<User>>(TokenOptions.DefaultEmailProvider);
|
|
|
|
|
|
|
|
|
|
var jwtIdentityOptions = provider.GetRequiredService<IOptions<JwtBearerIdentityOptions>>().Value;
|
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.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme, "Bearer2");
|
|
|
|
|
policy.RequireAuthenticatedUser();
|
|
|
|
|
policy.RequireClaim(ClaimTypes.AuthenticationMethod, jwtIdentityOptions.AuthenticationMethod);
|
|
|
|
|
});
|
2015-12-09 04:57:38 +01:00
|
|
|
|
|
2017-01-11 06:34:16 +01:00
|
|
|
|
config.AddPolicy("TwoFactor", policy =>
|
|
|
|
|
{
|
|
|
|
|
policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme, "Bearer2");
|
|
|
|
|
policy.RequireAuthenticatedUser();
|
|
|
|
|
policy.RequireClaim(ClaimTypes.AuthenticationMethod, jwtIdentityOptions.TwoFactorAuthenticationMethod);
|
|
|
|
|
});
|
2015-12-09 04:57:38 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
services.AddScoped<AuthenticatorTokenProvider>();
|
|
|
|
|
|
|
|
|
|
// Services
|
2016-12-03 05:37:08 +01:00
|
|
|
|
services.AddSingleton<IMailService, SendGridMailService>();
|
2015-12-27 05:09:53 +01:00
|
|
|
|
services.AddSingleton<ICipherService, CipherService>();
|
2015-12-09 04:57:38 +01:00
|
|
|
|
services.AddScoped<IUserService, UserService>();
|
2016-12-03 05:37:08 +01:00
|
|
|
|
services.AddScoped<IPushService, PushSharpPushService>();
|
2016-08-06 05:59:59 +02:00
|
|
|
|
services.AddScoped<IDeviceService, DeviceService>();
|
2016-12-03 05:37:08 +01:00
|
|
|
|
services.AddScoped<IBlockIpService, AzureQueueBlockIpService>();
|
2017-03-03 06:07:11 +01:00
|
|
|
|
services.AddScoped<IOrganizationService, OrganizationService>();
|
2017-04-03 18:27:02 +02:00
|
|
|
|
services.AddScoped<ISubvaultService, SubvaultService>();
|
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());
|
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-01-15 05:24:02 +01:00
|
|
|
|
if(env.IsProduction())
|
2016-02-07 05:45:33 +01:00
|
|
|
|
{
|
2017-01-15 05:24:02 +01:00
|
|
|
|
Func<LogEvent, bool> serilogFilter = (e) =>
|
|
|
|
|
{
|
|
|
|
|
var context = e.Properties["SourceContext"].ToString();
|
2017-01-17 05:55:48 +01:00
|
|
|
|
if(e.Exception != null && e.Exception.GetType() == typeof(SecurityTokenValidationException) || e.Exception.Message == "Bad security stamp.")
|
2016-10-29 08:59:17 +02:00
|
|
|
|
{
|
2017-01-15 05:24:02 +01:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(context == typeof(IpRateLimitMiddleware).FullName && e.Level == LogEventLevel.Information)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return e.Level >= LogEventLevel.Error;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var serilog = new LoggerConfiguration()
|
|
|
|
|
.Enrich.FromLogContext()
|
|
|
|
|
.Filter.ByIncludingOnly(serilogFilter)
|
|
|
|
|
.WriteTo.AzureDocumentDB(new Uri(globalSettings.DocumentDb.Uri), globalSettings.DocumentDb.Key,
|
|
|
|
|
timeToLive: TimeSpan.FromDays(7))
|
|
|
|
|
.CreateLogger();
|
|
|
|
|
|
|
|
|
|
loggerFactory.AddSerilog(serilog);
|
|
|
|
|
appLifetime.ApplicationStopped.Register(Log.CloseAndFlush);
|
2016-02-07 05:45:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-15 05:24:02 +01:00
|
|
|
|
loggerFactory.AddDebug();
|
|
|
|
|
|
2016-11-13 00:43:32 +01:00
|
|
|
|
// Rate limiting
|
|
|
|
|
app.UseMiddleware<CustomIpRateLimitMiddleware>();
|
|
|
|
|
|
2015-12-09 04:57:38 +01:00
|
|
|
|
// Add static files to the request pipeline.
|
|
|
|
|
app.UseStaticFiles();
|
|
|
|
|
|
|
|
|
|
// Add Cors
|
|
|
|
|
app.UseCors("All");
|
|
|
|
|
|
2017-01-11 06:34:16 +01:00
|
|
|
|
// Add IdentityServer to the request pipeline.
|
|
|
|
|
app.UseIdentityServer();
|
|
|
|
|
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
|
|
|
|
|
{
|
|
|
|
|
AllowedScopes = new string[] { "api" },
|
|
|
|
|
Authority = env.IsProduction() ? "https://api.bitwarden.com" : "http://localhost:4000",
|
|
|
|
|
RequireHttpsMetadata = env.IsProduction(),
|
2017-03-10 02:30:19 +01:00
|
|
|
|
ApiName = "api",
|
2017-01-17 04:02:12 +01:00
|
|
|
|
NameClaimType = ClaimTypes.Email,
|
|
|
|
|
// Version "2" until we retire the old jwt scheme and replace it with this one.
|
2017-01-11 06:34:16 +01:00
|
|
|
|
AuthenticationScheme = "Bearer2",
|
2017-01-26 04:31:14 +01:00
|
|
|
|
TokenRetriever = TokenRetrieval.FromAuthorizationHeaderOrQueryString("Bearer2", "access_token2")
|
2017-01-11 06:34:16 +01:00
|
|
|
|
});
|
|
|
|
|
|
2015-12-09 04:57:38 +01:00
|
|
|
|
// Add Jwt authentication to the request pipeline.
|
|
|
|
|
app.UseJwtBearerIdentity();
|
|
|
|
|
|
2017-01-25 04:15:21 +01:00
|
|
|
|
// Add current context
|
|
|
|
|
app.UseMiddleware<CurrentContextMiddleware>();
|
|
|
|
|
|
2015-12-09 04:57:38 +01:00
|
|
|
|
// Add MVC to the request pipeline.
|
|
|
|
|
app.UseMvc();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|