Skip to content

Commit

Permalink
Use SteamGuardAccount methods for fetching confirmations
Browse files Browse the repository at this point in the history
  • Loading branch information
Jessecar96 committed Jun 28, 2023
1 parent 1ec020d commit 6ce732e
Showing 1 changed file with 79 additions and 97 deletions.
176 changes: 79 additions & 97 deletions Steam Desktop Authenticator/ConfirmationFormWeb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;
using CefSharp;
using SteamAuth;
using Newtonsoft.Json;
using System.Net.Http;
using System.Drawing.Drawing2D;

namespace Steam_Desktop_Authenticator
{
public partial class ConfirmationFormWeb : Form
{
private readonly HttpClient httpClient;
private string steamCookies;
private SteamGuardAccount steamAccount;

public ConfirmationFormWeb(SteamGuardAccount steamAccount)
Expand All @@ -22,113 +17,94 @@ public ConfirmationFormWeb(SteamGuardAccount steamAccount)
this.steamAccount = steamAccount;
this.Text = String.Format("Trade Confirmations - {0}", steamAccount.AccountName);

CefSettings settings = new CefSettings();
settings.PersistSessionCookies = false;
settings.Locale = "en-US";
settings.UserAgent = "Mozilla/5.0 (Linux; U; Android 4.1.1; en-us; Google Nexus 4 - 4.1.1 - API 16 - 768x1280 Build/JRO03S) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30";
steamCookies = String.Format("mobileClientVersion=0 (2.1.3); mobileClient=android; steamid={0}; steamLoginSecure={1}; Steam_Language=english; dob=;", steamAccount.Session.SteamID.ToString(), steamAccount.Session.SteamLoginSecure);

if (!Cef.IsInitialized)
{
Cef.Initialize(settings);
}

HttpClientHandler handler = new HttpClientHandler();
handler.UseCookies = settings.PersistSessionCookies;

httpClient = new HttpClient(handler);
httpClient.DefaultRequestHeaders.Add("Cookie", steamCookies);
httpClient.DefaultRequestHeaders.Add("User-Agent", settings.UserAgent);

LoadData();
}
private async Task LoadData()
{
this.splitContainer1.Panel2.Controls.Clear();

if (steamAccount.Session.IsSessionExpired())
{
MessageBox.Show("Your session has expired. Use the login again button under the selected account menu.", "Trade Confirmations", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Close();
return;
}

try
{
var httpResponse = await httpClient.GetStringAsync(steamAccount.GenerateConfirmationURL());
var confirmations = await steamAccount.FetchConfirmationsAsync();

var response = JsonConvert.DeserializeObject<ConfirmationsResponse>(httpResponse);
if (confirmations == null || confirmations.Length == 0)
{
Label errorLabel = new Label() { Text = "Nothing to confirm/cancel", AutoSize = true, ForeColor = Color.Black, Location = new Point(150, 20) };
this.splitContainer1.Panel2.Controls.Add(errorLabel);
}

if (response.Success)
foreach (var confirmation in confirmations)
{
if(response.Confirmations != null && response.Confirmations.Length > 0)
Panel panel = new Panel() { Dock = DockStyle.Top, Height = 120 };
panel.Paint += (s, e) =>
{
foreach (var confirmation in response.Confirmations)
using (LinearGradientBrush brush = new LinearGradientBrush(panel.ClientRectangle, Color.Black, Color.DarkCyan, 90F))
{
Panel panel = new Panel() { Dock = DockStyle.Top, Height = 120 };
panel.Paint += (s, e) =>
{
using (LinearGradientBrush brush = new LinearGradientBrush(panel.ClientRectangle,
Color.Black,
Color.DarkCyan,
90F))
{
e.Graphics.FillRectangle(brush, panel.ClientRectangle);
}
};

PictureBox pictureBox = new PictureBox() { Width = 60, Height = 60, Location = new Point(20, 20), SizeMode = PictureBoxSizeMode.Zoom };
pictureBox.Load(confirmation.Icon);
panel.Controls.Add(pictureBox);

Label nameLabel = new Label() {
Text = $"{confirmation.Headline}\n{confirmation.Creator.ToString()}",
AutoSize = true,
ForeColor = Color.Snow,
Location = new Point(90, 20),
BackColor = Color.Transparent
};
panel.Controls.Add(nameLabel);

ConfirmationButton acceptButton = new ConfirmationButton() {
Text = confirmation.Accept,
Location = new Point(90, 50),
FlatStyle = FlatStyle.Flat,
FlatAppearance = { BorderSize = 0 },
BackColor = Color.Black,
ForeColor = Color.Snow,
Confirmation = confirmation
};
acceptButton.Click += btnAccept_Click;
panel.Controls.Add(acceptButton);

ConfirmationButton cancelButton = new ConfirmationButton() {
Text = confirmation.Cancel,
Location = new Point(180, 50),
FlatStyle = FlatStyle.Flat,
FlatAppearance = { BorderSize = 0 },
BackColor = Color.Black,
ForeColor = Color.Snow,
Confirmation = confirmation
};
cancelButton.Click += btnCancel_Click;
panel.Controls.Add(cancelButton);

Label summaryLabel = new Label() {
Text = String.Join("\n", confirmation.Summary),
AutoSize = true,
ForeColor = Color.Snow, Location = new Point(90, 80),
BackColor = Color.Transparent
};
panel.Controls.Add(summaryLabel);

this.splitContainer1.Panel2.Controls.Add(panel);
e.Graphics.FillRectangle(brush, panel.ClientRectangle);
}
}
else
};

PictureBox pictureBox = new PictureBox() { Width = 60, Height = 60, Location = new Point(20, 20), SizeMode = PictureBoxSizeMode.Zoom };
pictureBox.Load(confirmation.Icon);
panel.Controls.Add(pictureBox);

Label nameLabel = new Label()
{
Label errorLabel = new Label() { Text = "Nothing to confirm/cancel", AutoSize = true, ForeColor = Color.Black, Location = new Point(150, 20) };
this.splitContainer1.Panel2.Controls.Add(errorLabel);
}
}
else
{
Label errorLabel = new Label() { Text = "Your steam credentials probably expired, try to reauthenticate.", AutoSize = true, ForeColor = Color.Red, Location = new Point(20, 20) };
this.splitContainer1.Panel2.Controls.Add(errorLabel);
Text = $"{confirmation.Headline}\n{confirmation.Creator.ToString()}",
AutoSize = true,
ForeColor = Color.Snow,
Location = new Point(90, 20),
BackColor = Color.Transparent
};
panel.Controls.Add(nameLabel);

ConfirmationButton acceptButton = new ConfirmationButton()
{
Text = confirmation.Accept,
Location = new Point(90, 50),
FlatStyle = FlatStyle.Flat,
FlatAppearance = { BorderSize = 0 },
BackColor = Color.Black,
ForeColor = Color.Snow,
Confirmation = confirmation
};
acceptButton.Click += btnAccept_Click;
panel.Controls.Add(acceptButton);

ConfirmationButton cancelButton = new ConfirmationButton()
{
Text = confirmation.Cancel,
Location = new Point(180, 50),
FlatStyle = FlatStyle.Flat,
FlatAppearance = { BorderSize = 0 },
BackColor = Color.Black,
ForeColor = Color.Snow,
Confirmation = confirmation
};
cancelButton.Click += btnCancel_Click;
panel.Controls.Add(cancelButton);

Label summaryLabel = new Label()
{
Text = String.Join("\n", confirmation.Summary),
AutoSize = true,
ForeColor = Color.Snow,
Location = new Point(90, 80),
BackColor = Color.Transparent
};
panel.Controls.Add(summaryLabel);

this.splitContainer1.Panel2.Controls.Add(panel);
}
}catch(Exception ex)
}
catch (Exception ex)
{
Label errorLabel = new Label() { Text = "Something went wrong: " + ex.Message, AutoSize = true, ForeColor = Color.Red, Location = new Point(20, 20) };
this.splitContainer1.Panel2.Controls.Add(errorLabel);
Expand Down Expand Up @@ -156,7 +132,13 @@ private async void btnCancel_Click(object sender, EventArgs e)

private async void btnRefresh_Click(object sender, EventArgs e)
{
this.btnRefresh.Enabled = false;
this.btnRefresh.Text = "Refreshing...";

await this.LoadData();

this.btnRefresh.Enabled = true;
this.btnRefresh.Text = "Refresh";
}
}
}

0 comments on commit 6ce732e

Please sign in to comment.