From 83eccbd87d82c1217d2c121596e17c23fd8aaede Mon Sep 17 00:00:00 2001 From: TheJoKlLa Date: Sun, 24 Jan 2021 17:27:11 +0100 Subject: [PATCH] Added: Login --- Borepin/Borepin/Model/Credentials.cs | 15 +++ .../Borepin/PageModel/HostSelectPageModel.cs | 34 +++-- .../PageModel/LoginPasswordPageModel.cs | 22 ++-- Borepin/Borepin/Service/BFFHService.cs | 124 ++++++------------ .../Service/Credentials/CredentialsService.cs | 28 ++++ .../Service/Credentials/ICredentialService.cs | 15 +++ Borepin/Borepin/Service/Hosts/HostService.cs | 75 +++++++++++ Borepin/Borepin/Service/Hosts/IHostService.cs | 15 +++ 8 files changed, 222 insertions(+), 106 deletions(-) create mode 100644 Borepin/Borepin/Model/Credentials.cs create mode 100644 Borepin/Borepin/Service/Credentials/CredentialsService.cs create mode 100644 Borepin/Borepin/Service/Credentials/ICredentialService.cs create mode 100644 Borepin/Borepin/Service/Hosts/HostService.cs create mode 100644 Borepin/Borepin/Service/Hosts/IHostService.cs diff --git a/Borepin/Borepin/Model/Credentials.cs b/Borepin/Borepin/Model/Credentials.cs new file mode 100644 index 0000000..748ba47 --- /dev/null +++ b/Borepin/Borepin/Model/Credentials.cs @@ -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; } + } +} diff --git a/Borepin/Borepin/PageModel/HostSelectPageModel.cs b/Borepin/Borepin/PageModel/HostSelectPageModel.cs index 0e14ef1..c4b655a 100644 --- a/Borepin/Borepin/PageModel/HostSelectPageModel.cs +++ b/Borepin/Borepin/PageModel/HostSelectPageModel.cs @@ -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 hosts_list = await _BFFHService.GetKnownHostsAsync(); + + ObservableCollection host_string_list = new ObservableCollection(); + 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 _KnownHost_List; - public List KnownHost_List + private ObservableCollection _KnownHost_List; + public ObservableCollection KnownHost_List { - get - { - List list = new List(); - 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"; } } } diff --git a/Borepin/Borepin/PageModel/LoginPasswordPageModel.cs b/Borepin/Borepin/PageModel/LoginPasswordPageModel.cs index 9558282..81e11f1 100644 --- a/Borepin/Borepin/PageModel/LoginPasswordPageModel.cs +++ b/Borepin/Borepin/PageModel/LoginPasswordPageModel.cs @@ -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"); diff --git a/Borepin/Borepin/Service/BFFHService.cs b/Borepin/Borepin/Service/BFFHService.cs index 75ecd12..ef917dd 100644 --- a/Borepin/Borepin/Service/BFFHService.cs +++ b/Borepin/Borepin/Service/BFFHService.cs @@ -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 KnownHost + public BFFHService() { - get - { - string knownhosts = Preferences.Get("bffh_knownhost", ""); - - List knownhost_list = new List(); - - 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> 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 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 AuthenticateAsync() { - return SecureStorage.GetAsync(string.Format("bffh_password_{0}", SelectedHost.ToString())).Result; + HostCredentials credentials = await _CredentialsService.GetCredentialsAsync(ConnectedHost); + + await _Connection.Auth("PLAIN", new Dictionary { { "Username", credentials.Username }, { "Password", credentials.Password } }); + + return await Task.FromResult(true); } - private void AddUsername(string username) + public async Task 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 { { "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 { { "Username", username }, { "Password", password } }); - } - - public void Authenticate(string username, string password) - { - _Connection.Auth("PLAIN", new Dictionary { { "Username", username }, { "Password", password } }); - - AddUsername(username); - AddPasword(password); + return await Task.FromResult(true); } } } diff --git a/Borepin/Borepin/Service/Credentials/CredentialsService.cs b/Borepin/Borepin/Service/Credentials/CredentialsService.cs new file mode 100644 index 0000000..8ce035f --- /dev/null +++ b/Borepin/Borepin/Service/Credentials/CredentialsService.cs @@ -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 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 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); + } + } +} diff --git a/Borepin/Borepin/Service/Credentials/ICredentialService.cs b/Borepin/Borepin/Service/Credentials/ICredentialService.cs new file mode 100644 index 0000000..b535d73 --- /dev/null +++ b/Borepin/Borepin/Service/Credentials/ICredentialService.cs @@ -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 GetCredentialsAsync(Uri host); + + Task LogCredentialsAsync(HostCredentials credentials); + } +} diff --git a/Borepin/Borepin/Service/Hosts/HostService.cs b/Borepin/Borepin/Service/Hosts/HostService.cs new file mode 100644 index 0000000..7ac167e --- /dev/null +++ b/Borepin/Borepin/Service/Hosts/HostService.cs @@ -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 KnownHosts { get; set; } + + public HostService() + { + KnownHosts = new List(); + + LoadKnownHosts(); + } + + public Task> GetKnownHostsAsync() + { + return Task.FromResult(new ObservableCollection(KnownHosts)); + } + + public Task LogHostAsync(Uri host) + { + if (KnownHosts.Contains(host)) + { + return Task.FromResult(true); + } + + KnownHosts.Add(host); + return SaveKnownHosts(); + } + + public Task LoadKnownHosts() + { + string knownhosts = Preferences.Get("bffh_knownhost", ""); + + List knownhost_list = new List(); + + 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 SaveKnownHosts() + { + string knownhosts = ""; + + foreach(Uri host in KnownHosts) + { + knownhosts += "," + host.ToString(); + } + + Preferences.Set("knownhosts", knownhosts); + + return Task.FromResult(true); + } + } +} diff --git a/Borepin/Borepin/Service/Hosts/IHostService.cs b/Borepin/Borepin/Service/Hosts/IHostService.cs new file mode 100644 index 0000000..8c69064 --- /dev/null +++ b/Borepin/Borepin/Service/Hosts/IHostService.cs @@ -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> GetKnownHostsAsync(); + + Task LogHostAsync(Uri host); + } +}