2015-12-09 04:57:38 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
using Microsoft.AspNet.Authentication.JwtBearer;
|
|
|
|
|
using Microsoft.AspNet.Authorization;
|
|
|
|
|
using Microsoft.AspNet.Builder;
|
|
|
|
|
using Microsoft.AspNet.Hosting;
|
|
|
|
|
using Microsoft.AspNet.Identity;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Microsoft.Extensions.OptionsModel;
|
|
|
|
|
using Bit.Api.Utilities;
|
|
|
|
|
using Bit.Core;
|
|
|
|
|
using Bit.Core.Domains;
|
|
|
|
|
using Bit.Core.Identity;
|
|
|
|
|
using Bit.Core.Repositories;
|
|
|
|
|
using Bit.Core.Services;
|
2016-02-06 07:18:25 +01:00
|
|
|
|
using Repos = Bit.Core.Repositories.SqlServer;
|
2015-12-09 04:57:38 +01:00
|
|
|
|
|
|
|
|
|
namespace Bit.Api
|
|
|
|
|
{
|
|
|
|
|
public class Startup
|
|
|
|
|
{
|
|
|
|
|
public Startup(IHostingEnvironment env)
|
|
|
|
|
{
|
|
|
|
|
var builder = new ConfigurationBuilder()
|
|
|
|
|
.AddJsonFile("settings.json")
|
|
|
|
|
.AddJsonFile($"settings.{env.EnvironmentName}.json", optional: true);
|
|
|
|
|
|
|
|
|
|
if(env.IsDevelopment())
|
|
|
|
|
{
|
|
|
|
|
builder.AddUserSecrets();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
builder.AddEnvironmentVariables();
|
|
|
|
|
|
|
|
|
|
Configuration = builder.Build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IConfigurationRoot Configuration { get; private set; }
|
|
|
|
|
|
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
services.Configure<GlobalSettings>(Configuration.GetSection("globalSettings"));
|
|
|
|
|
|
|
|
|
|
// Options
|
|
|
|
|
services.AddOptions();
|
|
|
|
|
|
|
|
|
|
// Settings
|
|
|
|
|
var provider = services.BuildServiceProvider();
|
|
|
|
|
var globalSettings = provider.GetRequiredService<IOptions<GlobalSettings>>().Value;
|
|
|
|
|
services.AddSingleton(s => globalSettings);
|
|
|
|
|
|
|
|
|
|
// Repositories
|
2016-02-06 07:18:25 +01:00
|
|
|
|
services.AddSingleton<IUserRepository>(s => new Repos.UserRepository(globalSettings.SqlServer.ConnectionString));
|
|
|
|
|
services.AddSingleton<ISiteRepository>(s => new Repos.SiteRepository(globalSettings.SqlServer.ConnectionString));
|
|
|
|
|
services.AddSingleton<IFolderRepository>(s => new Repos.FolderRepository(globalSettings.SqlServer.ConnectionString));
|
|
|
|
|
services.AddSingleton<ICipherRepository>(s => new Repos.CipherRepository(globalSettings.SqlServer.ConnectionString));
|
2015-12-09 04:57:38 +01:00
|
|
|
|
|
|
|
|
|
// Context
|
|
|
|
|
services.AddScoped<CurrentContext>();
|
|
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
|
RequireNonLetterOrDigit = false,
|
|
|
|
|
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);
|
|
|
|
|
// TODO: Symmetric key
|
|
|
|
|
// waiting on https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet/issues/250
|
|
|
|
|
jwtBearerOptions.SigningCredentials = null;
|
|
|
|
|
})
|
|
|
|
|
.AddUserStore<UserStore>()
|
|
|
|
|
.AddRoleStore<RoleStore>()
|
|
|
|
|
.AddTokenProvider<AuthenticatorTokenProvider>("Authenticator")
|
|
|
|
|
.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
|
|
|
|
{
|
2015-12-31 00:49:43 +01:00
|
|
|
|
config.AddPolicy("Application", new AuthorizationPolicyBuilder()
|
2015-12-09 04:57:38 +01:00
|
|
|
|
.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
|
|
|
|
|
.RequireAuthenticatedUser().RequireClaim(ClaimTypes.AuthenticationMethod, jwtIdentityOptions.AuthenticationMethod).Build());
|
|
|
|
|
|
2015-12-31 00:49:43 +01:00
|
|
|
|
config.AddPolicy("TwoFactor", new AuthorizationPolicyBuilder()
|
2015-12-09 04:57:38 +01:00
|
|
|
|
.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
|
|
|
|
|
.RequireAuthenticatedUser().RequireClaim(ClaimTypes.AuthenticationMethod, jwtIdentityOptions.TwoFactorAuthenticationMethod).Build());
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
services.AddScoped<AuthenticatorTokenProvider>();
|
|
|
|
|
|
|
|
|
|
// Services
|
|
|
|
|
services.AddSingleton<IMailService, MailService>();
|
2015-12-27 05:09:53 +01:00
|
|
|
|
services.AddSingleton<ICipherService, CipherService>();
|
2015-12-09 04:57:38 +01:00
|
|
|
|
services.AddScoped<IUserService, UserService>();
|
|
|
|
|
|
|
|
|
|
// Cors
|
2015-12-31 00:49:43 +01:00
|
|
|
|
services.AddCors(config =>
|
|
|
|
|
{
|
|
|
|
|
config.AddPolicy("All", policy => policy.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());
|
|
|
|
|
});
|
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());
|
2015-12-09 04:57:38 +01:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
|
|
|
|
|
{
|
|
|
|
|
loggerFactory.MinimumLevel = LogLevel.Information;
|
|
|
|
|
loggerFactory.AddConsole();
|
|
|
|
|
loggerFactory.AddDebug();
|
|
|
|
|
|
|
|
|
|
// Add the platform handler to the request pipeline.
|
|
|
|
|
app.UseIISPlatformHandler();
|
|
|
|
|
|
|
|
|
|
// Add static files to the request pipeline.
|
|
|
|
|
app.UseStaticFiles();
|
|
|
|
|
|
|
|
|
|
// Add Cors
|
|
|
|
|
app.UseCors("All");
|
|
|
|
|
|
|
|
|
|
// Add Jwt authentication to the request pipeline.
|
|
|
|
|
app.UseJwtBearerIdentity();
|
|
|
|
|
|
|
|
|
|
// Add MVC to the request pipeline.
|
|
|
|
|
app.UseMvc();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Entry point for the application.
|
|
|
|
|
public static void Main(string[] args) => WebApplication.Run<Startup>(args);
|
|
|
|
|
}
|
|
|
|
|
}
|