LoginForm.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. using System;
  2. using System.Threading.Tasks;
  3. using System.Windows.Forms;
  4. using SteamAuth;
  5. using SteamKit2;
  6. using SteamKit2.Authentication;
  7. using SteamKit2.Internal;
  8. namespace Steam_Desktop_Authenticator
  9. {
  10. public partial class LoginForm : Form
  11. {
  12. public SteamGuardAccount account;
  13. public LoginType LoginReason;
  14. public SessionData Session;
  15. public LoginForm(LoginType loginReason = LoginType.Initial, SteamGuardAccount account = null)
  16. {
  17. InitializeComponent();
  18. this.LoginReason = loginReason;
  19. this.account = account;
  20. try
  21. {
  22. if (this.LoginReason != LoginType.Initial)
  23. {
  24. txtUsername.Text = account.AccountName;
  25. txtUsername.Enabled = false;
  26. }
  27. if (this.LoginReason == LoginType.Refresh)
  28. {
  29. labelLoginExplanation.Text = "Your Steam credentials have expired. For trade and market confirmations to work properly, please login again.";
  30. }
  31. else if (this.LoginReason == LoginType.Import)
  32. {
  33. labelLoginExplanation.Text = "Please login to your Steam account import it.";
  34. }
  35. }
  36. catch (Exception)
  37. {
  38. MessageBox.Show("Failed to find your account. Try closing and re-opening SDA.", "Login Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
  39. this.Close();
  40. }
  41. }
  42. public void SetUsername(string username)
  43. {
  44. txtUsername.Text = username;
  45. }
  46. public string FilterPhoneNumber(string phoneNumber)
  47. {
  48. return phoneNumber.Replace("-", "").Replace("(", "").Replace(")", "");
  49. }
  50. public bool PhoneNumberOkay(string phoneNumber)
  51. {
  52. if (phoneNumber == null || phoneNumber.Length == 0) return false;
  53. if (phoneNumber[0] != '+') return false;
  54. return true;
  55. }
  56. private void ResetLoginButton()
  57. {
  58. btnSteamLogin.Enabled = true;
  59. btnSteamLogin.Text = "Login";
  60. }
  61. private async void btnSteamLogin_Click(object sender, EventArgs e)
  62. {
  63. // Disable button while we login
  64. btnSteamLogin.Enabled = false;
  65. btnSteamLogin.Text = "Logging in...";
  66. string username = txtUsername.Text;
  67. string password = txtPassword.Text;
  68. // Start a new SteamClient instance
  69. SteamClient steamClient = new SteamClient();
  70. // Connect to Steam
  71. steamClient.Connect();
  72. // Really basic way to wait until Steam is connected
  73. while (!steamClient.IsConnected)
  74. await Task.Delay(500);
  75. // Create a new auth session
  76. CredentialsAuthSession authSession;
  77. try
  78. {
  79. authSession = await steamClient.Authentication.BeginAuthSessionViaCredentialsAsync(new AuthSessionDetails
  80. {
  81. Username = username,
  82. Password = password,
  83. IsPersistentSession = false,
  84. PlatformType = EAuthTokenPlatformType.k_EAuthTokenPlatformType_MobileApp,
  85. ClientOSType = EOSType.Android9,
  86. Authenticator = new UserFormAuthenticator(this.account),
  87. });
  88. }
  89. catch (Exception ex)
  90. {
  91. MessageBox.Show(ex.Message, "Steam Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  92. this.Close();
  93. return;
  94. }
  95. // Starting polling Steam for authentication response
  96. AuthPollResult pollResponse;
  97. try
  98. {
  99. pollResponse = await authSession.PollingWaitForResultAsync();
  100. }
  101. catch (Exception ex)
  102. {
  103. MessageBox.Show(ex.Message, "Steam Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  104. this.Close();
  105. return;
  106. }
  107. // Build a SessionData object
  108. SessionData sessionData = new SessionData()
  109. {
  110. SteamID = authSession.SteamID.ConvertToUInt64(),
  111. AccessToken = pollResponse.AccessToken,
  112. RefreshToken = pollResponse.RefreshToken,
  113. };
  114. //Login succeeded
  115. this.Session = sessionData;
  116. // If we're only logging in for an account import, stop here
  117. if (LoginReason == LoginType.Import)
  118. {
  119. this.Close();
  120. return;
  121. }
  122. // If we're only logging in for a session refresh then save it and exit
  123. if (LoginReason == LoginType.Refresh)
  124. {
  125. Manifest man = Manifest.GetManifest();
  126. account.FullyEnrolled = true;
  127. account.Session = sessionData;
  128. HandleManifest(man, true);
  129. this.Close();
  130. return;
  131. }
  132. // Show a dialog to make sure they really want to add their authenticator
  133. var result = MessageBox.Show("Steam account login succeeded. Press OK to continue adding SDA as your authenticator.", "Steam Login", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
  134. if (result == DialogResult.Cancel)
  135. {
  136. MessageBox.Show("Adding authenticator aborted.", "Steam Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
  137. ResetLoginButton();
  138. return;
  139. }
  140. // Begin linking mobile authenticator
  141. AuthenticatorLinker linker = new AuthenticatorLinker(sessionData);
  142. AuthenticatorLinker.LinkResult linkResponse = AuthenticatorLinker.LinkResult.GeneralFailure;
  143. while (linkResponse != AuthenticatorLinker.LinkResult.AwaitingFinalization)
  144. {
  145. try
  146. {
  147. linkResponse = await linker.AddAuthenticator();
  148. }
  149. catch (Exception ex)
  150. {
  151. MessageBox.Show("Error adding your authenticator: " + ex.Message, "Steam Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
  152. ResetLoginButton();
  153. return;
  154. }
  155. switch (linkResponse)
  156. {
  157. case AuthenticatorLinker.LinkResult.MustProvidePhoneNumber:
  158. // Show the phone input form
  159. PhoneInputForm phoneInputForm = new PhoneInputForm(account);
  160. phoneInputForm.ShowDialog();
  161. if (phoneInputForm.Canceled)
  162. {
  163. this.Close();
  164. return;
  165. }
  166. linker.PhoneNumber = phoneInputForm.PhoneNumber;
  167. linker.PhoneCountryCode = phoneInputForm.CountryCode;
  168. break;
  169. case AuthenticatorLinker.LinkResult.AuthenticatorPresent:
  170. MessageBox.Show("This account already has an authenticator linked. You must remove that authenticator to add SDA as your authenticator.", "Steam Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
  171. this.Close();
  172. return;
  173. case AuthenticatorLinker.LinkResult.FailureAddingPhone:
  174. MessageBox.Show("Failed to add your phone number. Please try again or use a different phone number.", "Steam Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
  175. linker.PhoneNumber = null;
  176. break;
  177. case AuthenticatorLinker.LinkResult.MustRemovePhoneNumber:
  178. linker.PhoneNumber = null;
  179. break;
  180. case AuthenticatorLinker.LinkResult.MustConfirmEmail:
  181. MessageBox.Show("Please check your email, and click the link Steam sent you before continuing.", "Steam Login", MessageBoxButtons.OK, MessageBoxIcon.Information);
  182. break;
  183. case AuthenticatorLinker.LinkResult.GeneralFailure:
  184. MessageBox.Show("Error adding your authenticator.", "Steam Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  185. this.Close();
  186. return;
  187. }
  188. } // End while loop checking for AwaitingFinalization
  189. Manifest manifest = Manifest.GetManifest();
  190. string passKey = null;
  191. if (manifest.Entries.Count == 0)
  192. {
  193. passKey = manifest.PromptSetupPassKey("Please enter an encryption passkey. Leave blank or hit cancel to not encrypt (VERY INSECURE).");
  194. }
  195. else if (manifest.Entries.Count > 0 && manifest.Encrypted)
  196. {
  197. bool passKeyValid = false;
  198. while (!passKeyValid)
  199. {
  200. InputForm passKeyForm = new InputForm("Please enter your current encryption passkey.");
  201. passKeyForm.ShowDialog();
  202. if (!passKeyForm.Canceled)
  203. {
  204. passKey = passKeyForm.txtBox.Text;
  205. passKeyValid = manifest.VerifyPasskey(passKey);
  206. if (!passKeyValid)
  207. {
  208. MessageBox.Show("That passkey is invalid. Please enter the same passkey you used for your other accounts.");
  209. }
  210. }
  211. else
  212. {
  213. this.Close();
  214. return;
  215. }
  216. }
  217. }
  218. //Save the file immediately; losing this would be bad.
  219. if (!manifest.SaveAccount(linker.LinkedAccount, passKey != null, passKey))
  220. {
  221. manifest.RemoveAccount(linker.LinkedAccount);
  222. MessageBox.Show("Unable to save mobile authenticator file. The mobile authenticator has not been linked.");
  223. this.Close();
  224. return;
  225. }
  226. MessageBox.Show("The Mobile Authenticator has not yet been linked. Before finalizing the authenticator, please write down your revocation code: " + linker.LinkedAccount.RevocationCode);
  227. AuthenticatorLinker.FinalizeResult finalizeResponse = AuthenticatorLinker.FinalizeResult.GeneralFailure;
  228. while (finalizeResponse != AuthenticatorLinker.FinalizeResult.Success)
  229. {
  230. InputForm smsCodeForm = new InputForm("Please input the SMS code sent to your phone.");
  231. smsCodeForm.ShowDialog();
  232. if (smsCodeForm.Canceled)
  233. {
  234. manifest.RemoveAccount(linker.LinkedAccount);
  235. this.Close();
  236. return;
  237. }
  238. InputForm confirmRevocationCode = new InputForm("Please enter your revocation code to ensure you've saved it.");
  239. confirmRevocationCode.ShowDialog();
  240. if (confirmRevocationCode.txtBox.Text.ToUpper() != linker.LinkedAccount.RevocationCode)
  241. {
  242. MessageBox.Show("Revocation code incorrect; the authenticator has not been linked.");
  243. manifest.RemoveAccount(linker.LinkedAccount);
  244. this.Close();
  245. return;
  246. }
  247. string smsCode = smsCodeForm.txtBox.Text;
  248. finalizeResponse = await linker.FinalizeAddAuthenticator(smsCode);
  249. switch (finalizeResponse)
  250. {
  251. case AuthenticatorLinker.FinalizeResult.BadSMSCode:
  252. continue;
  253. case AuthenticatorLinker.FinalizeResult.UnableToGenerateCorrectCodes:
  254. MessageBox.Show("Unable to generate the proper codes to finalize this authenticator. The authenticator should not have been linked. In the off-chance it was, please write down your revocation code, as this is the last chance to see it: " + linker.LinkedAccount.RevocationCode);
  255. manifest.RemoveAccount(linker.LinkedAccount);
  256. this.Close();
  257. return;
  258. case AuthenticatorLinker.FinalizeResult.GeneralFailure:
  259. MessageBox.Show("Unable to finalize this authenticator. The authenticator should not have been linked. In the off-chance it was, please write down your revocation code, as this is the last chance to see it: " + linker.LinkedAccount.RevocationCode);
  260. manifest.RemoveAccount(linker.LinkedAccount);
  261. this.Close();
  262. return;
  263. }
  264. }
  265. //Linked, finally. Re-save with FullyEnrolled property.
  266. manifest.SaveAccount(linker.LinkedAccount, passKey != null, passKey);
  267. MessageBox.Show("Mobile authenticator successfully linked. Please write down your revocation code: " + linker.LinkedAccount.RevocationCode);
  268. this.Close();
  269. }
  270. private void HandleManifest(Manifest man, bool IsRefreshing = false)
  271. {
  272. string passKey = null;
  273. if (man.Entries.Count == 0)
  274. {
  275. passKey = man.PromptSetupPassKey("Please enter an encryption passkey. Leave blank or hit cancel to not encrypt (VERY INSECURE).");
  276. }
  277. else if (man.Entries.Count > 0 && man.Encrypted)
  278. {
  279. bool passKeyValid = false;
  280. while (!passKeyValid)
  281. {
  282. InputForm passKeyForm = new InputForm("Please enter your current encryption passkey.");
  283. passKeyForm.ShowDialog();
  284. if (!passKeyForm.Canceled)
  285. {
  286. passKey = passKeyForm.txtBox.Text;
  287. passKeyValid = man.VerifyPasskey(passKey);
  288. if (!passKeyValid)
  289. {
  290. MessageBox.Show("That passkey is invalid. Please enter the same passkey you used for your other accounts.", "Steam Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
  291. }
  292. }
  293. else
  294. {
  295. this.Close();
  296. return;
  297. }
  298. }
  299. }
  300. man.SaveAccount(account, passKey != null, passKey);
  301. if (IsRefreshing)
  302. {
  303. MessageBox.Show("Your session was refreshed.", "Steam Login", MessageBoxButtons.OK, MessageBoxIcon.Information);
  304. }
  305. else
  306. {
  307. MessageBox.Show("Mobile authenticator successfully linked. Please write down your revocation code: " + account.RevocationCode, "Steam Login", MessageBoxButtons.OK);
  308. }
  309. this.Close();
  310. }
  311. private void LoginForm_Load(object sender, EventArgs e)
  312. {
  313. if (account != null && account.AccountName != null)
  314. {
  315. txtUsername.Text = account.AccountName;
  316. }
  317. }
  318. public enum LoginType
  319. {
  320. Initial,
  321. Refresh,
  322. Import
  323. }
  324. }
  325. }