mirror of
https://github.com/hmaxnl/DotBased.git
synced 2025-01-18 18:14:20 +01:00
Reimplementing Authorization system
This commit is contained in:
parent
eb277e0937
commit
4580c5f5dc
13
DotBased.ASP.Auth/AuthenticationService.cs
Normal file
13
DotBased.ASP.Auth/AuthenticationService.cs
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
namespace DotBased.ASP.Auth.Services;
|
||||||
|
|
||||||
|
public class AuthenticationService
|
||||||
|
{
|
||||||
|
public AuthenticationService()
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* - Login
|
||||||
|
* - Logout
|
||||||
|
* - Register
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
}
|
|
@ -48,3 +48,18 @@ public class BasedAuthConfiguration
|
||||||
where TSessionStateProviderType : ISessionStateProvider =>
|
where TSessionStateProviderType : ISessionStateProvider =>
|
||||||
SessionStateProviderType = typeof(TSessionStateProviderType);
|
SessionStateProviderType = typeof(TSessionStateProviderType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class BasedPasswordOptions
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public class BasedUserOptions
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public class BasedLockoutOptions
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
|
@ -1,5 +1,4 @@
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
using DotBased.ASP.Auth.Services;
|
|
||||||
using DotBased.Logging;
|
using DotBased.Logging;
|
||||||
using Microsoft.AspNetCore.Components.Authorization;
|
using Microsoft.AspNetCore.Components.Authorization;
|
||||||
using Microsoft.AspNetCore.Components.Server;
|
using Microsoft.AspNetCore.Components.Server;
|
||||||
|
|
|
@ -17,4 +17,9 @@
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<FrameworkReference Include="Microsoft.AspNetCore.App" />
|
<FrameworkReference Include="Microsoft.AspNetCore.App" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Folder Include="Models\Auth\States\" />
|
||||||
|
<Folder Include="Models\Repositories\" />
|
||||||
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
@ -18,7 +18,6 @@ public static class DotBasedAuthDependencyInjection
|
||||||
var Configuration = new BasedAuthConfiguration();
|
var Configuration = new BasedAuthConfiguration();
|
||||||
configurationAction?.Invoke(Configuration);
|
configurationAction?.Invoke(Configuration);
|
||||||
|
|
||||||
|
|
||||||
services.AddSingleton<BasedAuthConfiguration>(Configuration);
|
services.AddSingleton<BasedAuthConfiguration>(Configuration);
|
||||||
if (Configuration.AuthDataRepositoryType == null)
|
if (Configuration.AuthDataRepositoryType == null)
|
||||||
throw new ArgumentNullException(nameof(Configuration.AuthDataRepositoryType), $"No '{nameof(IAuthDataRepository)}' configured!");
|
throw new ArgumentNullException(nameof(Configuration.AuthDataRepositoryType), $"No '{nameof(IAuthDataRepository)}' configured!");
|
||||||
|
|
11
DotBased.ASP.Auth/Models/Configuration/AuthConfiguration.cs
Normal file
11
DotBased.ASP.Auth/Models/Configuration/AuthConfiguration.cs
Normal 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();
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
namespace DotBased.ASP.Auth.Models.Configuration;
|
||||||
|
|
||||||
|
public class CacheConfiguration
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
namespace DotBased.ASP.Auth.Models.Configuration;
|
||||||
|
|
||||||
|
public class LockoutConfiguration
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
namespace DotBased.ASP.Auth.Models.Configuration;
|
||||||
|
|
||||||
|
public class PasswordConfiguration
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
namespace DotBased.ASP.Auth.Models.Configuration;
|
||||||
|
|
||||||
|
public class ProviderConfiguration
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
namespace DotBased.ASP.Auth.Models.Configuration;
|
||||||
|
|
||||||
|
public class RepositoryConfiguration
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
namespace DotBased.ASP.Auth.Models.Configuration;
|
||||||
|
|
||||||
|
public class UserConfiguration
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
9
DotBased.ASP.Auth/SecurityManager.cs
Normal file
9
DotBased.ASP.Auth/SecurityManager.cs
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
namespace DotBased.ASP.Auth.Managers;
|
||||||
|
|
||||||
|
public class SecurityManager
|
||||||
|
{
|
||||||
|
public SecurityManager()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,7 +7,7 @@ using DotBased.Logging;
|
||||||
using Microsoft.AspNetCore.Components.Authorization;
|
using Microsoft.AspNetCore.Components.Authorization;
|
||||||
using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
|
using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
|
||||||
|
|
||||||
namespace DotBased.ASP.Auth.Services;
|
namespace DotBased.ASP.Auth;
|
||||||
|
|
||||||
public class SecurityService
|
public class SecurityService
|
||||||
{
|
{
|
||||||
|
@ -60,9 +60,9 @@ public class SecurityService
|
||||||
new(ClaimTypes.Surname, userResult.Value.FamilyName),
|
new(ClaimTypes.Surname, userResult.Value.FamilyName),
|
||||||
new(ClaimTypes.Email, userResult.Value.Email)
|
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.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.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 claimsIdentity = new ClaimsIdentity(claims, BasedAuthDefaults.AuthenticationScheme);
|
||||||
var authState = new AuthenticationState(new ClaimsPrincipal(claimsIdentity));
|
var authState = new AuthenticationState(new ClaimsPrincipal(claimsIdentity));
|
||||||
_dataCache.CacheSessionState(authStateModel, authState);
|
_dataCache.CacheSessionState(authStateModel, authState);
|
16
DotBased.ASP.Authentication/BasedAuthenticationBuilder.cs
Normal file
16
DotBased.ASP.Authentication/BasedAuthenticationBuilder.cs
Normal 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; }
|
||||||
|
}
|
34
DotBased.ASP.Authentication/BasedAuthenticationExtensions.cs
Normal file
34
DotBased.ASP.Authentication/BasedAuthenticationExtensions.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
namespace DotBased.ASP.Authentication.Configuration;
|
||||||
|
|
||||||
|
public class CacheConfiguration
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
namespace DotBased.ASP.Authentication.Configuration;
|
||||||
|
|
||||||
|
public class LockoutConfiguration
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
namespace DotBased.ASP.Authentication.Configuration;
|
||||||
|
|
||||||
|
public class PasswordConfiguration
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
namespace DotBased.ASP.Authentication.Configuration;
|
||||||
|
|
||||||
|
public class UserConfiguration
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
6
DotBased.ASP.Authentication/Providers/IStateProvider.cs
Normal file
6
DotBased.ASP.Authentication/Providers/IStateProvider.cs
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
namespace DotBased.ASP.Authentication.Providers;
|
||||||
|
|
||||||
|
public interface IStateProvider
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
namespace DotBased.ASP.Authentication.Repositories;
|
||||||
|
|
||||||
|
public interface IAttributeRepository
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
namespace DotBased.ASP.Authentication.Repositories;
|
||||||
|
|
||||||
|
public interface IAuthenticationRepository
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
namespace DotBased.ASP.Authentication.Repositories;
|
||||||
|
|
||||||
|
public interface IGroupRepository
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
namespace DotBased.ASP.Authentication.Repositories;
|
||||||
|
|
||||||
|
public interface IRoleRepository
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
namespace DotBased.ASP.Authentication.Repositories;
|
||||||
|
|
||||||
|
public interface ISessionRepository
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
namespace DotBased.ASP.Authentication.Repositories;
|
||||||
|
|
||||||
|
public interface IUserRepository
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
namespace DotBased.ASP.Authentication.Repositories;
|
||||||
|
|
||||||
|
public abstract class RepositoryBase : IAuthenticationRepository, IAttributeRepository, IGroupRepository, ISessionRepository, IUserRepository
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
17
DotBased.ASP.Authentication/SecurityManager.cs
Normal file
17
DotBased.ASP.Authentication/SecurityManager.cs
Normal 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; }
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user