Working on http settings

This commit is contained in:
Max 2024-07-06 00:02:02 +02:00
parent c4ae8ab195
commit 89cb9fbe1e
6 changed files with 56 additions and 13 deletions

View File

@ -8,6 +8,7 @@ using SharpRSS.Data;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
builder.UseSRSS(); builder.UseSRSS();
builder.Services.AddBlazoredLocalStorage(); builder.Services.AddBlazoredLocalStorage();
// Add services to the container. // Add services to the container.
@ -16,6 +17,16 @@ builder.Services.AddRazorComponents()
builder.Services.AddMudServices(); builder.Services.AddMudServices();
builder.Services.AddHttpContextAccessor(); // HttpContext accessor builder.Services.AddHttpContextAccessor(); // HttpContext accessor
/*
* HSTS config
*/
builder.Services.AddHsts(options =>
{
options.Preload = true;
options.IncludeSubDomains = true;
options.MaxAge = TimeSpan.FromDays(60); // For DEV = 60 days, PROD = 1 year
});
var app = builder.Build(); var app = builder.Build();
var contextFactory = app.Services.GetService<IDbContextFactory<SRSSContext>>(); var contextFactory = app.Services.GetService<IDbContextFactory<SRSSContext>>();
@ -24,12 +35,14 @@ if (contextFactory != null)
await using var context = await contextFactory.CreateDbContextAsync(); await using var context = await contextFactory.CreateDbContextAsync();
context.Database.EnsureCreated(); context.Database.EnsureCreated();
} }
else
{
}
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment()) if (!app.Environment.IsDevelopment())
{ {
app.UseExceptionHandler("/Error", createScopeForErrors: true); app.UseExceptionHandler("/Error", createScopeForErrors: true);
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts(); app.UseHsts();
} }

View File

@ -31,4 +31,8 @@
</Compile> </Compile>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Auth\" />
</ItemGroup>
</Project> </Project>

View File

@ -43,5 +43,15 @@
"Application": "SharpRSS" "Application": "SharpRSS"
} }
}, },
"HTTP":
{
"HSTS":
{
"EnableHSTS": true,
"MaxAgeSeconds": 31536000,
"IncludeSubdomains": true,
"Preload": true
}
},
"AllowedHosts": "*" "AllowedHosts": "*"
} }

View File

@ -1,14 +1,13 @@
using Blazored.LocalStorage;
using DotBased.Logging; using DotBased.Logging;
using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Http;
using SharpRSS.Business.Services; using SharpRSS.Business.Services;
using ILogger = DotBased.Logging.ILogger;
namespace SharpRSS.Blazor.Auth; namespace SharpRSS.Business.Auth;
public class SRSSAuthenticationStateProvider : AuthenticationStateProvider public class SRSSAuthenticationStateProvider : AuthenticationStateProvider
{ {
public SRSSAuthenticationStateProvider(IHttpContextAccessor contextAccessor, AuthService authService, ILocalStorageService localStorageService) public SRSSAuthenticationStateProvider(IHttpContextAccessor contextAccessor, AuthService authService)
{ {
_logger = LogService.RegisterLogger(typeof(SRSSAuthenticationStateProvider)); _logger = LogService.RegisterLogger(typeof(SRSSAuthenticationStateProvider));
if (contextAccessor.HttpContext != null) if (contextAccessor.HttpContext != null)
@ -20,7 +19,6 @@ public class SRSSAuthenticationStateProvider : AuthenticationStateProvider
throw ex; throw ex;
} }
_authService = authService; _authService = authService;
_localStorageService = localStorageService;
} }
/* /*
@ -29,7 +27,6 @@ public class SRSSAuthenticationStateProvider : AuthenticationStateProvider
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly HttpContext _httpContext; private readonly HttpContext _httpContext;
private readonly AuthService _authService; private readonly AuthService _authService;
private readonly ILocalStorageService _localStorageService;
/* /*
* Consts * Consts
*/ */
@ -37,6 +34,7 @@ public class SRSSAuthenticationStateProvider : AuthenticationStateProvider
public override Task<AuthenticationState> GetAuthenticationStateAsync() public override Task<AuthenticationState> GetAuthenticationStateAsync()
{ {
_logger.Debug("Getting authentication state...");
throw new NotImplementedException(); throw new NotImplementedException();
} }
} }

View File

@ -1,12 +1,14 @@
using DotBased.Logging; using DotBased.Logging;
using DotBased.Logging.Serilog; using DotBased.Logging.Serilog;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.EntityFrameworkCore.Diagnostics;
using Serilog; using Serilog;
using SharpRSS.Business.Auth;
using SharpRSS.Business.Services; using SharpRSS.Business.Services;
using SharpRSS.Core.Configuration; using SharpRSS.Core.Configuration;
using SharpRSS.Data; using SharpRSS.Data;
@ -24,7 +26,7 @@ public static class DependencyInjection
Log.Logger = serilogConfig.CreateLogger(); Log.Logger = serilogConfig.CreateLogger();
LogService.AddLogAdapter(new BasedSerilogAdapter(Log.Logger)); LogService.AddLogAdapter(new BasedSerilogAdapter(Log.Logger));
var logger = LogService.RegisterLogger(typeof(DependencyInjection)); var _logger = LogService.RegisterLogger(typeof(DependencyInjection));
builder.Logging.ClearProviders(); builder.Logging.ClearProviders();
builder.Logging.AddSerilog(); builder.Logging.AddSerilog();
@ -40,17 +42,17 @@ public static class DependencyInjection
switch (dbSettings.Server.ToUpper()) switch (dbSettings.Server.ToUpper())
{ {
case "SQLITE": case "SQLITE":
logger.Information("Configuring SQLite context..."); _logger.Information("Configuring SQLite context...");
options.UseSqlite(dbSettings.Connection); options.UseSqlite(dbSettings.Connection);
break; break;
case "MARIADB": case "MARIADB":
logger.Information("Configuring MariaDB context..."); _logger.Information("Configuring MariaDB context...");
var srvVersion = ServerVersion.AutoDetect(dbSettings.Connection); var srvVersion = ServerVersion.AutoDetect(dbSettings.Connection);
logger.Information("Server found, version: {SrvVersion}", srvVersion.Version.Build); _logger.Information("Server found, version: {SrvVersion}", srvVersion.Version.Build);
options.UseMySql(dbSettings.Connection, srvVersion); options.UseMySql(dbSettings.Connection, srvVersion);
break; break;
case "MSSQL": case "MSSQL":
logger.Information("Configuring MSSQL context..."); _logger.Information("Configuring MSSQL context...");
options.UseSqlServer(dbSettings.Connection); options.UseSqlServer(dbSettings.Connection);
break; break;
default: default:
@ -63,6 +65,11 @@ public static class DependencyInjection
*/ */
builder.Services.AddScoped<AuthService>(); builder.Services.AddScoped<AuthService>();
/*
* Authentication
*/
builder.Services.AddScoped<AuthenticationStateProvider, SRSSAuthenticationStateProvider>();
//TODO: Auth, Settings //TODO: Auth, Settings
return builder; return builder;
} }

View File

@ -0,0 +1,11 @@
namespace SharpRSS.Data.Domains.Configuration;
public class HstsConfiguration
{
public const string Hsts = "HSTS";
public bool EnableHsts { get; set; }
public long MaxAgeSeconds { get; set; }
public bool IncludeSubdomains { get; set; }
public bool Preload { get; set; }
}