mirror of
https://github.com/SoDOff-Project/sodoff-mmo.git
synced 2025-10-11 08:18:49 -07:00
user authentication
This commit is contained in:
parent
08b74957a8
commit
a03f6b8dc0
@ -26,11 +26,13 @@ class ChatMessageHandler : ICommandHandler {
|
||||
}
|
||||
|
||||
public void Chat(Client client, NetworkObject receivedObject) {
|
||||
if (client.PlayerData.DiplayName == "")
|
||||
return;
|
||||
string message = receivedObject.Get<NetworkObject>("p").Get<string>("chm");
|
||||
|
||||
NetworkObject cmd = 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("p", data);
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
using sodoffmmo.Attributes;
|
||||
using sodoffmmo.Core;
|
||||
using sodoffmmo.Data;
|
||||
using sodoffmmo.Management;
|
||||
|
||||
namespace sodoffmmo.CommandHandlers;
|
||||
|
||||
@ -10,6 +11,14 @@ class LoginHandler : ICommandHandler
|
||||
public void Handle(Client client, NetworkObject receivedObject)
|
||||
{
|
||||
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 r1 = new();
|
||||
@ -60,4 +69,36 @@ class LoginHandler : ICommandHandler
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ class SetPositionVariablesHandler : ICommandHandler {
|
||||
if (client.Room == null) {
|
||||
Console.WriteLine($"SPV Missing Room IID: {client.ClientID}");
|
||||
client.Send(NetworkObject.WrapObject(0, 1006, new NetworkObject()).Serialize());
|
||||
client.SheduleDisconnect();
|
||||
client.ScheduleDisconnect();
|
||||
return;
|
||||
}
|
||||
this.client = client;
|
||||
|
@ -14,7 +14,7 @@ class SetUserVariablesHandler : ICommandHandler {
|
||||
if (client.Room == null) {
|
||||
Console.WriteLine($"SUV Missing Room IID: {client.ClientID}");
|
||||
client.Send(NetworkObject.WrapObject(0, 1006, new NetworkObject()).Serialize());
|
||||
client.SheduleDisconnect();
|
||||
client.ScheduleDisconnect();
|
||||
return;
|
||||
}
|
||||
this.client = client;
|
||||
|
@ -40,7 +40,7 @@ public class Client {
|
||||
try {
|
||||
socket.Send(packet.SendData);
|
||||
} catch (SocketException) {
|
||||
SheduleDisconnect();
|
||||
ScheduleDisconnect();
|
||||
}
|
||||
}
|
||||
|
||||
@ -91,7 +91,7 @@ public class Client {
|
||||
}
|
||||
}
|
||||
|
||||
public void SheduleDisconnect() {
|
||||
public void ScheduleDisconnect() {
|
||||
if (Room != null) {
|
||||
// quiet remove from room (to avoid issues in Room.Send)
|
||||
// - do not change Room value here
|
||||
|
@ -20,6 +20,9 @@ internal static class Configuration {
|
||||
return;
|
||||
|
||||
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 bool EnableChat { get; set; } = true;
|
||||
public bool AllowChaos { get; set; } = false;
|
||||
public bool Authentication { get; set; } = false;
|
||||
public string ApiUrl { get; set; } = "";
|
||||
public string BypassToken { get; set; } = "";
|
||||
}
|
||||
|
@ -4,8 +4,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using sodoffmmo.Data;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace sodoffmmo.Core;
|
||||
internal static class Utils {
|
||||
@ -33,4 +32,10 @@ internal static class Utils {
|
||||
ret.Add("r", (int)roomID);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using sodoffmmo.Core;
|
||||
using sodoffmmo.Management;
|
||||
|
||||
namespace sodoffmmo.Data;
|
||||
public class PlayerData {
|
||||
@ -60,6 +61,9 @@ public class PlayerData {
|
||||
// membership status
|
||||
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) {
|
||||
NetworkArray arr = new();
|
||||
arr.Add(clientID);
|
||||
|
20
src/Management/AuthenticationInfo.cs
Normal file
20
src/Management/AuthenticationInfo.cs
Normal 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
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user