mirror of
https://gitlab.com/fabinfra/fabaccess/borepin.git
synced 2025-03-12 23:01:52 +01:00
Added: Login
This commit is contained in:
parent
2344bb92b9
commit
83eccbd87d
15
Borepin/Borepin/Model/Credentials.cs
Normal file
15
Borepin/Borepin/Model/Credentials.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Borepin.Model
|
||||
{
|
||||
public class HostCredentials
|
||||
{
|
||||
public Uri Host { get; set; }
|
||||
|
||||
public string Username { get; set; }
|
||||
|
||||
public string Password { get; set; }
|
||||
}
|
||||
}
|
@ -3,7 +3,9 @@ using Prism.Mvvm;
|
||||
using Prism.Navigation;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Input;
|
||||
using Xamarin.Forms;
|
||||
|
||||
@ -21,6 +23,21 @@ namespace Borepin.PageModel
|
||||
|
||||
UseHostCommand = new Command(UseHostCommandExecuted);
|
||||
DetectHostCommand = new Command(DetectHostCommandExecuted);
|
||||
|
||||
LoadData();
|
||||
}
|
||||
|
||||
private async Task LoadData()
|
||||
{
|
||||
ObservableCollection<Uri> hosts_list = await _BFFHService.GetKnownHostsAsync();
|
||||
|
||||
ObservableCollection<string> host_string_list = new ObservableCollection<string>();
|
||||
foreach(Uri host in hosts_list)
|
||||
{
|
||||
host_string_list.Add(host.ToString());
|
||||
}
|
||||
|
||||
KnownHost_List = host_string_list;
|
||||
}
|
||||
|
||||
private string _Host;
|
||||
@ -30,18 +47,11 @@ namespace Borepin.PageModel
|
||||
set => SetProperty(ref _Host, value);
|
||||
}
|
||||
|
||||
private List<string> _KnownHost_List;
|
||||
public List<string> KnownHost_List
|
||||
private ObservableCollection<string> _KnownHost_List;
|
||||
public ObservableCollection<string> KnownHost_List
|
||||
{
|
||||
get
|
||||
{
|
||||
List<string> list = new List<string>();
|
||||
foreach(Uri host in _BFFHService.KnownHost)
|
||||
{
|
||||
list.Add(host.ToString());
|
||||
}
|
||||
return list;
|
||||
}
|
||||
get => _KnownHost_List;
|
||||
set => SetProperty(ref _KnownHost_List, value);
|
||||
}
|
||||
|
||||
private ICommand _UseHostCommand;
|
||||
@ -78,7 +88,7 @@ namespace Borepin.PageModel
|
||||
private void DetectHostCommandExecuted()
|
||||
{
|
||||
// Use Demo Host
|
||||
Host = "127.0.0.1";
|
||||
Host = "127.0.0.1:59661";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,8 +2,7 @@
|
||||
using Prism.Mvvm;
|
||||
using Prism.Navigation;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Input;
|
||||
using Xamarin.Forms;
|
||||
|
||||
@ -14,7 +13,6 @@ namespace Borepin.PageModel
|
||||
private INavigationService _NavigationService;
|
||||
private BFFHService _BFFHService;
|
||||
|
||||
private bool _KnownCredentials = false;
|
||||
private string _KnownUsername = "";
|
||||
|
||||
public LoginPasswordPageModel(INavigationService navigationService, BFFHService bffhService)
|
||||
@ -22,14 +20,20 @@ namespace Borepin.PageModel
|
||||
_NavigationService = navigationService;
|
||||
_BFFHService = bffhService;
|
||||
|
||||
_KnownUsername = bffhService.LoadUsername();
|
||||
if(_KnownUsername != null)
|
||||
AuthenticateCommand = new Command(AuthenticateCommandExecuted);
|
||||
|
||||
LoadData();
|
||||
}
|
||||
|
||||
private async Task LoadData()
|
||||
{
|
||||
_KnownUsername = await _BFFHService.GetUsername();
|
||||
|
||||
if (_KnownUsername != null)
|
||||
{
|
||||
Username = _KnownUsername;
|
||||
Password = "********";
|
||||
}
|
||||
|
||||
AuthenticateCommand = new Command(AuthenticateCommandExecuted);
|
||||
}
|
||||
|
||||
private string _Username;
|
||||
@ -57,11 +61,11 @@ namespace Borepin.PageModel
|
||||
{
|
||||
if(_KnownUsername == Username)
|
||||
{
|
||||
_BFFHService.Authenticate();
|
||||
await _BFFHService.AuthenticateAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
_BFFHService.Authenticate(Username, Password);
|
||||
await _BFFHService.Authenticate(Username, Password);
|
||||
}
|
||||
|
||||
var result = await _NavigationService.NavigateAsync("//MainPage/NavigationPage/MachinesPage");
|
||||
|
@ -1,67 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Xamarin.Essentials;
|
||||
using FabAccessAPI;
|
||||
using Capnp.Rpc;
|
||||
using Xamarin.Forms;
|
||||
using Borepin.Service.Hosts;
|
||||
using Borepin.Service.Credentials;
|
||||
using Borepin.Model;
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace Borepin.Service
|
||||
{
|
||||
public class BFFHService
|
||||
{
|
||||
public Uri SelectedHost;
|
||||
|
||||
public Uri ConnectedHost;
|
||||
private Connection _Connection;
|
||||
private HostService _HostService;
|
||||
private CredentialsService _CredentialsService;
|
||||
|
||||
public List<Uri> KnownHost
|
||||
public BFFHService()
|
||||
{
|
||||
get
|
||||
{
|
||||
string knownhosts = Preferences.Get("bffh_knownhost", "");
|
||||
|
||||
List<Uri> knownhost_list = new List<Uri>();
|
||||
|
||||
string[] split = knownhosts.Split(',');
|
||||
|
||||
foreach(string host in split)
|
||||
{
|
||||
if(host == "")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
Uri host_new = new Uri(host);
|
||||
knownhost_list.Add(host_new);
|
||||
}
|
||||
return knownhost_list;
|
||||
}
|
||||
_HostService = new HostService();
|
||||
_CredentialsService = new CredentialsService();
|
||||
}
|
||||
|
||||
private void KnownHost_Add(Uri address)
|
||||
public async Task<ObservableCollection<Uri>> GetKnownHostsAsync()
|
||||
{
|
||||
if(KnownHost.Contains(address))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
string knownhosts = Preferences.Get("bffh_knownhost", "");
|
||||
|
||||
knownhosts += "," + address.ToString();
|
||||
|
||||
Preferences.Set("bffh_knownhost", knownhosts);
|
||||
return await _HostService.GetKnownHostsAsync();
|
||||
}
|
||||
|
||||
public void Connect()
|
||||
{
|
||||
if (SelectedHost == null)
|
||||
{
|
||||
throw new System.Exception("No Host selected");
|
||||
}
|
||||
|
||||
Connect(SelectedHost);
|
||||
}
|
||||
|
||||
public void Connect(Uri address)
|
||||
public void Connect(Uri host)
|
||||
{
|
||||
if(_Connection != null)
|
||||
{
|
||||
@ -69,14 +36,24 @@ namespace Borepin.Service
|
||||
}
|
||||
|
||||
var rpcClient = new TcpRpcClient();
|
||||
rpcClient.Connect(address.Host, address.Port);
|
||||
rpcClient.Connect(host.Host, host.Port);
|
||||
|
||||
Connection connection_test = new Connection(rpcClient);
|
||||
|
||||
KnownHost_Add(address);
|
||||
_HostService.LogHostAsync(host);
|
||||
|
||||
_Connection = connection_test;
|
||||
SelectedHost = address;
|
||||
ConnectedHost = host;
|
||||
}
|
||||
|
||||
public void Connect()
|
||||
{
|
||||
if (ConnectedHost == null)
|
||||
{
|
||||
throw new System.Exception("No Host selected");
|
||||
}
|
||||
|
||||
Connect(ConnectedHost);
|
||||
}
|
||||
|
||||
public void Disconnect()
|
||||
@ -85,50 +62,27 @@ namespace Borepin.Service
|
||||
_Connection = null;
|
||||
}
|
||||
|
||||
public string LoadUsername()
|
||||
public async Task<string> GetUsername()
|
||||
{
|
||||
string username = null;
|
||||
|
||||
Device.BeginInvokeOnMainThread(async () => {
|
||||
username = await SecureStorage.GetAsync(string.Format("bffh_username_{0}", SelectedHost.ToString()));
|
||||
});
|
||||
|
||||
return username;
|
||||
return await Task.FromResult((await _CredentialsService.GetCredentialsAsync(ConnectedHost)).Username);
|
||||
}
|
||||
|
||||
private string LoadPassword()
|
||||
public async Task<bool> AuthenticateAsync()
|
||||
{
|
||||
return SecureStorage.GetAsync(string.Format("bffh_password_{0}", SelectedHost.ToString())).Result;
|
||||
HostCredentials credentials = await _CredentialsService.GetCredentialsAsync(ConnectedHost);
|
||||
|
||||
await _Connection.Auth("PLAIN", new Dictionary<string, object> { { "Username", credentials.Username }, { "Password", credentials.Password } });
|
||||
|
||||
return await Task.FromResult(true);
|
||||
}
|
||||
|
||||
private void AddUsername(string username)
|
||||
public async Task<bool> Authenticate(string username, string password)
|
||||
{
|
||||
Device.BeginInvokeOnMainThread(async () => {
|
||||
await SecureStorage.SetAsync(string.Format("bffh_username_{0}", SelectedHost.ToString()), username);
|
||||
});
|
||||
}
|
||||
await _Connection.Auth("PLAIN", new Dictionary<string, object> { { "Username", username }, { "Password", password } });
|
||||
|
||||
private void AddPasword(string password)
|
||||
{
|
||||
Device.BeginInvokeOnMainThread(async () => {
|
||||
await SecureStorage.SetAsync(string.Format("bffh_password_{0}", SelectedHost.ToString()), password);
|
||||
});
|
||||
}
|
||||
await _CredentialsService.LogCredentialsAsync(new HostCredentials() { Host = ConnectedHost, Username = username, Password = password });
|
||||
|
||||
public void Authenticate()
|
||||
{
|
||||
string username = LoadUsername();
|
||||
string password = LoadPassword();
|
||||
|
||||
_Connection.Auth("PLAIN", new Dictionary<string, object> { { "Username", username }, { "Password", password } });
|
||||
}
|
||||
|
||||
public void Authenticate(string username, string password)
|
||||
{
|
||||
_Connection.Auth("PLAIN", new Dictionary<string, object> { { "Username", username }, { "Password", password } });
|
||||
|
||||
AddUsername(username);
|
||||
AddPasword(password);
|
||||
return await Task.FromResult(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
28
Borepin/Borepin/Service/Credentials/CredentialsService.cs
Normal file
28
Borepin/Borepin/Service/Credentials/CredentialsService.cs
Normal file
@ -0,0 +1,28 @@
|
||||
using Borepin.Model;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Xamarin.Essentials;
|
||||
|
||||
namespace Borepin.Service.Credentials
|
||||
{
|
||||
public class CredentialsService : ICredentialService
|
||||
{
|
||||
public async Task<HostCredentials> GetCredentialsAsync(Uri host)
|
||||
{
|
||||
string username = await SecureStorage.GetAsync(string.Format("bffh_username_{0}", host.ToString()));
|
||||
string password = await SecureStorage.GetAsync(string.Format("bffh_password_{0}", host.ToString()));
|
||||
|
||||
HostCredentials hc = new HostCredentials() { Host = host, Username = username, Password = password };
|
||||
|
||||
return await Task.FromResult(hc);
|
||||
}
|
||||
|
||||
public async Task<bool> LogCredentialsAsync(HostCredentials credentials)
|
||||
{
|
||||
await SecureStorage.SetAsync(string.Format("bffh_username_{0}", credentials.Host.ToString()), credentials.Username);
|
||||
await SecureStorage.SetAsync(string.Format("bffh_password_{0}", credentials.Host.ToString()), credentials.Password);
|
||||
|
||||
return await Task.FromResult(true);
|
||||
}
|
||||
}
|
||||
}
|
15
Borepin/Borepin/Service/Credentials/ICredentialService.cs
Normal file
15
Borepin/Borepin/Service/Credentials/ICredentialService.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Borepin.Model;
|
||||
|
||||
namespace Borepin.Service.Credentials
|
||||
{
|
||||
public interface ICredentialService
|
||||
{
|
||||
Task<HostCredentials> GetCredentialsAsync(Uri host);
|
||||
|
||||
Task<bool> LogCredentialsAsync(HostCredentials credentials);
|
||||
}
|
||||
}
|
75
Borepin/Borepin/Service/Hosts/HostService.cs
Normal file
75
Borepin/Borepin/Service/Hosts/HostService.cs
Normal file
@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Xamarin.Essentials;
|
||||
|
||||
namespace Borepin.Service.Hosts
|
||||
{
|
||||
public class HostService : IHostService
|
||||
{
|
||||
public List<Uri> KnownHosts { get; set; }
|
||||
|
||||
public HostService()
|
||||
{
|
||||
KnownHosts = new List<Uri>();
|
||||
|
||||
LoadKnownHosts();
|
||||
}
|
||||
|
||||
public Task<ObservableCollection<Uri>> GetKnownHostsAsync()
|
||||
{
|
||||
return Task.FromResult(new ObservableCollection<Uri>(KnownHosts));
|
||||
}
|
||||
|
||||
public Task<bool> LogHostAsync(Uri host)
|
||||
{
|
||||
if (KnownHosts.Contains(host))
|
||||
{
|
||||
return Task.FromResult(true);
|
||||
}
|
||||
|
||||
KnownHosts.Add(host);
|
||||
return SaveKnownHosts();
|
||||
}
|
||||
|
||||
public Task<bool> LoadKnownHosts()
|
||||
{
|
||||
string knownhosts = Preferences.Get("bffh_knownhost", "");
|
||||
|
||||
List<Uri> knownhost_list = new List<Uri>();
|
||||
|
||||
string[] split = knownhosts.Split(',');
|
||||
|
||||
foreach (string host in split)
|
||||
{
|
||||
if (host == "")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
Uri host_new = new Uri(host);
|
||||
knownhost_list.Add(host_new);
|
||||
}
|
||||
|
||||
KnownHosts = knownhost_list;
|
||||
|
||||
return Task.FromResult(true);
|
||||
}
|
||||
|
||||
public Task<bool> SaveKnownHosts()
|
||||
{
|
||||
string knownhosts = "";
|
||||
|
||||
foreach(Uri host in KnownHosts)
|
||||
{
|
||||
knownhosts += "," + host.ToString();
|
||||
}
|
||||
|
||||
Preferences.Set("knownhosts", knownhosts);
|
||||
|
||||
return Task.FromResult(true);
|
||||
}
|
||||
}
|
||||
}
|
15
Borepin/Borepin/Service/Hosts/IHostService.cs
Normal file
15
Borepin/Borepin/Service/Hosts/IHostService.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Borepin.Service.Hosts
|
||||
{
|
||||
public interface IHostService
|
||||
{
|
||||
Task<ObservableCollection<Uri>> GetKnownHostsAsync();
|
||||
|
||||
Task<bool> LogHostAsync(Uri host);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user