Reimplementing Authorization system

This commit is contained in:
max 2024-12-01 03:13:55 +01:00
parent eb277e0937
commit 4580c5f5dc
31 changed files with 250 additions and 4 deletions

View File

@ -0,0 +1,13 @@
namespace DotBased.ASP.Auth.Services;
public class AuthenticationService
{
public AuthenticationService()
{
/*
* - Login
* - Logout
* - Register
*/
}
}

View File

@ -48,3 +48,18 @@ public class BasedAuthConfiguration
where TSessionStateProviderType : ISessionStateProvider =>
SessionStateProviderType = typeof(TSessionStateProviderType);
}
public class BasedPasswordOptions
{
}
public class BasedUserOptions
{
}
public class BasedLockoutOptions
{
}

View File

@ -1,5 +1,4 @@
using System.Security.Claims;
using DotBased.ASP.Auth.Services;
using DotBased.Logging;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Server;

View File

@ -17,4 +17,9 @@
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
<ItemGroup>
<Folder Include="Models\Auth\States\" />
<Folder Include="Models\Repositories\" />
</ItemGroup>
</Project>

View File

@ -18,7 +18,6 @@ public static class DotBasedAuthDependencyInjection
var Configuration = new BasedAuthConfiguration();
configurationAction?.Invoke(Configuration);
services.AddSingleton<BasedAuthConfiguration>(Configuration);
if (Configuration.AuthDataRepositoryType == null)
throw new ArgumentNullException(nameof(Configuration.AuthDataRepositoryType), $"No '{nameof(IAuthDataRepository)}' configured!");

View File

