Skip to content

Commit

Permalink
fixed up the authentication code
Browse files Browse the repository at this point in the history
  • Loading branch information
dooly123 committed Dec 29, 2024
1 parent 3ec140e commit c6a42f9
Show file tree
Hide file tree
Showing 19 changed files with 265 additions and 116 deletions.
3 changes: 2 additions & 1 deletion Basis/BasisNetworkServer.Player.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@
<Compile Include="Packages\Basis Server\BasisNetworkServer\BasisNetworking\BasisNetworkOwnership.cs" />
<Compile Include="Packages\Basis Server\BasisNetworkServer\BasisNetworking\BasisServerReductionSystem.cs" />
<Compile Include="Packages\Basis Server\BasisNetworkServer\BasisNetworkHealthCheck.cs" />
<Compile Include="Packages\Basis Server\BasisNetworkServer\Auth\Password.cs" />
<Compile Include="Packages\Basis Server\BasisNetworkServer\BasisNetworking\BasisSavedState.cs" />
<Compile Include="Packages\Basis Server\BasisNetworkServer\BasisNetworkServer.cs" />
<Compile Include="Packages\Basis Server\BasisNetworkServer\Promethus\BasisPromethus.cs" />
<Compile Include="Packages\Basis Server\BasisNetworkServer\Password\BasisPasswordImplementation.cs" />
<Compile Include="Packages\Basis Server\BasisNetworkServer\Auth\Interface.cs" />
<Compile Include="Packages\Basis Server\BasisNetworkServer\BasisNetworking\BasisNetworkingGeneric.cs" />
<Compile Include="Packages\Basis Server\BasisNetworkServer\BasisNetworking\BasisServerConfiguration.cs" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public void Connect(ushort Port, string IpString,string PrimitivePassword)
Debug.Log("Network Starting Client");
BasisNetworkClient.AuthenticationMessage = new Network.Core.Serializable.SerializableBasis.AuthenticationMessage
{
Message = Encoding.UTF8.GetBytes(PrimitivePassword)
bytes = Encoding.UTF8.GetBytes(PrimitivePassword)
};
// Debug.Log("Size is " + BasisNetworkClient.AuthenticationMessage.Message.Length);
LocalPlayerPeer = BasisNetworkClient.StartClient(IpString, Port, readyMessage);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,42 +1,34 @@
using LiteNetLib.Utils;
#nullable enable

using LiteNetLib.Utils;

