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 variables private readonly TcpRpcClient? _rpcClient = null; private readonly IBootstrap? _bootstrapCap = null; private Auth? _auth = null; #endregion public TcpRpcClient? RpcClient => _rpcClient; #region Log //private static readonly log4net.ILog _Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); #endregion /// /// /// /// Should be an already configured and connected TcpRpcClient public Connection(TcpRpcClient rpcClient) { _rpcClient = rpcClient; _bootstrapCap = _rpcClient.GetMain(); //_Log.Debug($"Done bootstraping API connection."); } /// /// 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) { // _bootstrapCap = await _bootstrapCap.Unwrap(); if(_auth == null) { var authCap = await _bootstrapCap.AuthenticationSystem(cancellationToken_); _auth = new Auth(authCap); } var mechs = await _auth.GetMechanisms(); //_Log.Debug($"The Server supports the following auth mechs: {string.Join(", ", mechs)}"); 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(); } } }