using Capnp.Rpc; using FabAccessAPI.Exceptions; using FabAccessAPI.Schema; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace FabAccessAPI { public class Connection { #region Private Fields private readonly IBootstrap? _BootstrapCap = null; private Auth? _Auth = null; #endregion #region Constructors /// /// /// /// Should be an already configured and connected TcpRpcClient public Connection(TcpRpcClient rpcClient) { RpcClient = rpcClient; _BootstrapCap = RpcClient.GetMain(); } #endregion #region Fields public TcpRpcClient? RpcClient { get; } = null; public Session? Session { get; private set; } = null; #endregion #region Methods /// /// Authenticate this connection. /// Calling this more then once is UB /// /// The desired authentication mechanism /// Key-Value data specific to the mechanism /// public async Task Auth(string mech, Dictionary kvs, CancellationToken cancellationToken_ = default) { IReadOnlyList? mechs = await _BootstrapCap.Mechanisms(); if (!mechs.Contains(mech)) { throw new UnsupportedMechanismException(); } if (_Auth == null) { IAuthentication? authCap = await _BootstrapCap.CreateSession(mech, cancellationToken_).ConfigureAwait(false); _Auth = new Auth(authCap); } Session = await _Auth.Authenticate(mech, kvs); } #endregion } }