@ -0,0 +1,11 @@
namespace DotBased.ASP.Auth.Models.Configuration;
public class AuthConfiguration
{
public CacheConfiguration Cache { get; set; } = new();
public LockoutConfiguration Lockout { get; set; } = new();
public PasswordConfiguration Password { get; set; } = new();
public ProviderConfiguration Provider { get; set; } = new();
public RepositoryConfiguration Repository { get; set; } = new();
public UserConfiguration User { get; set; } = new();
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -0,0 +1,9 @@
namespace DotBased.ASP.Auth.Managers;
public class SecurityManager
{
public SecurityManager()
{
}
}

View File

@ -7,7 +7,7 @@ using DotBased.Logging;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
namespace DotBased.ASP.Auth.Services;
namespace DotBased.ASP.Auth;
public class SecurityService
{
@ -60,9 +60,9 @@ public class SecurityService
new(ClaimTypes.Surname, userResult.Value.FamilyName),
new(ClaimTypes.Email, userResult.Value.Email)
};
//TODO: combine group, user roles
claims.AddRange(userResult.Value.Groups.Select(group => new Claim(ClaimTypes.GroupSid, group.Id)));
claims.AddRange(userResult.Value.Roles.Select(role => new Claim(ClaimTypes.Role, role.Name)));
claims.AddRange(userResult.Value.Groups.Select(g => g.Roles).SelectMany(gRolesList => gRolesList, (_, role) => new Claim(ClaimTypes.Role, role.Name)));
var claimsIdentity = new ClaimsIdentity(claims, BasedAuthDefaults.AuthenticationScheme);
var authState = new AuthenticationState(new ClaimsPrincipal(claimsIdentity));
_dataCache.CacheSessionState(authStateModel, authState);

View File

@ -0,0 +1,16 @@
namespace DotBased.ASP.Authentication;
public class BasedAuthenticationBuilder
{
public BasedAuthenticationBuilder(Type authRepository)
{
if (authRepository.IsValueType)
{
throw new ArgumentException("Type cannot be a value type!", nameof(authRepository));
}
AuthenticationRepositoryType = authRepository;
}
public Type AuthenticationRepositoryType { get; }
}

View File

@ -0,0 +1,34 @@
using DotBased.ASP.Authentication.Configuration;
using DotBased.ASP.Authentication.Repositories;
using Microsoft.Extensions.DependencyInjection;
namespace DotBased.ASP.Authentication;
public static class BasedAuthenticationExtensions
{
public static BasedAuthenticationBuilder AddBasedAuthentication(this IServiceCollection services, Action<AuthenticationConfiguration>? configurationAction)
{
/*
* Add services
* - Validators
* - Managers
* - Services
*/
if (configurationAction != null)
{
services.Configure(configurationAction);
}
return new BasedAuthenticationBuilder(typeof(BasedAuthenticationBuilder));
}
public static BasedAuthenticationBuilder AddRepository<TRepository>(this BasedAuthenticationBuilder builder)
{
return builder;
}
public static BasedAuthenticationBuilder SeedData<TRepository>(this BasedAuthenticationBuilder builder, Action<TRepository> seeder) where TRepository : RepositoryBase
{
return builder;
}
}

View File

@ -0,0 +1,11 @@
using Microsoft.AspNetCore.Components.Authorization;
namespace DotBased.ASP.Authentication;
public class BasedAuthenticationStateProvider : AuthenticationStateProvider
{
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
{
throw new NotImplementedException();
}
}

View File

@ -0,0 +1,9 @@
namespace DotBased.ASP.Authentication.Configuration;
public class AuthenticationConfiguration
{
public CacheConfiguration Cache { get; set; } = new();
public LockoutConfiguration Lockout { get; set; } = new();
public PasswordConfiguration Password { get; set; } = new();
public UserConfiguration User { get; set; } = new();
}

View File

@ -0,0 +1,6 @@
namespace DotBased.ASP.Authentication.Configuration;
public class CacheConfiguration
{
}

View File

@ -0,0 +1,6 @@
namespace DotBased.ASP.Authentication.Configuration;
public class LockoutConfiguration
{
}

View File

@ -0,0 +1,6 @@
namespace DotBased.ASP.Authentication.Configuration;
public class PasswordConfiguration
{
}

View File

@ -0,0 +1,6 @@
namespace DotBased.ASP.Authentication.Configuration;
public class UserConfiguration
{
}

View File

@ -0,0 +1,6 @@
namespace DotBased.ASP.Authentication.Providers;
public interface IStateProvider
{
}

View File

@ -0,0 +1,6 @@
namespace DotBased.ASP.Authentication.Repositories;
public interface IAttributeRepository
{
}

View File

@ -0,0 +1,6 @@
namespace DotBased.ASP.Authentication.Repositories;
public interface IAuthenticationRepository
{
}

View File

@ -0,0 +1,6 @@
namespace DotBased.ASP.Authentication.Repositories;
public interface IGroupRepository
{
}

View File

@ -0,0 +1,6 @@
namespace DotBased.ASP.Authentication.Repositories;
public interface IRoleRepository
{
}

View File

@ -0,0 +1,6 @@
namespace DotBased.ASP.Authentication.Repositories;
public interface ISessionRepository
{
}

View File

@ -0,0 +1,6 @@
namespace DotBased.ASP.Authentication.Repositories;
public interface IUserRepository
{
}

View File

@ -0,0 +1,6 @@
namespace DotBased.ASP.Authentication.Repositories;
public abstract class RepositoryBase : IAuthenticationRepository, IAttributeRepository, IGroupRepository, ISessionRepository, IUserRepository
{
}

View File

@ -0,0 +1,17 @@
using DotBased.ASP.Authentication.Configuration;
using DotBased.Logging;
using Microsoft.Extensions.Options;
namespace DotBased.ASP.Authentication;
public class SecurityManager
{
public SecurityManager(IServiceProvider services, IOptions<AuthenticationConfiguration>? config)
{
_services = services;
Configuration = config?.Value ?? new AuthenticationConfiguration();
}
private ILogger _logger = LogService.RegisterLogger<SecurityManager>();
private IServiceProvider _services;
public AuthenticationConfiguration Configuration { get; set; }
}