using Capnp.Rpc; 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(); //_Log.Debug($"Done bootstraping API connection."); } #endregion #region Fields public TcpRpcClient? RpcClient { get; } = 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) { if(_Auth == null) { IAuthenticationSystem? authCap = await _BootstrapCap.AuthenticationSystem(cancellationToken_).ConfigureAwait(false); _Auth = new Auth(authCap); } IReadOnlyList? mechs = await _Auth.GetMechanisms(); if (!mechs.Contains(mech)) { throw new UnsupportedMechanismException(); } return await _Auth.Authenticate(mech, kvs); } /// /// Get a wrapped capability to interact with machines /// /// A wrapped capability to interact with machines public async Task AccessMachineSystem() { return await _BootstrapCap.MachineSystem(); } /// /// Get a wrapped capability to interact with users /// /// A wrapped capability to interact with users public async Task AccessUserSystem() { return await _BootstrapCap.UserSystem(); } /// /// Get a wrapped capability to interact with permissions /// /// A wrapped capability to interact with permissions public async Task AccessPermissionSystem() { return await _BootstrapCap.PermissionSystem(); } #endregion } }