Add models and interfaces for auth

This commit is contained in:
Stone_Red
2023-01-25 11:22:28 +01:00
parent e9b485bb04
commit a1b6f61e8c
7 changed files with 113 additions and 6 deletions
@@ -0,0 +1,10 @@
using Elektrifikatsiya.Models;
using Microsoft.EntityFrameworkCore;
namespace Elektrifikatsiya.Database;
public class UserDatabaseContext : DbContext
{
public DbSet<User> Users { get; set; }
}
@@ -20,7 +20,13 @@
<PackageReference Include="Blazorise" Version="1.1.*" />
<PackageReference Include="Blazorise.Material" Version="1.1.*" />
<PackageReference Include="Blazorise.Icons.Material" Version="1.1.*" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.2" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.17.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="Middleware\" />
</ItemGroup>
</Project>
@@ -0,0 +1,7 @@
namespace Elektrifikatsiya.Models;
public enum Role
{
Admin,
User
}
@@ -0,0 +1,22 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Elektrifikatsiya.Models;
public class User
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public int Id { get; private set; }
public string Name { get; private set; }
public string PasswordHash { get; private set; }
public string? SessionToken { get; set; }
public DateTime LastLoginDate { get; set; }
public User(string name, string passwordHash)
{
Name = name;
PasswordHash = passwordHash;
}
}
@@ -25,12 +25,8 @@ app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
_ = endpoints.MapBlazorHub();
_ = endpoints.MapFallbackToPage("/_Host");
});
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");
app.Run();
@@ -0,0 +1,53 @@
using Elektrifikatsiya.Models;
namespace Elektrifikatsiya.Services;
public interface IAuthenticationService
{
/// <summary>
/// Gets the current user.
/// </summary>
/// <returns> <see cref="Task"/> to <see langword="await"/> and the currently authenticated user.</returns>
Task<User?> GetUserAsync();
/// <summary>
/// Registers a new user.
/// </summary>
/// <param name="email">The E-mail address of the user.</param>
/// <returns>A <see cref="Task"/> to <see langword="await"/>.</returns>
Task RegisterUserAsync(string name, string password);
/// <summary>
/// Logs a user in.
/// </summary>
/// <param name="name">The E-mail address of the user.</param>
/// <returns>A <see cref="Task"/> to <see langword="await"/>.</returns>
Task<(bool Success, string? Message)> LoginUserAsync(string name, string password);
/// <summary>
/// Logs the user out.
/// </summary>
/// <returns>A <see cref="Task"/> to <see langword="await"/>.</returns>
Task LogoutUserAsync();
/// <summary>
/// Deletes the current user.
/// </summary>
/// <param name="deletionToken">The deletion token.</param>
/// <returns>A <see cref="Task"/> to <see langword="await"/> and a <see cref="bool"/> that indicates if the operation was successful.</returns>
Task<bool> DeleteUserAsync(string deletionToken);
/// <summary>
/// Checks if a user exists.
/// </summary>
/// <param name="name">The E-mail address of the user.</param>
/// <returns>A <see cref="Task"/> to <see langword="await"/> and a <see cref="bool"/> that indicates if the user exists.</returns>
Task<bool> UserExistsAsync(string name);
/// <summary>
/// Check if the current user is authenticated.
/// </summary>
/// <returns>A <see cref="Task"/> to <see langword="await"/> and a <see cref="bool"/> that indicates if the user is authenticated.</returns>
Task<bool> IsAuthenticated();
}
@@ -0,0 +1,13 @@
using Elektrifikatsiya.Models;
namespace Elektrifikatsiya.Services;
public interface IAuthorizationService
{
/// <summary>
/// Check if the current user is authorized.
/// </summary>
/// <param name="requiredRole">The minimum required role.</param>
/// <returns>A <see cref="Task"/> to <see langword="await"/> and a <see cref="bool"/> that indicates if the user is authorized.</returns>
Task<bool> IsAuthorized(Role requiredRole);
}