Updated auth configuration for DotBased ASP auth system

This commit is contained in:
Max 2024-07-27 16:08:06 +02:00
parent 0b8625f3d5
commit aeb2038b81
7 changed files with 63 additions and 21 deletions

@ -1 +1 @@
Subproject commit c7d654a0ba67419f8afa0613c3da7a94395a1b01 Subproject commit c092b8a679c218c845e4981deb7b5b5dd174a311

View File

@ -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<Result<string>> GetSessionStateAsync()
{
var localStorageValue = await _localStorage.GetItemAsync<string>(ISessionStateProvider.SessionStateName);
if (localStorageValue != null && !localStorageValue.IsNullOrWhiteSpace())
return Result<string>.Ok(localStorageValue);
_logger.Warning("Failed to get session token from local storage!");
return Result<string>.Failed("Local storage returned null or empty on session token.");
}
public async Task<Result> 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();
}
}

View File

@ -1,16 +1,35 @@
using Blazored.LocalStorage; using Blazored.LocalStorage;
using DotBased.ASP.Auth;
using DotBased.ASP.Auth.Domains.Auth;
using DotBased.ASP.Auth.Domains.Identity;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using MudBlazor.Services; using MudBlazor.Services;
using SharpRSS.Blazor.Auth;
using SharpRSS.Blazor.Components; using SharpRSS.Blazor.Components;
using SharpRSS.Business; using SharpRSS.Business;
using SharpRSS.Data; using SharpRSS.Data;
using SharpRSS.Data.Domains.Configuration; using SharpRSS.Data.Domains.Configuration;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
builder.UseSRSS(); builder.AddSRSS();
builder.Services.AddBlazoredLocalStorage(); 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<RoleModel>() { new RoleModel() { Name = "Admin", Description = "Administration role." }}});
service.CreateUserAsync(new UserModel() { UserName = "User", Email = "user@example.com", Enabled = true, PasswordHash = "password"});
};
options.SetDataProviderType<MemoryAuthDataProvider>();
options.SetSessionStateProviderType<LocalStorageSessionStateProvider>();
});
// Add services to the container. // Add services to the container.
builder.Services.AddRazorComponents() builder.Services.AddRazorComponents()
.AddInteractiveServerComponents(); .AddInteractiveServerComponents();
@ -56,8 +75,7 @@ if (!app.Environment.IsDevelopment())
app.UseHttpsRedirection(); app.UseHttpsRedirection();
app.UseStaticFiles(); app.UseStaticFiles();
app.UseAntiforgery(); app.UseAntiforgery();
app.UseAuthentication(); app.UseBasedServerAuth();
app.UseAuthorization();
app.MapRazorComponents<App>() app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode(); .AddInteractiveServerRenderMode();

BIN
SharpRSS.Blazor/SRSS.db-shm Normal file

Binary file not shown.

View File

View File

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

View File

@ -16,7 +16,7 @@ namespace SharpRSS.Business;
public static class DependencyInjection public static class DependencyInjection
{ {
public static WebApplicationBuilder UseSRSS(this WebApplicationBuilder builder) public static WebApplicationBuilder AddSRSS(this WebApplicationBuilder builder)
{ {
/* /*
* Logging (serilog) * Logging (serilog)
@ -64,18 +64,6 @@ public static class DependencyInjection
*/ */
builder.Services.AddScoped<AuthService>(); builder.Services.AddScoped<AuthService>();
/*
* 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 //TODO: Auth, Settings
return builder; return builder;
} }