From 7110337dcde27246eef70b82154bfef4984af53e Mon Sep 17 00:00:00 2001 From: David Sparer Date: Tue, 14 Feb 2017 09:28:14 -0700 Subject: [PATCH] added an interface for requesting a password from the user --- .../Authentication/IPasswordRequestor.cs | 9 +++ mRemoteV1/Tools/MiscTools.cs | 5 +- mRemoteV1/UI/Forms/PasswordForm.cs | 62 +++++++------------ mRemoteV1/mRemoteV1.csproj | 1 + 4 files changed, 34 insertions(+), 43 deletions(-) create mode 100644 mRemoteV1/Security/Authentication/IPasswordRequestor.cs diff --git a/mRemoteV1/Security/Authentication/IPasswordRequestor.cs b/mRemoteV1/Security/Authentication/IPasswordRequestor.cs new file mode 100644 index 000000000..1f24c4314 --- /dev/null +++ b/mRemoteV1/Security/Authentication/IPasswordRequestor.cs @@ -0,0 +1,9 @@ +using System.Security; + +namespace mRemoteNG.Security.Authentication +{ + public interface IPasswordRequestor + { + SecureString RequestPassword(); + } +} \ No newline at end of file diff --git a/mRemoteV1/Tools/MiscTools.cs b/mRemoteV1/Tools/MiscTools.cs index b09c86150..b02ab794a 100644 --- a/mRemoteV1/Tools/MiscTools.cs +++ b/mRemoteV1/Tools/MiscTools.cs @@ -4,10 +4,8 @@ using System.Drawing; using System.Globalization; using System.IO; using System.Security; -using System.Windows.Forms; using mRemoteNG.App; using mRemoteNG.Messages; -using mRemoteNG.Security; using mRemoteNG.UI.Forms; using mRemoteNG.UI.Window; using static System.String; @@ -39,8 +37,7 @@ namespace mRemoteNG.Tools public static SecureString PasswordDialog(string passwordName = null, bool verify = true) { var passwordForm = new PasswordForm(passwordName, verify); - - return passwordForm.ShowDialog() == DialogResult.OK ? passwordForm.Password.ConvertToSecureString() : "".ConvertToSecureString(); + return passwordForm.RequestPassword(); } diff --git a/mRemoteV1/UI/Forms/PasswordForm.cs b/mRemoteV1/UI/Forms/PasswordForm.cs index 24e97f69e..e871025a8 100644 --- a/mRemoteV1/UI/Forms/PasswordForm.cs +++ b/mRemoteV1/UI/Forms/PasswordForm.cs @@ -1,36 +1,34 @@ using System; +using System.Security; using System.Windows.Forms; -using mRemoteNG.UI.Controls; +using mRemoteNG.Security; +using mRemoteNG.Security.Authentication; namespace mRemoteNG.UI.Forms { - public partial class PasswordForm + public partial class PasswordForm : IPasswordRequestor { - private string _passwordName; + private readonly string _passwordName; - #region Public Properties + private bool Verify { get; } - private bool Verify { get; set; } - - public string Password => Verify ? txtVerify.Text : txtPassword.Text; - - #endregion - - #region Constructors public PasswordForm(string passwordName = null, bool verify = true) { - // This call is required by the designer. InitializeComponent(); - - // Add any initialization after the InitializeComponent() call. _passwordName = passwordName; Verify = verify; } - #endregion - - #region Event Handlers - private void frmPassword_Load(object sender, EventArgs e) + public SecureString RequestPassword() + { + var dialog = ShowDialog(); + return dialog == DialogResult.OK ? + (Verify ? txtVerify.Text : txtPassword.Text).ConvertToSecureString() : + new SecureString(); + } + + #region Event Handlers + private void frmPassword_Load(object sender, EventArgs e) { ApplyLanguage(); @@ -48,15 +46,10 @@ namespace mRemoteNG.UI.Forms private void btnOK_Click(object sender, EventArgs e) { - if (Verify) - { - if (VerifyPassword()) - DialogResult = DialogResult.OK; - } - else - { + if (Verify && VerifyPassword()) + DialogResult = DialogResult.OK; + else DialogResult = DialogResult.OK; - } } private void txtPassword_TextChanged(object sender, EventArgs e) @@ -72,7 +65,6 @@ namespace mRemoteNG.UI.Forms lblPassword.Text = Language.strLabelPassword; lblVerify.Text = Language.strLabelVerify; - btnCancel.Text = Language.strButtonCancel; btnOK.Text = Language.strButtonOK; } @@ -82,20 +74,12 @@ namespace mRemoteNG.UI.Forms if (txtPassword.Text.Length >= 3) { if (txtPassword.Text == txtVerify.Text) - { return true; - } - else - { - ShowStatus(Language.strPasswordStatusMustMatch); - return false; - } - } - else - { - ShowStatus(Language.strPasswordStatusTooShort); - return false; + ShowStatus(Language.strPasswordStatusMustMatch); + return false; } + ShowStatus(Language.strPasswordStatusTooShort); + return false; } private void ShowStatus(string status) diff --git a/mRemoteV1/mRemoteV1.csproj b/mRemoteV1/mRemoteV1.csproj index 380ed2d74..ef705e8a7 100644 --- a/mRemoteV1/mRemoteV1.csproj +++ b/mRemoteV1/mRemoteV1.csproj @@ -212,6 +212,7 @@ +