From d2b7826643610fda7c6912b8a67df418a5ec8f38 Mon Sep 17 00:00:00 2001 From: geel9 Date: Wed, 2 Dec 2015 12:07:41 -0500 Subject: [PATCH] Handle WGTokenInvalidException when fetching confirmations --- .../ConfirmationForm.cs | 31 +++++++++++++- .../MainForm.Designer.cs | 1 + Steam Desktop Authenticator/MainForm.cs | 39 ++++++----------- Steam Desktop Authenticator/Manifest.cs | 42 +++++++++++++++++-- 4 files changed, 82 insertions(+), 31 deletions(-) diff --git a/Steam Desktop Authenticator/ConfirmationForm.cs b/Steam Desktop Authenticator/ConfirmationForm.cs index af05e9c1..7ee9a748 100644 --- a/Steam Desktop Authenticator/ConfirmationForm.cs +++ b/Steam Desktop Authenticator/ConfirmationForm.cs @@ -45,12 +45,39 @@ private void listAccounts_SelectedValueChanged(object sender, EventArgs e) } - private void loadConfirmations() + private void loadConfirmations(bool retry = false) { listConfirmations.Items.Clear(); listConfirmations.SelectedIndex = -1; - Confirmations = mCurrentAccount.FetchConfirmations(); + try + { + Confirmations = mCurrentAccount.FetchConfirmations(); + } + catch (SteamGuardAccount.WGTokenInvalidException e) + { + if (retry) + { + this.Close(); + return; + } + //TODO: catch only the relevant exceptions + if (mCurrentAccount.RefreshSession()) + { + Manifest manifest = Manifest.GetManifest(); + + bool success; + string passKey = manifest.PromptForPassKey(out success); + + if (!success) + MessageBox.Show("Unable to fetch confirmations without your passkey."); + + manifest.SaveAccount(mCurrentAccount, manifest.Encrypted, passKey); + + loadConfirmations(true); + } + } + if (Confirmations.Length > 0) { for (int i = 0; i < Confirmations.Length; i++) diff --git a/Steam Desktop Authenticator/MainForm.Designer.cs b/Steam Desktop Authenticator/MainForm.Designer.cs index bf04404f..6e26260b 100644 --- a/Steam Desktop Authenticator/MainForm.Designer.cs +++ b/Steam Desktop Authenticator/MainForm.Designer.cs @@ -193,6 +193,7 @@ private void InitializeComponent() this.Resizable = false; this.ShadowType = MetroFramework.Forms.MetroForm.MetroFormShadowType.SystemShadow; this.Text = "Steam Desktop Authenticator"; + this.Shown += new System.EventHandler(this.MainForm_Shown); this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); this.groupBox2.ResumeLayout(false); diff --git a/Steam Desktop Authenticator/MainForm.cs b/Steam Desktop Authenticator/MainForm.cs index 812ddeb9..4de26c46 100644 --- a/Steam Desktop Authenticator/MainForm.cs +++ b/Steam Desktop Authenticator/MainForm.cs @@ -26,9 +26,9 @@ public partial class MainForm : MetroFramework.Forms.MetroForm public MainForm() { InitializeComponent(); + this.labelVersion.Text = String.Format("v{0}", Application.ProductVersion); this.mManifest = Manifest.GetManifest(); - loadAccountsList(); pbTimeout.Maximum = 30; pbTimeout.Minimum = 0; @@ -64,36 +64,19 @@ private void loadAccountsList() listAccounts.Items.Clear(); listAccounts.SelectedIndex = -1; - string passKey = null; - if (mManifest.Encrypted) + bool success; + string passKey = mManifest.PromptForPassKey(out success); + if (!success) { - bool passKeyValid = false; - while (!passKeyValid) - { - InputForm passKeyForm = new InputForm("Please enter your encryption passkey.", true); - passKeyForm.ShowDialog(); - if (!passKeyForm.Canceled) - { - passKey = passKeyForm.txtBox.Text; - passKeyValid = this.mManifest.VerifyPasskey(passKey); - if (!passKeyValid) - { - MessageBox.Show("That passkey is invalid."); - } - } - else - { - this.Close(); - return; - } - } + this.Close(); + return; + } + if (mManifest.Encrypted) btnManageEncryption.Text = "Manage Encryption"; - } + else - { btnManageEncryption.Text = "Setup Encryption"; - } btnManageEncryption.Enabled = mManifest.Entries.Count > 0; @@ -222,6 +205,10 @@ private void btnManageEncryption_Click(object sender, EventArgs e) } } + private void MainForm_Shown(object sender, EventArgs e) + { + loadAccountsList(); + } // Logic for version checking private Version newVersion = null; diff --git a/Steam Desktop Authenticator/Manifest.cs b/Steam Desktop Authenticator/Manifest.cs index cb8f3c60..1b7f530b 100644 --- a/Steam Desktop Authenticator/Manifest.cs +++ b/Steam Desktop Authenticator/Manifest.cs @@ -114,6 +114,39 @@ private static Manifest _generateNewManifest(bool scanDir = false) return null; } + public string PromptForPassKey(out bool success) + { + success = false; + if (!this.Encrypted) + { + success = true; + return null; + } + + bool passKeyValid = false; + string passKey = null; + while (!passKeyValid) + { + InputForm passKeyForm = new InputForm("Please enter your encryption passkey.", true); + passKeyForm.ShowDialog(); + if (!passKeyForm.Canceled) + { + passKey = passKeyForm.txtBox.Text; + passKeyValid = this.VerifyPasskey(passKey); + if (!passKeyValid) + { + MessageBox.Show("That passkey is invalid."); + } + } + else + { + return null; + } + } + success = passKeyValid; + return passKey; + } + public string PromptSetupPassKey(string initialPrompt = "Enter passkey, or hit cancel to remain unencrypted.") { InputForm newPassKeyForm = new InputForm(initialPrompt); @@ -152,7 +185,7 @@ public string PromptSetupPassKey(string initialPrompt = "Enter passkey, or hit c return newPassKey; } - public SteamAuth.SteamGuardAccount[] GetAllAccounts(string passKey = null) + public SteamAuth.SteamGuardAccount[] GetAllAccounts(string passKey = null, int limit = -1) { if (passKey == null && this.Encrypted) return new SteamGuardAccount[0]; string maDir = Manifest.GetExecutableDir() + "/maFiles/"; @@ -171,6 +204,9 @@ public SteamAuth.SteamGuardAccount[] GetAllAccounts(string passKey = null) var account = JsonConvert.DeserializeObject(fileText); if (account == null) continue; accounts.Add(account); + + if (limit != -1 && limit >= accounts.Count) + break; } return accounts.ToArray(); @@ -226,8 +262,8 @@ public bool VerifyPasskey(string passkey) { if (!this.Encrypted || this.Entries.Count == 0) return true; - var accounts = this.GetAllAccounts(passkey); - return accounts != null && accounts.Length == this.Entries.Count; + var accounts = this.GetAllAccounts(passkey, 1); + return accounts != null && accounts.Length == 1; } public bool RemoveAccount(SteamGuardAccount account)