Switched TextBox to PasswordBox for the Password

This commit is contained in:
erri120 2020-04-04 16:33:58 +02:00
parent cf79e82276
commit e3ed9bf4fd
No known key found for this signature in database
GPG Key ID: A8C0A18D8D4D3135
6 changed files with 45 additions and 21 deletions

View File

@ -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);
}
}
}
}

View File

@ -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);
}
}

View File

@ -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;
}

View File

@ -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<Unit, Unit> LoginCommand;
private readonly ObservableAsPropertyHelper<bool> _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();
}
}
}

View File

@ -23,9 +23,9 @@
</Grid.ColumnDefinitions>
<TextBox FontSize="20" x:Name="Username" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2"/>
<TextBox Margin="0 16 0 -8" FontSize="20" x:Name="Password" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"/>
<PasswordBox Margin="0 16 0 -8" FontSize="20" x:Name="Password" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"/>
<Button Margin="8 8 0 0" x:Name="LoginButton" Grid.Row="2" Grid.Column="2">
<Button Margin="8 8 0 0" x:Name="LoginButton" Grid.Row="2" Grid.Column="2" Click="LoginButton_OnClick">
<TextBlock FontSize="14">Login</TextBlock>
</Button>

View File

@ -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);
}
}
}