mirror of
https://github.com/hmaxnl/DotBased.git
synced 2025-01-18 18:14:20 +01:00
Implementing ASP auth module
This commit is contained in:
parent
d07d0f8a9d
commit
de656cc2e8
|
@ -24,6 +24,6 @@ ILogger SetupSerilog()
|
||||||
{
|
{
|
||||||
var logConfig = new LoggerConfiguration()
|
var logConfig = new LoggerConfiguration()
|
||||||
.MinimumLevel.Verbose()
|
.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();
|
return logConfig.CreateLogger();
|
||||||
}
|
}
|
6
DotBased.ASP.Auth/AuthService.cs
Normal file
6
DotBased.ASP.Auth/AuthService.cs
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
namespace DotBased.ASP.Auth;
|
||||||
|
|
||||||
|
public class AuthService
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
18
DotBased.ASP.Auth/BasedAuthConfiguration.cs
Normal file
18
DotBased.ASP.Auth/BasedAuthConfiguration.cs
Normal 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; }
|
||||||
|
}
|
20
DotBased.ASP.Auth/BasedAuthenticationStateProvider.cs
Normal file
20
DotBased.ASP.Auth/BasedAuthenticationStateProvider.cs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
namespace DotBased.ASP.Auth.Domains.Auth;
|
||||||
|
|
||||||
|
public class AuthenticationStateModel
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
8
DotBased.ASP.Auth/Domains/Auth/PermissionModel.cs
Normal file
8
DotBased.ASP.Auth/Domains/Auth/PermissionModel.cs
Normal 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;
|
||||||
|
}
|
9
DotBased.ASP.Auth/Domains/Auth/RoleModel.cs
Normal file
9
DotBased.ASP.Auth/Domains/Auth/RoleModel.cs
Normal 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; } = [];
|
||||||
|
}
|
8
DotBased.ASP.Auth/Domains/Identity/GroupItemModel.cs
Normal file
8
DotBased.ASP.Auth/Domains/Identity/GroupItemModel.cs
Normal 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;
|
||||||
|
}
|
11
DotBased.ASP.Auth/Domains/Identity/GroupModel.cs
Normal file
11
DotBased.ASP.Auth/Domains/Identity/GroupModel.cs
Normal 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; } = [];
|
||||||
|
}
|
10
DotBased.ASP.Auth/Domains/Identity/UserItemModel.cs
Normal file
10
DotBased.ASP.Auth/Domains/Identity/UserItemModel.cs
Normal 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;
|
||||||
|
}
|
14
DotBased.ASP.Auth/Domains/Identity/UserModel.cs
Normal file
14
DotBased.ASP.Auth/Domains/Identity/UserModel.cs
Normal 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>();
|
||||||
|
}
|
8
DotBased.ASP.Auth/Domains/LoginModel.cs
Normal file
8
DotBased.ASP.Auth/Domains/LoginModel.cs
Normal 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;
|
||||||
|
}
|
10
DotBased.ASP.Auth/Domains/RegisterModel.cs
Normal file
10
DotBased.ASP.Auth/Domains/RegisterModel.cs
Normal 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;
|
||||||
|
}
|
20
DotBased.ASP.Auth/DotBased.ASP.Auth.csproj
Normal file
20
DotBased.ASP.Auth/DotBased.ASP.Auth.csproj
Normal 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>
|
13
DotBased.ASP.Auth/DotBasedASPAuth.cs
Normal file
13
DotBased.ASP.Auth/DotBasedASPAuth.cs
Normal 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>();
|
||||||
|
}
|
||||||
|
}
|
38
DotBased.ASP.Auth/IAuthDataProvider.cs
Normal file
38
DotBased.ASP.Auth/IAuthDataProvider.cs
Normal 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
|
||||||
|
|
||||||
|
}
|
|
@ -8,6 +8,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotBased.Logging.Serilog",
|
||||||
EndProject
|
EndProject
|
||||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Modules", "Modules", "{2156FB93-C252-4B33-8A0C-73C82FABB163}"
|
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Modules", "Modules", "{2156FB93-C252-4B33-8A0C-73C82FABB163}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotBased.ASP.Auth", "DotBased.ASP.Auth\DotBased.ASP.Auth.csproj", "{CBD4111D-F1CA-466A-AC73-9EAB7F235B3D}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
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}.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.ActiveCfg = Release|Any CPU
|
||||||
{EBBDAF9A-BFC7-4BDC-8C51-0501B59A1DDC}.Release|Any CPU.Build.0 = 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
|
EndGlobalSection
|
||||||
GlobalSection(NestedProjects) = preSolution
|
GlobalSection(NestedProjects) = preSolution
|
||||||
{EBBDAF9A-BFC7-4BDC-8C51-0501B59A1DDC} = {2156FB93-C252-4B33-8A0C-73C82FABB163}
|
{EBBDAF9A-BFC7-4BDC-8C51-0501B59A1DDC} = {2156FB93-C252-4B33-8A0C-73C82FABB163}
|
||||||
|
{CBD4111D-F1CA-466A-AC73-9EAB7F235B3D} = {2156FB93-C252-4B33-8A0C-73C82FABB163}
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
EndGlobal
|
EndGlobal
|
||||||
|
|
|
@ -8,7 +8,7 @@ namespace DotBased.Logging;
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class LogService
|
public static class LogService
|
||||||
{
|
{
|
||||||
// TODO: Future: add middlewares and chanagable log processor
|
// TODO: Future: add middlewares and changeable log processor
|
||||||
static LogService()
|
static LogService()
|
||||||
{
|
{
|
||||||
Options = new LogOptions();
|
Options = new LogOptions();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user