diff --git a/SteamAuth/AuthenticatorLinker.cs b/SteamAuth/AuthenticatorLinker.cs index 39f1d99a9..ec2fd994a 100644 --- a/SteamAuth/AuthenticatorLinker.cs +++ b/SteamAuth/AuthenticatorLinker.cs @@ -43,7 +43,7 @@ namespace SteamAuth public AuthenticatorLinker(SessionData session) { this._session = session; - this.DeviceID = _generateDeviceID(); + this.DeviceID = GenerateDeviceID(); this._cookies = new CookieContainer(); session.AddCookies(_cookies); @@ -76,7 +76,17 @@ namespace SteamAuth if (response == null) return LinkResult.GeneralFailure; var addAuthenticatorResponse = JsonConvert.DeserializeObject(response); - if (addAuthenticatorResponse == null || addAuthenticatorResponse.Response == null || addAuthenticatorResponse.Response.Status != 1) + if (addAuthenticatorResponse == null || addAuthenticatorResponse.Response == null) + { + return LinkResult.GeneralFailure; + } + + if (addAuthenticatorResponse.Response.Status == 29) + { + return LinkResult.AuthenticatorPresent; + } + + if (addAuthenticatorResponse.Response.Status != 1) { return LinkResult.GeneralFailure; } @@ -181,7 +191,8 @@ namespace SteamAuth MustProvidePhoneNumber, //No phone number on the account MustRemovePhoneNumber, //A phone number is already on the account AwaitingFinalization, //Must provide an SMS code - GeneralFailure //General failure (really now!) + GeneralFailure, //General failure (really now!) + AuthenticatorPresent } public enum FinalizeResult @@ -231,7 +242,7 @@ namespace SteamAuth public bool Success { get; set; } } - private string _generateDeviceID() + public static string GenerateDeviceID() { using (var sha1 = new SHA1Managed()) { @@ -240,8 +251,27 @@ namespace SteamAuth secureRandom.GetBytes(randomBytes); byte[] hashedBytes = sha1.ComputeHash(randomBytes); - return "android:" + BitConverter.ToString(hashedBytes).Replace("-", ""); + string random32 = BitConverter.ToString(hashedBytes).Replace("-", "").Substring(0, 32).ToLower(); + + return "android:" + SplitOnRatios(random32, new[] { 8, 4, 4, 4, 12 }, "-"); } } + + private static string SplitOnRatios(string str, int[] ratios, string intermediate) + { + string result = ""; + + int pos = 0; + for (int index = 0; index < ratios.Length; index++) + { + result += str.Substring(pos, ratios[index]); + pos = ratios[index]; + + if (index < ratios.Length - 1) + result += intermediate; + } + + return result; + } } } diff --git a/SteamAuth/SteamGuardAccount.cs b/SteamAuth/SteamGuardAccount.cs index 1da6ae69f..abf2abcef 100644 --- a/SteamAuth/SteamGuardAccount.cs +++ b/SteamAuth/SteamGuardAccount.cs @@ -54,11 +54,11 @@ namespace SteamAuth private static byte[] steamGuardCodeTranslations = new byte[] { 50, 51, 52, 53, 54, 55, 56, 57, 66, 67, 68, 70, 71, 72, 74, 75, 77, 78, 80, 81, 82, 84, 86, 87, 88, 89 }; - public bool DeactivateAuthenticator() + public bool DeactivateAuthenticator(int scheme = 2) { var postData = new NameValueCollection(); postData.Add("steamid", this.Session.SteamID.ToString()); - postData.Add("steamguard_scheme", "2"); + postData.Add("steamguard_scheme", scheme.ToString()); postData.Add("revocation_code", this.RevocationCode); postData.Add("access_token", this.Session.OAuthToken); @@ -241,6 +241,9 @@ namespace SteamAuth public string GenerateConfirmationQueryParams(string tag) { + if (String.IsNullOrEmpty(DeviceID)) + DeviceID = AuthenticatorLinker.GenerateDeviceID(); + long time = TimeAligner.GetSteamTime(); return "p=" + this.DeviceID + "&a=" + this.Session.SteamID.ToString() + "&k=" + _generateConfirmationHashForTime(time, tag) + "&t=" + time + "&m=android&tag=" + tag; } diff --git a/SteamAuth/UserLogin.cs b/SteamAuth/UserLogin.cs index 2021a621f..1e4ea024b 100644 --- a/SteamAuth/UserLogin.cs +++ b/SteamAuth/UserLogin.cs @@ -59,7 +59,7 @@ namespace SteamAuth postData.Add("username", this.Username); response = SteamWeb.MobileLoginRequest(APIEndpoints.COMMUNITY_BASE + "/login/getrsakey", "POST", postData, cookies); - if (response == null) return LoginResult.GeneralFailure; + if (response == null || response.Contains("\nAn error occurred while processing your request.")) return LoginResult.GeneralFailure; var rsaResponse = JsonConvert.DeserializeObject(response); @@ -106,6 +106,11 @@ namespace SteamAuth var loginResponse = JsonConvert.DeserializeObject(response); + if (loginResponse.Message != null && loginResponse.Message.Contains("Incorrect login")) + { + return LoginResult.BadCredentials; + } + if (loginResponse.CaptchaNeeded) { this.RequiresCaptcha = true; @@ -126,6 +131,11 @@ namespace SteamAuth return LoginResult.Need2FA; } + if (loginResponse.Message != null && loginResponse.Message.Contains("too many login failures")) + { + return LoginResult.TooManyFailedLogins; + } + if (loginResponse.OAuthData == null || loginResponse.OAuthData.OAuthToken == null || loginResponse.OAuthData.OAuthToken.Length == 0) { return LoginResult.GeneralFailure; @@ -189,6 +199,9 @@ namespace SteamAuth [JsonProperty("requires_twofactor")] public bool TwoFactorNeeded { get; set; } + [JsonProperty("message")] + public string Message { get; set; } + internal class OAuth { [JsonProperty("steamid")] @@ -236,5 +249,6 @@ namespace SteamAuth NeedCaptcha, Need2FA, NeedEmail, + TooManyFailedLogins, } }