-
Notifications
You must be signed in to change notification settings - Fork 8
/
DiscordOAuth.cs
312 lines (270 loc) · 9.06 KB
/
DiscordOAuth.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
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;
using Spark.Properties;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Spark
{
/// <summary>
/// 🔑
/// </summary>
public class DiscordOAuth
{
/// <summary>
/// Called when the program gets authenticated with Discord
/// </summary>
public static Action Authenticated;
private const string REDIRECT_URI = "http://localhost:6722/oauth_login";
private static WebServer2 webServer;
static readonly HttpClient client = new HttpClient();
public static string oauthToken = "";
public static Dictionary<string, string> discordUserData;
public static string DiscordUsername => discordUserData?["username"];
public static string DiscordUserID => discordUserData?["id"];
public static string DiscordPFPURL => $"https://cdn.discordapp.com/avatars/{discordUserData["id"]}/{discordUserData["avatar"]}";
public static string igniteUploadKey = string.Empty;
public static string firebaseCred;
public static List<AccessCodeKey> availableAccessCodes = new List<AccessCodeKey>();
private static readonly AccessCodeKey personalAccessCode = new AccessCodeKey
{
username = "Personal",
series_name = "personal"
};
public class AccessCodeKey
{
public string series_name;
public string username;
}
public static AccessCodeKey AccessCode
{
get => accessCode;
set
{
if (value.series_name != accessCode.series_name)
{
SparkSettings.instance.accessCode = SecretKeys.Hash(value.series_name);
SparkSettings.instance.Save();
accessCode = value;
AccessCodeChanged?.Invoke(value);
}
accessCode = value;
}
}
public static Action<AccessCodeKey> AccessCodeChanged;
private static AccessCodeKey accessCode = personalAccessCode;
public static bool Personal => AccessCode.series_name == "personal" || AccessCode == null;
public static bool IsLoggedIn { get => oauthToken != string.Empty; }
public static int GetAccessCodeIndexByHash(string hash)
{
for (int i = 0; i < availableAccessCodes.Count; i++)
{
if (SecretKeys.Hash(availableAccessCodes[i].series_name) == hash)
{
return i;
}
}
return 0;
}
public static void SetAccessCodeByUsername(string username)
{
foreach (AccessCodeKey key in availableAccessCodes)
{
if (key.username == username)
{
AccessCode = key;
return;
}
}
Logger.LogRow(Logger.LogType.Error, "Chosen access code is not available. " + username);
}
public static void SetAccessCodeByHash(string hash)
{
foreach (AccessCodeKey key in availableAccessCodes)
{
if (SecretKeys.Hash(key.series_name) == hash)
{
AccessCode = key;
return;
}
}
RevertToPersonal();
Logger.LogRow(Logger.LogType.Error, "Chosen access code is not available. " + hash);
}
public static void OAuthLogin(bool force = false)
{
string token = SparkSettings.instance.discordOAuthRefreshToken;
if (string.IsNullOrEmpty(token) || force)
{
Process.Start(new ProcessStartInfo
{
FileName = SecretKeys.OAuthURL,
UseShellExecute = true
});
webServer = new WebServer2(OAuthResponse, "http://localhost:6722/");
webServer.Run();
}
else
{
OAuthLoginRefresh(token);
}
}
internal static void Unlink()
{
oauthToken = string.Empty;
discordUserData = null;
availableAccessCodes.Clear();
SparkSettings.instance.discordOAuthRefreshToken = string.Empty;
SparkSettings.instance.Save();
}
private static string OAuthResponse(HttpListenerRequest request)
{
NameValueCollection queryStrings = HttpUtility.ParseQueryString(request.Url.Query);
if (queryStrings["code"] != null)
{
OAuthLoginResponse(queryStrings["code"]);
return "<html><head></head><body onload=\"javascript: close(); \" style=\"background-color: #333;\"><div style=\"margin: 8em auto;width: max-content;font-family: arial, sans-serif;color: #ddd;\">You can close this window and return to Spark</div></body></html>";
}
else
{
return "<html><body onload=\"javascript: close(); \">There was an error. Close this window and try again.</body></html>";
}
}
public static async void OAuthLoginRefresh(string refresh_token)
{
Dictionary<string, string> postDataDict = new Dictionary<string, string>
{
{ "client_id", SecretKeys.CLIENT_ID },
{ "client_secret", SecretKeys.CLIENT_SECRET },
{ "grant_type", "refresh_token" },
{ "refresh_token", refresh_token },
{ "redirect_uri", REDIRECT_URI },
{ "scope", "identify" }
};
try
{
HttpResponseMessage response = await client.PostAsync("https://discord.com/api/v6/oauth2/token", new FormUrlEncodedContent(postDataDict));
if (!response.IsSuccessStatusCode)
{
RevertToPersonal();
}
else
{
if (webServer == null)
{
webServer = new WebServer2(OAuthResponse, "http://localhost:6722/");
webServer.Run();
}
string responseString = await response.Content.ReadAsStringAsync();
Dictionary<string, string> data = JsonConvert.DeserializeObject<Dictionary<string, string>>(responseString);
ProcessResponse(data);
}
}
catch (HttpRequestException)
{
RevertToPersonal();
new MessageBox(Resources.cant_connect_to_internet_for_discord, Resources.Error).Show();
}
}
public static async void OAuthLoginResponse(string code)
{
Dictionary<string, string> postDataDict = new Dictionary<string, string>
{
{ "client_id", SecretKeys.CLIENT_ID },
{ "client_secret", SecretKeys.CLIENT_SECRET },
{ "grant_type", "authorization_code" },
{ "code", code },
{ "redirect_uri", REDIRECT_URI },
{ "scope", "identify" }
};
HttpResponseMessage response = await client.PostAsync("https://discord.com/api/v6/oauth2/token", new FormUrlEncodedContent(postDataDict));
string responseString = await response.Content.ReadAsStringAsync();
Dictionary<string, string> data = JsonConvert.DeserializeObject<Dictionary<string, string>>(responseString);
ProcessResponse(data);
}
public static async void ProcessResponse(Dictionary<string, string> response)
{
if (response.ContainsKey("error"))
{
RevertToPersonal();
SparkSettings.instance.discordOAuthRefreshToken = string.Empty;
oauthToken = string.Empty;
return;
}
SparkSettings.instance.discordOAuthRefreshToken = response["refresh_token"];
SparkSettings.instance.Save();
oauthToken = response["access_token"];
webServer.Stop();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", oauthToken);
HttpResponseMessage userResponse = await client.GetAsync("https://discord.com/api/v6/users/@me");
string responseString = await userResponse.Content.ReadAsStringAsync();
Dictionary<string, string> data = JsonConvert.DeserializeObject<Dictionary<string, string>>(responseString);
if (data == null)
{
Logger.LogRow(Logger.LogType.Error, "Discord response data is null");
return;
}
if (data.ContainsKey("username"))
{
discordUserData = data;
}
else
{
Console.WriteLine("Not Authorized");
}
// get the access codes for this user
try
{
string url = $"{Program.APIURL}/auth/token/{oauthToken}?u={SparkSettings.instance.client_name}&v={Program.AppVersionString()}";
string accessCodesResponseString = await FetchUtils.GetRequestAsync(url, null);
Dictionary<string, JToken> accessCodesResponseData =
JsonConvert.DeserializeObject<Dictionary<string, JToken>>(accessCodesResponseString);
if (accessCodesResponseData == null)
{
Logger.LogRow(Logger.LogType.Error, "Ignite login response data is null");
return;
}
List<AccessCodeKey> newAccessCodes = accessCodesResponseData["keys"].ToObject<List<AccessCodeKey>>();
if (accessCodesResponseData.ContainsKey("write"))
{
igniteUploadKey = accessCodesResponseData["write"].ToObject<string>();
}
if (accessCodesResponseData.ContainsKey("firebase_cred"))
{
firebaseCred = accessCodesResponseData["firebase_cred"].ToObject<string>();
}
if (newAccessCodes == null)
{
Logger.LogRow(Logger.LogType.Error, "Ignite login response doesn't contain valid data.");
return;
}
newAccessCodes.Insert(0, personalAccessCode);
if (availableAccessCodes.Count != newAccessCodes.Count)
{
}
availableAccessCodes = newAccessCodes;
SetAccessCodeByHash(SparkSettings.instance.accessCode);
FetchUtils.client.DefaultRequestHeaders.Remove("x-api-key");
FetchUtils.client.DefaultRequestHeaders.Add("x-api-key", igniteUploadKey);
Authenticated?.Invoke();
}
catch (Exception e)
{
//Dispatcher.Invoke(() =>
//{
// new MessageBox("Error connecting to login server. Check your internet connect or check if ignitevr.gg is down.", Resources.Error).Show();
//});
Logger.LogRow(Logger.LogType.Error, $"Error connecting to login server. {e}");
}
}
public static void RevertToPersonal()
{
AccessCode = personalAccessCode;
}
}
}