Claim Logic

This commit is contained in:
Andre Beging
2022-04-11 15:51:11 +02:00
parent 1b2c6c4062
commit 5026196b46
9 changed files with 137 additions and 47 deletions

View File

@@ -98,6 +98,7 @@ namespace FoodsharingSiegen.Server.Auth
{
Name = "Andre",
Mail = "fs@beging.de",
GroupsList = new List<UserGroup> { UserGroup.Ambassador },
Type = UserType.Admin,
Created = DateTime.UtcNow,
EncryptedPassword = "qSIxTZo7J8M="
@@ -117,7 +118,15 @@ namespace FoodsharingSiegen.Server.Auth
{
var serializedToken = AuthHelper.CreateToken(_user);
await _localStorageService.SetItem(StorageKeys.TokenKey, serializedToken);
if (_user.ForceLogout)
{
_user.ForceLogout = false;
await Context.SaveChangesAsync();
}
Context.Entry(_user).State = EntityState.Detached;
return new OperationResult();
}

View File

@@ -1,6 +1,7 @@
using System.Security.Claims;
using FoodsharingSiegen.Contracts;
using FoodsharingSiegen.Server.Auth;
using FoodsharingSiegen.Server.Data.Service;
using FoodsharingSiegen.Shared.Helper;
using Microsoft.AspNetCore.Components.Authorization;
@@ -17,6 +18,8 @@ namespace FoodsharingSiegen.Server.Service
/// <summary> LocalStorageService </summary>
private readonly LocalStorageService _localStorageService;
private readonly UserService _userService;
#endregion
#region Setup/Teardown
@@ -25,7 +28,12 @@ namespace FoodsharingSiegen.Server.Service
/// Constructor
/// </summary>
/// <param name="localStorageService"></param>
public TokenAuthStateProvider(LocalStorageService localStorageService) => _localStorageService = localStorageService;
/// <param name="userService"></param>
public TokenAuthStateProvider(LocalStorageService localStorageService, UserService userService)
{
_localStorageService = localStorageService;
_userService = userService;
}
#endregion
@@ -38,8 +46,12 @@ namespace FoodsharingSiegen.Server.Service
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
{
var token = await _localStorageService.GetItem<string>(StorageKeys.TokenKey);
var tokenValid = AuthHelper.ValidateToken(token, out _);
var tokenValid = AuthHelper.ValidateToken(token, out var user);
var checkR = await _userService.CheckForceLogout(user);
if (checkR.Success && checkR.Data)
tokenValid = false;
var identity = new ClaimsIdentity();
if (tokenValid)
identity = new ClaimsIdentity(new[]