borepin/FabAccessAPI/Connection.cs
2022-05-11 15:02:17 +02:00

66 lines
2.1 KiB
C#

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
/// <summary>
///
/// </summary>
/// <param name="rpcClient">Should be an already configured and connected TcpRpcClient</param>
public Connection(TcpRpcClient rpcClient)
{
RpcClient = rpcClient;
_BootstrapCap = RpcClient.GetMain<IBootstrap>();
}
#endregion
#region Fields
public TcpRpcClient? RpcClient { get; } = null;
public Session? Session { get; private set; } = null;
#endregion
#region Methods
/// <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>
/// <exception cref="UnsupportedMechanismException"></exception>
/// <exception cref="InvalidCredentialsException"></exception>
/// <exception cref="AuthenticationFailedException"></exception>
public async Task Auth(string mech, Dictionary<string, object> kvs, CancellationToken cancellationToken_ = default)
{
IReadOnlyList<string>? 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
}
}