Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of signing in via QR code #1087

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion Steam Desktop Authenticator/MainForm.Designer.cs

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

77 changes: 76 additions & 1 deletion Steam Desktop Authenticator/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions Steam Desktop Authenticator/Manifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ManifestEntry> Entries { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,7 @@
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="SteamKit2" Version="3.0.0-Beta.4" />
<PackageReference Include="ZXing.Net" Version="0.16.9" />
<PackageReference Include="ZXing.Net.Bindings.Windows.Compatibility" Version="0.16.12" />
</ItemGroup>
</Project>