-
Notifications
You must be signed in to change notification settings - Fork 3
/
AuthenticatorLinker.cs
369 lines (299 loc) · 13.7 KB
/
AuthenticatorLinker.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
using Newtonsoft.Json;
using System;
using System.Collections.Specialized;
using System.Net;
using System.Security.Cryptography;
using System.Threading;
namespace SteamAuth
{
/// <summary>
/// Handles the linking process for a new mobile authenticator.
/// </summary>
public class AuthenticatorLinker
{
/// <summary>
/// Set to register a new phone number when linking. If a phone number is not set on the account, this must be set. If a phone number is set on the account, this must be null.
/// </summary>
public string PhoneNumber = null;
/// <summary>
/// Randomly-generated device ID. Should only be generated once per linker.
/// </summary>
public string DeviceID { get; private set; }
/// <summary>
/// After the initial link step, if successful, this will be the SteamGuard data for the account. PLEASE save this somewhere after generating it; it's vital data.
/// </summary>
public SteamGuardAccount LinkedAccount { get; private set; }
/// <summary>
/// True if the authenticator has been fully finalized.
/// </summary>
public bool Finalized = false;
private SessionData _session;
private CookieContainer _cookies;
private bool confirmationEmailSent = false;
public AuthenticatorLinker(UserLogin userLogin)
{
this._session = userLogin.Session;
this.DeviceID = GenerateDeviceID();
this._cookies = userLogin._cookies;
//session.AddCookies(_cookies);
}
public LinkResult AddAuthenticator()
{
bool hasPhone = _hasPhoneAttached();
/*if (hasPhone && PhoneNumber != null)
return LinkResult.MustRemovePhoneNumber;*/
if (!hasPhone && PhoneNumber == null)
return LinkResult.MustProvidePhoneNumber;
/*if (!hasPhone) {
if (confirmationEmailSent) {
if (!_checkEmailConfirmation()) {
return LinkResult.GeneralFailure;
}
} else if (!_get_phone_number()) {
return LinkResult.GeneralFailure;
} else {
confirmationEmailSent = true;
return LinkResult.MustConfirmEmail;
}
}*/
var postData = new NameValueCollection();
postData.Add("access_token", _session.OAuthToken);
postData.Add("steamid", _session.SteamID.ToString());
postData.Add("authenticator_type", "1");
postData.Add("device_identifier", this.DeviceID);
postData.Add("sms_phone_id", "1");
string response = SteamWeb.MobileLoginRequest(APIEndpoints.STEAMAPI_BASE + "/ITwoFactorService/AddAuthenticator/v0001", "POST", _session.proxy, _session.proxy_type, postData);
if (response == null) return LinkResult.GeneralFailure;
var addAuthenticatorResponse = JsonConvert.DeserializeObject<AddAuthenticatorResponse>(response);
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;
}
this.LinkedAccount = addAuthenticatorResponse.Response;
LinkedAccount.Session = this._session;
LinkedAccount.DeviceID = this.DeviceID;
return LinkResult.AwaitingFinalization;
}
/* public LinkResult AddAuthenticator()
{
bool hasPhone = _hasPhoneAttached();
if (hasPhone && PhoneNumber != null)
return LinkResult.MustRemovePhoneNumber;
if (!hasPhone && PhoneNumber == null)
return LinkResult.MustProvidePhoneNumber;
if (!hasPhone) {
if (confirmationEmailSent) {
if (!_checkEmailConfirmation()) {
return LinkResult.GeneralFailure;
}
} else if (!_get_phone_number()) {
return LinkResult.GeneralFailure;
} else {
confirmationEmailSent = true;
return LinkResult.MustConfirmEmail;
}
}
var postData = new NameValueCollection();
postData.Add("access_token", _session.OAuthToken);
postData.Add("steamid", _session.SteamID.ToString());
postData.Add("authenticator_type", "1");
postData.Add("device_identifier", this.DeviceID);
postData.Add("sms_phone_id", "1");
string response = SteamWeb.MobileLoginRequest(APIEndpoints.STEAMAPI_BASE + "/ITwoFactorService/AddAuthenticator/v0001", "POST", _session.proxy, _session.proxy_type, postData);
if (response == null) return LinkResult.GeneralFailure;
var addAuthenticatorResponse = JsonConvert.DeserializeObject<AddAuthenticatorResponse>(response);
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;
}
this.LinkedAccount = addAuthenticatorResponse.Response;
LinkedAccount.Session = this._session;
LinkedAccount.DeviceID = this.DeviceID;
return LinkResult.AwaitingFinalization;
}
*/
public FinalizeResult FinalizeAddAuthenticator(string smsCode)
{
//The act of checking the SMS code is necessary for Steam to finalize adding the phone number to the account.
//Of course, we only want to check it if we're adding a phone number in the first place...
/*if (!String.IsNullOrEmpty(this.PhoneNumber) && !this._checkSMSCode(smsCode))
{
return FinalizeResult.BadSMSCode;
}*/
var postData = new NameValueCollection();
postData.Add("steamid", _session.SteamID.ToString());
postData.Add("access_token", _session.OAuthToken);
postData.Add("activation_code", smsCode);
int tries = 0;
while (tries <= 30)
{
postData.Set("authenticator_code", LinkedAccount.GenerateSteamGuardCode());
postData.Set("authenticator_time", TimeAligner.GetSteamTime().ToString());
string response = SteamWeb.MobileLoginRequest(APIEndpoints.STEAMAPI_BASE + "/ITwoFactorService/FinalizeAddAuthenticator/v0001", "POST", _session.proxy, _session.proxy_type, postData);
if (response == null) return FinalizeResult.GeneralFailure;
var finalizeResponse = JsonConvert.DeserializeObject<FinalizeAuthenticatorResponse>(response);
if (finalizeResponse == null || finalizeResponse.Response == null)
{
return FinalizeResult.GeneralFailure;
}
if (finalizeResponse.Response.Status == 89)
{
return FinalizeResult.BadSMSCode;
}
if (finalizeResponse.Response.Status == 88)
{
if (tries >= 30)
{
return FinalizeResult.UnableToGenerateCorrectCodes;
}
}
if (!finalizeResponse.Response.Success)
{
return FinalizeResult.GeneralFailure;
}
if (finalizeResponse.Response.WantMore)
{
tries++;
continue;
}
this.LinkedAccount.FullyEnrolled = true;
return FinalizeResult.Success;
}
return FinalizeResult.GeneralFailure;
}
public bool _get_sms_code(string smscode)
{
var postData = new NameValueCollection();
postData.Add("op", "get_sms_code");
postData.Add("input", smscode);
postData.Add("sessionID", _cookies.GetCookies(new Uri("https://store.steampowered.com"))["sessionid"].Value);
postData.Add("confirmed", "1");
postData.Add("checkfortos", "1");
postData.Add("bisediting", "0");
postData.Add("token", "0");
string response = SteamWeb.Request("https://store.steampowered.com/phone/add_ajaxop", "POST", _session.proxy, _session.proxy_type, postData, _cookies);
if (response == null) return false;
var _get_phone_number = JsonConvert.DeserializeObject<get_phone_number>(response);
return _get_phone_number.success;
}
public bool _get_phone_number()
{
var postData = new NameValueCollection();
postData.Add("op", "get_phone_number");
postData.Add("input", PhoneNumber);
postData.Add("sessionID", _cookies.GetCookies(new Uri("https://store.steampowered.com"))["sessionid"].Value);
postData.Add("confirmed", "1");
postData.Add("checkfortos", "1");
postData.Add("bisediting", "0");
postData.Add("token", "0");
string response = SteamWeb.Request("https://store.steampowered.com/phone/add_ajaxop", "POST", _session.proxy, _session.proxy_type, postData, _cookies);
if (response == null) return false;
var _get_phone_number = JsonConvert.DeserializeObject<get_phone_number>(response);
return _get_phone_number.success;
}
public bool _email_verification()
{
var postData = new NameValueCollection();
postData.Add("op", "email_verification");
postData.Add("input", "");
postData.Add("sessionID", _cookies.GetCookies(new Uri("https://store.steampowered.com"))["sessionid"].Value);
postData.Add("confirmed", "1");
postData.Add("checkfortos", "1");
postData.Add("bisediting", "0");
postData.Add("token", "0");
string response = SteamWeb.Request("https://store.steampowered.com/phone/add_ajaxop", "POST", _session.proxy, _session.proxy_type, postData, _cookies);
if (response == null) return false;
var _get_phone_number = JsonConvert.DeserializeObject<get_phone_number>(response);
return _get_phone_number.success;
}
public bool _hasPhoneAttached()
{
var postData = new NameValueCollection();
postData.Add("op", "get_phone_number");
postData.Add("sessionID", _cookies.GetCookies(new Uri("https://store.steampowered.com"))["sessionid"].Value);
string response = SteamWeb.Request("https://store.steampowered.com/phone/add_ajaxop", "POST", _session.proxy, _session.proxy_type, postData, _cookies);
if (response == null) return false;
var _get_phone_number = JsonConvert.DeserializeObject<get_phone_number>(response);
if (_get_phone_number.errorText.Contains("Phone number is invalid"))
return false;
else
return true;
}
public enum LinkResult
{
MustProvidePhoneNumber, //No phone number on the account
MustRemovePhoneNumber, //A phone number is already on the account
MustConfirmEmail, //User need to click link from confirmation email
AwaitingFinalization, //Must provide an SMS code
GeneralFailure, //General failure (really now!)
AuthenticatorPresent
}
public enum FinalizeResult
{
BadSMSCode,
UnableToGenerateCorrectCodes,
Success,
GeneralFailure
}
private class AddAuthenticatorResponse
{
[JsonProperty("response")]
public SteamGuardAccount Response { get; set; }
}
private class FinalizeAuthenticatorResponse
{
[JsonProperty("response")]
public FinalizeAuthenticatorInternalResponse Response { get; set; }
internal class FinalizeAuthenticatorInternalResponse
{
[JsonProperty("status")]
public int Status { get; set; }
[JsonProperty("server_time")]
public long ServerTime { get; set; }
[JsonProperty("want_more")]
public bool WantMore { get; set; }
[JsonProperty("success")]
public bool Success { get; set; }
}
}
private class HasPhoneResponse
{
[JsonProperty("has_phone")]
public bool HasPhone { get; set; }
}
private class AddPhoneResponse
{
[JsonProperty("success")]
public bool Success { get; set; }
}
public class get_phone_number
{
public bool success { get; set; }
public bool showResend { get; set; }
public string state { get; set; }
public string errorText { get; set; }
public string token { get; set; }
public string phoneNumber { get; set; }
}
public static string GenerateDeviceID()
{
return "android:" + Guid.NewGuid().ToString();
}
}
}