From 27c147c8bc5a7898ae410550944143d01d76ec0d Mon Sep 17 00:00:00 2001 From: Shark191 Date: Sat, 2 Nov 2024 16:08:36 +0700 Subject: [PATCH] Implementation of signing in via QR code --- .../MainForm.Designer.cs | 17 +++- Steam Desktop Authenticator/MainForm.cs | 77 ++++++++++++++++++- Steam Desktop Authenticator/Manifest.cs | 3 + .../Steam Desktop Authenticator.csproj | 2 + 4 files changed, 97 insertions(+), 2 deletions(-) diff --git a/Steam Desktop Authenticator/MainForm.Designer.cs b/Steam Desktop Authenticator/MainForm.Designer.cs index 4173dfe1..c0765bc7 100644 --- a/Steam Desktop Authenticator/MainForm.Designer.cs +++ b/Steam Desktop Authenticator/MainForm.Designer.cs @@ -39,6 +39,7 @@ private void InitializeComponent() this.listAccounts = new System.Windows.Forms.ListBox(); this.timerSteamGuard = new System.Windows.Forms.Timer(this.components); this.btnTradeConfirmations = new System.Windows.Forms.Button(); + this.btnLoginViaQr = new System.Windows.Forms.Button(); this.btnManageEncryption = new System.Windows.Forms.Button(); this.groupAccount = new System.Windows.Forms.GroupBox(); this.labelVersion = new System.Windows.Forms.Label(); @@ -159,12 +160,24 @@ private void InitializeComponent() this.btnTradeConfirmations.Enabled = false; this.btnTradeConfirmations.Location = new System.Drawing.Point(6, 19); this.btnTradeConfirmations.Name = "btnTradeConfirmations"; - this.btnTradeConfirmations.Size = new System.Drawing.Size(298, 31); + this.btnTradeConfirmations.Size = new System.Drawing.Size(145, 31); this.btnTradeConfirmations.TabIndex = 4; this.btnTradeConfirmations.Text = "View Confirmations"; this.btnTradeConfirmations.UseVisualStyleBackColor = true; this.btnTradeConfirmations.Click += new System.EventHandler(this.btnTradeConfirmations_Click); // + // btnLoginViaQr + // + this.btnLoginViaQr.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right))); + this.btnLoginViaQr.Enabled = false; + this.btnLoginViaQr.Location = new System.Drawing.Point(159, 19); + this.btnLoginViaQr.Name = "btnLoginViaQr"; + this.btnLoginViaQr.Size = new System.Drawing.Size(145, 31); + this.btnLoginViaQr.TabIndex = 4; + this.btnLoginViaQr.Text = "Login via QR"; + this.btnLoginViaQr.UseVisualStyleBackColor = true; + this.btnLoginViaQr.Click += new System.EventHandler(this.btnLoginViaQr_Click); + // // btnManageEncryption // this.btnManageEncryption.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); @@ -181,6 +194,7 @@ private void InitializeComponent() this.groupAccount.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.groupAccount.Controls.Add(this.btnTradeConfirmations); + this.groupAccount.Controls.Add(this.btnLoginViaQr); this.groupAccount.Location = new System.Drawing.Point(12, 155); this.groupAccount.Name = "groupAccount"; this.groupAccount.Size = new System.Drawing.Size(310, 56); @@ -472,6 +486,7 @@ private void InitializeComponent() private System.Windows.Forms.ListBox listAccounts; private System.Windows.Forms.Timer timerSteamGuard; private System.Windows.Forms.Button btnTradeConfirmations; + private System.Windows.Forms.Button btnLoginViaQr; private System.Windows.Forms.Button btnManageEncryption; private System.Windows.Forms.GroupBox groupAccount; private System.Windows.Forms.Label labelVersion; diff --git a/Steam Desktop Authenticator/MainForm.cs b/Steam Desktop Authenticator/MainForm.cs index 85e98309..13b12701 100644 --- a/Steam Desktop Authenticator/MainForm.cs +++ b/Steam Desktop Authenticator/MainForm.cs @@ -10,6 +10,13 @@ using System.Drawing; using System.Linq; +using ZXing.QrCode; +using System.Runtime.InteropServices; +using ZXing.Common; +using ZXing; +using ZXing.Windows.Compatibility; +using System.Threading.Tasks; + namespace Steam_Desktop_Authenticator { public partial class MainForm : Form @@ -25,6 +32,13 @@ public partial class MainForm : Form private string passKey = null; private bool startSilent = false; + const int VK_RCONTROL = 0xA3; + [DllImport("user32.dll")] + private static extern bool GetCursorPos(out Point lpPoint); + + [DllImport("user32.dll", CharSet = CharSet.Auto)] + private static extern short GetAsyncKeyState(int vKey); + // Forms private TradePopupForm popupFrm = new TradePopupForm(); @@ -124,6 +138,67 @@ private void btnSteamLogin_Click(object sender, EventArgs e) this.loadAccountsList(); } + static async Task WaitForLeftAltKeyPress() + { + while (true) + { + if ((GetAsyncKeyState(VK_RCONTROL) & 0x8000) != 0) + break; + + await Task.Delay(100); + } + } + + private async void btnLoginViaQr_Click(object sender, EventArgs e) + { + if (currentAccount == null) return; + + this.btnLoginViaQr.Enabled = false; + if (this.manifest.FirstQR) + { + MessageBox.Show("Move your cursor to Steam QR code and press RIGHT CTRL to sign in", "How to use?", MessageBoxButtons.OK, MessageBoxIcon.Information); + this.manifest.FirstQR = false; + this.manifest.Save(); + } + + await WaitForLeftAltKeyPress(); + this.btnLoginViaQr.Enabled = true; + + var reader = new QRCodeReader(); + GetCursorPos(out Point cursorPos); + int scanWidth = 500; + int scanHeight = 500; + + using (Bitmap bitmap = new Bitmap(scanWidth, scanHeight)) + { + using (Graphics g = Graphics.FromImage(bitmap)) + { + g.CopyFromScreen(cursorPos.X - scanWidth / 2, cursorPos.Y - scanHeight / 2, 0, 0, bitmap.Size); + } + + var luminanceSource = new BitmapLuminanceSource(bitmap); + var binaryBitmap = new BinaryBitmap(new HybridBinarizer(luminanceSource)); + var result = reader.decode(binaryBitmap); + + if (result == null) + return; + + string idOfQR = result.Text.Split("/")[5] ?? null; + if (idOfQR == null) + { + MessageBox.Show("Can't get ID of QR code. You need to position your cursor at the center of the QR code on the Steam login page. If you have the same error again, this method might be deprecated.", "Wrong QR code.", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + bool success = await currentAccount.SignInViaQR(idOfQR); + if (!success) + MessageBox.Show("Can't log in to account. Your authenticator is not valid or try again later.", "Something wen't wrong!", MessageBoxButtons.OK, MessageBoxIcon.Error); + + + + } + } + private void btnTradeConfirmations_Click(object sender, EventArgs e) { if (currentAccount == null) return; @@ -576,7 +651,7 @@ private void loadAccountsList() listAccounts.Sorted = true; trayAccountList.Sorted = true; } - menuDeactivateAuthenticator.Enabled = btnTradeConfirmations.Enabled = allAccounts.Length > 0; + menuDeactivateAuthenticator.Enabled = btnTradeConfirmations.Enabled = btnLoginViaQr.Enabled = allAccounts.Length > 0; } private void listAccounts_KeyDown(object sender, KeyEventArgs e) diff --git a/Steam Desktop Authenticator/Manifest.cs b/Steam Desktop Authenticator/Manifest.cs index 69114849..54a55dc9 100644 --- a/Steam Desktop Authenticator/Manifest.cs +++ b/Steam Desktop Authenticator/Manifest.cs @@ -18,6 +18,9 @@ public class Manifest [JsonProperty("first_run")] public bool FirstRun { get; set; } = true; + [JsonProperty("first_qr")] + public bool FirstQR { get; set; } = true; + [JsonProperty("entries")] public List Entries { get; set; } diff --git a/Steam Desktop Authenticator/Steam Desktop Authenticator.csproj b/Steam Desktop Authenticator/Steam Desktop Authenticator.csproj index c29a6ca4..caea948b 100644 --- a/Steam Desktop Authenticator/Steam Desktop Authenticator.csproj +++ b/Steam Desktop Authenticator/Steam Desktop Authenticator.csproj @@ -35,5 +35,7 @@ + + \ No newline at end of file