add Optional Authentication mode

this allow run managment command (after authentication) on open mmo servers (with player from external api servers)
This commit is contained in:
Robert Paciorek 2024-04-09 13:43:31 +00:00
parent 87a199fc19
commit dcb0644af4
5 changed files with 29 additions and 9 deletions

View File

@ -32,8 +32,10 @@ class ChatMessageHandler : CommandHandler {
} }
public void Chat(Client client, string message) { 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; return;
}
client.Room.Send(Utils.BuildChatMessage(client.PlayerData.Uid, message, client.PlayerData.DiplayName), client); client.Room.Send(Utils.BuildChatMessage(client.PlayerData.Uid, message, client.PlayerData.DiplayName), client);

View File

@ -72,9 +72,10 @@ class LoginHandler : CommandHandler
} }
private bool ValidToken(Client client) { 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))) (client.PlayerData.UNToken == Configuration.ServerConfiguration.BypassToken && !string.IsNullOrEmpty(Configuration.ServerConfiguration.BypassToken)))
return true; return true;
try { try {
HttpClient httpClient = new(); HttpClient httpClient = new();
var content = new FormUrlEncodedContent( var content = new FormUrlEncodedContent(
@ -87,7 +88,7 @@ class LoginHandler : CommandHandler
string? responseString = response.Content.ReadAsStringAsync().Result; string? responseString = response.Content.ReadAsStringAsync().Result;
if (response.StatusCode != System.Net.HttpStatusCode.OK) if (response.StatusCode != System.Net.HttpStatusCode.OK)
return false; throw new Exception($"Response status code {response.StatusCode}");
if (responseString == null) if (responseString == null)
throw new Exception("Response string null"); throw new Exception("Response string null");
@ -100,6 +101,6 @@ class LoginHandler : CommandHandler
} catch (Exception ex) { } catch (Exception ex) {
Console.WriteLine($"Authentication exception IID: {client.ClientID} - {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
} }
} }

View File

@ -21,7 +21,7 @@ internal static class Configuration {
ServerConfiguration = serverConfiguration; ServerConfiguration = serverConfiguration;
if (string.IsNullOrEmpty(ServerConfiguration.ApiUrl)) { 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 int PingDelay { get; set; } = 17;
public bool EnableChat { get; set; } = true; public bool EnableChat { get; set; } = true;
public bool AllowChaos { get; set; } = false; 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 ApiUrl { get; set; } = "";
public string BypassToken { get; set; } = ""; public string BypassToken { get; set; } = "";
} }
public enum AuthenticationMode {
Disabled, Optional, RequiredForChat, Required
}

View File

@ -13,7 +13,7 @@ public class ManagementCommandProcessor {
static bool initialized = false; static bool initialized = false;
public static void Initialize() { public static void Initialize() {
if (!Configuration.ServerConfiguration.Authentication) if (Configuration.ServerConfiguration.Authentication == AuthenticationMode.Disabled)
return; return;
commands.Clear(); commands.Clear();
var handlerTypes = Assembly.GetExecutingAssembly().GetTypes() var handlerTypes = Assembly.GetExecutingAssembly().GetTypes()
@ -28,7 +28,7 @@ public class ManagementCommandProcessor {
} }
public static bool ProcessCommand(string message, Client client) { public static bool ProcessCommand(string message, Client client) {
if (!Configuration.ServerConfiguration.Authentication || !initialized) if (!initialized)
return false; return false;
if (!message.StartsWith("::") || message.Length < 3) if (!message.StartsWith("::") || message.Length < 3)
return false; return false;

View File

@ -25,6 +25,19 @@
"RacingMinPlayers": 2, "RacingMinPlayers": 2,
"// AllowChaos": "disable server side exploit protection", "// 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": ""
} }
} }