borepin/FabAccessAPI/Connection.cs

89 lines
2.8 KiB
C#
Raw Normal View History

2021-09-11 13:53:40 +02:00
using Capnp.Rpc;
using FabAccessAPI.Schema;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2020-10-26 14:35:31 +01:00
2021-09-11 13:53:40 +02:00
namespace FabAccessAPI
{
public class Connection
{
2022-01-03 21:13:03 +00:00
#region Private Fields
private readonly IBootstrap? _BootstrapCap = null;
private Auth? _Auth = null;
2021-09-11 13:53:40 +02:00
#endregion
2020-11-12 10:12:56 +01:00
2022-01-03 21:13:03 +00:00
#region Constructors
2021-09-11 13:53:40 +02:00
/// <summary>
///
/// </summary>
/// <param name="rpcClient">Should be an already configured and connected TcpRpcClient</param>
public Connection(TcpRpcClient rpcClient)
{
2022-01-03 21:13:03 +00:00
RpcClient = rpcClient;
_BootstrapCap = RpcClient.GetMain<IBootstrap>();
//_Log.Debug($"Done bootstraping API connection.");
2021-09-11 13:53:40 +02:00
}
2022-01-03 21:13:03 +00:00
#endregion
2020-11-12 10:12:56 +01:00
2022-01-03 21:13:03 +00:00
#region Fields
public TcpRpcClient? RpcClient { get; } = null;
#endregion
#region Methods
2021-09-11 13:53:40 +02:00
/// <summary>
/// Authenticate this connection.
/// Calling this more then once is UB
/// </summary>
/// <param name="mech">The desired authentication mechanism</param>
/// <param name="kvs">Key-Value data specific to the mechanism</param>
/// <returns></returns>
2021-09-21 23:59:14 +02:00
public async Task<bool> Auth(string mech, Dictionary<string, object> kvs, CancellationToken cancellationToken_ = default)
2021-09-11 13:53:40 +02:00
{
2022-01-03 21:13:03 +00:00
if(_Auth == null)
2021-09-22 00:11:18 +02:00
{
2022-01-03 21:13:03 +00:00
IAuthenticationSystem? authCap = await _BootstrapCap.AuthenticationSystem(cancellationToken_).ConfigureAwait(false);
_Auth = new Auth(authCap);
2021-09-22 00:11:18 +02:00
}
2021-09-11 13:53:40 +02:00
2022-01-03 21:13:03 +00:00
IReadOnlyList<string>? mechs = await _Auth.GetMechanisms();
2021-09-11 13:53:40 +02:00
if (!mechs.Contains(mech))
{
throw new UnsupportedMechanismException();
}
2022-01-03 21:13:03 +00:00
return await _Auth.Authenticate(mech, kvs);
2021-09-11 13:53:40 +02:00
}
/// <summary>
/// Get a wrapped capability to interact with machines
/// </summary>
/// <returns>A wrapped capability to interact with machines</returns>
public async Task<IMachineSystem> AccessMachineSystem()
{
2022-01-03 21:13:03 +00:00
return await _BootstrapCap.MachineSystem();
2021-09-11 13:53:40 +02:00
}
/// <summary>
/// Get a wrapped capability to interact with users
/// </summary>
/// <returns>A wrapped capability to interact with users</returns>
public async Task<IUserSystem> AccessUserSystem()
{
2022-01-03 21:13:03 +00:00
return await _BootstrapCap.UserSystem();
2021-09-11 13:53:40 +02:00
}
/// <summary>
/// Get a wrapped capability to interact with permissions
/// </summary>
/// <returns>A wrapped capability to interact with permissions</returns>
public async Task<IPermissionSystem> AccessPermissionSystem()
{
2022-01-03 21:13:03 +00:00
return await _BootstrapCap.PermissionSystem();
2021-09-11 13:53:40 +02:00
}
2022-01-03 21:13:03 +00:00
#endregion
2021-09-11 13:53:40 +02:00
}
}