From 4534d24fe664b1e21afba950346f6656943ecabf Mon Sep 17 00:00:00 2001 From: RaidMax Date: Thu, 16 Jun 2022 10:07:03 -0500 Subject: [PATCH] fix token auth issue --- Application/Misc/TokenAuthentication.cs | 23 ++++++++----------- .../AutomessageFeed/AutomessageFeed.csproj | 2 +- Plugins/LiveRadar/LiveRadar.csproj | 2 +- Plugins/Login/Commands/LoginCommand.cs | 3 +-- Plugins/Login/Login.csproj | 2 +- .../ProfanityDeterment.csproj | 2 +- Plugins/Stats/Stats.csproj | 2 +- Plugins/Welcome/Welcome.csproj | 2 +- .../Commands/RequestTokenCommand.cs | 3 +-- SharedLibraryCore/Helpers/TokenIdentifier.cs | 6 ++--- .../Interfaces/ITokenIdentifier.cs | 10 +++----- .../Controllers/API/ClientController.cs | 5 ++-- WebfrontCore/Controllers/AccountController.cs | 3 +-- WebfrontCore/Controllers/ActionController.cs | 3 +-- WebfrontCore/wwwroot/css/src/main.scss | 4 ++-- 15 files changed, 29 insertions(+), 43 deletions(-) diff --git a/Application/Misc/TokenAuthentication.cs b/Application/Misc/TokenAuthentication.cs index d87887d26..f88a3e427 100644 --- a/Application/Misc/TokenAuthentication.cs +++ b/Application/Misc/TokenAuthentication.cs @@ -9,25 +9,25 @@ namespace IW4MAdmin.Application.Misc { internal class TokenAuthentication : ITokenAuthentication { - private readonly ConcurrentDictionary _tokens; + private readonly ConcurrentDictionary _tokens; private readonly RandomNumberGenerator _random; private static readonly TimeSpan TimeoutPeriod = new(0, 0, 120); private const short TokenLength = 4; public TokenAuthentication() { - _tokens = new ConcurrentDictionary(); + _tokens = new ConcurrentDictionary(); _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}"; } } diff --git a/Plugins/AutomessageFeed/AutomessageFeed.csproj b/Plugins/AutomessageFeed/AutomessageFeed.csproj index 67c07f89a..7f279a2a2 100644 --- a/Plugins/AutomessageFeed/AutomessageFeed.csproj +++ b/Plugins/AutomessageFeed/AutomessageFeed.csproj @@ -10,7 +10,7 @@ - + diff --git a/Plugins/LiveRadar/LiveRadar.csproj b/Plugins/LiveRadar/LiveRadar.csproj index 110d4ca4b..5c9bb5df5 100644 --- a/Plugins/LiveRadar/LiveRadar.csproj +++ b/Plugins/LiveRadar/LiveRadar.csproj @@ -16,7 +16,7 @@ - + diff --git a/Plugins/Login/Commands/LoginCommand.cs b/Plugins/Login/Commands/LoginCommand.cs index 64ce6abb5..613920688 100644 --- a/Plugins/Login/Commands/LoginCommand.cs +++ b/Plugins/Login/Commands/LoginCommand.cs @@ -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 }); diff --git a/Plugins/Login/Login.csproj b/Plugins/Login/Login.csproj index ba07b94f6..d412e524f 100644 --- a/Plugins/Login/Login.csproj +++ b/Plugins/Login/Login.csproj @@ -19,7 +19,7 @@ - + diff --git a/Plugins/ProfanityDeterment/ProfanityDeterment.csproj b/Plugins/ProfanityDeterment/ProfanityDeterment.csproj index 7cb9202cd..b52745c33 100644 --- a/Plugins/ProfanityDeterment/ProfanityDeterment.csproj +++ b/Plugins/ProfanityDeterment/ProfanityDeterment.csproj @@ -16,7 +16,7 @@ - + diff --git a/Plugins/Stats/Stats.csproj b/Plugins/Stats/Stats.csproj index d466aba9f..b355f5b6e 100644 --- a/Plugins/Stats/Stats.csproj +++ b/Plugins/Stats/Stats.csproj @@ -17,7 +17,7 @@ - + diff --git a/Plugins/Welcome/Welcome.csproj b/Plugins/Welcome/Welcome.csproj index 29d82435b..30b841bf3 100644 --- a/Plugins/Welcome/Welcome.csproj +++ b/Plugins/Welcome/Welcome.csproj @@ -20,7 +20,7 @@ - + diff --git a/SharedLibraryCore/Commands/RequestTokenCommand.cs b/SharedLibraryCore/Commands/RequestTokenCommand.cs index 9988b52aa..a4be24708 100644 --- a/SharedLibraryCore/Commands/RequestTokenCommand.cs +++ b/SharedLibraryCore/Commands/RequestTokenCommand.cs @@ -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)); diff --git a/SharedLibraryCore/Helpers/TokenIdentifier.cs b/SharedLibraryCore/Helpers/TokenIdentifier.cs index b03eb9331..da99aa342 100644 --- a/SharedLibraryCore/Helpers/TokenIdentifier.cs +++ b/SharedLibraryCore/Helpers/TokenIdentifier.cs @@ -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; } } diff --git a/SharedLibraryCore/Interfaces/ITokenIdentifier.cs b/SharedLibraryCore/Interfaces/ITokenIdentifier.cs index 0e2e0a0e9..a42e014ff 100644 --- a/SharedLibraryCore/Interfaces/ITokenIdentifier.cs +++ b/SharedLibraryCore/Interfaces/ITokenIdentifier.cs @@ -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; } } diff --git a/WebfrontCore/Controllers/API/ClientController.cs b/WebfrontCore/Controllers/API/ClientController.cs index 9c980e773..503725029 100644 --- a/WebfrontCore/Controllers/API/ClientController.cs +++ b/WebfrontCore/Controllers/API/ClientController.cs @@ -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 = diff --git a/WebfrontCore/Controllers/AccountController.cs b/WebfrontCore/Controllers/AccountController.cs index e2302dbe7..d90d3cc0a 100644 --- a/WebfrontCore/Controllers/AccountController.cs +++ b/WebfrontCore/Controllers/AccountController.cs @@ -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] == diff --git a/WebfrontCore/Controllers/ActionController.cs b/WebfrontCore/Controllers/ActionController.cs index bfd872b37..552e59931 100644 --- a/WebfrontCore/Controllers/ActionController.cs +++ b/WebfrontCore/Controllers/ActionController.cs @@ -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"], diff --git a/WebfrontCore/wwwroot/css/src/main.scss b/WebfrontCore/wwwroot/css/src/main.scss index 62a03560c..1bf08edab 100644 --- a/WebfrontCore/wwwroot/css/src/main.scss +++ b/WebfrontCore/wwwroot/css/src/main.scss @@ -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; }