From de656cc2e861b3a95df2a00ac90bcf0ba03e18c6 Mon Sep 17 00:00:00 2001 From: Max Date: Sun, 7 Jul 2024 17:41:54 +0200 Subject: [PATCH] Implementing ASP auth module --- CLI/Program.cs | 2 +- DotBased.ASP.Auth/AuthService.cs | 6 +++ DotBased.ASP.Auth/BasedAuthConfiguration.cs | 18 +++++++++ .../BasedAuthenticationStateProvider.cs | 20 ++++++++++ .../Domains/Auth/AuthenticationStateModel.cs | 6 +++ .../Domains/Auth/PermissionModel.cs | 8 ++++ DotBased.ASP.Auth/Domains/Auth/RoleModel.cs | 9 +++++ .../Domains/Identity/GroupItemModel.cs | 8 ++++ .../Domains/Identity/GroupModel.cs | 11 ++++++ .../Domains/Identity/UserItemModel.cs | 10 +++++ .../Domains/Identity/UserModel.cs | 14 +++++++ DotBased.ASP.Auth/Domains/LoginModel.cs | 8 ++++ DotBased.ASP.Auth/Domains/RegisterModel.cs | 10 +++++ DotBased.ASP.Auth/DotBased.ASP.Auth.csproj | 20 ++++++++++ DotBased.ASP.Auth/DotBasedASPAuth.cs | 13 +++++++ DotBased.ASP.Auth/IAuthDataProvider.cs | 38 +++++++++++++++++++ DotBased.sln | 7 ++++ DotBased/Logging/LogService.cs | 2 +- 18 files changed, 208 insertions(+), 2 deletions(-) create mode 100644 DotBased.ASP.Auth/AuthService.cs create mode 100644 DotBased.ASP.Auth/BasedAuthConfiguration.cs create mode 100644 DotBased.ASP.Auth/BasedAuthenticationStateProvider.cs create mode 100644 DotBased.ASP.Auth/Domains/Auth/AuthenticationStateModel.cs create mode 100644 DotBased.ASP.Auth/Domains/Auth/PermissionModel.cs create mode 100644 DotBased.ASP.Auth/Domains/Auth/RoleModel.cs create mode 100644 DotBased.ASP.Auth/Domains/Identity/GroupItemModel.cs create mode 100644 DotBased.ASP.Auth/Domains/Identity/GroupModel.cs create mode 100644 DotBased.ASP.Auth/Domains/Identity/UserItemModel.cs create mode 100644 DotBased.ASP.Auth/Domains/Identity/UserModel.cs create mode 100644 DotBased.ASP.Auth/Domains/LoginModel.cs create mode 100644 DotBased.ASP.Auth/Domains/RegisterModel.cs create mode 100644 DotBased.ASP.Auth/DotBased.ASP.Auth.csproj create mode 100644 DotBased.ASP.Auth/DotBasedASPAuth.cs create mode 100644 DotBased.ASP.Auth/IAuthDataProvider.cs diff --git a/CLI/Program.cs b/CLI/Program.cs index e874b32..4f90baf 100755 --- a/CLI/Program.cs +++ b/CLI/Program.cs @@ -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(); } \ No newline at end of file diff --git a/DotBased.ASP.Auth/AuthService.cs b/DotBased.ASP.Auth/AuthService.cs new file mode 100644 index 0000000..7b973d1 --- /dev/null +++ b/DotBased.ASP.Auth/AuthService.cs @@ -0,0 +1,6 @@ +namespace DotBased.ASP.Auth; + +public class AuthService +{ + +} \ No newline at end of file diff --git a/DotBased.ASP.Auth/BasedAuthConfiguration.cs b/DotBased.ASP.Auth/BasedAuthConfiguration.cs new file mode 100644 index 0000000..a996d1e --- /dev/null +++ b/DotBased.ASP.Auth/BasedAuthConfiguration.cs @@ -0,0 +1,18 @@ +namespace DotBased.ASP.Auth; + +public class BasedAuthConfiguration +{ + /// + /// Allow users to registrate a user account. + /// + 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; + /// + /// The max age before a AuthenticationState will expire (default: 7 days). + /// + public TimeSpan AuthenticationStateMaxAgeBeforeExpire { get; set; } = TimeSpan.FromDays(7); + //TODO: Data seeding + public Action? SeedData { get; set; } +} \ No newline at end of file diff --git a/DotBased.ASP.Auth/BasedAuthenticationStateProvider.cs b/DotBased.ASP.Auth/BasedAuthenticationStateProvider.cs new file mode 100644 index 0000000..2491651 --- /dev/null +++ b/DotBased.ASP.Auth/BasedAuthenticationStateProvider.cs @@ -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 GetAuthenticationStateAsync() + { + + throw new NotImplementedException(); + } +} \ No newline at end of file diff --git a/DotBased.ASP.Auth/Domains/Auth/AuthenticationStateModel.cs b/DotBased.ASP.Auth/Domains/Auth/AuthenticationStateModel.cs new file mode 100644 index 0000000..ab4fc1f --- /dev/null +++ b/DotBased.ASP.Auth/Domains/Auth/AuthenticationStateModel.cs @@ -0,0 +1,6 @@ +namespace DotBased.ASP.Auth.Domains.Auth; + +public class AuthenticationStateModel +{ + +} \ No newline at end of file diff --git a/DotBased.ASP.Auth/Domains/Auth/PermissionModel.cs b/DotBased.ASP.Auth/Domains/Auth/PermissionModel.cs new file mode 100644 index 0000000..93353fe --- /dev/null +++ b/DotBased.ASP.Auth/Domains/Auth/PermissionModel.cs @@ -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; +} \ No newline at end of file diff --git a/DotBased.ASP.Auth/Domains/Auth/RoleModel.cs b/DotBased.ASP.Auth/Domains/Auth/RoleModel.cs new file mode 100644 index 0000000..7822404 --- /dev/null +++ b/DotBased.ASP.Auth/Domains/Auth/RoleModel.cs @@ -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 Permissions { get; set; } = []; +} \ No newline at end of file diff --git a/DotBased.ASP.Auth/Domains/Identity/GroupItemModel.cs b/DotBased.ASP.Auth/Domains/Identity/GroupItemModel.cs new file mode 100644 index 0000000..30508d8 --- /dev/null +++ b/DotBased.ASP.Auth/Domains/Identity/GroupItemModel.cs @@ -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; +} \ No newline at end of file diff --git a/DotBased.ASP.Auth/Domains/Identity/GroupModel.cs b/DotBased.ASP.Auth/Domains/Identity/GroupModel.cs new file mode 100644 index 0000000..065f937 --- /dev/null +++ b/DotBased.ASP.Auth/Domains/Identity/GroupModel.cs @@ -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 Roles { get; set; } = []; +} \ No newline at end of file diff --git a/DotBased.ASP.Auth/Domains/Identity/UserItemModel.cs b/DotBased.ASP.Auth/Domains/Identity/UserItemModel.cs new file mode 100644 index 0000000..1d07566 --- /dev/null +++ b/DotBased.ASP.Auth/Domains/Identity/UserItemModel.cs @@ -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; +} \ No newline at end of file diff --git a/DotBased.ASP.Auth/Domains/Identity/UserModel.cs b/DotBased.ASP.Auth/Domains/Identity/UserModel.cs new file mode 100644 index 0000000..ea92d97 --- /dev/null +++ b/DotBased.ASP.Auth/Domains/Identity/UserModel.cs @@ -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(); + public string[] Roles { get; set; } = Array.Empty(); +} \ No newline at end of file diff --git a/DotBased.ASP.Auth/Domains/LoginModel.cs b/DotBased.ASP.Auth/Domains/LoginModel.cs new file mode 100644 index 0000000..a011eb6 --- /dev/null +++ b/DotBased.ASP.Auth/Domains/LoginModel.cs @@ -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; +} \ No newline at end of file diff --git a/DotBased.ASP.Auth/Domains/RegisterModel.cs b/DotBased.ASP.Auth/Domains/RegisterModel.cs new file mode 100644 index 0000000..877476c --- /dev/null +++ b/DotBased.ASP.Auth/Domains/RegisterModel.cs @@ -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; +} \ No newline at end of file diff --git a/DotBased.ASP.Auth/DotBased.ASP.Auth.csproj b/DotBased.ASP.Auth/DotBased.ASP.Auth.csproj new file mode 100644 index 0000000..ba40443 --- /dev/null +++ b/DotBased.ASP.Auth/DotBased.ASP.Auth.csproj @@ -0,0 +1,20 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + + + diff --git a/DotBased.ASP.Auth/DotBasedASPAuth.cs b/DotBased.ASP.Auth/DotBasedASPAuth.cs new file mode 100644 index 0000000..49fd0e2 --- /dev/null +++ b/DotBased.ASP.Auth/DotBasedASPAuth.cs @@ -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(); + } +} \ No newline at end of file diff --git a/DotBased.ASP.Auth/IAuthDataProvider.cs b/DotBased.ASP.Auth/IAuthDataProvider.cs new file mode 100644 index 0000000..508d6c4 --- /dev/null +++ b/DotBased.ASP.Auth/IAuthDataProvider.cs @@ -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 CreateUserAsync(UserModel user); + public Task UpdateUserAsync(UserModel user); + public Task DeleteUserAsync(UserModel user); + public Task> GetUserAsync(string id, string email, string username); + public Task> GetUsersAsync(int start = 0, int amount = 30, string search = ""); + + // Group + public Task CreateGroupAsync(GroupModel group); + public Task UpdateGroupAsync(GroupModel group); + public Task DeleteGroupAsync(GroupModel group); + public Task> GetGroupAsync(string id); + public Task> GetGroupsAsync(int start = 0, int amount = 30, string search = ""); + + /* + * Auth + */ + + // AuthenticationState + public Task CreateAuthenticationStateAsync(AuthenticationStateModel authenticationState); + 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.sln b/DotBased.sln index 9e1a483..7a653af 100755 --- a/DotBased.sln +++ b/DotBased.sln @@ -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 diff --git a/DotBased/Logging/LogService.cs b/DotBased/Logging/LogService.cs index bc5666f..8584f30 100755 --- a/DotBased/Logging/LogService.cs +++ b/DotBased/Logging/LogService.cs @@ -8,7 +8,7 @@ namespace DotBased.Logging; /// 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();