namespace Basis.Network.Core.Serializable
{
public static partial class SerializableBasis
{
/// Consistes of a ushort length, followed by byte array (of same length)
[System.Serializable]
public struct AuthenticationMessage
{
public ushort MessageLength;
public byte[] Message;
public void Deserialize(NetDataReader Writer)
public byte[] bytes;
public void Deserialize(NetDataReader Reader)
{
if (Writer.TryGetUShort(out MessageLength))
if (Reader.TryGetUShort(out ushort msgLength))
{
Message = new byte[MessageLength];
Writer.GetBytes(Message, MessageLength);
bytes = new byte[msgLength];
Reader.GetBytes(bytes, msgLength);
}
else
{
BNL.LogError("missing Message Length!");
}
}
public void Dispose()

public readonly void Serialize(NetDataWriter Writer)
{
}
public void Serialize(NetDataWriter Writer)
{
if (Message != null)
{
MessageLength = (ushort)Message.Length;
Writer.Put(MessageLength);
Writer.Put(Message);
}
else
{
MessageLength = 0;
Writer.Put(MessageLength);
}
Writer.Put(checked((ushort)bytes.Length));
Writer.Put(bytes);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void Deserialize(NetDataReader Writer)
throw new ArgumentException($"Invalid recipientsSize: {recipientsSize}");
}
recipients = new ushort[recipientsSize];
BNL.Log("Recipients is " + recipientsSize);
// BNL.Log("Recipients is " + recipientsSize);
for (int index = 0; index < recipientsSize; index++)
{
if (!Writer.TryGetUShort(out recipients[index]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,16 @@ public void Dispose()

public void Serialize(NetDataWriter Writer)
{
Writer.Put(array);
if (array == null || array.Length != 386)
{
BNL.LogError("Missing Array in Local Player Data!!");
array = new byte[386];
Writer.Put(array);
}
else
{
Writer.Put(array);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using DarkRift;
using DarkRift;
using LiteNetLib.Utils;
public static partial class SerializableBasis
{
Expand All @@ -16,7 +16,7 @@ public void Dispose()

public void Serialize(NetDataWriter Writer)
{
Writer.Put(playerID);
Writer.Put(playerID);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


using DarkRift;
using LiteNetLib.Utils;

Expand All @@ -20,8 +20,22 @@ public void Dispose()

public void Serialize(NetDataWriter Writer)
{
Writer.Put(playerUUID);
Writer.Put(playerDisplayName);
if (string.IsNullOrEmpty(playerUUID) == false)
{
Writer.Put(playerUUID);
}
else
{
Writer.Put("Failure");
}
if (string.IsNullOrEmpty(playerDisplayName) == false)
{
Writer.Put(playerDisplayName);
}
else
{
Writer.Put("Failure");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,15 @@ public void Serialize(NetDataWriter Writer)
{
// Write the load mode
Writer.Put(loadMode);
Writer.Put((ushort)byteArray.Length);
Writer.Put(byteArray);
if (byteArray == null)
{
Writer.Put((ushort)0);
}
else
{
Writer.Put((ushort)byteArray.Length);
Writer.Put(byteArray);
}
/*
if (AvatarLoadMessages == null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using LiteNetLib.Utils;
using LiteNetLib.Utils;
public static partial class SerializableBasis
{
public struct ServerSideSyncPlayerMessage
Expand Down Expand Up @@ -26,4 +26,4 @@ public void Serialize(NetDataWriter Writer)
avatarSerialization.Serialize(Writer);
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using AuthenticationMessage = Basis.Network.Core.Serializable.SerializableBasis.AuthenticationMessage;

namespace Basis.Network.Server.Auth
{
public interface IAuth
{
public bool IsAuthenticated(AuthenticationMessage msg);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 77 additions & 0 deletions Basis/Packages/Basis Server/BasisNetworkServer/Auth/Password.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#nullable enable

using System.Runtime.Serialization;
using Encoding = System.Text.Encoding;
using static Basis.Network.Core.Serializable.SerializableBasis;
using System.Diagnostics;
using System;

namespace Basis.Network.Server.Auth
{

/// Newtype on `string`. This represents the server's configured password.
internal readonly struct ServerPassword
{
public readonly string V { get; }
public ServerPassword(string password) { V = password; }
}

/// Newtype on `string`. This represents the user's password.
internal readonly struct UserPassword
{
public readonly string V { get; }
public UserPassword(string password) { V = password; }
}

internal readonly struct Deserialized
{
public readonly UserPassword Password { get; }
public Deserialized(AuthenticationMessage msg)
{
Password = new UserPassword(Encoding.UTF8.GetString(msg.bytes));
}
}

public class PasswordAuth : IAuth
{
private readonly ServerPassword serverPassword;

/// If `serverPassword` is an empty string, the server has no password and any user can connect.
public PasswordAuth(string serverPassword)
{
this.serverPassword = new ServerPassword(serverPassword);
}

private static bool CheckPassword(ServerPassword serverPassword, UserPassword userPassword)
{
if (string.IsNullOrEmpty(serverPassword.V))
{
BNL.Log("No server password set, user is allowed");
return true;
}

if (string.IsNullOrEmpty(userPassword.V))
{
BNL.Log("User had an empty password, user is rejected");
return false;
}

// Compare strings with explicit options
if (string.Equals(serverPassword.V, userPassword.V, StringComparison.Ordinal))
{
// BNL.Log("Passwords match successfully.");
return true;
}
else
{
BNL.LogError($"Passwords do not match: ServerPassword [{serverPassword.V}], UserPassword [{userPassword.V}]");
return false;
}
}
public bool IsAuthenticated(AuthenticationMessage msg)
{
var deserialized = new Deserialized(msg);
return CheckPassword(serverPassword, deserialized.Password);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c6a42f9

Please sign in to comment.