147 lines
5.0 KiB
C#
147 lines
5.0 KiB
C#
using System.IdentityModel.Tokens.Jwt;
|
|
using System.Security.Claims;
|
|
using FoodsharingSiegen.Contracts;
|
|
using FoodsharingSiegen.Contracts.Entity;
|
|
using FoodsharingSiegen.Server.Data;
|
|
using FoodsharingSiegen.Server.Data.Service;
|
|
using FoodsharingSiegen.Server.Service;
|
|
using Microsoft.AspNetCore.Components.Authorization;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
namespace FoodsharingSiegen.Server.Auth
|
|
{
|
|
/// <summary>
|
|
/// The auth service class (a. beging, 04.04.2022)
|
|
/// </summary>
|
|
/// <seealso cref="ServiceBase"/>
|
|
public class AuthService : ServiceBase
|
|
{
|
|
#region Public Properties
|
|
|
|
/// <summary>
|
|
/// Gets or sets the value of the user (ab)
|
|
/// </summary>
|
|
public User? User { get; set; }
|
|
|
|
#endregion
|
|
|
|
#region Private Fields
|
|
|
|
/// <summary>
|
|
/// The authentication state provider
|
|
/// </summary>
|
|
private readonly AuthenticationStateProvider _authenticationStateProvider;
|
|
|
|
/// <summary>
|
|
/// The local storage service
|
|
/// </summary>
|
|
private readonly LocalStorageService _localStorageService;
|
|
|
|
#endregion
|
|
|
|
#region Setup/Teardown
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="AuthService"/> class
|
|
/// </summary>
|
|
/// <param name="context">The context</param>
|
|
/// <param name="localStorageService">The local storage service</param>
|
|
/// <param name="authenticationStateProvider">The authentication state provider</param>
|
|
public AuthService(FsContext context, LocalStorageService localStorageService, AuthenticationStateProvider authenticationStateProvider) : base(context)
|
|
{
|
|
_localStorageService = localStorageService;
|
|
_authenticationStateProvider = authenticationStateProvider;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Public Method Login
|
|
|
|
/// <summary>
|
|
/// Logins the mail address (a. beging, 04.04.2022)
|
|
/// </summary>
|
|
/// <param name="mailAddress">The mail address</param>
|
|
/// <param name="password">The password</param>
|
|
/// <returns>A task containing the operation result</returns>
|
|
public async Task<OperationResult> Login(string mailAddress, string password)
|
|
{
|
|
#region Ensure Admin
|
|
|
|
var existingTroogS = await Context.Users.AnyAsync(x => x.Mail == "fs@beging.de");
|
|
if (!existingTroogS)
|
|
{
|
|
var troogs = new User
|
|
{
|
|
Name = "Andre",
|
|
Mail = "fs@beging.de",
|
|
Type = UserType.Admin,
|
|
Created = DateTime.UtcNow,
|
|
EncryptedPassword = "qSIxTZo7J8M="
|
|
};
|
|
|
|
await Context.Users.AddAsync(troogs);
|
|
await Context.SaveChangesAsync();
|
|
}
|
|
|
|
#endregion Ensure Admin
|
|
|
|
var encryptedPassword = AuthHelper.Encrypt(password);
|
|
|
|
User = await Context.Users.FirstOrDefaultAsync(x => x.Mail.ToLower() == mailAddress.ToLower() && x.EncryptedPassword == encryptedPassword);
|
|
|
|
if (User != null)
|
|
{
|
|
|
|
|
|
// Daten korrekt
|
|
var tokenHandler = new JwtSecurityTokenHandler();
|
|
var tokenDescriptor = new SecurityTokenDescriptor
|
|
{
|
|
Subject = new ClaimsIdentity(new[]
|
|
{
|
|
new Claim(ClaimTypes.NameIdentifier, User.Id.ToString()),
|
|
}),
|
|
Expires = DateTime.UtcNow.AddDays(30),
|
|
Issuer = "FS-Siegen",
|
|
Audience = "FS-Siegen",
|
|
SigningCredentials = new SigningCredentials(AuthHelper.GetSigningKey(), SecurityAlgorithms.HmacSha256Signature)
|
|
};
|
|
|
|
var token = tokenHandler.CreateToken(tokenDescriptor);
|
|
var serializedToken = tokenHandler.WriteToken(token);
|
|
|
|
await _localStorageService.SetItem(StorageKeys.TokenKey, serializedToken);
|
|
|
|
return new OperationResult();
|
|
}
|
|
|
|
return new OperationResult(new Exception("Invalid"));
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Public Method Logout
|
|
|
|
/// <summary>
|
|
/// Logouts this instance (a. beging, 04.04.2022)
|
|
/// </summary>
|
|
/// <returns>A task containing the operation result</returns>
|
|
public async Task<OperationResult> Logout()
|
|
{
|
|
try
|
|
{
|
|
await _localStorageService.RemoveItem(StorageKeys.TokenKey);
|
|
User = null;
|
|
((TokenAuthStateProvider) _authenticationStateProvider).MarkUserAsLoggedOut();
|
|
return new OperationResult();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return new OperationResult(e);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |