user authentication

This commit is contained in:
Spirtix 2024-04-07 15:42:21 +02:00
parent 08b74957a8
commit a03f6b8dc0
9 changed files with 85 additions and 7 deletions

View File

@ -26,11 +26,13 @@ class ChatMessageHandler : ICommandHandler {
} }
public void Chat(Client client, NetworkObject receivedObject) { public void Chat(Client client, NetworkObject receivedObject) {
if (client.PlayerData.DiplayName == "")
return;
string message = receivedObject.Get<NetworkObject>("p").Get<string>("chm"); string message = receivedObject.Get<NetworkObject>("p").Get<string>("chm");
NetworkObject cmd = new(); NetworkObject cmd = new();
NetworkObject data = new(); NetworkObject data = new();
data.Add("arr", new string[] { "CMR", "-1", client.PlayerData.Uid, "1", message, "", "1", "placeholder" }); data.Add("arr", new string[] { "CMR", "-1", client.PlayerData.Uid, "1", message, "", "1", client.PlayerData.DiplayName });
cmd.Add("c", "CMR"); cmd.Add("c", "CMR");
cmd.Add("p", data); cmd.Add("p", data);

View File

@ -1,6 +1,7 @@
using sodoffmmo.Attributes; using sodoffmmo.Attributes;
using sodoffmmo.Core; using sodoffmmo.Core;
using sodoffmmo.Data; using sodoffmmo.Data;
using sodoffmmo.Management;
namespace sodoffmmo.CommandHandlers; namespace sodoffmmo.CommandHandlers;
@ -10,6 +11,14 @@ class LoginHandler : ICommandHandler
public void Handle(Client client, NetworkObject receivedObject) public void Handle(Client client, NetworkObject receivedObject)
{ {
client.PlayerData.UNToken = receivedObject.Get<string>("un"); client.PlayerData.UNToken = receivedObject.Get<string>("un");
if (!ValidToken(client)) {
NetworkObject obj = new();
obj.Add("dr", (byte)1);
client.Send(NetworkObject.WrapObject(0, 1005, obj).Serialize());
client.ScheduleDisconnect();
return;
}
NetworkArray rl = new(); NetworkArray rl = new();
NetworkArray r1 = new(); NetworkArray r1 = new();
@ -60,4 +69,36 @@ class LoginHandler : ICommandHandler
client.Send(NetworkObject.WrapObject(0, 1, content).Serialize()); client.Send(NetworkObject.WrapObject(0, 1, content).Serialize());
} }
private bool ValidToken(Client client) {
if (!Configuration.ServerConfiguration.Authentication ||
(client.PlayerData.UNToken == Configuration.ServerConfiguration.BypassToken && !string.IsNullOrEmpty(Configuration.ServerConfiguration.BypassToken)))
return true;
try {
HttpClient httpClient = new();
var content = new FormUrlEncodedContent(
new Dictionary<string, string> {
{ "token", client.PlayerData.UNToken },
});
httpClient.Timeout = new TimeSpan(0, 0, 3);
var response = httpClient.PostAsync($"{Configuration.ServerConfiguration.ApiUrl}/Authentication/MMOAuthentication", content).Result;
string? responseString = response.Content.ReadAsStringAsync().Result;
if (response.StatusCode != System.Net.HttpStatusCode.OK)
return false;
if (responseString == null)
throw new Exception("Response string null");
Console.WriteLine(responseString);
AuthenticationInfo info = Utils.DeserializeXml<AuthenticationInfo>(responseString);
if (info.Authenticated) {
client.PlayerData.DiplayName = info.DisplayName;
client.PlayerData.Role = info.Role;
return true;
}
} catch (Exception ex) {
Console.WriteLine($"Authentication exception IID: {client.ClientID} - {ex}");
}
return false;
}
} }

View File

@ -12,7 +12,7 @@ class SetPositionVariablesHandler : ICommandHandler {
if (client.Room == null) { if (client.Room == null) {
Console.WriteLine($"SPV Missing Room IID: {client.ClientID}"); Console.WriteLine($"SPV Missing Room IID: {client.ClientID}");
client.Send(NetworkObject.WrapObject(0, 1006, new NetworkObject()).Serialize()); client.Send(NetworkObject.WrapObject(0, 1006, new NetworkObject()).Serialize());
client.SheduleDisconnect(); client.ScheduleDisconnect();
return; return;
} }
this.client = client; this.client = client;

View File

@ -14,7 +14,7 @@ class SetUserVariablesHandler : ICommandHandler {
if (client.Room == null) { if (client.Room == null) {
Console.WriteLine($"SUV Missing Room IID: {client.ClientID}"); Console.WriteLine($"SUV Missing Room IID: {client.ClientID}");
client.Send(NetworkObject.WrapObject(0, 1006, new NetworkObject()).Serialize()); client.Send(NetworkObject.WrapObject(0, 1006, new NetworkObject()).Serialize());
client.SheduleDisconnect(); client.ScheduleDisconnect();
return; return;
} }
this.client = client; this.client = client;

View File

@ -40,7 +40,7 @@ public class Client {
try { try {
socket.Send(packet.SendData); socket.Send(packet.SendData);
} catch (SocketException) { } catch (SocketException) {
SheduleDisconnect(); ScheduleDisconnect();
} }
} }
@ -91,7 +91,7 @@ public class Client {
} }
} }
public void SheduleDisconnect() { public void ScheduleDisconnect() {
if (Room != null) { if (Room != null) {
// quiet remove from room (to avoid issues in Room.Send) // quiet remove from room (to avoid issues in Room.Send)
// - do not change Room value here // - do not change Room value here

View File

@ -20,6 +20,9 @@ internal static class Configuration {
return; return;
ServerConfiguration = serverConfiguration; ServerConfiguration = serverConfiguration;
if (string.IsNullOrEmpty(ServerConfiguration.ApiUrl)) {
ServerConfiguration.Authentication = false;
}
} }
} }
@ -33,4 +36,7 @@ internal sealed class ServerConfiguration {
public int RacingMainLobbyTimer { get; set; } = 15; public int RacingMainLobbyTimer { get; set; } = 15;
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 string ApiUrl { get; set; } = "";
public string BypassToken { get; set; } = "";
} }

View File

@ -4,8 +4,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Xml.Serialization;
using sodoffmmo.Data;
namespace sodoffmmo.Core; namespace sodoffmmo.Core;
internal static class Utils { internal static class Utils {
@ -33,4 +32,10 @@ internal static class Utils {
ret.Add("r", (int)roomID); ret.Add("r", (int)roomID);
return ret.Serialize(); return ret.Serialize();
} }
public static T DeserializeXml<T>(string xmlString) {
var serializer = new XmlSerializer(typeof(T));
using (var reader = new StringReader(xmlString))
return (T)serializer.Deserialize(reader);
}
} }

View File

@ -1,5 +1,6 @@
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using sodoffmmo.Core; using sodoffmmo.Core;
using sodoffmmo.Management;
namespace sodoffmmo.Data; namespace sodoffmmo.Data;
public class PlayerData { public class PlayerData {
@ -60,6 +61,9 @@ public class PlayerData {
// membership status // membership status
public string M { get; set; } = "True"; public string M { get; set; } = "True";
public string DiplayName { get; set; } = "";
public Role Role { get; set; } = Role.User;
public NetworkArray GetNetworkData(int clientID, out NetworkArray paramArr) { public NetworkArray GetNetworkData(int clientID, out NetworkArray paramArr) {
NetworkArray arr = new(); NetworkArray arr = new();
arr.Add(clientID); arr.Add(clientID);

View File

@ -0,0 +1,20 @@
using System.Xml.Serialization;
namespace sodoffmmo.Management;
[Serializable]
public class AuthenticationInfo {
[XmlElement]
public bool Authenticated { get; set; }
[XmlElement]
public string DisplayName { get; set; }
[XmlElement]
public Role Role { get; set; }
}
[Serializable]
public enum Role {
User, Admin, Moderator
}