DocSpace-buildtools/common/ASC.Api.Core/Core/BaseStartup.cs

307 lines
12 KiB
C#
Raw Normal View History

2022-03-15 18:00:53 +00:00
// (c) Copyright Ascensio System SIA 2010-2022
//
// This program is a free software product.
// You can redistribute it and/or modify it under the terms
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
// Foundation. In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended
// to the effect that Ascensio System SIA expressly excludes the warranty of non-infringement of
// any third-party rights.
//
// This program is distributed WITHOUT ANY WARRANTY, without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, see
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
//
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
//
// The interactive user interfaces in modified source and object code versions of the Program must
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
//
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
// trademark law for use of our trademarks.
//
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
2022-08-18 16:00:22 +00:00
using System.IdentityModel.Tokens.Jwt;
2022-08-19 10:56:32 +00:00
2022-08-18 16:00:22 +00:00
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.Net.Http.Headers;
2022-03-15 18:00:53 +00:00
using JsonConverter = System.Text.Json.Serialization.JsonConverter;
namespace ASC.Api.Core;
public abstract class BaseStartup
2021-08-08 12:56:54 +00:00
{
2022-04-15 09:08:06 +00:00
private readonly IConfiguration _configuration;
private readonly IHostEnvironment _hostEnvironment;
protected virtual JsonConverter[] Converters { get; }
protected virtual bool AddControllersAsServices { get; }
protected virtual bool ConfirmAddScheme { get; }
protected virtual bool AddAndUseSession { get; }
2022-03-03 11:04:17 +00:00
protected DIHelper DIHelper { get; }
protected bool LoadProducts { get; set; } = true;
protected bool LoadConsumers { get; } = true;
public BaseStartup(IConfiguration configuration, IHostEnvironment hostEnvironment)
{
2022-04-15 09:08:06 +00:00
_configuration = configuration;
_hostEnvironment = hostEnvironment;
2022-03-03 11:04:17 +00:00
DIHelper = new DIHelper();
2022-04-15 09:08:06 +00:00
if (bool.TryParse(_configuration["core:products"], out var loadProducts))
2022-03-03 11:04:17 +00:00
{
LoadProducts = loadProducts;
2020-07-15 12:33:44 +00:00
}
2022-03-03 11:04:17 +00:00
}
2020-07-15 12:33:44 +00:00
2022-03-03 11:04:17 +00:00
public virtual void ConfigureServices(IServiceCollection services)
{
2022-04-15 09:08:06 +00:00
services.AddCustomHealthCheck(_configuration);
2022-03-03 11:04:17 +00:00
services.AddHttpContextAccessor();
services.AddMemoryCache();
services.AddHttpClient();
2022-07-28 18:00:49 +00:00
services.AddScoped<EFLoggerFactory>();
2022-07-29 17:23:19 +00:00
services.AddBaseDbContextPool<AccountLinkContext>();
services.AddBaseDbContextPool<CoreDbContext>();
services.AddBaseDbContextPool<TenantDbContext>();
services.AddBaseDbContextPool<UserDbContext>();
services.AddBaseDbContextPool<TelegramDbContext>();
2022-08-14 22:00:11 +00:00
services.AddBaseDbContextPool<FirebaseDbContext>();
2022-07-29 17:23:19 +00:00
services.AddBaseDbContextPool<CustomDbContext>();
services.AddBaseDbContextPool<WebstudioDbContext>();
services.AddBaseDbContextPool<InstanceRegistrationContext>();
services.AddBaseDbContextPool<IntegrationEventLogContext>();
services.AddBaseDbContextPool<FeedDbContext>();
services.AddBaseDbContextPool<MessagesContext>();
services.AddBaseDbContextPool<WebhooksDbContext>();
2020-07-15 12:33:44 +00:00
2022-03-03 11:04:17 +00:00
if (AddAndUseSession)
{
2022-03-03 11:04:17 +00:00
services.AddSession();
}
2021-04-20 07:55:52 +00:00
2022-03-03 11:04:17 +00:00
DIHelper.Configure(services);
Action<JsonOptions> jsonOptions = options =>
{
options.JsonSerializerOptions.WriteIndented = false;
options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
options.JsonSerializerOptions.Converters.Add(new ApiDateTimeConverter());
if (Converters != null)
{
2022-03-03 11:04:17 +00:00
foreach (var c in Converters)
{
options.JsonSerializerOptions.Converters.Add(c);
}
2022-03-03 11:04:17 +00:00
}
};
services.AddControllers()
.AddXmlSerializerFormatters()
.AddJsonOptions(jsonOptions);
services.AddSingleton(jsonOptions);
DIHelper.AddControllers();
DIHelper.TryAdd<CultureMiddleware>();
DIHelper.TryAdd<IpSecurityFilter>();
DIHelper.TryAdd<PaymentFilter>();
DIHelper.TryAdd<ProductSecurityFilter>();
DIHelper.TryAdd<TenantStatusFilter>();
DIHelper.TryAdd<ConfirmAuthHandler>();
2022-08-18 16:00:22 +00:00
DIHelper.TryAdd<BasicAuthHandler>();
2022-03-03 11:04:17 +00:00
DIHelper.TryAdd<CookieAuthHandler>();
DIHelper.TryAdd<WebhooksGlobalFilterAttribute>();
2022-04-15 09:08:06 +00:00
services.AddDistributedCache(_configuration);
services.AddEventBus(_configuration);
services.AddDistributedTaskQueue();
2022-04-15 09:08:06 +00:00
services.AddCacheNotify(_configuration);
2021-09-03 14:02:52 +00:00
2022-03-03 11:04:17 +00:00
DIHelper.TryAdd(typeof(IWebhookPublisher), typeof(WebhookPublisher));
2020-07-15 12:33:44 +00:00
2022-03-03 11:04:17 +00:00
if (LoadProducts)
{
2022-04-15 09:08:06 +00:00
DIHelper.RegisterProducts(_configuration, _hostEnvironment.ContentRootPath);
2022-03-03 11:04:17 +00:00
}
2020-10-19 19:04:07 +00:00
2022-03-22 12:10:05 +00:00
services.AddOptions();
2022-03-03 11:04:17 +00:00
services.AddMvcCore(config =>
{
config.Conventions.Add(new ControllerNameAttributeConvention());
2022-03-03 11:04:17 +00:00
var policy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build();
2022-03-03 11:04:17 +00:00
config.Filters.Add(new AuthorizeFilter(policy));
config.Filters.Add(new TypeFilterAttribute(typeof(TenantStatusFilter)));
config.Filters.Add(new TypeFilterAttribute(typeof(PaymentFilter)));
config.Filters.Add(new TypeFilterAttribute(typeof(IpSecurityFilter)));
config.Filters.Add(new TypeFilterAttribute(typeof(ProductSecurityFilter)));
config.Filters.Add(new CustomResponseFilterAttribute());
config.Filters.Add(new CustomExceptionFilterAttribute());
config.Filters.Add(new TypeFilterAttribute(typeof(WebhooksGlobalFilterAttribute)));
2022-08-05 15:47:33 +00:00
config.Filters.Add(new TypeFilterAttribute(typeof(FormatFilter)));
2022-03-03 11:04:17 +00:00
config.OutputFormatters.RemoveType<XmlSerializerOutputFormatter>();
config.OutputFormatters.Add(new XmlOutputFormatter());
});
2022-08-18 16:00:22 +00:00
var authBuilder = services.AddAuthentication(options =>
2022-03-03 11:04:17 +00:00
{
2022-08-18 16:00:22 +00:00
options.DefaultScheme = "MultiAuthSchemes";
options.DefaultChallengeScheme = "MultiAuthSchemes";
}).AddScheme<AuthenticationSchemeOptions, CookieAuthHandler>(CookieAuthenticationDefaults.AuthenticationScheme, a => { })
.AddScheme<AuthenticationSchemeOptions, BasicAuthHandler>("Basic", a => { })
.AddScheme<AuthenticationSchemeOptions, ConfirmAuthHandler>("confirm", a => { })
.AddJwtBearer("Bearer", options =>
{
options.Authority = _configuration["core:oidc:authority"];
options.IncludeErrorDetails = true;
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateAudience = false
};
options.Events = new JwtBearerEvents
{
OnTokenValidated = ctx =>
{
using var scope = ctx.HttpContext.RequestServices.CreateScope();
var securityContext = scope.ServiceProvider.GetService<ASC.Core.SecurityContext>();
var claimUserId = ctx.Principal.FindFirstValue("userId");
if (String.IsNullOrEmpty(claimUserId))
{
throw new Exception("Claim 'UserId' is not present in claim list");
}
var userId = new Guid(claimUserId);
securityContext.AuthenticateMeWithoutCookie(userId, ctx.Principal.Claims.ToList());
return Task.CompletedTask;
}
};
2022-08-19 10:56:32 +00:00
})
2022-08-18 16:00:22 +00:00
.AddPolicyScheme("MultiAuthSchemes", JwtBearerDefaults.AuthenticationScheme, options =>
{
2022-08-19 10:56:32 +00:00
2022-08-18 16:00:22 +00:00
options.ForwardDefaultSelector = context =>
{
var authorizationHeader = context.Request.Headers[HeaderNames.Authorization].FirstOrDefault();
if (String.IsNullOrEmpty(authorizationHeader)) return CookieAuthenticationDefaults.AuthenticationScheme;
if (authorizationHeader.StartsWith("Basic ")) return "Basic";
2022-08-19 10:56:32 +00:00
2022-08-18 16:00:22 +00:00
if (authorizationHeader.StartsWith("Bearer "))
{
var token = authorizationHeader.Substring("Bearer ".Length).Trim();
var jwtHandler = new JwtSecurityTokenHandler();
return (jwtHandler.CanReadToken(token) && jwtHandler.ReadJwtToken(token).Issuer.Equals(_configuration["core:oidc:authority"]))
? JwtBearerDefaults.AuthenticationScheme : CookieAuthenticationDefaults.AuthenticationScheme;
}
return CookieAuthenticationDefaults.AuthenticationScheme;
};
});
2021-04-20 07:55:52 +00:00
2022-08-09 16:13:07 +00:00
services.AddAutoMapper(GetAutoMapperProfileAssemblies());
2022-08-18 16:00:22 +00:00
2022-08-08 16:25:22 +00:00
if (!_hostEnvironment.IsDevelopment())
{
services.AddStartupTask<WarmupServicesStartupTask>()
.TryAddSingleton(services);
}
2022-03-03 11:04:17 +00:00
}
2022-08-09 16:13:07 +00:00
private IEnumerable<Assembly> GetAutoMapperProfileAssemblies()
{
return AppDomain.CurrentDomain.GetAssemblies().Where(x => x.GetName().Name.StartsWith("ASC."));
}
2022-03-03 11:04:17 +00:00
public virtual void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
2022-03-03 11:04:17 +00:00
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
app.UseRouting();
if (AddAndUseSession)
{
app.UseSession();
}
2022-03-03 11:04:17 +00:00
app.UseAuthentication();
app.UseAuthorization();
app.UseCultureMiddleware();
app.UseEndpoints(endpoints =>
{
2022-03-03 11:04:17 +00:00
endpoints.MapCustom();
endpoints.MapHealthChecks("/health", new HealthCheckOptions()
{
Predicate = _ => true,
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});
endpoints.MapHealthChecks("/liveness", new HealthCheckOptions
{
Predicate = r => r.Name.Contains("self")
});
});
}
public void ConfigureContainer(ContainerBuilder builder)
{
2022-04-15 09:08:06 +00:00
builder.Register(_configuration, LoadProducts, LoadConsumers);
2022-03-03 11:04:17 +00:00
}
}
2021-08-08 12:56:54 +00:00
public static class LogNLogConfigureExtenstion
{
2022-04-14 19:42:15 +00:00
private static LoggingConfiguration GetXmlLoggingConfiguration(IHostEnvironment hostEnvironment, IConfiguration configuration)
{
var loggerConfiguration = new XmlLoggingConfiguration(CrossPlatform.PathCombine(configuration["pathToConf"], "nlog.config"));
2022-04-14 19:42:15 +00:00
var settings = new ConfigurationExtension(configuration).GetSetting<NLogSettings>("log");
if (!string.IsNullOrEmpty(settings.Name))
{
loggerConfiguration.Variables["name"] = settings.Name;
}
if (!string.IsNullOrEmpty(settings.Dir))
{
Merge branch 'release/v1.2' into develop # Conflicts: # common/ASC.Common/Caching/KafkaCache.cs # common/ASC.Common/Logging/Log.cs # common/ASC.Core.Common/Notify/Engine/DispatchEngine.cs # common/ASC.Core.Common/Notify/Engine/NotifyEngine.cs # common/ASC.Data.Backup.Core/ASC.Data.Backup.Core.csproj # common/ASC.Data.Backup.Core/BackupResource.Designer.cs # common/ASC.Data.Storage/ChunkedUploader/CommonChunkedUploadSession.cs # common/ASC.Data.Storage/DiscStorage/DiscDataStore.cs # common/ASC.Data.Storage/GoogleCloud/GoogleCloudStorage.cs # common/ASC.Data.Storage/RackspaceCloud/RackspaceCloudStorage.cs # common/ASC.Data.Storage/S3/S3Storage.cs # common/ASC.Notify.Textile/ASC.Notify.Textile.csproj # common/services/ASC.AuditTrail/ASC.AuditTrail.csproj # products/ASC.Files/Core/ASC.Files.Core.csproj # products/ASC.Files/Core/Core/Compress/CompressToArchive.cs # products/ASC.Files/Core/Core/Compress/CompressToTarGz.cs # products/ASC.Files/Core/Core/Compress/CompressToZip.cs # products/ASC.Files/Core/Core/Compress/ICompress.cs # products/ASC.Files/Core/Core/Thirdparty/GoogleDrive/GoogleDriveStorage.cs # products/ASC.Files/Core/Core/Thirdparty/OneDrive/OneDriveStorage.cs # products/ASC.Files/Core/Helpers/Global.cs # products/ASC.Files/Core/Resources/FilesJSResource.Designer.cs # products/ASC.Files/Core/Services/DocumentService/DocumentServiceTracker.cs # products/ASC.Files/Core/Services/WCFService/FileOperations/FileDownloadOperation.cs # products/ASC.Files/Core/Services/WCFService/FileOperations/FileMarkAsReadOperation.cs # products/ASC.Files/Core/Utils/EntryManager.cs # web/ASC.Web.Api/ASC.Web.Api.csproj # web/ASC.Web.Core/Notify/StudioNotifyServiceHelper.cs # web/ASC.Web.Core/Notify/StudioNotifyServiceSender.cs
2022-06-30 13:13:31 +00:00
var dir = Path.IsPathRooted(settings.Dir) ? settings.Dir : CrossPlatform.PathCombine(hostEnvironment.ContentRootPath, settings.Dir);
loggerConfiguration.Variables["dir"] = dir.TrimEnd('/').TrimEnd('\\') + Path.DirectorySeparatorChar;
}
return loggerConfiguration;
}
2022-04-14 19:42:15 +00:00
2022-03-03 11:04:17 +00:00
public static IHostBuilder ConfigureNLogLogging(this IHostBuilder hostBuilder)
{
return hostBuilder.ConfigureLogging((hostBuildexContext, r) =>
2021-08-08 12:56:54 +00:00
{
LogManager.ThrowConfigExceptions = false;
r.AddNLog(GetXmlLoggingConfiguration(hostBuildexContext.HostingEnvironment, hostBuildexContext.Configuration));
2022-03-03 11:04:17 +00:00
});
}
2020-07-03 11:47:27 +00:00
}