forked from ClassiCube/MCGalaxy-Plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MicrosoftAuthenticator.cs
115 lines (97 loc) · 3.33 KB
/
MicrosoftAuthenticator.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
//reference System.dll
//reference System.Core.dll
using System;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using MCGalaxy;
using MCGalaxy.Authentication;
using MCGalaxy.Network;
namespace AuthPlugin
{
public sealed class MicrosoftAuthenticationPlugin : Plugin
{
public override string name { get { return "MicrosoftAuthenticationPlugin"; } }
public override string MCGalaxy_Version { get { return "1.9.3.6"; } }
public override void Load(bool auto)
{
Authenticator.Current = new MicrosoftFallbackAuthenticator(Authenticator.Current);
}
public override void Unload(bool auto)
{
Authenticator.Current = ((MicrosoftFallbackAuthenticator)Authenticator.Current).underlyingAuthenticator;
}
}
class MicrosoftFallbackAuthenticator : Authenticator
{
// underlyingAuthenticator is used to allow the plugin to wrap an already wrapped authenticator if there is one.
// In addition, the default authenticator is sealed, and so cannot be inherited / extended.
public readonly Authenticator underlyingAuthenticator;
string externalIP;
public MicrosoftFallbackAuthenticator(Authenticator underlyingAuthenticator)
{
this.underlyingAuthenticator = underlyingAuthenticator;
GetExternalIP();
}
void GetExternalIP()
{
if (externalIP != null) return;
try
{
externalIP = new WebClient().DownloadString("http://ipv4.icanhazip.com").Replace("\\r\\n", "").Replace("\\n", "").Trim();
}
catch (Exception ex)
{
Logger.LogError("Retrieving external IP", ex);
}
}
public override bool HasPassword(string name)
{
return underlyingAuthenticator.HasPassword(name);
}
public override bool ResetPassword(string name)
{
return underlyingAuthenticator.ResetPassword(name);
}
public override void StorePassword(string name, string password)
{
underlyingAuthenticator.StorePassword(name, password);
}
public override bool VerifyPassword(string name, string password)
{
return underlyingAuthenticator.VerifyPassword(name, password);
}
public override bool VerifyLogin(Player p, string mppass)
{
bool mppass_valid = underlyingAuthenticator.VerifyLogin(p, mppass);
if (mppass_valid) return true;
GetExternalIP();
string serverId = externalIP + ":" + Server.Config.Port;
var hash = new SHA1Managed().ComputeHash(Encoding.UTF8.GetBytes(serverId));
serverId = string.Concat(hash.Select(b => b.ToString("x2")));
// Check if the player has authenticated with Mojang's session server.
if (!HasJoined(p.truename, serverId)) return false;
p.verifiedName = true;
return true;
}
bool HasJoined(string username, string serverId)
{
string url = "https://sessionserver.mojang.com/session/minecraft/hasJoined?username=" + username + "&serverId=" + serverId;
try
{
HttpWebRequest request = HttpUtil.CreateRequest(url);
request.Timeout = 10 * 1000; // give up after 10 seconds
request.ReadWriteTimeout = 10 * 1000;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
return response.StatusCode == HttpStatusCode.OK;
}
catch (Exception ex)
{
HttpUtil.DisposeErrorResponse(ex);
Logger.LogError("Verifying Mojang session", ex);
}
return false;
}
}
}