From dcb0644af434715a9272f13f422486e83cc848c7 Mon Sep 17 00:00:00 2001 From: Robert Paciorek Date: Tue, 9 Apr 2024 13:43:31 +0000 Subject: [PATCH] add Optional Authentication mode this allow run managment command (after authentication) on open mmo servers (with player from external api servers) --- src/CommandHandlers/ChatMessageHandler.cs | 4 +++- src/CommandHandlers/LoginHandler.cs | 7 ++++--- src/Core/Configuration.cs | 8 ++++++-- src/Management/ManagementCommandProcessor.cs | 4 ++-- src/appsettings.json | 15 ++++++++++++++- 5 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/CommandHandlers/ChatMessageHandler.cs b/src/CommandHandlers/ChatMessageHandler.cs index 412ce51..ffdacf7 100644 --- a/src/CommandHandlers/ChatMessageHandler.cs +++ b/src/CommandHandlers/ChatMessageHandler.cs @@ -32,8 +32,10 @@ class ChatMessageHandler : CommandHandler { } public void Chat(Client client, string message) { - if (Configuration.ServerConfiguration.Authentication && client.PlayerData.DiplayName == "placeholder") + if (Configuration.ServerConfiguration.Authentication >= AuthenticationMode.RequiredForChat && client.PlayerData.DiplayName == "placeholder") { + client.Send(Utils.BuildServerSideMessage("You must be authenticated to use the chat", "Server")); return; + } client.Room.Send(Utils.BuildChatMessage(client.PlayerData.Uid, message, client.PlayerData.DiplayName), client); diff --git a/src/CommandHandlers/LoginHandler.cs b/src/CommandHandlers/LoginHandler.cs index af626a2..3eceffa 100644 --- a/src/CommandHandlers/LoginHandler.cs +++ b/src/CommandHandlers/LoginHandler.cs @@ -72,9 +72,10 @@ class LoginHandler : CommandHandler } private bool ValidToken(Client client) { - if (!Configuration.ServerConfiguration.Authentication || + if (Configuration.ServerConfiguration.Authentication == AuthenticationMode.Disabled || (client.PlayerData.UNToken == Configuration.ServerConfiguration.BypassToken && !string.IsNullOrEmpty(Configuration.ServerConfiguration.BypassToken))) return true; + try { HttpClient httpClient = new(); var content = new FormUrlEncodedContent( @@ -87,7 +88,7 @@ class LoginHandler : CommandHandler string? responseString = response.Content.ReadAsStringAsync().Result; if (response.StatusCode != System.Net.HttpStatusCode.OK) - return false; + throw new Exception($"Response status code {response.StatusCode}"); if (responseString == null) throw new Exception("Response string null"); @@ -100,6 +101,6 @@ class LoginHandler : CommandHandler } catch (Exception ex) { Console.WriteLine($"Authentication exception IID: {client.ClientID} - {ex}"); } - return false; + return Configuration.ServerConfiguration.Authentication != AuthenticationMode.Required; // return true on auth err if not Required mode } } diff --git a/src/Core/Configuration.cs b/src/Core/Configuration.cs index e99cbf1..80d5b0d 100644 --- a/src/Core/Configuration.cs +++ b/src/Core/Configuration.cs @@ -21,7 +21,7 @@ internal static class Configuration { ServerConfiguration = serverConfiguration; if (string.IsNullOrEmpty(ServerConfiguration.ApiUrl)) { - ServerConfiguration.Authentication = false; + ServerConfiguration.Authentication = AuthenticationMode.Disabled; } } } @@ -37,7 +37,11 @@ internal sealed class ServerConfiguration { public int PingDelay { get; set; } = 17; public bool EnableChat { get; set; } = true; public bool AllowChaos { get; set; } = false; - public bool Authentication { get; set; } = false; + public AuthenticationMode Authentication { get; set; } = AuthenticationMode.Disabled; public string ApiUrl { get; set; } = ""; public string BypassToken { get; set; } = ""; } + +public enum AuthenticationMode { + Disabled, Optional, RequiredForChat, Required +} diff --git a/src/Management/ManagementCommandProcessor.cs b/src/Management/ManagementCommandProcessor.cs index 7dec864..c4b35eb 100644 --- a/src/Management/ManagementCommandProcessor.cs +++ b/src/Management/ManagementCommandProcessor.cs @@ -13,7 +13,7 @@ public class ManagementCommandProcessor { static bool initialized = false; public static void Initialize() { - if (!Configuration.ServerConfiguration.Authentication) + if (Configuration.ServerConfiguration.Authentication == AuthenticationMode.Disabled) return; commands.Clear(); var handlerTypes = Assembly.GetExecutingAssembly().GetTypes() @@ -28,7 +28,7 @@ public class ManagementCommandProcessor { } public static bool ProcessCommand(string message, Client client) { - if (!Configuration.ServerConfiguration.Authentication || !initialized) + if (!initialized) return false; if (!message.StartsWith("::") || message.Length < 3) return false; diff --git a/src/appsettings.json b/src/appsettings.json index 4eda7c5..23e44fd 100644 --- a/src/appsettings.json +++ b/src/appsettings.json @@ -25,6 +25,19 @@ "RacingMinPlayers": 2, "// AllowChaos": "disable server side exploit protection", - "AllowChaos": false + "AllowChaos": false, + + "// Authentication": "Player authentication mode: Disabled, Optional, RequiredForChat, Required", + "// Authentication Disabled": "authentication is disabled, anyone can connect to mmo", + "// Authentication Optional": "authentication is required only for moderation activities", + "// Authentication RequiredForChat": "authentication is required only for moderation activities and using chat (if chat is enabled)", + "// Authentication Required": "authentication is required to connect to mmo", + "Authentication": "Disabled", + + "// ApiUrl": "SoDOff API server URL for authentication calls", + "ApiUrl": "http://localhost:5000", + + "// BypassToken": "Token allowed to connect without authentication", + "BypassToken": "" } }