2022-09-06 16:44:13 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
2019-10-07 18:35:37 -04:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2020-05-22 22:38:38 -04:00
|
|
|
|
using SharedLibraryCore;
|
2019-10-07 18:35:37 -04:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
using Data.Models.Client;
|
2022-04-19 19:43:58 -04:00
|
|
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
2022-09-06 16:44:13 -04:00
|
|
|
|
using SharedLibraryCore.Commands;
|
2019-10-07 18:35:37 -04:00
|
|
|
|
using static SharedLibraryCore.GameEvent;
|
|
|
|
|
|
|
|
|
|
namespace WebfrontCore.Middleware
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Facilitates the removal of identity claims when client is demoted
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal class ClaimsPermissionRemoval
|
|
|
|
|
{
|
|
|
|
|
private readonly IManager _manager;
|
2022-09-06 16:44:13 -04:00
|
|
|
|
private static readonly ConcurrentDictionary<int, (ClaimsState, DateTimeOffset?)> PrivilegedClientIds = new();
|
2019-10-07 18:35:37 -04:00
|
|
|
|
private readonly RequestDelegate _nextRequest;
|
|
|
|
|
|
2022-09-06 16:44:13 -04:00
|
|
|
|
private enum ClaimsState
|
|
|
|
|
{
|
|
|
|
|
Current,
|
|
|
|
|
Tainted
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-07 18:35:37 -04:00
|
|
|
|
public ClaimsPermissionRemoval(RequestDelegate nextRequest, IManager manager)
|
|
|
|
|
{
|
|
|
|
|
_manager = manager;
|
2020-05-22 22:38:38 -04:00
|
|
|
|
_manager.OnGameEventExecuted += OnGameEvent;
|
2019-10-07 18:35:37 -04:00
|
|
|
|
_nextRequest = nextRequest;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-06 16:44:13 -04:00
|
|
|
|
public async Task Invoke(HttpContext context)
|
|
|
|
|
{
|
|
|
|
|
await Initialize();
|
|
|
|
|
|
|
|
|
|
// sid stores the clientId
|
|
|
|
|
var claimsId = context.User.Claims.FirstOrDefault(claim => claim.Type == ClaimTypes.Sid)?.Value;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(claimsId))
|
|
|
|
|
{
|
|
|
|
|
var clientId = int.Parse(claimsId);
|
|
|
|
|
bool isTainted;
|
|
|
|
|
bool hasPrivilege;
|
|
|
|
|
|
|
|
|
|
lock (PrivilegedClientIds)
|
|
|
|
|
{
|
|
|
|
|
hasPrivilege = PrivilegedClientIds.ContainsKey(clientId);
|
|
|
|
|
isTainted = hasPrivilege && PrivilegedClientIds[clientId].Item1 == ClaimsState.Tainted;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!hasPrivilege || isTainted)
|
|
|
|
|
{
|
|
|
|
|
await context.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await _nextRequest.Invoke(context);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-22 22:38:38 -04:00
|
|
|
|
private void OnGameEvent(object sender, GameEvent gameEvent)
|
2019-10-07 18:35:37 -04:00
|
|
|
|
{
|
2022-09-06 16:44:13 -04:00
|
|
|
|
if (gameEvent.Extra?.GetType() == typeof(SetPasswordCommand))
|
|
|
|
|
{
|
|
|
|
|
lock (PrivilegedClientIds)
|
|
|
|
|
{
|
|
|
|
|
PrivilegedClientIds[gameEvent.Origin.ClientId] = (ClaimsState.Tainted, DateTimeOffset.UtcNow);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-15 20:37:34 -04:00
|
|
|
|
if (gameEvent.Type != EventType.ChangePermission || gameEvent.Extra is not EFClient.Permission perm)
|
2019-10-07 18:35:37 -04:00
|
|
|
|
{
|
2022-06-15 20:37:34 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-06 16:44:13 -04:00
|
|
|
|
lock (PrivilegedClientIds)
|
2022-06-15 20:37:34 -04:00
|
|
|
|
{
|
|
|
|
|
switch (perm)
|
2019-10-07 18:35:37 -04:00
|
|
|
|
{
|
2022-06-15 20:37:34 -04:00
|
|
|
|
// we want to remove the claims when the client is demoted
|
2022-09-06 16:44:13 -04:00
|
|
|
|
case < EFClient.Permission.Trusted when PrivilegedClientIds.ContainsKey(gameEvent.Target.ClientId):
|
2019-10-07 18:35:37 -04:00
|
|
|
|
{
|
2022-09-06 16:44:13 -04:00
|
|
|
|
PrivilegedClientIds.Remove(gameEvent.Target.ClientId, out _);
|
2022-06-15 20:37:34 -04:00
|
|
|
|
break;
|
2019-10-07 18:35:37 -04:00
|
|
|
|
}
|
2022-06-15 20:37:34 -04:00
|
|
|
|
// and add if promoted
|
2022-09-06 16:44:13 -04:00
|
|
|
|
case > EFClient.Permission.Trusted:
|
2019-10-07 18:35:37 -04:00
|
|
|
|
{
|
2022-09-06 16:44:13 -04:00
|
|
|
|
if (!PrivilegedClientIds.ContainsKey(gameEvent.Target.ClientId))
|
|
|
|
|
{
|
|
|
|
|
PrivilegedClientIds.TryAdd(gameEvent.Target.ClientId, (ClaimsState.Current, null));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// they've been intra-moted, so we need to taint their claims
|
|
|
|
|
PrivilegedClientIds[gameEvent.Target.ClientId] =
|
|
|
|
|
(ClaimsState.Tainted, DateTimeOffset.UtcNow);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-15 20:37:34 -04:00
|
|
|
|
break;
|
2019-10-07 18:35:37 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-06 16:44:13 -04:00
|
|
|
|
private async Task Initialize()
|
2019-10-07 18:35:37 -04:00
|
|
|
|
{
|
|
|
|
|
// we want to load the initial list of privileged clients
|
2022-06-15 20:37:34 -04:00
|
|
|
|
bool hasAny;
|
2022-09-06 16:44:13 -04:00
|
|
|
|
lock (PrivilegedClientIds)
|
2022-06-15 20:37:34 -04:00
|
|
|
|
{
|
2022-09-06 16:44:13 -04:00
|
|
|
|
hasAny = PrivilegedClientIds.Any();
|
2022-06-15 20:37:34 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-15 22:00:01 -04:00
|
|
|
|
if (!hasAny)
|
2019-10-07 18:35:37 -04:00
|
|
|
|
{
|
|
|
|
|
var ids = (await _manager.GetClientService().GetPrivilegedClients())
|
2022-06-15 20:37:34 -04:00
|
|
|
|
.Select(client => client.ClientId);
|
2019-10-07 18:35:37 -04:00
|
|
|
|
|
2022-09-06 16:44:13 -04:00
|
|
|
|
lock (PrivilegedClientIds)
|
2019-10-07 18:35:37 -04:00
|
|
|
|
{
|
2022-09-06 16:44:13 -04:00
|
|
|
|
foreach (var id in ids)
|
|
|
|
|
{
|
|
|
|
|
PrivilegedClientIds.TryAdd(id, (ClaimsState.Current, null));
|
|
|
|
|
}
|
2019-10-07 18:35:37 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-06 16:44:13 -04:00
|
|
|
|
}
|
2019-10-07 18:35:37 -04:00
|
|
|
|
|
2022-09-06 16:44:13 -04:00
|
|
|
|
public static async Task ValidateAsync(CookieValidatePrincipalContext context)
|
|
|
|
|
{
|
|
|
|
|
if (context.Principal is null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-10-07 18:35:37 -04:00
|
|
|
|
|
2022-09-06 16:44:13 -04:00
|
|
|
|
var claimsId = context.Principal.Claims.FirstOrDefault(claim => claim.Type == ClaimTypes.Sid)?.Value;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(claimsId))
|
2019-10-07 18:35:37 -04:00
|
|
|
|
{
|
2022-09-06 16:44:13 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
2022-06-15 20:37:34 -04:00
|
|
|
|
|
2022-09-06 16:44:13 -04:00
|
|
|
|
var clientId = int.Parse(claimsId);
|
|
|
|
|
|
|
|
|
|
bool shouldSignOut;
|
|
|
|
|
|
|
|
|
|
lock (PrivilegedClientIds)
|
|
|
|
|
{
|
|
|
|
|
// we want to log them out if they aren't in the privileged clients list
|
|
|
|
|
// or the token is tainted or the taint event occured after the token was generated
|
|
|
|
|
shouldSignOut = PrivilegedClientIds.ContainsKey(clientId) &&
|
|
|
|
|
(PrivilegedClientIds[clientId].Item1 == ClaimsState.Tainted ||
|
|
|
|
|
PrivilegedClientIds[clientId].Item2 is not null &&
|
|
|
|
|
PrivilegedClientIds[clientId].Item2.Value - context.Properties.IssuedUtc >
|
|
|
|
|
TimeSpan.FromSeconds(30));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (shouldSignOut)
|
|
|
|
|
{
|
|
|
|
|
context.RejectPrincipal();
|
|
|
|
|
await context.HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Task OnSignedIn(CookieSignedInContext context)
|
|
|
|
|
{
|
|
|
|
|
if (context.Principal is null)
|
|
|
|
|
{
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var claimsId = context.Principal.Claims.FirstOrDefault(claim => claim.Type == ClaimTypes.Sid)?.Value;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(claimsId))
|
|
|
|
|
{
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var clientId = int.Parse(claimsId);
|
|
|
|
|
|
|
|
|
|
lock (PrivilegedClientIds)
|
|
|
|
|
{
|
|
|
|
|
if (PrivilegedClientIds.ContainsKey(clientId))
|
2019-10-07 18:35:37 -04:00
|
|
|
|
{
|
2022-09-06 16:44:13 -04:00
|
|
|
|
PrivilegedClientIds[clientId] = PrivilegedClientIds[clientId].Item1 == ClaimsState.Tainted
|
|
|
|
|
? (ClaimsState.Current, DateTimeOffset.UtcNow)
|
|
|
|
|
: (ClaimsState.Current, null);
|
2019-10-07 18:35:37 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-06 16:44:13 -04:00
|
|
|
|
return Task.CompletedTask;
|
2019-10-07 18:35:37 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|