2020-11-12 10:12:56 +01:00
|
|
|
|
using Capnp.Rpc;
|
|
|
|
|
using FabAccessAPI.Schema;
|
|
|
|
|
using System;
|
2020-10-26 14:35:31 +01:00
|
|
|
|
using System.Collections.Generic;
|
2020-11-07 22:08:40 +01:00
|
|
|
|
using System.Threading.Tasks;
|
2020-10-26 14:35:31 +01:00
|
|
|
|
|
2020-11-12 10:12:56 +01:00
|
|
|
|
namespace FabAccessAPI {
|
2020-12-19 21:04:17 +01:00
|
|
|
|
public class Connection {
|
|
|
|
|
#region private variables
|
2020-11-12 10:12:56 +01:00
|
|
|
|
private TcpRpcClient? _rpcClient = null;
|
|
|
|
|
private IBootstrap? _bootstrapCap = null;
|
|
|
|
|
private AuthUser? _authUser = null;
|
|
|
|
|
private Auth? _auth = null;
|
|
|
|
|
private Machines? _machines = null;
|
2020-12-19 21:04:17 +01:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Log
|
|
|
|
|
private static readonly log4net.ILog _Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
|
|
|
|
#endregion
|
|
|
|
|
|
2020-11-07 22:08:40 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="rpcClient">Should be an already configured and connected TcpRpcClient</param>
|
|
|
|
|
public Connection(TcpRpcClient rpcClient) {
|
|
|
|
|
_rpcClient = rpcClient;
|
|
|
|
|
_bootstrapCap = _rpcClient.GetMain<IBootstrap>();
|
2020-12-19 21:04:17 +01:00
|
|
|
|
_Log.Debug($"Done bootstraping API connection.");
|
2020-10-26 14:35:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-12 10:12:56 +01: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>
|
2020-12-19 21:04:17 +01:00
|
|
|
|
public async Task Auth(string mech, Dictionary<string, object> kvs) {
|
|
|
|
|
// _bootstrapCap = await _bootstrapCap.Unwrap();
|
|
|
|
|
var authCap = await _bootstrapCap.Auth();
|
|
|
|
|
_auth = new Auth(authCap);
|
2020-11-07 22:08:40 +01:00
|
|
|
|
var mechs = await _auth.GetMechanisms().ConfigureAwait(false);
|
2020-12-19 21:04:17 +01:00
|
|
|
|
_Log.Debug($"The Server supports the following auth mechs: {mechs}");
|
2020-11-12 10:12:56 +01:00
|
|
|
|
|
|
|
|
|
// TODO: Check that the requested auth mech is actually available.
|
|
|
|
|
|
|
|
|
|
await _auth.Authenticate(mech, kvs).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get a wrapped capability to interact with machines
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>A wrapped capability to interact with machines</returns>
|
2020-12-19 21:04:17 +01:00
|
|
|
|
public async Task<Machines> AccessMachines() {
|
2020-11-12 10:12:56 +01:00
|
|
|
|
_machines ??= new Machines((await _bootstrapCap.Machines().ConfigureAwait(false)));
|
|
|
|
|
return _machines;
|
2020-11-07 22:08:40 +01:00
|
|
|
|
}
|
2020-10-26 14:35:31 +01:00
|
|
|
|
}
|
|
|
|
|
}
|