Skip to content

Commit

Permalink
Handle WGTokenInvalidException when fetching confirmations
Browse files Browse the repository at this point in the history
  • Loading branch information
geel9 committed Dec 2, 2015
1 parent 74f4f40 commit d2b7826
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 31 deletions.
31 changes: 29 additions & 2 deletions Steam Desktop Authenticator/ConfirmationForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
Expand Down
1 change: 1 addition & 0 deletions Steam Desktop Authenticator/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 13 additions & 26 deletions Steam Desktop Authenticator/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
42 changes: 39 additions & 3 deletions Steam Desktop Authenticator/Manifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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/";
Expand All @@ -171,6 +204,9 @@ public SteamAuth.SteamGuardAccount[] GetAllAccounts(string passKey = null)
var account = JsonConvert.DeserializeObject<SteamAuth.SteamGuardAccount>(fileText);
if (account == null) continue;
accounts.Add(account);

if (limit != -1 && limit >= accounts.Count)
break;
}

return accounts.ToArray();
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d2b7826

Please sign in to comment.