Implementing ASP auth module

This commit is contained in:
Max 2024-07-07 17:41:54 +02:00
parent d07d0f8a9d
commit de656cc2e8
18 changed files with 208 additions and 2 deletions

View File

@ -24,6 +24,6 @@ ILogger SetupSerilog()
{
var logConfig = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} - {Caller}->{Assembly}] | {Level:u3}] {Message:lj}{NewLine}{Exception}");
.WriteTo.Console(outputTemplate: BasedSerilog.OutputTemplate);
return logConfig.CreateLogger();
}

View File

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

View File

@ -0,0 +1,18 @@
namespace DotBased.ASP.Auth;
public class BasedAuthConfiguration
{
/// <summary>
/// Allow users to registrate a user account.
/// </summary>
public bool AllowRegistration { get; set; }
//TODO: Callback when a user registers, so the application can handle sending emails or generate a code to complete the registration.
public string LoginPath { get; set; } = string.Empty;
public string LogoutPath { get; set; } = string.Empty;
/// <summary>
/// The max age before a AuthenticationState will expire (default: 7 days).
/// </summary>
public TimeSpan AuthenticationStateMaxAgeBeforeExpire { get; set; } = TimeSpan.FromDays(7);
//TODO: Data seeding
public Action<AuthService>? SeedData { get; set; }
}

View File

@ -0,0 +1,20 @@
using DotBased.Logging;
using Microsoft.AspNetCore.Components.Authorization;
namespace DotBased.ASP.Auth;
public class BasedAuthenticationStateProvider : AuthenticationStateProvider
{
public BasedAuthenticationStateProvider()
{
_logger = LogService.RegisterLogger(typeof(BasedAuthenticationStateProvider));
}
private ILogger _logger;
public override Task<AuthenticationState> GetAuthenticationStateAsync()
{
throw new NotImplementedException();
}
}

View File

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

View File

@ -0,0 +1,8 @@
namespace DotBased.ASP.Auth.Domains.Auth;
public class PermissionModel
{
public string Name { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public string Permission { get; set; } = string.Empty;
}

View File

@ -0,0 +1,9 @@
namespace DotBased.ASP.Auth.Domains.Auth;
public class RoleModel
{
public string Id { get; set; } = Guid.NewGuid().ToString();
public string Name { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public List<PermissionModel> Permissions { get; set; } = [];
}

View File

@ -0,0 +1,8 @@
namespace DotBased.ASP.Auth.Domains.Identity;
public class GroupItemModel
{
public string Id { get; set; } = Guid.NewGuid().ToString();
public string Name { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
}

View File

@ -0,0 +1,11 @@
using DotBased.ASP.Auth.Domains.Auth;
namespace DotBased.ASP.Auth.Domains.Identity;
public class GroupModel
{
public string Id { get; set; } = Guid.NewGuid().ToString();
public string Name { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public List<RoleModel> Roles { get; set; } = [];
}

View File

@ -0,0 +1,10 @@
namespace DotBased.ASP.Auth.Domains.Identity;
public class UserItemModel
{
public string Id { get; set; } = string.Empty;
public string UserName { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string Name { get; set; } = string.Empty;
public string FamilyName { get; set; } = string.Empty;
}

View File

@ -0,0 +1,14 @@
namespace DotBased.ASP.Auth.Domains.Identity;
public class UserModel
{
public string UserName { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string Name { get; set; } = string.Empty;
public string FamilyName { get; set; } = string.Empty;
public string Id { get; set; } = Guid.NewGuid().ToString();
public string PasswordHash { get; set; } = string.Empty;
public string[] GroupIds { get; set; } = Array.Empty<string>();
public string[] Roles { get; set; } = Array.Empty<string>();
}

View File

@ -0,0 +1,8 @@
namespace DotBased.ASP.Auth.Domains;
public class LoginModel
{
public string UserName { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
}

View File

@ -0,0 +1,10 @@
namespace DotBased.ASP.Auth.Domains;
public class RegisterModel
{
public string UserName { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
public string Name { get; set; } = string.Empty;
public string FamilyName { get; set; } = string.Empty;
}

View File

@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\DotBased\DotBased.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="8.0.6" />
</ItemGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,13 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.Extensions.DependencyInjection;
namespace DotBased.ASP.Auth;
public static class DotBasedASPAuth
{
public static void UseBasedAuth(this WebApplicationBuilder builder, BasedAuthConfiguration configuration)
{
builder.Services.AddScoped<AuthenticationStateProvider, BasedAuthenticationStateProvider>();
}
}

View File

@ -0,0 +1,38 @@
using DotBased.ASP.Auth.Domains.Auth;
using DotBased.ASP.Auth.Domains.Identity;
namespace DotBased.ASP.Auth;
public interface IAuthDataProvider
{
/*
* Identity
*/
// User
public Task<Result> CreateUserAsync(UserModel user);
public Task<Result> UpdateUserAsync(UserModel user);
public Task<Result> DeleteUserAsync(UserModel user);
public Task<Result<UserModel>> GetUserAsync(string id, string email, string username);
public Task<ListResult<UserItemModel>> GetUsersAsync(int start = 0, int amount = 30, string search = "");
// Group
public Task<Result> CreateGroupAsync(GroupModel group);
public Task<Result> UpdateGroupAsync(GroupModel group);
public Task<Result> DeleteGroupAsync(GroupModel group);
public Task<Result<GroupModel>> GetGroupAsync(string id);
public Task<ListResult<GroupItemModel>> GetGroupsAsync(int start = 0, int amount = 30, string search = "");
/*
* Auth
*/
// AuthenticationState
public Task<Result> CreateAuthenticationStateAsync(AuthenticationStateModel authenticationState);
public Task<Result> UpdateAuthenticationStateAsync(AuthenticationStateModel authenticationState);
public Task<Result> DeleteAuthenticationStateAsync(AuthenticationStateModel authenticationState);
public Task<Result<AuthenticationStateModel>> GetAuthenticationStateAsync(string id);
// Authorization
}

View File

@ -8,6 +8,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotBased.Logging.Serilog",
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Modules", "Modules", "{2156FB93-C252-4B33-8A0C-73C82FABB163}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotBased.ASP.Auth", "DotBased.ASP.Auth\DotBased.ASP.Auth.csproj", "{CBD4111D-F1CA-466A-AC73-9EAB7F235B3D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -26,8 +28,13 @@ Global
{EBBDAF9A-BFC7-4BDC-8C51-0501B59A1DDC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EBBDAF9A-BFC7-4BDC-8C51-0501B59A1DDC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EBBDAF9A-BFC7-4BDC-8C51-0501B59A1DDC}.Release|Any CPU.Build.0 = Release|Any CPU
{CBD4111D-F1CA-466A-AC73-9EAB7F235B3D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CBD4111D-F1CA-466A-AC73-9EAB7F235B3D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CBD4111D-F1CA-466A-AC73-9EAB7F235B3D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CBD4111D-F1CA-466A-AC73-9EAB7F235B3D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{EBBDAF9A-BFC7-4BDC-8C51-0501B59A1DDC} = {2156FB93-C252-4B33-8A0C-73C82FABB163}
{CBD4111D-F1CA-466A-AC73-9EAB7F235B3D} = {2156FB93-C252-4B33-8A0C-73C82FABB163}
EndGlobalSection
EndGlobal

View File

@ -8,7 +8,7 @@ namespace DotBased.Logging;
/// </summary>
public static class LogService
{
// TODO: Future: add middlewares and chanagable log processor
// TODO: Future: add middlewares and changeable log processor
static LogService()
{
Options = new LogOptions();