Merge pull request #28 from Stefan-5422/feature-login_service

Feature login service
This commit is contained in:
Stone_Red
2023-02-22 09:09:25 +01:00
committed by GitHub
5 changed files with 91 additions and 66 deletions
@@ -1,4 +1,5 @@
<Blazorise.ThemeProvider Theme="@theme"> @using Blazorise.Components
<Blazorise.ThemeProvider Theme="@theme">
<Router AppAssembly="typeof(App).Assembly"> <Router AppAssembly="typeof(App).Assembly">
<Found Context="routeData"> <Found Context="routeData">
<RouteView RouteData="routeData" DefaultLayout="typeof(Elektrifikatsiya.Layouts.MainLayout)" /> <RouteView RouteData="routeData" DefaultLayout="typeof(Elektrifikatsiya.Layouts.MainLayout)" />
@@ -17,6 +17,13 @@
<DockerComposeProjectPath>..\docker-compose.dcproj</DockerComposeProjectPath> <DockerComposeProjectPath>..\docker-compose.dcproj</DockerComposeProjectPath>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<Compile Remove="Model\**" />
<Content Remove="Model\**" />
<EmbeddedResource Remove="Model\**" />
<None Remove="Model\**" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Blazorise" Version="1.2.0" /> <PackageReference Include="Blazorise" Version="1.2.0" />
<PackageReference Include="Blazorise.Charts" Version="1.2.0" /> <PackageReference Include="Blazorise.Charts" Version="1.2.0" />
@@ -32,7 +39,6 @@
<ItemGroup> <ItemGroup>
<Folder Include="Middleware\" /> <Folder Include="Middleware\" />
<Folder Include="Model\" />
</ItemGroup> </ItemGroup>
</Project> </Project>
@@ -7,10 +7,13 @@ public class Device
{ {
[Key] [Key]
public string Key { get; private set; } public string Key { get; private set; }
[Required] [Required]
public string Name { get; private set; } = string.Empty; public string Name { get; private set; }
[Required] [Required]
public IPAddress Address { get; private set; } public IPAddress Address { get; private set; }
public int PowerUsage { get; set; } public int PowerUsage { get; set; }
public string User { get; set; } public string User { get; set; }
public string Room { get; set; } public string Room { get; set; }
@@ -15,7 +15,9 @@ public interface IAuthenticationService
/// <summary> /// <summary>
/// Registers a new user. /// Registers a new user.
/// </summary> /// </summary>
/// <param name="email">The E-mail address of the user.</param> /// <param name="name">The name address of the user.</param>
/// <param name="password">The password of the user.</param>
/// <param name="role">The default role of the user.</param>
/// <returns>A <see cref="Task"/> to <see langword="await"/>.</returns> /// <returns>A <see cref="Task"/> to <see langword="await"/>.</returns>
Task<Result> RegisterUserAsync(string name, string password, Role role); Task<Result> RegisterUserAsync(string name, string password, Role role);
@@ -23,7 +25,8 @@ public interface IAuthenticationService
/// <summary> /// <summary>
/// Logs a user in. /// Logs a user in.
/// </summary> /// </summary>
/// <param name="name">The E-mail address of the user.</param> /// <param name="name">The name of the user.</param>
/// <param name="password">The password of the user.</param>
/// <returns>A <see cref="Task"/> to <see langword="await"/>.</returns> /// <returns>A <see cref="Task"/> to <see langword="await"/>.</returns>
Task<Result> LoginUserAsync(string name, string password); Task<Result> LoginUserAsync(string name, string password);
@@ -36,14 +39,13 @@ public interface IAuthenticationService
/// <summary> /// <summary>
/// Deletes the current user. /// Deletes the current user.
/// </summary> /// </summary>
/// <param name="deletionToken">The deletion token.</param> /// <returns>A <see cref="Task"/> to <see langword="await"/>.</returns>
/// <returns>A <see cref="Task"/> to <see langword="await"/> and a <see cref="bool"/> that indicates if the operation was successful.</returns>
Task<Result> DeleteUserAsync(); Task<Result> DeleteUserAsync();
/// <summary> /// <summary>
/// Checks if a user exists. /// Checks if a user exists.
/// </summary> /// </summary>
/// <param name="name">The E-mail address of the user.</param> /// <param name="name">The name 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> /// <returns>A <see cref="Task"/> to <see langword="await"/> and a <see cref="bool"/> that indicates if the user exists.</returns>
Task<Result<bool>> UserExistsAsync(string name); Task<Result<bool>> UserExistsAsync(string name);
@@ -2,11 +2,24 @@
public static class TokenGenerator public static class TokenGenerator
{ {
/// <summary>
/// Creates secure token and uses an UID to make it unique.
/// </summary>
/// <param name="tokenType">The type of the token.</param>
/// <param name="uid">The UID of a user.</param>
/// <param name="length">The length of the token.</param>
/// <returns></returns>
public static string GenerateToken(string tokenType, int uid = 0, int length = 128) public static string GenerateToken(string tokenType, int uid = 0, int length = 128)
{ {
return tokenType + "-" + SecureStringGenerator.CreateCryptographicRandomString(length, uid); return tokenType + "-" + SecureStringGenerator.CreateCryptographicRandomString(length, uid);
} }
/// <summary>
/// Checks if a token is valid.
/// </summary>
/// <param name="token">The token to validate.</param>
/// <param name="tokenType">The type the token should have.</param>
/// <returns></returns>
public static bool ValidateToken(string? token, string tokenType) public static bool ValidateToken(string? token, string tokenType)
{ {
if (token is null) if (token is null)