Skip to content

Commit cd62298

Browse files
committed
SteamAuth sync
1 parent 8251274 commit cd62298

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

SteamAuth/AuthenticatorLinker.cs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class AuthenticatorLinker
4343
public AuthenticatorLinker(SessionData session)
4444
{
4545
this._session = session;
46-
this.DeviceID = _generateDeviceID();
46+
this.DeviceID = GenerateDeviceID();
4747

4848
this._cookies = new CookieContainer();
4949
session.AddCookies(_cookies);
@@ -76,7 +76,17 @@ public LinkResult AddAuthenticator()
7676
if (response == null) return LinkResult.GeneralFailure;
7777

7878
var addAuthenticatorResponse = JsonConvert.DeserializeObject<AddAuthenticatorResponse>(response);
79-
if (addAuthenticatorResponse == null || addAuthenticatorResponse.Response == null || addAuthenticatorResponse.Response.Status != 1)
79+
if (addAuthenticatorResponse == null || addAuthenticatorResponse.Response == null)
80+
{
81+
return LinkResult.GeneralFailure;
82+
}
83+
84+
if (addAuthenticatorResponse.Response.Status == 29)
85+
{
86+
return LinkResult.AuthenticatorPresent;
87+
}
88+
89+
if (addAuthenticatorResponse.Response.Status != 1)
8090
{
8191
return LinkResult.GeneralFailure;
8292
}
@@ -181,7 +191,8 @@ public enum LinkResult
181191
MustProvidePhoneNumber, //No phone number on the account
182192
MustRemovePhoneNumber, //A phone number is already on the account
183193
AwaitingFinalization, //Must provide an SMS code
184-
GeneralFailure //General failure (really now!)
194+
GeneralFailure, //General failure (really now!)
195+
AuthenticatorPresent
185196
}
186197

187198
public enum FinalizeResult
@@ -231,7 +242,7 @@ private class AddPhoneResponse
231242
public bool Success { get; set; }
232243
}
233244

234-
private string _generateDeviceID()
245+
public static string GenerateDeviceID()
235246
{
236247
using (var sha1 = new SHA1Managed())
237248
{
@@ -240,8 +251,27 @@ private string _generateDeviceID()
240251
secureRandom.GetBytes(randomBytes);
241252

242253
byte[] hashedBytes = sha1.ComputeHash(randomBytes);
243-
return "android:" + BitConverter.ToString(hashedBytes).Replace("-", "");
254+
string random32 = BitConverter.ToString(hashedBytes).Replace("-", "").Substring(0, 32).ToLower();
255+
256+
return "android:" + SplitOnRatios(random32, new[] { 8, 4, 4, 4, 12 }, "-");
244257
}
245258
}
259+
260+
private static string SplitOnRatios(string str, int[] ratios, string intermediate)
261+
{
262+
string result = "";
263+
264+
int pos = 0;
265+
for (int index = 0; index < ratios.Length; index++)
266+
{
267+
result += str.Substring(pos, ratios[index]);
268+
pos = ratios[index];
269+
270+
if (index < ratios.Length - 1)
271+
result += intermediate;
272+
}
273+
274+
return result;
275+
}
246276
}
247277
}

SteamAuth/SteamGuardAccount.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ public class SteamGuardAccount
5454

5555
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 };
5656

57-
public bool DeactivateAuthenticator()
57+
public bool DeactivateAuthenticator(int scheme = 2)
5858
{
5959
var postData = new NameValueCollection();
6060
postData.Add("steamid", this.Session.SteamID.ToString());
61-
postData.Add("steamguard_scheme", "2");
61+
postData.Add("steamguard_scheme", scheme.ToString());
6262
postData.Add("revocation_code", this.RevocationCode);
6363
postData.Add("access_token", this.Session.OAuthToken);
6464

@@ -241,6 +241,9 @@ public string GenerateConfirmationURL(string tag = "conf")
241241

242242
public string GenerateConfirmationQueryParams(string tag)
243243
{
244+
if (String.IsNullOrEmpty(DeviceID))
245+
DeviceID = AuthenticatorLinker.GenerateDeviceID();
246+
244247
long time = TimeAligner.GetSteamTime();
245248
return "p=" + this.DeviceID + "&a=" + this.Session.SteamID.ToString() + "&k=" + _generateConfirmationHashForTime(time, tag) + "&t=" + time + "&m=android&tag=" + tag;
246249
}

SteamAuth/UserLogin.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public LoginResult DoLogin()
5959

6060
postData.Add("username", this.Username);
6161
response = SteamWeb.MobileLoginRequest(APIEndpoints.COMMUNITY_BASE + "/login/getrsakey", "POST", postData, cookies);
62-
if (response == null) return LoginResult.GeneralFailure;
62+
if (response == null || response.Contains("<BODY>\nAn error occurred while processing your request.")) return LoginResult.GeneralFailure;
6363

6464
var rsaResponse = JsonConvert.DeserializeObject<RSAResponse>(response);
6565

@@ -106,6 +106,11 @@ public LoginResult DoLogin()
106106

107107
var loginResponse = JsonConvert.DeserializeObject<LoginResponse>(response);
108108

109+
if (loginResponse.Message != null && loginResponse.Message.Contains("Incorrect login"))
110+
{
111+
return LoginResult.BadCredentials;
112+
}
113+
109114
if (loginResponse.CaptchaNeeded)
110115
{
111116
this.RequiresCaptcha = true;
@@ -126,6 +131,11 @@ public LoginResult DoLogin()
126131
return LoginResult.Need2FA;
127132
}
128133

134+
if (loginResponse.Message != null && loginResponse.Message.Contains("too many login failures"))
135+
{
136+
return LoginResult.TooManyFailedLogins;
137+
}
138+
129139
if (loginResponse.OAuthData == null || loginResponse.OAuthData.OAuthToken == null || loginResponse.OAuthData.OAuthToken.Length == 0)
130140
{
131141
return LoginResult.GeneralFailure;
@@ -189,6 +199,9 @@ public OAuth OAuthData
189199
[JsonProperty("requires_twofactor")]
190200
public bool TwoFactorNeeded { get; set; }
191201

202+
[JsonProperty("message")]
203+
public string Message { get; set; }
204+
192205
internal class OAuth
193206
{
194207
[JsonProperty("steamid")]
@@ -236,5 +249,6 @@ public enum LoginResult
236249
NeedCaptcha,
237250
Need2FA,
238251
NeedEmail,
252+
TooManyFailedLogins,
239253
}
240254
}

0 commit comments

Comments
 (0)