-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
265 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 0 additions & 7 deletions
7
Basis/Packages/Basis Server/BasisNetworkCore/BasisNetworkCore.csproj.user.meta
This file was deleted.
Oops, something went wrong.
38 changes: 15 additions & 23 deletions
38
Basis/Packages/Basis Server/BasisNetworkCore/Serializable/AuthenticationMessage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...lient/BasisNetworkClient.csproj.user.meta → ...Basis Server/BasisNetworkServer/Auth.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
Basis/Packages/Basis Server/BasisNetworkServer/Auth/Interface.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
Basis/Packages/Basis Server/BasisNetworkServer/Auth/Interface.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
77 changes: 77 additions & 0 deletions
77
Basis/Packages/Basis Server/BasisNetworkServer/Auth/Password.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
Basis/Packages/Basis Server/BasisNetworkServer/Auth/Password.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.