2017-05-05 20:57:33 -04: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-06 23:19:42 -04:00
|
|
|
|
using IdentityModel;
|
2017-05-05 20:57:33 -04:00
|
|
|
|
using IdentityServer4.Services;
|
|
|
|
|
|
using IdentityServer4.Stores;
|
|
|
|
|
|
using IdentityServer4.Validation;
|
2017-07-21 12:53:26 -04:00
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
2017-05-05 20:57:33 -04:00
|
|
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2017-08-25 08:57:43 -04:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2017-05-05 20:57:33 -04:00
|
|
|
|
using Microsoft.WindowsAzure.Storage;
|
2017-06-23 10:08:29 -04:00
|
|
|
|
using System;
|
2017-08-08 00:02:52 -04:00
|
|
|
|
using System.IO;
|
2017-05-05 20:57:33 -04:00
|
|
|
|
using SqlServerRepos = Bit.Core.Repositories.SqlServer;
|
2019-01-15 22:07:13 -05:00
|
|
|
|
using PostgreSqlRepos = Bit.Core.Repositories.PostgreSql;
|
2019-03-19 00:39:03 -04:00
|
|
|
|
using NoopRepos = Bit.Core.Repositories.Noop;
|
2017-08-25 08:57:43 -04:00
|
|
|
|
using System.Threading.Tasks;
|
2017-12-08 16:03:20 -05:00
|
|
|
|
using TableStorageRepos = Bit.Core.Repositories.TableStorage;
|
2018-03-21 14:26:49 -04:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
2018-08-15 09:26:19 -04:00
|
|
|
|
using IdentityServer4.AccessTokenValidation;
|
|
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2019-04-26 09:52:54 -04:00
|
|
|
|
using Microsoft.AspNetCore.HttpOverrides;
|
|
|
|
|
|
using System.Linq;
|
2017-05-05 20:57:33 -04:00
|
|
|
|
|
|
|
|
|
|
namespace Bit.Core.Utilities
|
|
|
|
|
|
{
|
|
|
|
|
|
public static class ServiceCollectionExtensions
|
|
|
|
|
|
{
|
2017-12-01 09:22:04 -05:00
|
|
|
|
public static void AddSqlServerRepositories(this IServiceCollection services, GlobalSettings globalSettings)
|
2017-05-05 20:57:33 -04:00
|
|
|
|
{
|
2019-01-15 22:07:13 -05:00
|
|
|
|
if(!string.IsNullOrWhiteSpace(globalSettings.PostgreSql?.ConnectionString))
|
|
|
|
|
|
{
|
|
|
|
|
|
services.AddSingleton<IUserRepository, PostgreSqlRepos.UserRepository>();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
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>();
|
|
|
|
|
|
services.AddSingleton<IGroupRepository, SqlServerRepos.GroupRepository>();
|
|
|
|
|
|
services.AddSingleton<IU2fRepository, SqlServerRepos.U2fRepository>();
|
|
|
|
|
|
services.AddSingleton<IInstallationRepository, SqlServerRepos.InstallationRepository>();
|
|
|
|
|
|
services.AddSingleton<IMaintenanceRepository, SqlServerRepos.MaintenanceRepository>();
|
2019-01-31 16:45:01 -05:00
|
|
|
|
services.AddSingleton<ITransactionRepository, SqlServerRepos.TransactionRepository>();
|
2019-01-15 22:07:13 -05:00
|
|
|
|
}
|
2017-12-01 09:22:04 -05:00
|
|
|
|
|
|
|
|
|
|
if(globalSettings.SelfHosted)
|
|
|
|
|
|
{
|
2017-12-12 14:22:22 -05:00
|
|
|
|
services.AddSingleton<IEventRepository, SqlServerRepos.EventRepository>();
|
2019-03-19 00:39:03 -04:00
|
|
|
|
services.AddSingleton<IInstallationDeviceRepository, NoopRepos.InstallationDeviceRepository>();
|
2017-12-01 09:22:04 -05:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
services.AddSingleton<IEventRepository, TableStorageRepos.EventRepository>();
|
2019-03-19 00:39:03 -04:00
|
|
|
|
services.AddSingleton<IInstallationDeviceRepository, TableStorageRepos.InstallationDeviceRepository>();
|
2017-12-01 09:22:04 -05:00
|
|
|
|
}
|
2017-05-05 20:57:33 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void AddBaseServices(this IServiceCollection services)
|
|
|
|
|
|
{
|
2017-12-01 14:06:16 -05:00
|
|
|
|
services.AddScoped<ICipherService, CipherService>();
|
2017-05-05 20:57:33 -04:00
|
|
|
|
services.AddScoped<IUserService, UserService>();
|
2017-12-01 16:00:30 -05:00
|
|
|
|
services.AddScoped<IOrganizationService, OrganizationService>();
|
|
|
|
|
|
services.AddScoped<ICollectionService, CollectionService>();
|
|
|
|
|
|
services.AddScoped<IGroupService, GroupService>();
|
2017-12-08 16:03:20 -05:00
|
|
|
|
services.AddScoped<Services.IEventService, EventService>();
|
2017-12-04 09:32:42 -05:00
|
|
|
|
services.AddSingleton<IDeviceService, DeviceService>();
|
2017-05-05 20:57:33 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-07 16:31:00 -04:00
|
|
|
|
public static void AddDefaultServices(this IServiceCollection services, GlobalSettings globalSettings)
|
2017-05-05 20:57:33 -04:00
|
|
|
|
{
|
2019-02-08 23:53:09 -05:00
|
|
|
|
services.AddSingleton<IPaymentService, StripePaymentService>();
|
2018-08-03 23:04:47 -04:00
|
|
|
|
services.AddSingleton<IMailService, HandlebarsMailService>();
|
2017-08-22 15:27:29 -04:00
|
|
|
|
services.AddSingleton<ILicensingService, LicensingService>();
|
2017-12-19 16:02:39 -05:00
|
|
|
|
services.AddSingleton<IApplicationCacheService, InMemoryApplicationCacheService>();
|
2017-08-07 16:31:00 -04:00
|
|
|
|
|
2017-08-08 17:27:01 -04:00
|
|
|
|
if(CoreHelpers.SettingHasValue(globalSettings.Mail.SendGridApiKey))
|
2017-08-07 16:31:00 -04:00
|
|
|
|
{
|
|
|
|
|
|
services.AddSingleton<IMailDeliveryService, SendGridMailDeliveryService>();
|
|
|
|
|
|
}
|
2019-03-13 16:19:00 -04:00
|
|
|
|
else if(CoreHelpers.SettingHasValue(globalSettings.Amazon?.AccessKeySecret))
|
|
|
|
|
|
{
|
|
|
|
|
|
services.AddSingleton<IMailDeliveryService, AmazonSesMailDeliveryService>();
|
|
|
|
|
|
}
|
2017-10-05 08:34:46 -04:00
|
|
|
|
else if(CoreHelpers.SettingHasValue(globalSettings.Mail?.Smtp?.Host))
|
2017-08-08 17:27:01 -04:00
|
|
|
|
{
|
2019-01-22 19:44:03 -05:00
|
|
|
|
services.AddSingleton<IMailDeliveryService, MailKitSmtpMailDeliveryService>();
|
2017-08-08 17:27:01 -04:00
|
|
|
|
}
|
2017-08-07 16:31:00 -04:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
services.AddSingleton<IMailDeliveryService, NoopMailDeliveryService>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-02 17:23:37 -04:00
|
|
|
|
services.AddSingleton<IPushNotificationService, MultiServicePushNotificationService>();
|
2017-08-11 10:04:59 -04:00
|
|
|
|
if(globalSettings.SelfHosted &&
|
|
|
|
|
|
CoreHelpers.SettingHasValue(globalSettings.PushRelayBaseUri) &&
|
|
|
|
|
|
globalSettings.Installation?.Id != null &&
|
|
|
|
|
|
CoreHelpers.SettingHasValue(globalSettings.Installation?.Key))
|
2017-08-08 17:27:01 -04:00
|
|
|
|
{
|
2017-08-11 10:04:59 -04:00
|
|
|
|
services.AddSingleton<IPushRegistrationService, RelayPushRegistrationService>();
|
2017-08-08 17:27:01 -04:00
|
|
|
|
}
|
2017-08-11 10:04:59 -04:00
|
|
|
|
else if(!globalSettings.SelfHosted)
|
2017-08-08 17:27:01 -04:00
|
|
|
|
{
|
|
|
|
|
|
services.AddSingleton<IPushRegistrationService, NotificationHubPushRegistrationService>();
|
|
|
|
|
|
}
|
2017-08-11 10:04:59 -04:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
services.AddSingleton<IPushRegistrationService, NoopPushRegistrationService>();
|
|
|
|
|
|
}
|
2017-08-08 17:27:01 -04:00
|
|
|
|
|
2017-08-17 20:18:16 -04:00
|
|
|
|
if(!globalSettings.SelfHosted && CoreHelpers.SettingHasValue(globalSettings.Storage.ConnectionString))
|
2017-08-07 16:31:00 -04:00
|
|
|
|
{
|
|
|
|
|
|
services.AddSingleton<IBlockIpService, AzureQueueBlockIpService>();
|
|
|
|
|
|
}
|
2019-03-18 16:23:37 -04:00
|
|
|
|
else if(!globalSettings.SelfHosted && CoreHelpers.SettingHasValue(globalSettings.Amazon?.AccessKeySecret))
|
|
|
|
|
|
{
|
|
|
|
|
|
services.AddSingleton<IBlockIpService, AmazonSqsBlockIpService>();
|
|
|
|
|
|
}
|
2017-08-07 16:31:00 -04:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
services.AddSingleton<IBlockIpService, NoopBlockIpService>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-27 22:39:14 -05:00
|
|
|
|
if(!globalSettings.SelfHosted && CoreHelpers.SettingHasValue(globalSettings.Events.ConnectionString))
|
2017-12-08 16:03:20 -05:00
|
|
|
|
{
|
|
|
|
|
|
services.AddSingleton<IEventWriteService, AzureQueueEventWriteService>();
|
|
|
|
|
|
}
|
|
|
|
|
|
else if(globalSettings.SelfHosted)
|
|
|
|
|
|
{
|
|
|
|
|
|
services.AddSingleton<IEventWriteService, RepositoryEventWriteService>();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
services.AddSingleton<IEventWriteService, NoopEventWriteService>();
|
|
|
|
|
|
}
|
2017-12-04 12:17:26 -05:00
|
|
|
|
|
2017-08-08 17:27:01 -04:00
|
|
|
|
if(CoreHelpers.SettingHasValue(globalSettings.Attachment.ConnectionString))
|
2017-08-07 16:31:00 -04:00
|
|
|
|
{
|
|
|
|
|
|
services.AddSingleton<IAttachmentStorageService, AzureAttachmentStorageService>();
|
|
|
|
|
|
}
|
2017-08-08 17:27:01 -04:00
|
|
|
|
else if(CoreHelpers.SettingHasValue(globalSettings.Attachment.BaseDirectory))
|
|
|
|
|
|
{
|
|
|
|
|
|
services.AddSingleton<IAttachmentStorageService, LocalAttachmentStorageService>();
|
|
|
|
|
|
}
|
2017-08-07 16:31:00 -04:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
services.AddSingleton<IAttachmentStorageService, NoopAttachmentStorageService>();
|
|
|
|
|
|
}
|
2017-05-05 20:57:33 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void AddNoopServices(this IServiceCollection services)
|
|
|
|
|
|
{
|
|
|
|
|
|
services.AddSingleton<IMailService, NoopMailService>();
|
2017-06-16 14:24:04 -04:00
|
|
|
|
services.AddSingleton<IMailDeliveryService, NoopMailDeliveryService>();
|
2017-05-26 09:44:54 -04:00
|
|
|
|
services.AddSingleton<IPushNotificationService, NoopPushNotificationService>();
|
2017-05-05 20:57:33 -04:00
|
|
|
|
services.AddSingleton<IBlockIpService, NoopBlockIpService>();
|
2017-05-26 00:50:27 -04:00
|
|
|
|
services.AddSingleton<IPushRegistrationService, NoopPushRegistrationService>();
|
2017-06-15 15:34:12 -04:00
|
|
|
|
services.AddSingleton<IAttachmentStorageService, NoopAttachmentStorageService>();
|
2017-08-11 22:55:25 -04:00
|
|
|
|
services.AddSingleton<ILicensingService, NoopLicensingService>();
|
2017-12-04 12:17:26 -05:00
|
|
|
|
services.AddSingleton<IEventWriteService, NoopEventWriteService>();
|
2017-05-05 20:57:33 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static IdentityBuilder AddCustomIdentityServices(
|
|
|
|
|
|
this IServiceCollection services, GlobalSettings globalSettings)
|
|
|
|
|
|
{
|
2018-04-03 14:31:33 -04:00
|
|
|
|
services.AddSingleton<IOrganizationDuoWebTokenProvider, OrganizationDuoWebTokenProvider>();
|
2018-09-12 00:15:59 -04:00
|
|
|
|
services.Configure<PasswordHasherOptions>(options => options.IterationCount = 100000);
|
2017-06-23 10:08:29 -04:00
|
|
|
|
services.Configure<TwoFactorRememberTokenProviderOptions>(options =>
|
|
|
|
|
|
{
|
|
|
|
|
|
options.TokenLifespan = TimeSpan.FromDays(30);
|
|
|
|
|
|
});
|
2018-09-12 10:35:05 -04:00
|
|
|
|
|
2018-09-12 00:15:59 -04:00
|
|
|
|
var identityBuilder = services.AddIdentityWithoutCookieAuth<User, Role>(options =>
|
2017-05-05 20:57:33 -04: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-06 23:19:42 -04:00
|
|
|
|
SecurityStampClaimType = "sstamp",
|
|
|
|
|
|
UserNameClaimType = JwtClaimTypes.Email,
|
2017-08-10 14:39:11 -04:00
|
|
|
|
UserIdClaimType = JwtClaimTypes.Subject
|
2017-05-05 20:57:33 -04:00
|
|
|
|
};
|
|
|
|
|
|
options.Tokens.ChangeEmailTokenProvider = TokenOptions.DefaultEmailProvider;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
identityBuilder
|
2018-09-12 00:15:59 -04:00
|
|
|
|
.AddUserStore<UserStore>()
|
|
|
|
|
|
.AddRoleStore<RoleStore>()
|
|
|
|
|
|
.AddTokenProvider<DataProtectorTokenProvider<User>>(TokenOptions.DefaultProvider)
|
2018-12-19 22:27:45 -05:00
|
|
|
|
.AddTokenProvider<AuthenticatorTokenProvider>(
|
|
|
|
|
|
CoreHelpers.CustomProviderName(TwoFactorProviderType.Authenticator))
|
|
|
|
|
|
.AddTokenProvider<EmailTokenProvider>(
|
|
|
|
|
|
CoreHelpers.CustomProviderName(TwoFactorProviderType.Email))
|
|
|
|
|
|
.AddTokenProvider<YubicoOtpTokenProvider>(
|
|
|
|
|
|
CoreHelpers.CustomProviderName(TwoFactorProviderType.YubiKey))
|
|
|
|
|
|
.AddTokenProvider<DuoWebTokenProvider>(
|
|
|
|
|
|
CoreHelpers.CustomProviderName(TwoFactorProviderType.Duo))
|
|
|
|
|
|
.AddTokenProvider<U2fTokenProvider>(
|
|
|
|
|
|
CoreHelpers.CustomProviderName(TwoFactorProviderType.U2f))
|
|
|
|
|
|
.AddTokenProvider<TwoFactorRememberTokenProvider>(
|
|
|
|
|
|
CoreHelpers.CustomProviderName(TwoFactorProviderType.Remember))
|
2017-05-05 20:57:33 -04:00
|
|
|
|
.AddTokenProvider<EmailTokenProvider<User>>(TokenOptions.DefaultEmailProvider);
|
|
|
|
|
|
|
|
|
|
|
|
return identityBuilder;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-12 10:35:05 -04:00
|
|
|
|
public static Tuple<IdentityBuilder, IdentityBuilder> AddPasswordlessIdentityServices<TUserStore>(
|
2018-09-12 00:15:59 -04:00
|
|
|
|
this IServiceCollection services, GlobalSettings globalSettings) where TUserStore : class
|
2018-08-28 17:40:08 -04:00
|
|
|
|
{
|
|
|
|
|
|
services.TryAddTransient<ILookupNormalizer, LowerInvariantLookupNormalizer>();
|
2018-03-21 14:26:49 -04:00
|
|
|
|
services.Configure<DataProtectionTokenProviderOptions>(options =>
|
|
|
|
|
|
{
|
|
|
|
|
|
options.TokenLifespan = TimeSpan.FromMinutes(15);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2018-09-12 10:35:05 -04:00
|
|
|
|
var passwordlessIdentityBuilder = services.AddIdentity<IdentityUser, Role>()
|
2018-03-21 14:26:49 -04:00
|
|
|
|
.AddUserStore<TUserStore>()
|
|
|
|
|
|
.AddRoleStore<RoleStore>()
|
|
|
|
|
|
.AddDefaultTokenProviders();
|
|
|
|
|
|
|
2018-09-12 10:35:05 -04:00
|
|
|
|
var regularIdentityBuilder = services.AddIdentityCore<User>()
|
|
|
|
|
|
.AddUserStore<UserStore>();
|
|
|
|
|
|
|
2018-03-21 14:26:49 -04:00
|
|
|
|
services.TryAddScoped<PasswordlessSignInManager<IdentityUser>, PasswordlessSignInManager<IdentityUser>>();
|
|
|
|
|
|
|
|
|
|
|
|
services.ConfigureApplicationCookie(options =>
|
|
|
|
|
|
{
|
|
|
|
|
|
options.LoginPath = "/login";
|
|
|
|
|
|
options.LogoutPath = "/";
|
2018-03-28 10:38:01 -04:00
|
|
|
|
options.AccessDeniedPath = "/login?accessDenied=true";
|
2018-03-21 14:26:49 -04:00
|
|
|
|
options.Cookie.Name = $"Bitwarden_{globalSettings.ProjectName}";
|
|
|
|
|
|
options.Cookie.HttpOnly = true;
|
2018-03-23 10:12:09 -04:00
|
|
|
|
options.Cookie.Expiration = options.ExpireTimeSpan = TimeSpan.FromDays(2);
|
2018-03-21 14:26:49 -04:00
|
|
|
|
options.ReturnUrlParameter = "returnUrl";
|
|
|
|
|
|
options.SlidingExpiration = true;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2018-09-12 10:35:05 -04:00
|
|
|
|
return new Tuple<IdentityBuilder, IdentityBuilder>(passwordlessIdentityBuilder, regularIdentityBuilder);
|
2018-03-21 14:26:49 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-15 09:26:19 -04:00
|
|
|
|
public static void AddIdentityAuthenticationServices(
|
|
|
|
|
|
this IServiceCollection services, GlobalSettings globalSettings, IHostingEnvironment environment,
|
2018-08-15 18:43:26 -04:00
|
|
|
|
Action<AuthorizationOptions> addAuthorization)
|
2018-08-15 09:26:19 -04:00
|
|
|
|
{
|
|
|
|
|
|
services
|
|
|
|
|
|
.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
|
|
|
|
|
|
.AddIdentityServerAuthentication(options =>
|
|
|
|
|
|
{
|
|
|
|
|
|
options.Authority = globalSettings.BaseServiceUri.InternalIdentity;
|
|
|
|
|
|
options.RequireHttpsMetadata = !environment.IsDevelopment() &&
|
|
|
|
|
|
globalSettings.BaseServiceUri.InternalIdentity.StartsWith("https");
|
|
|
|
|
|
options.TokenRetriever = TokenRetrieval.FromAuthorizationHeaderOrQueryString();
|
|
|
|
|
|
options.NameClaimType = ClaimTypes.Email;
|
|
|
|
|
|
options.SupportedTokens = SupportedTokens.Jwt;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2018-08-15 18:43:26 -04:00
|
|
|
|
if(addAuthorization != null)
|
2018-08-15 09:26:19 -04:00
|
|
|
|
{
|
2018-08-15 18:43:26 -04:00
|
|
|
|
services.AddAuthorization(config =>
|
2018-08-15 09:26:19 -04:00
|
|
|
|
{
|
2018-08-15 18:43:26 -04:00
|
|
|
|
addAuthorization.Invoke(config);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2019-02-26 17:01:06 -05:00
|
|
|
|
|
|
|
|
|
|
if(environment.IsDevelopment())
|
|
|
|
|
|
{
|
|
|
|
|
|
Microsoft.IdentityModel.Logging.IdentityModelEventSource.ShowPII = true;
|
|
|
|
|
|
}
|
2018-08-15 09:26:19 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-05-05 20:57:33 -04:00
|
|
|
|
public static IIdentityServerBuilder AddCustomIdentityServerServices(
|
|
|
|
|
|
this IServiceCollection services, IHostingEnvironment env, GlobalSettings globalSettings)
|
|
|
|
|
|
{
|
2018-03-29 14:59:36 -04:00
|
|
|
|
var issuerUri = new Uri(globalSettings.BaseServiceUri.InternalIdentity);
|
2017-05-05 20:57:33 -04: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;
|
2018-03-29 14:59:36 -04:00
|
|
|
|
options.IssuerUri = $"{issuerUri.Scheme}://{issuerUri.Host}";
|
2017-08-10 14:39:11 -04:00
|
|
|
|
options.Caching.ClientStoreExpiration = new TimeSpan(0, 5, 0);
|
2017-05-05 20:57:33 -04:00
|
|
|
|
})
|
2017-08-10 14:39:11 -04:00
|
|
|
|
.AddInMemoryCaching()
|
2017-05-05 20:57:33 -04:00
|
|
|
|
.AddInMemoryApiResources(ApiResources.GetApiResources())
|
2017-08-10 14:39:11 -04:00
|
|
|
|
.AddClientStoreCache<ClientStore>();
|
2017-05-05 20:57:33 -04:00
|
|
|
|
|
2017-07-14 13:29:52 -04:00
|
|
|
|
if(env.IsDevelopment())
|
2017-05-05 20:57:33 -04:00
|
|
|
|
{
|
2017-10-06 11:38:47 -04:00
|
|
|
|
identityServerBuilder.AddDeveloperSigningCredential(false);
|
2017-05-05 20:57:33 -04:00
|
|
|
|
}
|
2017-08-08 17:27:01 -04:00
|
|
|
|
else if(!string.IsNullOrWhiteSpace(globalSettings.IdentityServer.CertificatePassword)
|
|
|
|
|
|
&& File.Exists("identity.pfx"))
|
2017-08-07 11:24:16 -04:00
|
|
|
|
{
|
2017-08-07 16:31:00 -04:00
|
|
|
|
var identityServerCert = CoreHelpers.GetCertificate("identity.pfx",
|
2017-08-07 11:24:16 -04:00
|
|
|
|
globalSettings.IdentityServer.CertificatePassword);
|
|
|
|
|
|
identityServerBuilder.AddSigningCredential(identityServerCert);
|
|
|
|
|
|
}
|
2017-08-07 16:31:00 -04:00
|
|
|
|
else if(!string.IsNullOrWhiteSpace(globalSettings.IdentityServer.CertificateThumbprint))
|
2017-05-05 20:57:33 -04:00
|
|
|
|
{
|
2017-07-14 13:29:52 -04:00
|
|
|
|
var identityServerCert = CoreHelpers.GetCertificate(globalSettings.IdentityServer.CertificateThumbprint);
|
|
|
|
|
|
identityServerBuilder.AddSigningCredential(identityServerCert);
|
2017-05-05 20:57:33 -04:00
|
|
|
|
}
|
2017-08-07 16:31:00 -04:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new Exception("No identity certificate to use.");
|
|
|
|
|
|
}
|
2017-05-05 20:57:33 -04:00
|
|
|
|
|
2017-08-10 14:39:11 -04:00
|
|
|
|
services.AddTransient<ClientStore>();
|
|
|
|
|
|
services.AddTransient<ICorsPolicyService, AllowAllCorsPolicyService>();
|
2017-05-05 20:57:33 -04: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 00:02:52 -04:00
|
|
|
|
if(env.IsDevelopment())
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-08 23:06:28 -04:00
|
|
|
|
if(globalSettings.SelfHosted && CoreHelpers.SettingHasValue(globalSettings.DataProtection.Directory))
|
2017-08-08 00:02:52 -04:00
|
|
|
|
{
|
2017-08-08 23:06:28 -04:00
|
|
|
|
services.AddDataProtection()
|
|
|
|
|
|
.PersistKeysToFileSystem(new DirectoryInfo(globalSettings.DataProtection.Directory));
|
2017-08-08 00:02:52 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-02-28 00:02:52 -05:00
|
|
|
|
if(!globalSettings.SelfHosted && CoreHelpers.SettingHasValue(globalSettings.Storage.ConnectionString) &&
|
|
|
|
|
|
CoreHelpers.SettingHasValue(globalSettings.DataProtection.CertificateThumbprint))
|
2017-05-05 20:57:33 -04:00
|
|
|
|
{
|
2019-02-28 00:02:52 -05:00
|
|
|
|
var dataProtectionCert = CoreHelpers.GetCertificate(
|
|
|
|
|
|
globalSettings.DataProtection.CertificateThumbprint);
|
2017-05-05 20:57:33 -04:00
|
|
|
|
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 00:08:09 -04:00
|
|
|
|
IConfiguration configuration)
|
2017-05-05 20:57:33 -04:00
|
|
|
|
{
|
|
|
|
|
|
var globalSettings = new GlobalSettings();
|
2017-10-19 00:08:09 -04:00
|
|
|
|
ConfigurationBinder.Bind(configuration.GetSection("GlobalSettings"), globalSettings);
|
2017-05-05 20:57:33 -04:00
|
|
|
|
services.AddSingleton(s => globalSettings);
|
|
|
|
|
|
return globalSettings;
|
|
|
|
|
|
}
|
2017-07-21 12:53:26 -04:00
|
|
|
|
|
2017-08-25 08:57:43 -04:00
|
|
|
|
public static void UseDefaultMiddleware(this IApplicationBuilder app, IHostingEnvironment env)
|
2017-07-21 12:53:26 -04:00
|
|
|
|
{
|
2017-08-25 08:57:43 -04:00
|
|
|
|
// Add version information to response headers
|
|
|
|
|
|
app.Use(async (httpContext, next) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
httpContext.Response.OnStarting((state) =>
|
|
|
|
|
|
{
|
2017-09-06 23:57:14 -04:00
|
|
|
|
httpContext.Response.Headers.Append("Server-Version", CoreHelpers.GetVersion());
|
2017-08-25 08:57:43 -04:00
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
|
}, null);
|
2017-07-21 12:53:26 -04:00
|
|
|
|
|
2017-08-25 08:57:43 -04:00
|
|
|
|
await next.Invoke();
|
|
|
|
|
|
});
|
2017-07-21 12:53:26 -04:00
|
|
|
|
}
|
2019-04-26 09:52:54 -04:00
|
|
|
|
|
|
|
|
|
|
public static void UseForwardedHeaders(this IApplicationBuilder app, GlobalSettings globalSettings)
|
|
|
|
|
|
{
|
|
|
|
|
|
var options = new ForwardedHeadersOptions
|
|
|
|
|
|
{
|
|
|
|
|
|
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
|
|
|
|
|
|
};
|
|
|
|
|
|
if(!string.IsNullOrWhiteSpace(globalSettings.KnownProxies))
|
|
|
|
|
|
{
|
|
|
|
|
|
var proxies = globalSettings.KnownProxies.Split(',');
|
|
|
|
|
|
foreach(var proxy in proxies)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(System.Net.IPAddress.TryParse(proxy, out var ip))
|
|
|
|
|
|
{
|
|
|
|
|
|
options.KnownProxies.Add(ip);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if(options.KnownProxies.Count > 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
options.ForwardLimit = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
app.UseForwardedHeaders(options);
|
|
|
|
|
|
}
|
2017-05-05 20:57:33 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|