This repository has been archived by the owner on Nov 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Yggdrasil.cs
267 lines (220 loc) · 11.4 KB
/
Yggdrasil.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
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace ProtocolModern
{
public enum YggdrasilStatus
{
Error,
Success,
WrongPassword,
Blocked,
AccountMigrated,
InvalidToken,
NotFound,
UnsupportedMediaType
}
public static partial class Yggdrasil
{
/// <summary>
/// Authenticates a user using his password.
/// </summary>
/// <param name="username">Login</param>
/// <param name="password">Password</param>
/// <returns></returns>
public static async Task<YggdrasilAnswer> Login(string username, string password)
{
try
{
var request = WebRequest.Create(new Uri("https://authserver.mojang.com/authenticate")) as HttpWebRequest;
if (request == null)
return new YggdrasilAnswer { Status = YggdrasilStatus.Error };
request.ContentType = "application/json";
request.Method = "POST";
var json = JsonConvert.SerializeObject(new JsonLogin
{
Agent = Agent.Minecraft,
Username = username,
Password = password
// "clientToken": "client identifier" // optional
});
using (var writer = new StreamWriter(await request.GetRequestStreamAsync().ConfigureAwait(false)))
await writer.WriteAsync(json).ConfigureAwait(false);
using (var resp = await request.GetResponseAsync().ConfigureAwait(false))
using (var reader = new StreamReader(resp.GetResponseStream(), Encoding.UTF8))
return new YggdrasilAnswer { Status = YggdrasilStatus.Success, Response = JsonConvert.DeserializeObject<Response>(await reader.ReadToEndAsync().ConfigureAwait(false)) };
}
catch (WebException ex) { return HandleWebException(ex).Result; }
}
/// <summary>
/// Refreshes a valid accessToken.
/// </summary>
/// <param name="accessToken"></param>
/// <param name="clientToken"></param>
/// <returns></returns>
public static async Task<YggdrasilAnswer> RefreshSession(string accessToken, string clientToken)
{
try
{
var request = WebRequest.Create(new Uri("https://authserver.mojang.com/refresh")) as HttpWebRequest;
if (request == null)
return new YggdrasilAnswer { Status = YggdrasilStatus.Error };
request.ContentType = "application/json";
request.Method = "POST";
var json = JsonConvert.SerializeObject(new JsonRefreshSession
{
AccessToken = accessToken,
ClientToken = clientToken,
});
using (var writer = new StreamWriter(await request.GetRequestStreamAsync().ConfigureAwait(false)))
await writer.WriteAsync(json).ConfigureAwait(false);
var resp = await request.GetResponseAsync().ConfigureAwait(false);
using (var reader = new StreamReader(resp.GetResponseStream(), Encoding.UTF8))
return new YggdrasilAnswer { Status = YggdrasilStatus.Success, Response = JsonConvert.DeserializeObject<Response>(await reader.ReadToEndAsync().ConfigureAwait(false)) };
}
catch (WebException ex) { return HandleWebException(ex).Result; }
}
/// <summary>
/// Checks if an accessToken is a valid session token with a currently-active session.
/// </summary>
/// <param name="accessToken"></param>
/// <returns></returns>
public static async Task<bool> VerifySession(string accessToken)
{
try
{
var request = WebRequest.Create(new Uri("https://authserver.mojang.com/validate")) as HttpWebRequest;
if (request == null)
return false;
request.ContentType = "application/json";
request.Method = "POST";
var json = JsonConvert.SerializeObject(new JsonVerifySession { AccessToken = accessToken });
using (var writer = new StreamWriter(await request.GetRequestStreamAsync().ConfigureAwait(false)))
await writer.WriteAsync(json).ConfigureAwait(false);
var resp = await request.GetResponseAsync().ConfigureAwait(false);
using (var reader = new StreamReader(resp.GetResponseStream(), Encoding.UTF8))
return string.IsNullOrEmpty(await reader.ReadToEndAsync().ConfigureAwait(false));
}
catch (WebException) { return false; }
}
/// <summary>
/// Invalidates accessTokens using an account's username and password
/// </summary>
/// <param name="username">Login</param>
/// <param name="password">Password</param>
/// <returns></returns>
public static async Task<bool> Logout(string username, string password)
{
try
{
var request = WebRequest.Create(new Uri("https://authserver.mojang.com/signout")) as HttpWebRequest;
if (request == null)
return false;
request.ContentType = "application/json";
request.Method = "POST";
var json = JsonConvert.SerializeObject(new JsonLogout
{
Username = username,
Password = password
});
using (var writer = new StreamWriter(await request.GetRequestStreamAsync().ConfigureAwait(false)))
await writer.WriteAsync(json);
var resp = await request.GetResponseAsync().ConfigureAwait(false);
using (var reader = new StreamReader(resp.GetResponseStream(), Encoding.UTF8))
return string.IsNullOrEmpty(await reader.ReadToEndAsync().ConfigureAwait(false));
}
catch (WebException) { return false; }
}
/// <summary>
/// Invalidates accessTokens using a client/access token pair.
/// </summary>
/// <param name="accessToken"></param>
/// <param name="clientToken"></param>
/// <returns></returns>
public static async Task<bool> Invalidate(string accessToken, string clientToken)
{
try
{
var request = WebRequest.Create(new Uri("https://authserver.mojang.com/invalidate")) as HttpWebRequest;
if (request == null)
return false;
request.ContentType = "application/json";
request.Method = "POST";
var json = JsonConvert.SerializeObject(new JsonInvalidate
{
AccessToken = accessToken,
ClientToken = clientToken
});
using (var writer = new StreamWriter(await request.GetRequestStreamAsync().ConfigureAwait(false)))
await writer.WriteAsync(json).ConfigureAwait(false);
var resp = await request.GetResponseAsync().ConfigureAwait(false);
using (var reader = new StreamReader(resp.GetResponseStream(), Encoding.UTF8))
return string.IsNullOrEmpty(await reader.ReadToEndAsync().ConfigureAwait(false));
}
catch (WebException) { return false; }
}
/// <summary>
/// Both server and client need to make a request to sessionserver.mojang.com if the server is in online-mode.
/// </summary>
/// <param name="accessToken"></param>
/// <param name="selectedProfile"></param>
/// <param name="serverHash"></param>
/// <returns></returns>
public static async Task<bool> JoinSession(string accessToken, string selectedProfile, string serverHash)
{
try
{
var request = (HttpWebRequest)WebRequest.Create(new Uri("https://sessionserver.mojang.com/session/minecraft/join"));
request.ContentType = "application/json";
request.Method = "POST";
var json = JsonConvert.SerializeObject(new JsonClientAuth
{
AccessToken = accessToken,
SelectedProfile = selectedProfile,
ServerID = serverHash
});
using (var writer = new StreamWriter(await request.GetRequestStreamAsync().ConfigureAwait(false)))
await writer.WriteAsync(json).ConfigureAwait(false);
using (var resp = await request.GetResponseAsync().ConfigureAwait(false))
using (var reader = new StreamReader(resp.GetResponseStream(), Encoding.UTF8))
return string.IsNullOrEmpty(await reader.ReadToEndAsync().ConfigureAwait(false));
}
catch (WebException) { return false; }
}
private static async Task<YggdrasilAnswer> HandleWebException(WebException e)
{
// That code would break in case they ever change the underlying value.
const int ProtocolError = 7;
if ((int) e.Status != ProtocolError)
return new YggdrasilAnswer { Status = YggdrasilStatus.Error };
using (var response = e.Response as HttpWebResponse)
{
if (response == null)
return new YggdrasilAnswer { Status = YggdrasilStatus.Error };
if (response.StatusCode != HttpStatusCode.Forbidden)
return new YggdrasilAnswer { Status = YggdrasilStatus.Blocked };
if (response.StatusCode == HttpStatusCode.UnsupportedMediaType)
return new YggdrasilAnswer { Status = YggdrasilStatus.UnsupportedMediaType };
if (response.StatusCode == HttpStatusCode.NotFound)
return new YggdrasilAnswer { Status = YggdrasilStatus.NotFound };
using (var sr = new StreamReader(response.GetResponseStream()))
{
var result = await sr.ReadToEndAsync();
if (!response.ContentType.Contains("application/json"))
return new YggdrasilAnswer { Status = YggdrasilStatus.Error };
var error = JsonConvert.DeserializeObject<Error>(result);
if (error.ErrorDescription != ErrorType.ForbiddenOperationException)
return new YggdrasilAnswer { Status = YggdrasilStatus.Error };
if (error.Cause != null && error.Cause.Contains("UserMigratedException"))
return new YggdrasilAnswer { Status = YggdrasilStatus.AccountMigrated };
if (error.ErrorMessage.Contains("Invalid token"))
return new YggdrasilAnswer { Status = YggdrasilStatus.InvalidToken };
return new YggdrasilAnswer { Status = YggdrasilStatus.WrongPassword };
}
}
}
}
}