forked from Aida-Enna/PSO2JP_Token_Generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
293 lines (276 loc) · 12.1 KB
/
Copy pathProgram.cs
File metadata and controls
293 lines (276 loc) · 12.1 KB
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
/*
Humans must learn to apply their intelligence correctly and evolve beyond their current state.
People must change. Otherwise, even if humanity expands into space, it will only create new
conflicts, and that would be a very sad thing. - Aeolia Schenberg, 2091 A.D.
,r‐、 , -、
! ヽ / }
ヽ、 ,! -─‐- 、{ ノ
/。 。 r`'、´
/ ,.-─- 、 ヽ、.ヽ Haro
!/ ヽ、._, ニ| Haro!
{ ,'
ヽ /,ソ
ヽ、.____r',/
*/
using Newtonsoft.Json;
using System;
using System.IO;
using System.Net;
using System.Threading;
namespace PSO2JP_Token_Generator
{
internal class Program
{
public static string username;
public static string password;
//Pretty sure the OTP is *always* numbers but let's just play it safe because it -is- SEGA.
public static string otp;
public static string token;
public static string userid;
public static int ResponseCode;
public static int OTPCodeTries = 5;
private static void Main()
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("This application takes your username and password and generates a token for the Tweaker to start the game with.");
Console.WriteLine("This will allow you to switch characters without needing to re-enter your username or password.");
Console.Write("This program is open source and available at ");
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write("http://github.com/Aida-Enna/PSO2JP_Token_Generator");
Console.ForegroundColor = ConsoleColor.Yellow;
//Aesthetic!
Console.WriteLine(".");
Console.Write("As with all sensitive data, ");
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("you should never share your username, password, OTP code, or token with ANYONE");
Console.ForegroundColor = ConsoleColor.Yellow;
//Aesthetic!
Console.WriteLine(".");
Console.ResetColor();
TokenResponse SEGAResponse;
do
{
//Get the user's info
GetUserInfo();
//Get the response from SEGA
SEGAResponse = GetSEGAInfo();
//Parse the response
ResponseCode = SEGAResponse.Result;
if (ResponseCode == 5002)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Incorrect username. Please enter your information again.");
Console.ResetColor();
}
else if (ResponseCode == 5001)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Incorrect password. Please enter your information again.");
Console.ResetColor();
}
else if (ResponseCode != 0)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Unknown error. Please try logging in again.");
Console.ResetColor();
}
} while (ResponseCode != 0);
token = SEGAResponse.Token;
userid = SEGAResponse.UserID;
bool otpRequired = SEGAResponse.OTPRequired;
if (otpRequired)
{
do
{
if (OTPCodeTries == 0)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("You have failed the OTP code 5 times. For your safety, this program will not allow you to try a 6th time.");
Console.WriteLine("Please wait 5 minutes and then try again. If you do not wait 5 minutes and you fail the OTP again, you will get locked out for up to an hour.");
Console.ResetColor();
Console.WriteLine("Press Enter to exit this program.");
Console.ReadLine();
return;
}
GetOTP();
//Get the OTP response from SEGA
SEGAResponse = GetSEGAInfo(true);
//Parse the response
ResponseCode = SEGAResponse.Result;
if (ResponseCode == 5002)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Incorrect username. Please enter your information again.");
Console.ResetColor();
}
else if (ResponseCode == 5001)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Incorrect password. Please enter your information again.");
Console.ResetColor();
}
else if (ResponseCode != 0)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Unknown error. Please try logging in again.");
Console.ResetColor();
}
} while (ResponseCode != 0);
}
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Logged in - Token generation complete. Passing info back to Tweaker...");
Console.ResetColor();
for (int countdown = 3; countdown > 0; countdown--)
{
Console.Write("\rContinuing in {0} seconds... ", countdown);
Thread.Sleep(1000);
}
Console.WriteLine("\rContinuing in 0 seconds... ");
Console.ResetColor();
File.WriteAllText("temp.tmp", token + "|" + userid);
return;
}
private static void GetUserInfo()
{
do
{
Console.WriteLine("Enter your SEGAID username:");
username = Console.ReadLine();
if (String.IsNullOrWhiteSpace(username))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("You must enter a valid username.");
Console.ResetColor();
}
} while (String.IsNullOrWhiteSpace(username));
do
{
Console.WriteLine("Enter your SEGAID password:");
//Set the password to nothing
password = string.Empty;
ConsoleKey key;
//This puts asteriks (*) on the console instead of your text, for security.
do
{
var keyInfo = Console.ReadKey(intercept: true);
key = keyInfo.Key;
if (key == ConsoleKey.Backspace && password.Length > 0)
{
Console.Write("\b \b");
}
else if (!char.IsControl(keyInfo.KeyChar))
{
Console.Write("*");
password += keyInfo.KeyChar;
}
} while (key != ConsoleKey.Enter);
if (String.IsNullOrWhiteSpace(password))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("");
Console.WriteLine("You must enter a valid password.");
Console.ResetColor();
}
} while (String.IsNullOrWhiteSpace(password));
Console.WriteLine("");
}
private static void GetOTP()
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Your OTP (One Time Password) code will be valid for approximately 1 minute.");
Console.WriteLine("If it is close to expiring, it's recommended to wait until a new one is generated.");
Console.WriteLine("Your account will be locked if you fail to enter the correct code 6 times.");
Console.WriteLine("Therefore, for your own safety, you will only be allowed to enter your OTP through this program 5 times.");
Console.WriteLine("You have " + OTPCodeTries + " more attempt(s) to login via OTP.");
Console.ResetColor();
do
{
Console.WriteLine("Enter your SEGAID OTP (" + OTPCodeTries + " attempt(s) remaining):");
otp = Console.ReadLine();
if (String.IsNullOrWhiteSpace(otp) || otp.Length != 6)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("You must enter a valid OTP. (6 characters)");
Console.ResetColor();
}
} while (String.IsNullOrWhiteSpace(otp) || otp.Length != 6);
Console.WriteLine("");
OTPCodeTries--;
}
private static TokenResponse GetSEGAInfo(bool DoingOTP = false)
{
string payload;
HttpWebRequest TokenRequest = (HttpWebRequest)WebRequest.Create(DoingOTP ? "https://auth.pso2.jp/auth/v1/otpAuth" : "https://auth.pso2.jp/auth/v1/auth");
if (DoingOTP)
{
CredentialsWithOTP otp_credentials = new CredentialsWithOTP
{
userId = userid,
token = token,
otp = otp
};
payload = JsonConvert.SerializeObject(otp_credentials);
}
else
{
Credentials simple_credentials = new Credentials
{
id = username,
password = password
};
payload = JsonConvert.SerializeObject(simple_credentials);
}
TokenRequest.ContentType = "application/json; charset=utf-8";
TokenRequest.Host = "auth.pso2.jp";
TokenRequest.UserAgent = "PSO2 Launcher";
TokenRequest.Method = "POST";
//Send SEGA your credentials
using (var streamWriter = new StreamWriter(TokenRequest.GetRequestStream()))
{
streamWriter.Write(payload);
}
//Get the response from them
var response = TokenRequest.GetResponse();
using (var streamReader = new StreamReader(response.GetResponseStream()))
using (var reader = new JsonTextReader(streamReader))
{
//Return the response back to the main code
return JsonSerializer.CreateDefault().Deserialize<TokenResponse>(reader);
}
}
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
private struct Credentials
{
[JsonProperty("id")]
public string id;
[JsonProperty("password")]
public string password;
}
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
private struct CredentialsWithOTP
{
[JsonProperty("userId")]
public string userId;
[JsonProperty("token")]
public string token;
[JsonProperty("otp")]
public string otp;
}
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
private struct TokenResponse
{
//The result of the login attempt
[JsonProperty("result")]
public int Result { get; set; }
//Unique user ID
[JsonProperty("userId")]
public string UserID { get; set; }
//Temporary token
[JsonProperty("token")]
public string Token { get; set; }
//Whether we need to ask for OTP
[JsonProperty("otpRequired")]
public bool OTPRequired { get; set; }
}
}
}