mirror of
https://gitlab.com/fabinfra/fabaccess/borepin.git
synced 2025-03-13 23:31:48 +01:00
110 lines
3.3 KiB
C#
110 lines
3.3 KiB
C#
using Borepin.Model;
|
|
using Borepin.Service.Credentials;
|
|
using System.Threading.Tasks;
|
|
using System.Collections.Generic;
|
|
using FabAccessAPI.Schema;
|
|
using System;
|
|
|
|
namespace Borepin.Service.BFFH
|
|
{
|
|
public class ConnectionActiveException : Exception
|
|
{
|
|
public ConnectionActiveException()
|
|
{
|
|
}
|
|
|
|
public ConnectionActiveException(string message) : base(message)
|
|
{
|
|
}
|
|
|
|
public ConnectionActiveException(string message, Exception inner) : base(message, inner)
|
|
{
|
|
}
|
|
}
|
|
|
|
public class BFFHService : IBFFHService
|
|
{
|
|
private readonly ICredentialService _CredentialService;
|
|
|
|
private FabAccessAPI.Connection _Connection;
|
|
|
|
public BFFHService(ICredentialService credentialService)
|
|
{
|
|
_CredentialService = credentialService;
|
|
}
|
|
|
|
public Connection ActiveConnection { get; private set; }
|
|
|
|
public bool IsAuthenticated { get; private set; }
|
|
|
|
public async Task Connect(Connection connection)
|
|
{
|
|
if (_Connection != null || ActiveConnection != null)
|
|
{
|
|
throw new ConnectionActiveException();
|
|
}
|
|
|
|
Capnp.Rpc.TcpRpcClient rpcClient = new Capnp.Rpc.TcpRpcClient();
|
|
|
|
rpcClient.Connect(connection.Address.Host, connection.Address.Port);
|
|
|
|
// IMPORTANT: without ConfigureAwait(false) every Call for CapnProto Runtime deadlocks on Android
|
|
await rpcClient.WhenConnected.ConfigureAwait(false);
|
|
|
|
FabAccessAPI.Connection connection_test = new FabAccessAPI.Connection(rpcClient);
|
|
|
|
_Connection = connection_test;
|
|
ActiveConnection = connection;
|
|
}
|
|
|
|
public async Task Disconnect()
|
|
{
|
|
_Connection.RpcClient?.Dispose();
|
|
_Connection = null;
|
|
ActiveConnection = null;
|
|
|
|
await Task.CompletedTask;
|
|
}
|
|
|
|
public bool CanAuthenticate()
|
|
{
|
|
return ActiveConnection.AuthenticationTyp == AuthenticationTyp.PLAIN && ActiveConnection.Username != string.Empty;
|
|
}
|
|
|
|
public async Task<bool> Authenticate()
|
|
{
|
|
string password = await _CredentialService.GetPasswordAsync(ActiveConnection);
|
|
|
|
await _Connection.Auth("PLAIN", new Dictionary<string, object> { { "Username", ActiveConnection.Username }, { "Password", password } });
|
|
|
|
return await Task.FromResult(true);
|
|
}
|
|
|
|
public async Task<bool> Authenticate(Connection connection, string password)
|
|
{
|
|
await _Connection.Auth("PLAIN", new Dictionary<string, object> { { "Username", ActiveConnection.Username }, { "Password", password } }).ConfigureAwait(false);
|
|
|
|
await _CredentialService.AddCredentialsAsync(connection, password);
|
|
|
|
ActiveConnection = connection;
|
|
|
|
return await Task.FromResult(true);
|
|
}
|
|
|
|
public Task<IMachineSystem> GetMachineSystemInterface()
|
|
{
|
|
return _Connection.AccessMachineSystem();
|
|
}
|
|
|
|
public Task<IUserSystem> GetUserSystemInterface()
|
|
{
|
|
return _Connection.AccessUserSystem();
|
|
}
|
|
|
|
public Task<IPermissionSystem> GetPermissionSystemInterface()
|
|
{
|
|
return _Connection.AccessPermissionSystem();
|
|
}
|
|
}
|
|
}
|