-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from TheButlah/thebutlah/break-out-password-auth
introduce IAuth and PasswordAuth
- Loading branch information
Showing
24 changed files
with
388 additions
and
201 deletions.
There are no files selected for viewing
32 changes: 12 additions & 20 deletions
32
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
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); | ||
} | ||
} |
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,64 @@ | ||
#nullable enable | ||
|
||
using System.Runtime.Serialization; | ||
using Encoding = System.Text.Encoding; | ||
using static Basis.Network.Core.Serializable.SerializableBasis; | ||
|
||
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 (serverPassword.V == string.Empty) | ||
{ | ||
BNL.Log("No server password set, user is allowed"); | ||
return true; | ||
} | ||
if (userPassword.V == string.Empty) | ||
{ | ||
BNL.Log("User had an empty password, user is rejected"); | ||
return false; | ||
} | ||
return serverPassword.V == userPassword.V; | ||
} | ||
|
||
public bool IsAuthenticated(AuthenticationMessage msg) | ||
{ | ||
var deserialized = new Deserialized(msg); | ||
return CheckPassword(serverPassword, deserialized.Password); | ||
} | ||
} | ||
} |
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
31 changes: 0 additions & 31 deletions
31
Basis Server/BasisNetworkServer/Password/BasisPasswordImplementation.cs
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.