Password encryption, Claim groups

This commit is contained in:
Andre Beging
2022-04-11 13:05:15 +02:00
parent c553047369
commit 208ea99a42
12 changed files with 273 additions and 125 deletions

View File

@@ -1,14 +1,12 @@
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using FoodsharingSiegen.Contracts;
using FoodsharingSiegen.Contracts.Entity;
using FoodsharingSiegen.Contracts.Helper;
using FoodsharingSiegen.Server.Data;
using FoodsharingSiegen.Server.Data.Service;
using FoodsharingSiegen.Server.Service;
using FoodsharingSiegen.Shared.Helper;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
namespace FoodsharingSiegen.Server.Auth
{
@@ -23,7 +21,7 @@ namespace FoodsharingSiegen.Server.Auth
/// <summary>
/// Gets or sets the value of the user (ab)
/// </summary>
public User? User { get; set; }
public User? User => _user;
#endregion
@@ -39,6 +37,11 @@ namespace FoodsharingSiegen.Server.Auth
/// </summary>
private readonly LocalStorageService _localStorageService;
/// <summary>
/// The user
/// </summary>
private User? _user;
#endregion
#region Setup/Teardown
@@ -49,7 +52,10 @@ namespace FoodsharingSiegen.Server.Auth
/// <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)
public AuthService(
FsContext context,
LocalStorageService localStorageService,
AuthenticationStateProvider authenticationStateProvider) : base(context)
{
_localStorageService = localStorageService;
_authenticationStateProvider = authenticationStateProvider;
@@ -57,6 +63,22 @@ namespace FoodsharingSiegen.Server.Auth
#endregion
#region Public Method Initialize
/// <summary>
/// Initializes this instance (a. beging, 11.04.2022)
/// </summary>
public async Task Initialize()
{
if (_user != null) return;
var token = await _localStorageService.GetItem<string>(StorageKeys.TokenKey);
if (AuthHelper.ValidateToken(token, out var user) && user != null)
_user = user;
}
#endregion
#region Public Method Login
/// <summary>
@@ -87,13 +109,13 @@ namespace FoodsharingSiegen.Server.Auth
#endregion Ensure Admin
var encryptedPassword = AuthHelper.Encrypt(password);
var encryptedPassword = Cryptor.Encrypt(password);
User = await Context.Users.FirstOrDefaultAsync(x => x.Mail.ToLower() == mailAddress.ToLower() && x.EncryptedPassword == encryptedPassword);
_user = await Context.Users.FirstOrDefaultAsync(x => x.Mail.ToLower() == mailAddress.ToLower() && x.EncryptedPassword == encryptedPassword);
if (User != null)
if (_user != null)
{
var serializedToken = AuthHelper.CreateToken(User.Id);
var serializedToken = AuthHelper.CreateToken(_user);
await _localStorageService.SetItem(StorageKeys.TokenKey, serializedToken);
return new OperationResult();
@@ -115,7 +137,7 @@ namespace FoodsharingSiegen.Server.Auth
try
{
await _localStorageService.RemoveItem(StorageKeys.TokenKey);
User = null;
_user = null;
((TokenAuthStateProvider) _authenticationStateProvider).MarkUserAsLoggedOut();
return new OperationResult();
}