diff --git a/DotBased.ASP.Auth/AuthService.cs b/DotBased.ASP.Auth/AuthService.cs deleted file mode 100644 index 7b973d1..0000000 --- a/DotBased.ASP.Auth/AuthService.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace DotBased.ASP.Auth; - -public class AuthService -{ - -} \ No newline at end of file diff --git a/DotBased.ASP.Auth/BasedAuthBuilder.cs b/DotBased.ASP.Auth/BasedAuthBuilder.cs new file mode 100644 index 0000000..e9a824b --- /dev/null +++ b/DotBased.ASP.Auth/BasedAuthBuilder.cs @@ -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? configurationAction = null) + { + _services = services; + Configuration = new BasedAuthConfiguration(); + configurationAction?.Invoke(Configuration); + + services.AddSingleton(Configuration); + services.AddScoped(); + + services.AddScoped(); + services.AddAuthentication(options => + { + options.DefaultScheme = BasedAuthenticationHandler.AuthenticationScheme; + }).AddScheme(BasedAuthenticationHandler.AuthenticationScheme, null); + services.AddAuthorization(); + services.AddCascadingAuthenticationState(); + } + public BasedAuthConfiguration Configuration { get; } + private readonly IServiceCollection _services; + + public void AddSessionStateProvider() where TSessionStateProviderType : ISessionStateProvider => _services.AddScoped(typeof(ISessionStateProvider), typeof(TSessionStateProviderType)); +} \ No newline at end of file diff --git a/DotBased.ASP.Auth/BasedAuthConfiguration.cs b/DotBased.ASP.Auth/BasedAuthConfiguration.cs index b09ecf0..ad12d36 100644 --- a/DotBased.ASP.Auth/BasedAuthConfiguration.cs +++ b/DotBased.ASP.Auth/BasedAuthConfiguration.cs @@ -1,3 +1,5 @@ +using DotBased.ASP.Auth.Services; + namespace DotBased.ASP.Auth; public class BasedAuthConfiguration diff --git a/DotBased.ASP.Auth/BasedAuthenticationStateProvider.cs b/DotBased.ASP.Auth/BasedAuthenticationStateProvider.cs deleted file mode 100644 index 0b497b7..0000000 --- a/DotBased.ASP.Auth/BasedAuthenticationStateProvider.cs +++ /dev/null @@ -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() {new Claim(ClaimTypes.Role, "test")}))); - - public override Task GetAuthenticationStateAsync() - { - return Task.FromResult(_anonState); - } -} \ No newline at end of file diff --git a/DotBased.ASP.Auth/BasedServerAuthenticationStateProvider.cs b/DotBased.ASP.Auth/BasedServerAuthenticationStateProvider.cs new file mode 100644 index 0000000..579019c --- /dev/null +++ b/DotBased.ASP.Auth/BasedServerAuthenticationStateProvider.cs @@ -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() {new Claim(ClaimTypes.Role, "test")}))); + + public override Task GetAuthenticationStateAsync() + { + return Task.FromResult(_anonState); + } +} \ No newline at end of file diff --git a/DotBased.ASP.Auth/DotBasedAuthDependencyInjection.cs b/DotBased.ASP.Auth/DotBasedAuthDependencyInjection.cs index 1fceae2..38e315d 100644 --- a/DotBased.ASP.Auth/DotBasedAuthDependencyInjection.cs +++ b/DotBased.ASP.Auth/DotBasedAuthDependencyInjection.cs @@ -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 /// /// Use the app.UseAuthentication() and app.UseAuthorization()! - /// Service colllection + /// Service collection /// DotBased auth configuration - public static void UseBasedAuth(this IServiceCollection services, Action? configurationAction = null) + public static BasedAuthBuilder UseBasedServerAuth(this IServiceCollection services, Action? configurationAction = null) { - var config = new BasedAuthConfiguration(); - configurationAction?.Invoke(config); - services.AddSingleton(config); - - services.AddScoped(); - services.AddAuthentication(options => - { - options.DefaultScheme = BasedAuthenticationHandler.AuthenticationScheme; - }).AddScheme(BasedAuthenticationHandler.AuthenticationScheme, null); - services.AddAuthorization(); + var authBuilder = new BasedAuthBuilder(services, configurationAction); + return authBuilder; } } \ No newline at end of file diff --git a/DotBased.ASP.Auth/IAuthDataProvider.cs b/DotBased.ASP.Auth/IAuthDataProvider.cs index 508d6c4..468b820 100644 --- a/DotBased.ASP.Auth/IAuthDataProvider.cs +++ b/DotBased.ASP.Auth/IAuthDataProvider.cs @@ -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 UpdateAuthenticationStateAsync(AuthenticationStateModel authenticationState); public Task DeleteAuthenticationStateAsync(AuthenticationStateModel authenticationState); public Task> GetAuthenticationStateAsync(string id); - - // Authorization - } \ No newline at end of file diff --git a/DotBased.ASP.Auth/ISessionStateProvider.cs b/DotBased.ASP.Auth/ISessionStateProvider.cs new file mode 100644 index 0000000..ee8850a --- /dev/null +++ b/DotBased.ASP.Auth/ISessionStateProvider.cs @@ -0,0 +1,8 @@ +namespace DotBased.ASP.Auth; + +public interface ISessionStateProvider +{ + public const string SessionStateName = "BasedServerSession"; + public Task> GetSessionStateAsync(); + public Task SetSessionStateAsync(string state); +} \ No newline at end of file diff --git a/DotBased.ASP.Auth/Services/AuthService.cs b/DotBased.ASP.Auth/Services/AuthService.cs new file mode 100644 index 0000000..ab447a8 --- /dev/null +++ b/DotBased.ASP.Auth/Services/AuthService.cs @@ -0,0 +1,6 @@ +namespace DotBased.ASP.Auth.Services; + +public class AuthService +{ + +} \ No newline at end of file