2017-05-06 02:57:33 +02:00
|
|
|
|
using Bit.Core.Enums;
|
|
|
|
|
using Bit.Core.Identity;
|
|
|
|
|
using Bit.Core.IdentityServer;
|
|
|
|
|
using Bit.Core.Models.Table;
|
|
|
|
|
using Bit.Core.Repositories;
|
|
|
|
|
using Bit.Core.Services;
|
2017-06-07 05:19:42 +02:00
|
|
|
|
using IdentityModel;
|
2017-05-06 02:57:33 +02:00
|
|
|
|
using IdentityServer4.Services;
|
|
|
|
|
using IdentityServer4.Stores;
|
|
|
|
|
using IdentityServer4.Validation;
|
2017-07-21 18:53:26 +02:00
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
2017-05-06 02:57:33 +02:00
|
|
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2017-07-21 18:53:26 +02:00
|
|
|
|
using Microsoft.AspNetCore.HttpOverrides;
|
2017-05-06 02:57:33 +02:00
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2017-08-25 14:57:43 +02:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2017-05-06 02:57:33 +02:00
|
|
|
|
using Microsoft.WindowsAzure.Storage;
|
2017-06-23 16:08:29 +02:00
|
|
|
|
using System;
|
2017-08-08 06:02:52 +02:00
|
|
|
|
using System.IO;
|
2017-05-06 02:57:33 +02:00
|
|
|
|
using SqlServerRepos = Bit.Core.Repositories.SqlServer;
|
2017-08-25 14:57:43 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2017-05-06 02:57:33 +02:00
|
|
|
|
|
|
|
|
|
namespace Bit.Core.Utilities
|
|
|
|
|
{
|
|
|
|
|
public static class ServiceCollectionExtensions
|
|
|
|
|
{
|
|
|
|
|
public static void AddSqlServerRepositories(this IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
services.AddSingleton<IUserRepository, SqlServerRepos.UserRepository>();
|
|
|
|
|
services.AddSingleton<ICipherRepository, SqlServerRepos.CipherRepository>();
|
|
|
|
|
services.AddSingleton<IDeviceRepository, SqlServerRepos.DeviceRepository>();
|
|
|
|
|
services.AddSingleton<IGrantRepository, SqlServerRepos.GrantRepository>();
|
|
|
|
|
services.AddSingleton<IOrganizationRepository, SqlServerRepos.OrganizationRepository>();
|
|
|
|
|
services.AddSingleton<IOrganizationUserRepository, SqlServerRepos.OrganizationUserRepository>();
|
|
|
|
|
services.AddSingleton<ICollectionRepository, SqlServerRepos.CollectionRepository>();
|
|
|
|
|
services.AddSingleton<IFolderRepository, SqlServerRepos.FolderRepository>();
|
|
|
|
|
services.AddSingleton<ICollectionCipherRepository, SqlServerRepos.CollectionCipherRepository>();
|
2017-05-08 20:08:44 +02:00
|
|
|
|
services.AddSingleton<IGroupRepository, SqlServerRepos.GroupRepository>();
|
2017-06-22 04:33:45 +02:00
|
|
|
|
services.AddSingleton<IU2fRepository, SqlServerRepos.U2fRepository>();
|
2017-08-10 20:39:11 +02:00
|
|
|
|
services.AddSingleton<IInstallationRepository, SqlServerRepos.InstallationRepository>();
|
2017-05-06 02:57:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void AddBaseServices(this IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
services.AddSingleton<ICipherService, CipherService>();
|
|
|
|
|
services.AddScoped<IUserService, UserService>();
|
|
|
|
|
services.AddSingleton<IDeviceService, DeviceService>();
|
|
|
|
|
services.AddSingleton<IOrganizationService, OrganizationService>();
|
|
|
|
|
services.AddSingleton<ICollectionService, CollectionService>();
|
2017-05-09 20:17:22 +02:00
|
|
|
|
services.AddSingleton<IGroupService, GroupService>();
|
2017-05-06 02:57:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 22:31:00 +02:00
|
|
|
|
public static void AddDefaultServices(this IServiceCollection services, GlobalSettings globalSettings)
|
2017-05-06 02:57:33 +02:00
|
|
|
|
{
|
2017-10-31 03:10:40 +01:00
|
|
|
|
services.AddSingleton<IMailService, BackupMailService>();
|
2017-08-22 21:27:29 +02:00
|
|
|
|
services.AddSingleton<ILicensingService, LicensingService>();
|
2017-08-07 22:31:00 +02:00
|
|
|
|
|
2017-08-08 23:27:01 +02:00
|
|
|
|
if(CoreHelpers.SettingHasValue(globalSettings.Mail.SendGridApiKey))
|
2017-08-07 22:31:00 +02:00
|
|
|
|
{
|
|
|
|
|
services.AddSingleton<IMailDeliveryService, SendGridMailDeliveryService>();
|
|
|
|
|
}
|
2017-10-05 14:34:46 +02:00
|
|
|
|
else if(CoreHelpers.SettingHasValue(globalSettings.Mail?.Smtp?.Host))
|
2017-08-08 23:27:01 +02:00
|
|
|
|
{
|
|
|
|
|
services.AddSingleton<IMailDeliveryService, SmtpMailDeliveryService>();
|
|
|
|
|
}
|
2017-08-07 22:31:00 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
services.AddSingleton<IMailDeliveryService, NoopMailDeliveryService>();
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-11 16:04:59 +02:00
|
|
|
|
if(globalSettings.SelfHosted &&
|
|
|
|
|
CoreHelpers.SettingHasValue(globalSettings.PushRelayBaseUri) &&
|
|
|
|
|
globalSettings.Installation?.Id != null &&
|
|
|
|
|
CoreHelpers.SettingHasValue(globalSettings.Installation?.Key))
|
2017-08-08 23:27:01 +02:00
|
|
|
|
{
|
2017-08-11 16:04:59 +02:00
|
|
|
|
services.AddSingleton<IPushNotificationService, RelayPushNotificationService>();
|
|
|
|
|
services.AddSingleton<IPushRegistrationService, RelayPushRegistrationService>();
|
2017-08-08 23:27:01 +02:00
|
|
|
|
}
|
2017-08-11 16:04:59 +02:00
|
|
|
|
#if NET461
|
|
|
|
|
else if(!globalSettings.SelfHosted)
|
2017-08-08 23:27:01 +02:00
|
|
|
|
{
|
|
|
|
|
services.AddSingleton<IPushNotificationService, NotificationHubPushNotificationService>();
|
|
|
|
|
services.AddSingleton<IPushRegistrationService, NotificationHubPushRegistrationService>();
|
|
|
|
|
}
|
2017-07-31 22:58:27 +02:00
|
|
|
|
#endif
|
2017-08-11 16:04:59 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
services.AddSingleton<IPushNotificationService, NoopPushNotificationService>();
|
|
|
|
|
services.AddSingleton<IPushRegistrationService, NoopPushRegistrationService>();
|
|
|
|
|
}
|
2017-08-08 23:27:01 +02:00
|
|
|
|
|
2017-08-18 02:18:16 +02:00
|
|
|
|
if(!globalSettings.SelfHosted && CoreHelpers.SettingHasValue(globalSettings.Storage.ConnectionString))
|
2017-08-07 22:31:00 +02:00
|
|
|
|
{
|
|
|
|
|
services.AddSingleton<IBlockIpService, AzureQueueBlockIpService>();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
services.AddSingleton<IBlockIpService, NoopBlockIpService>();
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-08 23:27:01 +02:00
|
|
|
|
if(CoreHelpers.SettingHasValue(globalSettings.Attachment.ConnectionString))
|
2017-08-07 22:31:00 +02:00
|
|
|
|
{
|
|
|
|
|
services.AddSingleton<IAttachmentStorageService, AzureAttachmentStorageService>();
|
|
|
|
|
}
|
2017-08-08 23:27:01 +02:00
|
|
|
|
else if(CoreHelpers.SettingHasValue(globalSettings.Attachment.BaseDirectory))
|
|
|
|
|
{
|
|
|
|
|
services.AddSingleton<IAttachmentStorageService, LocalAttachmentStorageService>();
|
|
|
|
|
}
|
2017-08-07 22:31:00 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
services.AddSingleton<IAttachmentStorageService, NoopAttachmentStorageService>();
|
|
|
|
|
}
|
2017-05-06 02:57:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void AddNoopServices(this IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
services.AddSingleton<IMailService, NoopMailService>();
|
2017-06-16 20:24:04 +02:00
|
|
|
|
services.AddSingleton<IMailDeliveryService, NoopMailDeliveryService>();
|
2017-05-26 15:44:54 +02:00
|
|
|
|
services.AddSingleton<IPushNotificationService, NoopPushNotificationService>();
|
2017-05-06 02:57:33 +02:00
|
|
|
|
services.AddSingleton<IBlockIpService, NoopBlockIpService>();
|
2017-05-26 06:50:27 +02:00
|
|
|
|
services.AddSingleton<IPushRegistrationService, NoopPushRegistrationService>();
|
2017-06-15 21:34:12 +02:00
|
|
|
|
services.AddSingleton<IAttachmentStorageService, NoopAttachmentStorageService>();
|
2017-08-12 04:55:25 +02:00
|
|
|
|
services.AddSingleton<ILicensingService, NoopLicensingService>();
|
2017-05-06 02:57:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IdentityBuilder AddCustomIdentityServices(
|
|
|
|
|
this IServiceCollection services, GlobalSettings globalSettings)
|
|
|
|
|
{
|
|
|
|
|
services.AddTransient<ILookupNormalizer, LowerInvariantLookupNormalizer>();
|
|
|
|
|
|
2017-06-23 16:08:29 +02:00
|
|
|
|
services.Configure<TwoFactorRememberTokenProviderOptions>(options =>
|
|
|
|
|
{
|
|
|
|
|
options.TokenLifespan = TimeSpan.FromDays(30);
|
|
|
|
|
});
|
|
|
|
|
|
2017-10-06 20:02:28 +02:00
|
|
|
|
var identityBuilder = services.AddIdentityWithoutCookieAuth<User, Role>(options =>
|
2017-05-06 02:57:33 +02:00
|
|
|
|
{
|
|
|
|
|
options.User = new UserOptions
|
|
|
|
|
{
|
|
|
|
|
RequireUniqueEmail = true,
|
|
|
|
|
AllowedUserNameCharacters = null // all
|
|
|
|
|
};
|
|
|
|
|
options.Password = new PasswordOptions
|
|
|
|
|
{
|
|
|
|
|
RequireDigit = false,
|
|
|
|
|
RequireLowercase = false,
|
|
|
|
|
RequiredLength = 8,
|
|
|
|
|
RequireNonAlphanumeric = false,
|
|
|
|
|
RequireUppercase = false
|
|
|
|
|
};
|
|
|
|
|
options.ClaimsIdentity = new ClaimsIdentityOptions
|
|
|
|
|
{
|
2017-06-07 05:19:42 +02:00
|
|
|
|
SecurityStampClaimType = "sstamp",
|
|
|
|
|
UserNameClaimType = JwtClaimTypes.Email,
|
2017-08-10 20:39:11 +02:00
|
|
|
|
UserIdClaimType = JwtClaimTypes.Subject
|
2017-05-06 02:57:33 +02:00
|
|
|
|
};
|
|
|
|
|
options.Tokens.ChangeEmailTokenProvider = TokenOptions.DefaultEmailProvider;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
identityBuilder
|
|
|
|
|
.AddUserStore<UserStore>()
|
|
|
|
|
.AddRoleStore<RoleStore>()
|
2017-07-05 21:35:46 +02:00
|
|
|
|
.AddTokenProvider<DataProtectorTokenProvider<User>>(TokenOptions.DefaultProvider)
|
2017-05-06 02:57:33 +02:00
|
|
|
|
.AddTokenProvider<AuthenticatorTokenProvider>(TwoFactorProviderType.Authenticator.ToString())
|
2017-06-15 04:40:33 +02:00
|
|
|
|
.AddTokenProvider<YubicoOtpTokenProvider>(TwoFactorProviderType.YubiKey.ToString())
|
2017-06-21 06:04:25 +02:00
|
|
|
|
.AddTokenProvider<DuoWebTokenProvider>(TwoFactorProviderType.Duo.ToString())
|
2017-06-23 04:14:51 +02:00
|
|
|
|
.AddTokenProvider<U2fTokenProvider>(TwoFactorProviderType.U2f.ToString())
|
2017-06-23 16:08:29 +02:00
|
|
|
|
.AddTokenProvider<TwoFactorRememberTokenProvider>(TwoFactorProviderType.Remember.ToString())
|
2017-05-06 02:57:33 +02:00
|
|
|
|
.AddTokenProvider<EmailTokenProvider<User>>(TokenOptions.DefaultEmailProvider);
|
|
|
|
|
|
|
|
|
|
return identityBuilder;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IIdentityServerBuilder AddCustomIdentityServerServices(
|
|
|
|
|
this IServiceCollection services, IHostingEnvironment env, GlobalSettings globalSettings)
|
|
|
|
|
{
|
|
|
|
|
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-08-04 18:16:31 +02:00
|
|
|
|
options.IssuerUri = globalSettings.BaseServiceUri.InternalIdentity;
|
2017-08-10 20:39:11 +02:00
|
|
|
|
options.Caching.ClientStoreExpiration = new TimeSpan(0, 5, 0);
|
2017-05-06 02:57:33 +02:00
|
|
|
|
})
|
2017-08-10 20:39:11 +02:00
|
|
|
|
.AddInMemoryCaching()
|
2017-05-06 02:57:33 +02:00
|
|
|
|
.AddInMemoryApiResources(ApiResources.GetApiResources())
|
2017-08-10 20:39:11 +02:00
|
|
|
|
.AddClientStoreCache<ClientStore>();
|
2017-05-06 02:57:33 +02:00
|
|
|
|
|
2017-07-14 19:29:52 +02:00
|
|
|
|
if(env.IsDevelopment())
|
2017-05-06 02:57:33 +02:00
|
|
|
|
{
|
2017-10-06 17:38:47 +02:00
|
|
|
|
identityServerBuilder.AddDeveloperSigningCredential(false);
|
2017-05-06 02:57:33 +02:00
|
|
|
|
}
|
2017-08-08 23:27:01 +02:00
|
|
|
|
else if(!string.IsNullOrWhiteSpace(globalSettings.IdentityServer.CertificatePassword)
|
|
|
|
|
&& File.Exists("identity.pfx"))
|
2017-08-07 17:24:16 +02:00
|
|
|
|
{
|
2017-08-07 22:31:00 +02:00
|
|
|
|
var identityServerCert = CoreHelpers.GetCertificate("identity.pfx",
|
2017-08-07 17:24:16 +02:00
|
|
|
|
globalSettings.IdentityServer.CertificatePassword);
|
|
|
|
|
identityServerBuilder.AddSigningCredential(identityServerCert);
|
|
|
|
|
}
|
2017-08-07 22:31:00 +02:00
|
|
|
|
else if(!string.IsNullOrWhiteSpace(globalSettings.IdentityServer.CertificateThumbprint))
|
2017-05-06 02:57:33 +02:00
|
|
|
|
{
|
2017-07-14 19:29:52 +02:00
|
|
|
|
var identityServerCert = CoreHelpers.GetCertificate(globalSettings.IdentityServer.CertificateThumbprint);
|
|
|
|
|
identityServerBuilder.AddSigningCredential(identityServerCert);
|
2017-05-06 02:57:33 +02:00
|
|
|
|
}
|
2017-08-07 22:31:00 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("No identity certificate to use.");
|
|
|
|
|
}
|
2017-05-06 02:57:33 +02:00
|
|
|
|
|
2017-08-10 20:39:11 +02:00
|
|
|
|
services.AddTransient<ClientStore>();
|
|
|
|
|
services.AddTransient<ICorsPolicyService, AllowAllCorsPolicyService>();
|
2017-05-06 02:57:33 +02:00
|
|
|
|
services.AddScoped<IResourceOwnerPasswordValidator, ResourceOwnerPasswordValidator>();
|
|
|
|
|
services.AddScoped<IProfileService, ProfileService>();
|
|
|
|
|
services.AddSingleton<IPersistedGrantStore, PersistedGrantStore>();
|
|
|
|
|
|
|
|
|
|
return identityServerBuilder;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void AddCustomDataProtectionServices(
|
|
|
|
|
this IServiceCollection services, IHostingEnvironment env, GlobalSettings globalSettings)
|
|
|
|
|
{
|
2017-08-08 06:02:52 +02:00
|
|
|
|
if(env.IsDevelopment())
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-09 05:06:28 +02:00
|
|
|
|
if(globalSettings.SelfHosted && CoreHelpers.SettingHasValue(globalSettings.DataProtection.Directory))
|
2017-08-08 06:02:52 +02:00
|
|
|
|
{
|
2017-08-09 05:06:28 +02:00
|
|
|
|
services.AddDataProtection()
|
|
|
|
|
.PersistKeysToFileSystem(new DirectoryInfo(globalSettings.DataProtection.Directory));
|
2017-08-08 06:02:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!globalSettings.SelfHosted)
|
2017-05-06 02:57:33 +02:00
|
|
|
|
{
|
|
|
|
|
var dataProtectionCert = CoreHelpers.GetCertificate(globalSettings.DataProtection.CertificateThumbprint);
|
|
|
|
|
var storageAccount = CloudStorageAccount.Parse(globalSettings.Storage.ConnectionString);
|
|
|
|
|
services.AddDataProtection()
|
|
|
|
|
.PersistKeysToAzureBlobStorage(storageAccount, "aspnet-dataprotection/keys.xml")
|
|
|
|
|
.ProtectKeysWithCertificate(dataProtectionCert);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static GlobalSettings AddGlobalSettingsServices(this IServiceCollection services,
|
2017-10-19 06:08:09 +02:00
|
|
|
|
IConfiguration configuration)
|
2017-05-06 02:57:33 +02:00
|
|
|
|
{
|
|
|
|
|
var globalSettings = new GlobalSettings();
|
2017-10-19 06:08:09 +02:00
|
|
|
|
ConfigurationBinder.Bind(configuration.GetSection("GlobalSettings"), globalSettings);
|
2017-05-06 02:57:33 +02:00
|
|
|
|
services.AddSingleton(s => globalSettings);
|
|
|
|
|
return globalSettings;
|
|
|
|
|
}
|
2017-07-21 18:53:26 +02:00
|
|
|
|
|
2017-08-25 14:57:43 +02:00
|
|
|
|
public static void UseDefaultMiddleware(this IApplicationBuilder app, IHostingEnvironment env)
|
2017-07-21 18:53:26 +02:00
|
|
|
|
{
|
2017-08-25 14:57:43 +02:00
|
|
|
|
if(!env.IsDevelopment())
|
2017-07-21 18:53:26 +02:00
|
|
|
|
{
|
2017-08-25 14:57:43 +02:00
|
|
|
|
// Adjust headers for proxy.
|
|
|
|
|
// ref: https://github.com/aspnet/Docs/issues/2384
|
|
|
|
|
var forwardOptions = new ForwardedHeadersOptions
|
|
|
|
|
{
|
|
|
|
|
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto,
|
|
|
|
|
RequireHeaderSymmetry = false
|
|
|
|
|
};
|
|
|
|
|
forwardOptions.KnownNetworks.Clear();
|
|
|
|
|
forwardOptions.KnownProxies.Clear();
|
|
|
|
|
app.UseForwardedHeaders(forwardOptions);
|
|
|
|
|
}
|
2017-07-21 18:53:26 +02:00
|
|
|
|
|
2017-08-25 14:57:43 +02:00
|
|
|
|
// Add version information to response headers
|
|
|
|
|
app.Use(async (httpContext, next) =>
|
|
|
|
|
{
|
|
|
|
|
httpContext.Response.OnStarting((state) =>
|
|
|
|
|
{
|
2017-09-07 05:57:14 +02:00
|
|
|
|
httpContext.Response.Headers.Append("Server-Version", CoreHelpers.GetVersion());
|
2017-08-25 14:57:43 +02:00
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
}, null);
|
2017-07-21 18:53:26 +02:00
|
|
|
|
|
2017-08-25 14:57:43 +02:00
|
|
|
|
await next.Invoke();
|
|
|
|
|
});
|
2017-07-21 18:53:26 +02:00
|
|
|
|
}
|
2017-05-06 02:57:33 +02:00
|
|
|
|
}
|
|
|
|
|
}
|