From e3ed9bf4fd8b86ec4a7e4991a013cdbef5bad576 Mon Sep 17 00:00:00 2001 From: erri120 Date: Sat, 4 Apr 2020 16:33:58 +0200 Subject: [PATCH] Switched TextBox to PasswordBox for the Password --- Wabbajack.Common/Utils.cs | 15 ++++++++++ Wabbajack.Lib/Downloaders/INeedsLogin.cs | 3 +- Wabbajack.Lib/Downloaders/MEGADownloader.cs | 6 ++-- .../Settings/CredentialsLoginVM.cs | 28 +++++++++++-------- .../Views/Settings/CredentialsLoginView.xaml | 4 +-- .../Settings/CredentialsLoginView.xaml.cs | 10 ++++--- 6 files changed, 45 insertions(+), 21 deletions(-) diff --git a/Wabbajack.Common/Utils.cs b/Wabbajack.Common/Utils.cs index 2494701a..895abfa3 100644 --- a/Wabbajack.Common/Utils.cs +++ b/Wabbajack.Common/Utils.cs @@ -11,6 +11,7 @@ using System.Reactive.Linq; using System.Reactive.Subjects; using System.Reflection; using System.Runtime.InteropServices; +using System.Security; using System.Security.Cryptography; using System.Text; using System.Threading; @@ -1385,5 +1386,19 @@ namespace Wabbajack.Common await using var d = File.Create(dest); await s.CopyToAsync(d); } + + public static string ToNormalString(this SecureString value) + { + var valuePtr = IntPtr.Zero; + try + { + valuePtr = Marshal.SecureStringToGlobalAllocUnicode(value); + return Marshal.PtrToStringUni(valuePtr); + } + finally + { + Marshal.ZeroFreeGlobalAllocUnicode(valuePtr); + } + } } } diff --git a/Wabbajack.Lib/Downloaders/INeedsLogin.cs b/Wabbajack.Lib/Downloaders/INeedsLogin.cs index 0c2ed77f..6f331011 100644 --- a/Wabbajack.Lib/Downloaders/INeedsLogin.cs +++ b/Wabbajack.Lib/Downloaders/INeedsLogin.cs @@ -1,5 +1,6 @@ using System; using System.Reactive; +using System.Security; using ReactiveUI; namespace Wabbajack.Lib.Downloaders @@ -29,6 +30,6 @@ namespace Wabbajack.Lib.Downloaders public interface INeedsLoginCredentials : INeedsLogin { - LoginReturnMessage LoginWithCredentials(string username, string password); + LoginReturnMessage LoginWithCredentials(string username, SecureString password); } } diff --git a/Wabbajack.Lib/Downloaders/MEGADownloader.cs b/Wabbajack.Lib/Downloaders/MEGADownloader.cs index 3518ff55..8ed4c325 100644 --- a/Wabbajack.Lib/Downloaders/MEGADownloader.cs +++ b/Wabbajack.Lib/Downloaders/MEGADownloader.cs @@ -1,6 +1,7 @@ using System; using System.Reactive; using System.Reactive.Linq; +using System.Security; using System.Threading.Tasks; using CG.Web.MegaApiClient; using ReactiveUI; @@ -13,14 +14,13 @@ namespace Wabbajack.Lib.Downloaders public MegaApiClient MegaApiClient; private const string DataName = "mega-cred"; - public LoginReturnMessage LoginWithCredentials(string username, string password) + public LoginReturnMessage LoginWithCredentials(string username, SecureString password) { MegaApiClient.AuthInfos authInfos; try { - authInfos = MegaApiClient.GenerateAuthInfos(username, password); - // purge credentials + authInfos = MegaApiClient.GenerateAuthInfos(username, password.ToNormalString()); username = null; password = null; } diff --git a/Wabbajack/View Models/Settings/CredentialsLoginVM.cs b/Wabbajack/View Models/Settings/CredentialsLoginVM.cs index dca0436a..08f860cc 100644 --- a/Wabbajack/View Models/Settings/CredentialsLoginVM.cs +++ b/Wabbajack/View Models/Settings/CredentialsLoginVM.cs @@ -1,6 +1,6 @@ using System.Reactive; using System.Reactive.Linq; -using System.Windows.Controls; +using System.Security; using ReactiveUI; using ReactiveUI.Fody.Helpers; using Wabbajack.Lib; @@ -13,22 +13,28 @@ namespace Wabbajack [Reactive] public string Username { get; set; } - [Reactive] - public string Password { get; set; } - [Reactive] public LoginReturnMessage ReturnMessage { get; set; } - public ReactiveCommand LoginCommand; + private readonly ObservableAsPropertyHelper _loginEnabled; + public bool LoginEnabled => _loginEnabled.Value; + + private readonly INeedsLoginCredentials _downloader; public CredentialsLoginVM(INeedsLoginCredentials downloader) { - LoginCommand = ReactiveCommand.Create(() => - { - ReturnMessage = downloader.LoginWithCredentials(Username, Password); - Password = ""; - }, this.WhenAny(x => x.Username).CombineLatest(this.WhenAny(x => x.Password), - (username, password) => !string.IsNullOrWhiteSpace(username) && !string.IsNullOrWhiteSpace(password))); + _downloader = downloader; + + _loginEnabled = this.WhenAny(x => x.Username) + .Select(username => !string.IsNullOrWhiteSpace(username)) + .ToGuiProperty(this, + nameof(LoginEnabled)); + } + + public void Login(SecureString password) + { + ReturnMessage = _downloader.LoginWithCredentials(Username, password); + password.Clear(); } } } diff --git a/Wabbajack/Views/Settings/CredentialsLoginView.xaml b/Wabbajack/Views/Settings/CredentialsLoginView.xaml index 89539bed..957337c1 100644 --- a/Wabbajack/Views/Settings/CredentialsLoginView.xaml +++ b/Wabbajack/Views/Settings/CredentialsLoginView.xaml @@ -23,9 +23,9 @@ - + - diff --git a/Wabbajack/Views/Settings/CredentialsLoginView.xaml.cs b/Wabbajack/Views/Settings/CredentialsLoginView.xaml.cs index 36a2ac1e..efc1951c 100644 --- a/Wabbajack/Views/Settings/CredentialsLoginView.xaml.cs +++ b/Wabbajack/Views/Settings/CredentialsLoginView.xaml.cs @@ -1,6 +1,5 @@ using System.Reactive.Disposables; using System.Windows; -using System.Windows.Controls; using ReactiveUI; using Wabbajack.Lib.Downloaders; @@ -23,13 +22,16 @@ namespace Wabbajack { this.Bind(ViewModel, x => x.Username, x => x.Username.Text) .DisposeWith(disposable); - this.Bind(ViewModel, x => x.Password, x => x.Password.Text) - .DisposeWith(disposable); - this.OneWayBindStrict(ViewModel, x => x.LoginCommand, x => x.LoginButton.Command) + this.Bind(ViewModel, x => x.LoginEnabled, x => x.LoginButton.IsEnabled) .DisposeWith(disposable); this.OneWayBind(ViewModel, x => x.ReturnMessage.Message, x => x.Message.Text) .DisposeWith(disposable); }); } + + private void LoginButton_OnClick(object sender, RoutedEventArgs e) + { + ViewModel.Login(Password.SecurePassword); + } } }