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

@@ -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
}