From aeb2038b81a92a42ba34c19c907884ddf0f6beb8 Mon Sep 17 00:00:00 2001 From: Max Date: Sat, 27 Jul 2024 16:08:06 +0200 Subject: [PATCH] Updated auth configuration for DotBased ASP auth system --- DotBased | 2 +- .../Auth/LocalStorageSessionStateProvider.cs | 40 ++++++++++++++++++ SharpRSS.Blazor/Program.cs | 24 +++++++++-- SharpRSS.Blazor/SRSS.db-shm | Bin 0 -> 32768 bytes SharpRSS.Blazor/SRSS.db-wal | 0 SharpRSS.Blazor/SharpRSS.Blazor.csproj | 4 -- SharpRSS.Business/DependencyInjection.cs | 14 +----- 7 files changed, 63 insertions(+), 21 deletions(-) create mode 100644 SharpRSS.Blazor/Auth/LocalStorageSessionStateProvider.cs create mode 100644 SharpRSS.Blazor/SRSS.db-shm create mode 100644 SharpRSS.Blazor/SRSS.db-wal diff --git a/DotBased b/DotBased index c7d654a..c092b8a 160000 --- a/DotBased +++ b/DotBased @@ -1 +1 @@ -Subproject commit c7d654a0ba67419f8afa0613c3da7a94395a1b01 +Subproject commit c092b8a679c218c845e4981deb7b5b5dd174a311 diff --git a/SharpRSS.Blazor/Auth/LocalStorageSessionStateProvider.cs b/SharpRSS.Blazor/Auth/LocalStorageSessionStateProvider.cs new file mode 100644 index 0000000..b65b3ba --- /dev/null +++ b/SharpRSS.Blazor/Auth/LocalStorageSessionStateProvider.cs @@ -0,0 +1,40 @@ +using Blazored.LocalStorage; +using DotBased; +using DotBased.ASP.Auth; +using DotBased.Extensions; +using DotBased.Logging; +using Serilog; +using ILogger = DotBased.Logging.ILogger; + +namespace SharpRSS.Blazor.Auth; + +public class LocalStorageSessionStateProvider : ISessionStateProvider +{ + public LocalStorageSessionStateProvider(ILocalStorageService localStorage) + { + _localStorage = localStorage; + _logger = LogService.RegisterLogger(typeof(LocalStorageSessionStateProvider)); + } + private readonly ILocalStorageService _localStorage; + private readonly ILogger _logger; + + public async Task> GetSessionStateAsync() + { + var localStorageValue = await _localStorage.GetItemAsync(ISessionStateProvider.SessionStateName); + if (localStorageValue != null && !localStorageValue.IsNullOrWhiteSpace()) + return Result.Ok(localStorageValue); + _logger.Warning("Failed to get session token from local storage!"); + return Result.Failed("Local storage returned null or empty on session token."); + } + + public async Task SetSessionStateAsync(string state) + { + if (state.IsNullOrWhiteSpace()) + { + Log.Warning("Tried to save empty or null session state to local storage!"); + return Result.Failed("Could not set session state to local storage, value is empty or null!"); + } + await _localStorage.SetItemAsync(ISessionStateProvider.SessionStateName, state); + return Result.Ok(); + } +} \ No newline at end of file diff --git a/SharpRSS.Blazor/Program.cs b/SharpRSS.Blazor/Program.cs index 6514e6c..ba68c42 100644 --- a/SharpRSS.Blazor/Program.cs +++ b/SharpRSS.Blazor/Program.cs @@ -1,16 +1,35 @@ using Blazored.LocalStorage; +using DotBased.ASP.Auth; +using DotBased.ASP.Auth.Domains.Auth; +using DotBased.ASP.Auth.Domains.Identity; using Microsoft.EntityFrameworkCore; using MudBlazor.Services; +using SharpRSS.Blazor.Auth; using SharpRSS.Blazor.Components; using SharpRSS.Business; using SharpRSS.Data; using SharpRSS.Data.Domains.Configuration; var builder = WebApplication.CreateBuilder(args); -builder.UseSRSS(); +builder.AddSRSS(); builder.Services.AddBlazoredLocalStorage(); +builder.Services.AddBasedServerAuth(options => +{ + options.AllowRegistration = false; + options.AuthenticationStateMaxAgeBeforeExpire = TimeSpan.FromDays(7); + options.LoginPath = "/auth/login"; + options.LogoutPath = "/auth/logout"; + options.SeedData = service => + { + service.CreateUserAsync(new UserModel() { UserName = "Admin", Email = "admin@example.com", Enabled = true, PasswordHash = "password", Roles = new List() { new RoleModel() { Name = "Admin", Description = "Administration role." }}}); + service.CreateUserAsync(new UserModel() { UserName = "User", Email = "user@example.com", Enabled = true, PasswordHash = "password"}); + }; + options.SetDataProviderType(); + options.SetSessionStateProviderType(); +}); + // Add services to the container. builder.Services.AddRazorComponents() .AddInteractiveServerComponents(); @@ -56,8 +75,7 @@ if (!app.Environment.IsDevelopment()) app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseAntiforgery(); -app.UseAuthentication(); -app.UseAuthorization(); +app.UseBasedServerAuth(); app.MapRazorComponents() .AddInteractiveServerRenderMode(); diff --git a/SharpRSS.Blazor/SRSS.db-shm b/SharpRSS.Blazor/SRSS.db-shm new file mode 100644 index 0000000000000000000000000000000000000000..fe9ac2845eca6fe6da8a63cd096d9cf9e24ece10 GIT binary patch literal 32768 zcmeIuAr62r3 - - - - diff --git a/SharpRSS.Business/DependencyInjection.cs b/SharpRSS.Business/DependencyInjection.cs index 4862287..b6ab115 100644 --- a/SharpRSS.Business/DependencyInjection.cs +++ b/SharpRSS.Business/DependencyInjection.cs @@ -16,7 +16,7 @@ namespace SharpRSS.Business; public static class DependencyInjection { - public static WebApplicationBuilder UseSRSS(this WebApplicationBuilder builder) + public static WebApplicationBuilder AddSRSS(this WebApplicationBuilder builder) { /* * Logging (serilog) @@ -64,18 +64,6 @@ public static class DependencyInjection */ builder.Services.AddScoped(); - /* - * Authentication - */ - builder.Services.UseBasedAuth(options => - { - options.AllowRegistration = false; - options.AuthenticationStateMaxAgeBeforeExpire = TimeSpan.FromDays(7); - options.LoginPath = "/auth/login"; - options.LogoutPath = "/auth/logout"; - }); - builder.Services.AddCascadingAuthenticationState(); - //TODO: Auth, Settings return builder; }