1
0
mirror of https://github.com/RaidMax/IW4M-Admin.git synced 2025-06-07 21:58:06 -05:00
IW4M-Admin/Plugins/Login/Commands/LoginCommand.cs
Ayymoss 279016c405
Refactor Login plugin and clean up code
The Login plugin code has been optimized and cleaned up. This refactoring includes removing unnecessary conditions, switching to a more readable syntax, and deleting deprecated methods or properties. The possibility of a null configuration is also eliminated to improve code reliability.
2024-07-16 23:58:18 +01:00

67 lines
2.3 KiB
C#

using System.Threading.Tasks;
using SharedLibraryCore;
using SharedLibraryCore.Commands;
using SharedLibraryCore.Configuration;
using SharedLibraryCore.Helpers;
using SharedLibraryCore.Interfaces;
using EFClient = Data.Models.Client.EFClient;
namespace IW4MAdmin.Plugins.Login.Commands
{
public class LoginCommand : Command
{
private readonly LoginConfiguration _loginConfig;
private readonly LoginStates _loginStates;
public LoginCommand(CommandConfiguration config, ITranslationLookup translationLookup, LoginConfiguration loginConfig,
LoginStates loginStates) : base(config, translationLookup)
{
_loginConfig = loginConfig;
_loginStates = loginStates;
Name = "login";
Description = _translationLookup["PLUGINS_LOGIN_COMMANDS_LOGIN_DESC"];
Alias = "li";
Permission = EFClient.Permission.Trusted;
RequiresTarget = false;
Arguments =
[
new CommandArgument
{
Name = Utilities.CurrentLocalization.LocalizationIndex["COMMANDS_ARGS_PASSWORD"],
Required = true
}
];
}
public override async Task ExecuteAsync(GameEvent gameEvent)
{
if (!_loginConfig.RequirePrivilegedClientLogin)
{
gameEvent.Origin.Tell(_translationLookup["PLUGINS_LOGIN_COMMANDS_LOGIN_DISABLED"]);
return;
}
var success = gameEvent.Owner.Manager.TokenAuthenticator.AuthorizeToken(new TokenIdentifier
{
ClientId = gameEvent.Origin.ClientId,
Token = gameEvent.Data
});
if (!success)
{
var hashedPassword = await Task.FromResult(Hashing.Hash(gameEvent.Data, gameEvent.Origin.PasswordSalt));
success = hashedPassword[0] == gameEvent.Origin.Password;
}
if (success)
{
_loginStates.AuthorizedClients[gameEvent.Origin.ClientId] = true;
}
gameEvent.Origin.Tell(success
? _translationLookup["PLUGINS_LOGIN_COMMANDS_LOGIN_SUCCESS"]
: _translationLookup["PLUGINS_LOGIN_COMMANDS_LOGIN_FAIL"]);
}
}
}