SessionHeaders

This commit is contained in:
2026-07-14 06:35:52 +03:00
parent 1ca9fc477b
commit 13898323dd
5 changed files with 157 additions and 63 deletions
+29 -57
View File
@@ -193,7 +193,7 @@ public partial class EpConnection : NetworkConnection, IStore
set
{
_remoteDomain = value;
_session.RemoteHeaders[EpAuthPacketHeader.Domain] = value;
_session.RemoteHeaders.Domain = value;
}
}
@@ -203,7 +203,7 @@ public partial class EpConnection : NetworkConnection, IStore
set
{
_localDomain = value;
_session.LocalHeaders[EpAuthPacketHeader.Domain] = value;
_session.LocalHeaders.Domain = value;
}
}
@@ -262,8 +262,7 @@ public partial class EpConnection : NetworkConnection, IStore
{
base.Assign(socket);
_session.LocalHeaders[EpAuthPacketHeader.IPAddress]
= socket.RemoteEndPoint.Address.GetAddressBytes();
_session.LocalHeaders.IPAddress = socket.RemoteEndPoint.Address.GetAddressBytes();
if (socket.State == SocketState.Established &&
_authDirection == AuthenticationDirection.Initiator)
@@ -277,8 +276,7 @@ public partial class EpConnection : NetworkConnection, IStore
if (_authDirection != AuthenticationDirection.Initiator)
return;
// change to Map<byte, object> for compatibility
var headers = _session.LocalHeaders.Select(x => new KeyValuePair<byte, object>((byte)x.Key, x.Value));
var headers = _session.LocalHeaders.Copy();
if (_session.AuthenticationMode != AuthenticationMode.None)
{
@@ -287,9 +285,9 @@ public partial class EpConnection : NetworkConnection, IStore
var initAuthResult = _session.AuthenticationHandler.Process(null);
headers.Add((byte)EpAuthPacketHeader.AuthenticationProtocol, _session.AuthenticationHandler.Protocol);
headers.Add((byte)EpAuthPacketHeader.AuthenticationData, initAuthResult.AuthenticationData);
headers.Add((byte)EpAuthPacketHeader.Domain, _remoteDomain);
headers.AuthenticationProtocol = _session.AuthenticationHandler.Protocol;
headers.AuthenticationData = initAuthResult.AuthenticationData;
headers.Domain = _remoteDomain;
}
@@ -308,10 +306,9 @@ public partial class EpConnection : NetworkConnection, IStore
/// Create a new distributed connection.
/// </summary>
/// <param name="socket">Socket to transfer data through.</param>
/// <param name="domain">Working domain.</param>
/// <param name="username">Username.</param>
/// <param name="password">Password.</param>
public EpConnection(ISocket socket, IAuthenticationHandler authenticationHandler, Map<EpAuthPacketHeader, object> headers)
/// <param name="authenticationHandler">Authentication handler for the session.</param>
/// <param name="headers">Initial local session headers.</param>
public EpConnection(ISocket socket, IAuthenticationHandler authenticationHandler, SessionHeaders headers)
{
_session = new Session();
@@ -728,30 +725,18 @@ public partial class EpConnection : NetworkConnection, IStore
if (_authPacket.Command == EpAuthPacketCommand.Initialize)
{
var remoteHeaders = new Map<byte, object>();
var remoteHeaders = new SessionHeaders();
object remoteAuthData = null;
if (_authPacket.Tdu != null)
{
var parsed = Codec.ParseSync(_authPacket.Tdu.Value, null);
if (parsed is Map<byte, object> headers)
{
remoteHeaders = headers;
foreach (var header in headers)
{
if (header.Key == (byte)EpAuthPacketHeader.AuthenticationData)
remoteAuthData = header.Value;
else
_session.RemoteHeaders.Add((EpAuthPacketHeader)header.Key, header.Value);
}
}
remoteHeaders = Codec.ParseIndexedType<SessionHeaders>(_authPacket.Tdu.Value, null);
remoteAuthData = remoteHeaders.AuthenticationData;
remoteHeaders.AuthenticationData = null;
}
var localHeaders = new Map<byte, object>();
foreach (var header in _session.LocalHeaders)
localHeaders.Add((byte)header.Key, header.Value);
_session.RemoteHeaders = remoteHeaders;
var localHeaders = _session.LocalHeaders.Copy();
if (_authPacket.AuthMode == AuthenticationMode.None)
{
@@ -773,7 +758,7 @@ public partial class EpConnection : NetworkConnection, IStore
return offset;
}
if (!_session.RemoteHeaders.ContainsKey(EpAuthPacketHeader.AuthenticationProtocol))
if (_session.RemoteHeaders.AuthenticationProtocol == null)
{
SendAuthHeaders(EpAuthPacketMethod.NotSupported, localHeaders);
_invalidCredentials = true;
@@ -781,13 +766,13 @@ public partial class EpConnection : NetworkConnection, IStore
return offset;
}
var provider = _serverWarehouse.GetAuthenticationProvider(_session.RemoteHeaders[EpAuthPacketHeader.AuthenticationProtocol].ToString());
var provider = _serverWarehouse.GetAuthenticationProvider(_session.RemoteHeaders.AuthenticationProtocol);
var handler = provider.CreateAuthenticationHandler(new AuthenticationContext()
{
Direction = AuthenticationDirection.Responder,
Mode = _authPacket.AuthMode,
Domain = _session.RemoteHeaders.ContainsKey(EpAuthPacketHeader.Domain) ? _session.RemoteHeaders[EpAuthPacketHeader.Domain].ToString() : null,
Domain = _session.RemoteHeaders.Domain,
Materials = new AuthenticationMaterial[] { new AuthenticationMaterial() { Type = AuthenticationMaterialType.Data, Value = remoteAuthData } }
});
@@ -807,8 +792,7 @@ public partial class EpConnection : NetworkConnection, IStore
// send acknowledgements
localHeaders.Add((byte)EpAuthPacketHeader.AuthenticationData,
authResult.AuthenticationData);
localHeaders.AuthenticationData = authResult.AuthenticationData;
if (authResult.Ruling == AuthenticationRuling.Failed)
{
@@ -853,32 +837,20 @@ public partial class EpConnection : NetworkConnection, IStore
if (_authPacket.Method == EpAuthPacketMethod.ProceedToHandshake
|| _authPacket.Method == EpAuthPacketMethod.ProceedToFinalHandshake)
{
var remoteHeaders
= new Map<EpAuthPacketHeader, object>();
var remoteHeaders = new SessionHeaders();
object remoteAuthData = null;
if (_authPacket.Tdu != null)
{
var parsed = Codec.ParseSync(_authPacket.Tdu.Value, Instance.Warehouse);
if (parsed is Map<byte, object> headers)
{
foreach (var header in headers)
{
if (header.Key == (byte)EpAuthPacketHeader.AuthenticationData)
{
remoteAuthData = header.Value;
}
else
{
remoteHeaders.Add((EpAuthPacketHeader)header.Key, header.Value);
}
}
_session.RemoteHeaders = remoteHeaders;// headers.Select(x => new KeyValuePair<EpAuthPacketHeader, object>((EpAuthPacketHeader)x.Key, x.Value));
}
remoteHeaders = Codec.ParseIndexedType<SessionHeaders>(
_authPacket.Tdu.Value,
Instance.Warehouse);
remoteAuthData = remoteHeaders.AuthenticationData;
remoteHeaders.AuthenticationData = null;
}
_session.RemoteHeaders = remoteHeaders;
if (_session.AuthenticationMode == AuthenticationMode.None)
{
if (_authPacket.Method == EpAuthPacketMethod.SessionEstablished)
@@ -1972,7 +1944,7 @@ public partial class EpConnection : NetworkConnection, IStore
_authDirection = AuthenticationDirection.Initiator;
_invalidCredentials = false;
_session.LocalHeaders[EpAuthPacketHeader.Domain] = domain;
_session.LocalHeaders.Domain = domain;
_hostname = hostname;
}
@@ -375,7 +375,7 @@ partial class EpConnection
}
void SendAuthHeaders(EpAuthPacketMethod method,
Map<byte, object> authHeaders)
SessionHeaders authHeaders)
{
if (authHeaders != null)
{
@@ -388,9 +388,10 @@ partial class EpConnection
var bl = new BinaryList();
bl.AddUInt8((byte)((byte)method | 0x20));
bl.AddUInt8Array(Codec.Compose(authHeaders,
this.Instance?.Warehouse ?? _serverWarehouse,
this));
bl.AddUInt8Array(Codec.ComposeIndexedType(
authHeaders,
this.Instance?.Warehouse ?? _serverWarehouse,
this));
Send(bl.ToArray());
}
else
+64 -2
View File
@@ -34,6 +34,68 @@ using Esiur.Security.Cryptography;
using Esiur.Net.Packets;
namespace Esiur.Security.Authority;
/// <summary>
/// Authentication metadata exchanged while establishing a session.
/// Members retain the indexes defined by <see cref="EpAuthPacketHeader"/>, so this
/// structure has the same wire representation as the former header map.
/// </summary>
public sealed class SessionHeaders : IndexedStructure
{
[Index((int)EpAuthPacketHeader.Version)]
public object? Version { get; set; }
[Index((int)EpAuthPacketHeader.Domain)]
public string? Domain { get; set; }
[Index((int)EpAuthPacketHeader.SupportedAuthentications)]
public object? SupportedAuthentications { get; set; }
[Index((int)EpAuthPacketHeader.SupportedHashAlgorithms)]
public object? SupportedHashAlgorithms { get; set; }
[Index((int)EpAuthPacketHeader.SupportedCiphers)]
public object? SupportedCiphers { get; set; }
[Index((int)EpAuthPacketHeader.SupportedCompression)]
public object? SupportedCompression { get; set; }
[Index((int)EpAuthPacketHeader.SupportedMultiFactorAuthentications)]
public object? SupportedMultiFactorAuthentications { get; set; }
[Index((int)EpAuthPacketHeader.CipherType)]
public object? CipherType { get; set; }
[Index((int)EpAuthPacketHeader.CipherKey)]
public byte[]? CipherKey { get; set; }
[Index((int)EpAuthPacketHeader.SoftwareIdentity)]
public string? SoftwareIdentity { get; set; }
[Index((int)EpAuthPacketHeader.Referrer)]
public string? Referrer { get; set; }
[Index((int)EpAuthPacketHeader.Time)]
public DateTime? Time { get; set; }
[Index((int)EpAuthPacketHeader.IPAddress)]
public byte[]? IPAddress { get; set; }
[Index((int)EpAuthPacketHeader.Identity)]
public string? Identity { get; set; }
[Index((int)EpAuthPacketHeader.AuthenticationProtocol)]
public string? AuthenticationProtocol { get; set; }
[Index((int)EpAuthPacketHeader.AuthenticationData)]
public object? AuthenticationData { get; set; }
[Index((int)EpAuthPacketHeader.ErrorMessage)]
public string? ErrorMessage { get; set; }
internal SessionHeaders Copy() => (SessionHeaders)MemberwiseClone();
}
public class Session
{
public byte[] Id { get; set; }
@@ -47,8 +109,8 @@ public class Session
public ISymetricCipher SymetricCipher { get; set; } = null;
public Map<EpAuthPacketHeader, object> LocalHeaders { get; set; } = new Map<EpAuthPacketHeader, object>();
public Map<EpAuthPacketHeader, object> RemoteHeaders { get; set; } = new Map<EpAuthPacketHeader, object>();
public SessionHeaders LocalHeaders { get; set; } = new SessionHeaders();
public SessionHeaders RemoteHeaders { get; set; } = new SessionHeaders();
//public AuthenticationMethod AuthenticationMethod { get; set; }
//public AuthenticationMethod RemoteMethod { get; set; }