-
Notifications
You must be signed in to change notification settings - Fork 0
/
Yggdrasil.cs
210 lines (179 loc) · 8.79 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
using System;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace ProtocolModern
{
public enum YggdrasilResponse
{
Error,
Success,
AccountMigrated,
WrongPassword,
TooMuchAttempts,
InvalidToken
}
public static partial class Yggdrasil
{
/// <summary>
/// Authenticates a user using their <paramref name="username"/> and <paramref name="password"/>.
/// </summary>
/// <param name="username">Login</param>
/// <param name="password">Password</param>
public static async Task<YggdrasilAuthenticate> Authenticate(string username, string password)
{
var json = JsonConvert.SerializeObject(new JsonLogin
{
Agent = Agent.Minecraft,
Username = username,
Password = password
// "clientToken": "client identifier" // optional
});
using (var client = new HttpClient())
using (var response = await client.PostAsync(new Uri("https://authserver.mojang.com/authenticate"), new StringContent(json, Encoding.UTF8, "application/json")))
using (var content = response.Content)
{
var result = await content.ReadAsStringAsync();
if (response.StatusCode == HttpStatusCode.OK)
return new YggdrasilAuthenticate(YggdrasilResponse.Success, JsonConvert.DeserializeObject<AuthenticateInfo>(result));
else
return new YggdrasilAuthenticate(await HandleHttpResponseMessage(response), AuthenticateInfo.Empty);
}
}
/// <summary>
/// Refreshes a valid <paramref name="accessToken"/>.
/// It can be used to keep a user logged in between gaming sessions.
/// </summary>
/// <param name="accessToken"></param>
/// <param name="clientToken"></param>
public static async Task<YggdrasilAuthenticate> RefreshSession(string accessToken, string clientToken)
{
var json = JsonConvert.SerializeObject(new JsonRefreshSession
{
AccessToken = accessToken,
ClientToken = clientToken,
});
using (var client = new HttpClient())
using (var response = await client.PostAsync(new Uri("https://authserver.mojang.com/refresh"), new StringContent(json, Encoding.UTF8, "application/json")))
using (var content = response.Content)
{
var result = await content.ReadAsStringAsync();
if (response.StatusCode == HttpStatusCode.OK)
return new YggdrasilAuthenticate(YggdrasilResponse.Success, JsonConvert.DeserializeObject<AuthenticateInfo>(result));
else
return new YggdrasilAuthenticate(await HandleHttpResponseMessage(response), AuthenticateInfo.Empty);
}
}
/// <summary>
/// Checks if an <paramref name="accessToken"/> is a valid session token with a currently-active session.
/// </summary>
/// <param name="accessToken"></param>
public static async Task<YggdrasilStatus> VerifySession(string accessToken)
{
var json = JsonConvert.SerializeObject(new JsonVerifySession
{
AccessToken = accessToken
});
using (var client = new HttpClient())
using (var response = await client.PostAsync(new Uri("https://authserver.mojang.com/validate"), new StringContent(json, Encoding.UTF8, "application/json")))
{
if (response.StatusCode == HttpStatusCode.NoContent)
return new YggdrasilStatus(YggdrasilResponse.Success, true);
else
return new YggdrasilStatus(await HandleHttpResponseMessage(response), true);
}
}
/// <summary>
/// Invalidates accessTokens using an account's <paramref name="username"/> and <paramref name="password"/>.
/// </summary>
/// <param name="username">Login</param>
/// <param name="password">Password</param>
public static async Task<YggdrasilStatus> Logout(string username, string password)
{
var json = JsonConvert.SerializeObject(new JsonLogout
{
Username = username,
Password = password
});
using (var client = new HttpClient())
using (var response = await client.PostAsync(new Uri("https://authserver.mojang.com/signout"), new StringContent(json, Encoding.UTF8, "application/json")))
{
if (response.StatusCode == HttpStatusCode.NoContent)
return new YggdrasilStatus(YggdrasilResponse.Success, true);
else
return new YggdrasilStatus(await HandleHttpResponseMessage(response), true);
}
}
/// <summary>
/// Invalidates <paramref name="accessToken"/> using a <paramref name="clientToken"/>/<paramref name="accessToken"/> pair.
/// </summary>
/// <param name="accessToken"></param>
/// <param name="clientToken"></param>
public static async Task<YggdrasilStatus> Invalidate(string accessToken, string clientToken)
{
var json = JsonConvert.SerializeObject(new JsonInvalidate
{
AccessToken = accessToken,
ClientToken = clientToken
});
using (var client = new HttpClient())
using (var response = await client.PostAsync(new Uri("https://authserver.mojang.com/invalidate"), new StringContent(json, Encoding.UTF8, "application/json")))
{
if (response.StatusCode == HttpStatusCode.NoContent)
return new YggdrasilStatus(YggdrasilResponse.Success, true);
else
return new YggdrasilStatus(await HandleHttpResponseMessage(response), true);
}
}
/// <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>
public static async Task<YggdrasilStatus> JoinSession(string accessToken, string selectedProfile, string serverHash)
{
var json = JsonConvert.SerializeObject(new JsonClientAuth
{
AccessToken = accessToken,
SelectedProfile = selectedProfile,
ServerID = serverHash
});
using (var client = new HttpClient())
using (var response = await client.PostAsync(new Uri("https://sessionserver.mojang.com/session/minecraft/join"), new StringContent(json, Encoding.UTF8, "application/json")))
{
if (response.StatusCode == HttpStatusCode.NoContent)
return new YggdrasilStatus(YggdrasilResponse.Success, true);
else
return new YggdrasilStatus(await HandleHttpResponseMessage(response), true);
}
}
private static async Task<YggdrasilResponse> HandleHttpResponseMessage(HttpResponseMessage message)
{
if (message == null)
return YggdrasilResponse.Error;
var error = JsonConvert.DeserializeObject<Error>(await message.Content.ReadAsStringAsync());
if (error.ErrorDescription == ErrorType.ForbiddenOperationException)
{
if (error.Cause != null && error.Cause.Contains("UserMigratedException"))
return YggdrasilResponse.AccountMigrated;
if (error.ErrorMessage != null && error.ErrorMessage.Contains("Invalid username or password"))
return YggdrasilResponse.WrongPassword;
if (error.ErrorMessage != null && error.ErrorMessage.Contains("Invalid credentials"))
return YggdrasilResponse.TooMuchAttempts;
if (error.ErrorMessage != null && error.ErrorMessage.Contains("Invalid token"))
return YggdrasilResponse.InvalidToken;
}
if (error.ErrorDescription == ErrorType.IllegalArgumentException)
{
if (error.ErrorMessage != null && error.ErrorMessage.Contains("Access Token can not be null or empty"))
return YggdrasilResponse.InvalidToken;
if (error.ErrorMessage != null && error.ErrorMessage.Contains("Access token already has a profile assigned"))
return YggdrasilResponse.InvalidToken;
}
return YggdrasilResponse.Error;
}
}
}