This commit is contained in:
2023-06-17 14:16:09 +03:00
parent 62178f1f32
commit 7ae5d3bc81
180 changed files with 12932 additions and 192 deletions
+57 -9
View File
@@ -23,7 +23,11 @@ using IdentityServer4.Extensions;
using IdentityServer4.Models;
using Microsoft.IdentityModel.Tokens;
using System.Security.Cryptography;
using System.Text.Json;
using System.Text.Json.Serialization;
using Core.Identity;
using Data.Repositories.Account;
using Data.Repositories.Currency;
using Data.Repositories.Users;
using DbContext;
using Services.Users;
@@ -32,6 +36,10 @@ using Identity.Configure;
using Infrastructure;
using Microsoft.Extensions.Logging.Console;
using IdentityServer4.Services;
using MyOffice.Services.Currency;
using Services.Account;
using MyOffice.Data.Repositories.Motion;
using Services.Motion;
public class Program
{
@@ -92,6 +100,9 @@ public class Program
.AddJsonOptions(options =>
{
options.AllowInputFormatterExceptionMessages = builder.Environment.IsDevelopment();
options.JsonSerializerOptions.MaxDepth = 0;
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
});
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
@@ -155,8 +166,9 @@ public class Program
LoginUrl = "/authentication/signin",
LoginReturnUrlParameter = "returnUrl"
};
//o.IssuerUri = "http://localhost:9300";
})
.AddSigningCredential(CreateSigningCredential())
.AddSigningCredential(CreateSigningCredential(builder))
.AddPersistedGrantStore<PersistedGrantStore>()
.AddInMemoryIdentityResources(IdentityServerConfig.GetIdentityResources())
.AddInMemoryApiScopes(IdentityServerConfig.GetApiScopes())
@@ -196,11 +208,27 @@ public class Program
{
builder.Services.AddScoped<IUserRepository, UserRepository>();
builder.Services.AddScoped<IUserExternalRepository, UserExternalRepository>();
builder.Services.AddScoped<ICurrencyGlobalRepository, CurrencyGlobalRepository>();
builder.Services.AddScoped<ICurrencyRepository, CurrencyRepository>();
builder.Services.AddScoped<ICurrencyRateRepository, CurrencyRateRepository>();
builder.Services.AddScoped<IAccountCategoryRepository, AccountCategoryRepository>();
builder.Services.AddScoped<IAccountRepository, AccountRepository>();
builder.Services.AddScoped<IAccountAccountCategoryRepository, AccountAccountCategoryRepository>();
builder.Services.AddScoped<IMotionCategoryRepository, MotionCategoryRepository>();
builder.Services.AddScoped<IMotionRepository, MotionRepository>();
builder.Services.AddScoped<IMotionGlobalRepository, MotionGlobalRepository>();
builder.Services.AddScoped<IAccountMotionRepository, AccountMotionRepository>();
}
private static void AddBusinessServices(WebApplicationBuilder builder)
{
builder.Services.AddScoped<UserService, UserService>();
builder.Services.AddScoped<CurrencyService, CurrencyService>();
builder.Services.AddScoped<AccountService, AccountService>();
builder.Services.AddScoped<MotionService, MotionService>();
}
private static readonly ConcurrentDictionary<string, PhysicalFileInfo> _staticFilesCache = new();
@@ -225,6 +253,8 @@ public class Program
app.UseDefaultFiles();
app.UseStaticFiles();
var logger = app.Logger;
// configuration on first request
app.Use(async (ctx, next) =>
{
@@ -244,7 +274,7 @@ public class Program
// set real frontend host
globalSettings = ctx.RequestServices.GetRequiredService<GlobalSettings>();
globalSettings.Host = GetFrontendHost(ctx);
globalSettings.Host = GetFrontendHost(ctx, logger);
// update identity server
var options = ctx.RequestServices.GetRequiredService<IdentityServerOptions>();
@@ -265,7 +295,10 @@ public class Program
if (path.Value.EqualsIgnoreCase("/.well-known/openid-configuration"))
{
globalSettings ??= ctx.RequestServices.GetRequiredService<GlobalSettings>();
logger.LogInformation($"OC Host: {globalSettings.Host}");
ctx.SetIdentityServerOrigin(globalSettings.Host);
var options = ctx.RequestServices.GetRequiredService<IdentityServerOptions>();
options.IssuerUri = globalSettings.Host;
}
await next();
@@ -286,13 +319,11 @@ public class Program
var wwwrootPath = Path.Combine(webRootPath, "wwwroot");
app.Use(async (context, next) =>
{
//app.Logger.LogWarning($"?path: {context.Request.Path}");
var path = context.Request.Path;
if (path.Value == null) return;
if (coreRoutes.Any(x => path.StartsWithSegments(x, StringComparison.OrdinalIgnoreCase)))
{
//app.Logger.LogWarning($"*path: {path.Value}");
await next();
}
else
@@ -303,11 +334,9 @@ public class Program
var fileInfo = new FileInfo(Path.Combine(wwwrootPath, segments.LastOrDefault()!));
if (!fileInfo.Exists)
{
//app.Logger.LogWarning($"-path: {fileInfo.FullName}");
return;
}
//app.Logger.LogWarning($"+path: {fileInfo.FullName}");
physicalFileInfo = new PhysicalFileInfo(fileInfo);
_staticFilesCache.TryAdd(path.Value, physicalFileInfo);
}
@@ -328,7 +357,7 @@ public class Program
app.MapControllers();
}
private static string GetFrontendHost(HttpContext ctx)
private static string GetFrontendHost(HttpContext ctx, ILogger logger)
{
// get X-Forwarded-Proto when reversed proxy used
// internet <-> nginx (https) <-> site(http)
@@ -340,12 +369,31 @@ public class Program
host += $"://{ctx.Request.Host.Value}";
logger.LogCritical($"FR Host: {host}");
if (host.Contains(":4300"))
{
logger.LogCritical(ctx.Request.Path);
logger.LogCritical(string.Join('|', ctx.Request.Headers.Select(x => $"{x.Key}={x.Value}")));
}
return host;
}
private static SigningCredentials CreateSigningCredential()
private static SigningCredentials CreateSigningCredential(WebApplicationBuilder builder)
{
var rsaSecurityKey = new RsaSecurityKey(new RSACryptoServiceProvider(2048));
var rsa = RSA.Create();
var webRootPath = builder.Configuration.GetValue<string>(WebHostDefaults.ContentRootKey);
var privateKey = File.ReadAllText(Path.Combine(webRootPath, "rsa_key.pem"));
privateKey = privateKey
.Replace("-----BEGIN RSA PRIVATE KEY-----", string.Empty)
.Replace("-----END RSA PRIVATE KEY-----", string.Empty)
.Replace(Environment.NewLine, string.Empty);
var privateKeyBytes = Convert.FromBase64String(privateKey);
rsa.ImportRSAPrivateKey(privateKeyBytes, out int _);
var rsaSecurityKey = new RsaSecurityKey(rsa);
var credentials = new SigningCredentials(rsaSecurityKey, SecurityAlgorithms.RsaSha256);
return credentials;