Added ISessionStateProvider interface & base implementation

This commit is contained in:
Max 2024-07-27 14:38:39 +02:00
parent c7d654a0ba
commit 2b17ed4cd7
9 changed files with 81 additions and 47 deletions

View File

@ -1,6 +0,0 @@
namespace DotBased.ASP.Auth;
public class AuthService
{
}

View File

@ -0,0 +1,31 @@
using DotBased.ASP.Auth.Scheme;
using DotBased.ASP.Auth.Services;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.Extensions.DependencyInjection;
namespace DotBased.ASP.Auth;
public class BasedAuthBuilder
{
public BasedAuthBuilder(IServiceCollection services, Action<BasedAuthConfiguration>? configurationAction = null)
{
_services = services;
Configuration = new BasedAuthConfiguration();
configurationAction?.Invoke(Configuration);
services.AddSingleton<BasedAuthConfiguration>(Configuration);
services.AddScoped<AuthService>();
services.AddScoped<AuthenticationStateProvider, BasedServerAuthenticationStateProvider>();
services.AddAuthentication(options =>
{
options.DefaultScheme = BasedAuthenticationHandler.AuthenticationScheme;
}).AddScheme<BasedAuthenticationHandlerOptions, BasedAuthenticationHandler>(BasedAuthenticationHandler.AuthenticationScheme, null);
services.AddAuthorization();
services.AddCascadingAuthenticationState();
}
public BasedAuthConfiguration Configuration { get; }
private readonly IServiceCollection _services;
public void AddSessionStateProvider<TSessionStateProviderType>() where TSessionStateProviderType : ISessionStateProvider => _services.AddScoped(typeof(ISessionStateProvider), typeof(TSessionStateProviderType));
}

View File

@ -1,3 +1,5 @@
using DotBased.ASP.Auth.Services;
namespace DotBased.ASP.Auth;
public class BasedAuthConfiguration

View File

@ -1,24 +0,0 @@
using System.Security.Claims;
using DotBased.Logging;
using Microsoft.AspNetCore.Components.Authorization;
namespace DotBased.ASP.Auth;
// RevalidatingServerAuthenticationStateProvider
public class BasedAuthenticationStateProvider : AuthenticationStateProvider
{
public BasedAuthenticationStateProvider(BasedAuthConfiguration configuration)
{
_config = configuration;
_logger = LogService.RegisterLogger(typeof(BasedAuthenticationStateProvider));
}
private BasedAuthConfiguration _config;
private ILogger _logger;
private AuthenticationState _anonState = new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>() {new Claim(ClaimTypes.Role, "test")})));
public override Task<AuthenticationState> GetAuthenticationStateAsync()
{
return Task.FromResult(_anonState);
}
}

View File

@ -0,0 +1,29 @@
using System.Security.Claims;
using DotBased.Logging;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Server;
using ILogger = DotBased.Logging.ILogger;
namespace DotBased.ASP.Auth;
// RevalidatingServerAuthenticationStateProvider
// AuthenticationStateProvider
public class BasedServerAuthenticationStateProvider : ServerAuthenticationStateProvider
{
public BasedServerAuthenticationStateProvider(BasedAuthConfiguration configuration, ISessionStateProvider stateProvider)
{
_config = configuration;
_stateProvider = stateProvider;
_logger = LogService.RegisterLogger(typeof(BasedServerAuthenticationStateProvider));
}
private BasedAuthConfiguration _config;
private ISessionStateProvider _stateProvider;
private ILogger _logger;
private readonly AuthenticationState _anonState = new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>() {new Claim(ClaimTypes.Role, "test")})));
public override Task<AuthenticationState> GetAuthenticationStateAsync()
{
return Task.FromResult(_anonState);
}
}

View File

@ -1,5 +1,3 @@
using DotBased.ASP.Auth.Scheme;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.Extensions.DependencyInjection;
namespace DotBased.ASP.Auth;
@ -10,19 +8,11 @@ public static class DotBasedAuthDependencyInjection
/// Use the DotBased authentication implementation
/// </summary>
/// <remarks>Use the app.UseAuthentication() and app.UseAuthorization()!</remarks>
/// <param name="services">Service colllection</param>
/// <param name="services">Service collection</param>
/// <param name="configurationAction">DotBased auth configuration</param>
public static void UseBasedAuth(this IServiceCollection services, Action<BasedAuthConfiguration>? configurationAction = null)
public static BasedAuthBuilder UseBasedServerAuth(this IServiceCollection services, Action<BasedAuthConfiguration>? configurationAction = null)
{
var config = new BasedAuthConfiguration();
configurationAction?.Invoke(config);
services.AddSingleton<BasedAuthConfiguration>(config);
services.AddScoped<AuthenticationStateProvider, BasedAuthenticationStateProvider>();
services.AddAuthentication(options =>
{
options.DefaultScheme = BasedAuthenticationHandler.AuthenticationScheme;
}).AddScheme<BasedAuthenticationHandlerOptions, BasedAuthenticationHandler>(BasedAuthenticationHandler.AuthenticationScheme, null);
services.AddAuthorization();
var authBuilder = new BasedAuthBuilder(services, configurationAction);
return authBuilder;
}
}

View File

@ -1,5 +1,6 @@
using DotBased.ASP.Auth.Domains.Auth;
using DotBased.ASP.Auth.Domains.Identity;
using DotBased.Objects;
namespace DotBased.ASP.Auth;
@ -32,7 +33,4 @@ public interface IAuthDataProvider
public Task<Result> UpdateAuthenticationStateAsync(AuthenticationStateModel authenticationState);
public Task<Result> DeleteAuthenticationStateAsync(AuthenticationStateModel authenticationState);
public Task<Result<AuthenticationStateModel>> GetAuthenticationStateAsync(string id);
// Authorization
}

View File

@ -0,0 +1,8 @@
namespace DotBased.ASP.Auth;
public interface ISessionStateProvider
{
public const string SessionStateName = "BasedServerSession";
public Task<Result<string>> GetSessionStateAsync();
public Task<Result> SetSessionStateAsync(string state);
}

View File

@ -0,0 +1,6 @@
namespace DotBased.ASP.Auth.Services;
public class AuthService
{
}