Implement auth services

This commit is contained in:
Stone_Red
2023-01-26 16:43:37 +01:00
parent a1b6f61e8c
commit 85cc9d56ba
6 changed files with 89 additions and 9 deletions
@@ -1,5 +1,7 @@
namespace Elektrifikatsiya.Models;
// Higher up = Higher permission
// Or you can set the priority manually <Role> = <value>
public enum Role
{
Admin,
@@ -13,10 +13,12 @@ public class User
public string PasswordHash { get; private set; }
public string? SessionToken { get; set; }
public DateTime LastLoginDate { get; set; }
public Role Role { get; set; }
public User(string name, string passwordHash)
public User(string name, string passwordHash, Role role)
{
Name = name;
PasswordHash = passwordHash;
Role = role;
}
}
@@ -1,5 +1,7 @@
using Elektrifikatsiya.Models;
using FluentResults;
namespace Elektrifikatsiya.Services;
public interface IAuthenticationService
@@ -8,7 +10,7 @@ public interface IAuthenticationService
/// Gets the current user.
/// </summary>
/// <returns> <see cref="Task"/> to <see langword="await"/> and the currently authenticated user.</returns>
Task<User?> GetUserAsync();
Task<Result<User>> GetUserAsync();
/// <summary>
/// Registers a new user.
@@ -16,38 +18,38 @@ public interface IAuthenticationService
/// <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);
Task<Result> 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);
Task<Result> LoginUserAsync(string name, string password);
/// <summary>
/// Logs the user out.
/// </summary>
/// <returns>A <see cref="Task"/> to <see langword="await"/>.</returns>
Task LogoutUserAsync();
Task<Result> 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);
Task<Result> 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);
Task<Result<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();
Task<Result<bool>> IsAuthenticated();
}
@@ -1,5 +1,7 @@
using Elektrifikatsiya.Models;
using FluentResults;
namespace Elektrifikatsiya.Services;
public interface IAuthorizationService
@@ -9,5 +11,5 @@ public interface IAuthorizationService
/// </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);
Task<Result<bool>> IsAuthorized(Role requiredRole);
}
@@ -0,0 +1,43 @@
using Elektrifikatsiya.Models;
using FluentResults;
namespace Elektrifikatsiya.Services.Implementations;
public class AuthenticationService : IAuthenticationService
{
public Task<Result> DeleteUserAsync(string deletionToken)
{
throw new NotImplementedException();
}
public Task<Result<User>> GetUserAsync()
{
throw new NotImplementedException();
}
public Task<Result<bool>> IsAuthenticated()
{
throw new NotImplementedException();
}
public Task<Result> LoginUserAsync(string name, string password)
{
throw new NotImplementedException();
}
public Task<Result> LogoutUserAsync()
{
throw new NotImplementedException();
}
public Task<Result> RegisterUserAsync(string name, string password)
{
throw new NotImplementedException();
}
public Task<Result<bool>> UserExistsAsync(string name)
{
throw new NotImplementedException();
}
}
@@ -0,0 +1,29 @@
using Elektrifikatsiya.Models;
using FluentResults;
namespace Elektrifikatsiya.Services.Implementations;
public class AuthorizationService : IAuthorizationService
{
private readonly IAuthenticationService authenticationService;
public AuthorizationService(IAuthenticationService authenticationService)
{
this.authenticationService = authenticationService;
}
public async Task<Result<bool>> IsAuthorized(Role requiredRole)
{
Result<User> userResult = await authenticationService.GetUserAsync();
if (userResult.IsSuccess && userResult.Value.Role <= requiredRole)
{
return Result.Ok(true);
}
else
{
return Result.Fail("Use doesn't have the required role!");
}
}
}