fix token auth issue

This commit is contained in:
RaidMax 2022-06-16 10:07:03 -05:00
parent 73c8d0da33
commit 4534d24fe6
15 changed files with 29 additions and 43 deletions

View File

@ -9,25 +9,25 @@ namespace IW4MAdmin.Application.Misc
{
internal class TokenAuthentication : ITokenAuthentication
{
private readonly ConcurrentDictionary<string, TokenState> _tokens;
private readonly ConcurrentDictionary<int, TokenState> _tokens;
private readonly RandomNumberGenerator _random;
private static readonly TimeSpan TimeoutPeriod = new(0, 0, 120);
private const short TokenLength = 4;
public TokenAuthentication()
{
_tokens = new ConcurrentDictionary<string, TokenState>();
_tokens = new ConcurrentDictionary<int, TokenState>();
_random = RandomNumberGenerator.Create();
}
public bool AuthorizeToken(ITokenIdentifier authInfo)
{
var key = BuildKey(authInfo);
var authorizeSuccessful = _tokens.ContainsKey(key) && _tokens[key].Token == key;
var authorizeSuccessful = _tokens.ContainsKey(authInfo.ClientId) &&
_tokens[authInfo.ClientId].Token == authInfo.Token;
if (authorizeSuccessful)
{
_tokens.TryRemove(key, out _);
_tokens.TryRemove(authInfo.ClientId, out _);
}
return authorizeSuccessful;
@ -36,15 +36,14 @@ namespace IW4MAdmin.Application.Misc
public TokenState GenerateNextToken(ITokenIdentifier authInfo)
{
TokenState state;
var genKey = BuildKey(authInfo);
if (_tokens.ContainsKey(genKey))
if (_tokens.ContainsKey(authInfo.ClientId))
{
state = _tokens[genKey];
state = _tokens[authInfo.ClientId];
if (DateTime.Now - state.RequestTime > TimeoutPeriod)
{
_tokens.TryRemove(genKey, out _);
_tokens.TryRemove(authInfo.ClientId, out _);
}
else
@ -59,12 +58,12 @@ namespace IW4MAdmin.Application.Misc
TokenDuration = TimeoutPeriod
};
_tokens.TryAdd(genKey, state);
_tokens.TryAdd(authInfo.ClientId, state);
// perform some housekeeping so we don't have built up tokens if they're not ever used
foreach (var (key, value) in _tokens)
{
if ((DateTime.Now - value.RequestTime) > TimeoutPeriod)
if (DateTime.Now - value.RequestTime > TimeoutPeriod)
{
_tokens.TryRemove(key, out _);
}
@ -97,7 +96,5 @@ namespace IW4MAdmin.Application.Misc
_random.Dispose();
return token.ToString();
}
private string BuildKey(ITokenIdentifier authInfo) => $"{authInfo.NetworkId}_${authInfo.Game}";
}
}

View File

@ -10,7 +10,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.SyndicationFeed.ReaderWriter" Version="1.0.2" />
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2022.6.15.1" PrivateAssets="All" />
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2022.6.16.1" PrivateAssets="All" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">

View File

@ -16,7 +16,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2022.6.15.1" PrivateAssets="All" />
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2022.6.16.1" PrivateAssets="All" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">

View File

@ -31,8 +31,7 @@ namespace IW4MAdmin.Plugins.Login.Commands
{
var success = gameEvent.Owner.Manager.TokenAuthenticator.AuthorizeToken(new TokenIdentifier
{
NetworkId = gameEvent.Origin.NetworkId,
Game = gameEvent.Origin.GameName,
ClientId = gameEvent.Origin.ClientId,
Token = gameEvent.Data
});

View File

@ -19,7 +19,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2022.6.15.1" PrivateAssets="All" />
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2022.6.16.1" PrivateAssets="All" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">

View File

@ -16,7 +16,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2022.6.15.1" PrivateAssets="All" />
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2022.6.16.1" PrivateAssets="All" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">

View File

@ -17,7 +17,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2022.6.15.1" PrivateAssets="All" />
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2022.6.16.1" PrivateAssets="All" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">

View File

@ -20,7 +20,7 @@
</Target>
<ItemGroup>
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2022.6.15.1" PrivateAssets="All" />
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2022.6.16.1" PrivateAssets="All" />
</ItemGroup>
</Project>

View File

@ -24,8 +24,7 @@ namespace SharedLibraryCore.Commands
{
var state = gameEvent.Owner.Manager.TokenAuthenticator.GenerateNextToken(new TokenIdentifier
{
Game = gameEvent.Origin.GameName,
NetworkId = gameEvent.Origin.NetworkId
ClientId = gameEvent.Origin.ClientId
});
gameEvent.Origin.Tell(string.Format(_translationLookup["COMMANDS_GENERATETOKEN_SUCCESS"], state.Token,
$"{state.RemainingTime} {_translationLookup["GLOBAL_MINUTES"]}", gameEvent.Origin.ClientId));

View File

@ -1,11 +1,9 @@
using Data.Models;
using SharedLibraryCore.Interfaces;
using SharedLibraryCore.Interfaces;
namespace SharedLibraryCore.Helpers;
public class TokenIdentifier : ITokenIdentifier
{
public long NetworkId { get; set; }
public Reference.Game Game { get; set; }
public int ClientId { get; set; }
public string Token { get; set; }
}

View File

@ -1,11 +1,7 @@

using Data.Models;
namespace SharedLibraryCore.Interfaces;
namespace SharedLibraryCore.Interfaces;
public interface ITokenIdentifier
{
long NetworkId { get; }
Reference.Game Game { get; set; }
string Token { get; set; }
int ClientId { get; }
string Token { get; }
}

View File

@ -103,9 +103,8 @@ namespace WebfrontCore.Controllers.API
{
var tokenData = new TokenIdentifier
{
Game = privilegedClient.GameName,
Token = request.Password,
NetworkId = privilegedClient.NetworkId
ClientId = clientId,
Token = request.Password
};
loginSuccess =

View File

@ -41,8 +41,7 @@ namespace WebfrontCore.Controllers
{
loginSuccess = Manager.TokenAuthenticator.AuthorizeToken(new TokenIdentifier
{
NetworkId = privilegedClient.NetworkId,
Game = privilegedClient.GameName,
ClientId = clientId,
Token = password
}) ||
(await Task.FromResult(Hashing.Hash(password, privilegedClient.PasswordSalt)))[0] ==

View File

@ -277,8 +277,7 @@ namespace WebfrontCore.Controllers
{
var state = Manager.TokenAuthenticator.GenerateNextToken(new TokenIdentifier
{
NetworkId = Client.NetworkId,
Game = Client.GameName
ClientId = Client.ClientId
});
return string.Format(Utilities.CurrentLocalization.LocalizationIndex["COMMANDS_GENERATETOKEN_SUCCESS"],

View File

@ -454,6 +454,6 @@ img.social-icon {
margin-top: 3px;
}
.sidebar-link .oi {
min-width: 16px;
.sidebar-link .oi, .sidebar-link img {
min-width: 1.2rem;
}