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();
}

View File

@@ -38,7 +38,7 @@ namespace FoodsharingSiegen.Server.Service
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
{
var token = await _localStorageService.GetItem<string>(StorageKeys.TokenKey);
var tokenValid = await AuthHelper.ValidateToken(token);
var tokenValid = AuthHelper.ValidateToken(token, out _);
var identity = new ClaimsIdentity();
if (tokenValid)

View File

@@ -8,6 +8,7 @@
<ItemGroup>
<ProjectReference Include="..\FoodsharingSiegen.Contracts\FoodsharingSiegen.Contracts.csproj" />
<ProjectReference Include="..\FoodsharingSiegen.Shared\FoodsharingSiegen.Shared.csproj" />
</ItemGroup>
<ItemGroup>

View File

@@ -0,0 +1,41 @@
using FoodsharingSiegen.Contracts.Entity;
using FoodsharingSiegen.Server.Auth;
using Microsoft.AspNetCore.Components;
namespace FoodsharingSiegen.Server.Pages
{
/// <summary>
/// The fs base class (a. beging, 08.04.2022)
/// </summary>
/// <seealso cref="ComponentBase"/>
public class FsBase : ComponentBase
{
#region Dependencies (Injected)
/// <summary>
/// Gets or sets the value of the auth service (ab)
/// </summary>
[Inject] private AuthService? AuthService { get; set; }
#endregion
#region Override OnInitializedAsync
/// <summary>
/// Ons the initialized (a. beging, 11.04.2022)
/// </summary>
protected override async Task OnInitializedAsync()
{
await AuthService!.Initialize();
await base.OnInitializedAsync();
}
#endregion
/// <summary>
/// Gets the value of the current user (ab)
/// </summary>
protected User CurrentUser => AuthService?.User ?? new User();
}
}

View File

@@ -4,12 +4,19 @@
@using FoodsharingSiegen.Server.Dialogs
@using FoodsharingSiegen.Server.Controls
@using FoodsharingSiegen.Contracts.Entity
@using FoodsharingSiegen.Contracts.Helper
@inherits FsBase
<PageTitle>Einarbeitungen</PageTitle>
<h4>Aktuelle Einarbeitungen</h4>
<Button Color="Color.Primary" Clicked="() => ProspectModal.Show()">Hinzufügen</Button>
<Button
Color="Color.Primary"
Clicked="() => ProspectModal.Show()"
Visibility="@(CurrentUser.IsInGroup(UserGroup.WelcomeTeam, UserGroup.Ambassador) ? Visibility.Default : Visibility.Invisible)"
>Hinzufügen</Button>
@{
var activeProspects = ProspectList?.Where(x => x.Interactions.All(i => i.Type != InteractionType.Complete));

View File

@@ -2,6 +2,8 @@
@page "/users"
@using FoodsharingSiegen.Contracts.Entity
@inherits FsBase
@code {
private RenderFragment PopupTitleTemplate(PopupTitleContext<User> value)
@@ -24,7 +26,7 @@
<h2>Benutzerverwaltung <span style="font-size: .5em; line-height: 0;">Admin</span></h2>
<div class="my-2">
<Button Color="Color.Primary" Disabled="@(SelectedUser == null)">Passwort setzen</Button>
<Button Color="Color.Primary" Disabled="@(SelectedUser == null)"><i class="fa-solid fa-key"></i>&nbsp;setzen</Button>
</div>
<DataGrid TItem="User"
@@ -35,7 +37,7 @@
PopupTitleTemplate="PopupTitleTemplate"
RowInserted="RowInserted"
RowUpdated="RowUpdated"
SelectedRow="SelectedUser"
@bind-SelectedRow="SelectedUser"
RowDoubleClicked="arg => UserDataGrid.Edit(arg.Item)"
Editable
Responsive>