8 #include "../ClientHandle.h" 11 #include "../IniFile.h" 12 #include "json/json.h" 14 #include "../mbedTLS++/BlockingSslClientSocket.h" 20 #define DEFAULT_AUTH_SERVER "sessionserver.mojang.com" 21 #define DEFAULT_AUTH_ADDRESS "/session/minecraft/hasJoined?username=%USERNAME%&serverId=%SERVERID%" 28 super(
"cAuthenticator"),
31 m_ShouldAuthenticate(true)
69 m_Queue.push_back(
cUser(a_ClientID, a_UserName, a_ServerHash));
115 int ClientID = User.m_ClientID;
116 AString UserName = User.m_Name;
117 AString ServerID = User.m_ServerID;
121 AString NewUserName = UserName;
123 Json::Value Properties;
126 LOGINFO(
"User %s authenticated with UUID %s", NewUserName.c_str(), UUID.
ToShortString().c_str());
142 LOGD(
"Trying to authenticate user %s", a_UserName.c_str());
150 Request +=
"GET " + ActualAddress +
" HTTP/1.0\r\n";
151 Request +=
"Host: " +
m_Server +
"\r\n";
152 Request +=
"User-Agent: Cuberite\r\n";
153 Request +=
"Connection: close\r\n";
163 const AString Prefix(
"HTTP/1.1 200 OK");
165 if (Response.compare(0, Prefix.size(), Prefix))
167 LOGINFO(
"User %s failed to auth, bad HTTP status line received", a_UserName.c_str());
168 LOGD(
"Response: \n%s",
CreateHexDump(HexDump, Response.data(), Response.size(), 16).c_str());
173 size_t idxHeadersEnd = Response.find(
"\r\n\r\n");
174 if (idxHeadersEnd == AString::npos)
176 LOGINFO(
"User %s failed to authenticate, bad HTTP response header received", a_UserName.c_str());
177 LOGD(
"Response: \n%s",
CreateHexDump(HexDump, Response.data(), Response.size(), 16).c_str());
180 Response.erase(0, idxHeadersEnd + 4);
183 if (Response.empty())
189 if (!reader.parse(Response, root,
false))
191 LOGWARNING(
"cAuthenticator: Cannot parse received data (authentication) to JSON!");
194 a_UserName = root.get(
"name",
"Unknown").asString();
195 a_Properties = root[
"properties"];
196 if (!a_UUID.
FromString(root.get(
"id",
"").asString()))
198 LOGWARNING(
"cAuthenticator: Recieved invalid UUID format");
void Set(void)
Sets the event - releases one thread that has been waiting in Wait().
std::atomic< bool > m_ShouldTerminate
The overriden Execute() method should check this value periodically and terminate if this is true...
void ReadSettings(cSettingsRepositoryInterface &a_Settings)
(Re-)read server and address from INI:
AString ToShortString() const
Converts the UUID to a short form string (i.e without dashes).
bool FromString(const AString &a_StringUUID)
Tries to interpret the string as a short or long form UUID and assign from it.
bool m_ShouldAuthenticate
#define DEFAULT_AUTH_SERVER
void ReplaceString(AString &iHayStack, const AString &iNeedle, const AString &iReplaceWith)
Replaces each occurence of iNeedle in iHayStack with iReplaceWith.
virtual ~cAuthenticator() override
void AddPlayerProfile(const AString &a_PlayerName, const cUUID &a_UUID, const Json::Value &a_Properties)
Called by the Authenticator to add a profile that it has received from authenticating a user...
void AuthenticateUser(int a_ClientID, const AString &a_Name, const cUUID &a_UUID, const Json::Value &a_Properties)
Called by cAuthenticator to auth the specified user.
static bool SecureRequest(const AString &a_ServerName, const AString &a_Request, AString &a_Response)
Connects to the specified server using SSL, sends the given request and receives the response...
void Wait(void)
Waits until the event has been set.
void KickUser(int a_ClientID, const AString &a_Reason)
Kicks the user, no matter in what world they are.
void Authenticate(int a_ClientID, const AString &a_UserName, const AString &a_ServerHash)
Queues a request for authenticating a user.
bool AuthWithYggdrasil(AString &a_UserName, const AString &a_ServerId, cUUID &a_UUID, Json::Value &a_Properties)
Returns true if the user authenticated okay, false on error Returns the case-corrected username...
cMojangAPI & GetMojangAPI(void)
void LOGINFO(const char *a_Format, fmt::ArgList a_ArgList)
Temporary RAII unlock for a cCSLock.
#define DEFAULT_AUTH_ADDRESS
void LOGWARNING(const char *a_Format, fmt::ArgList a_ArgList)
virtual AString GetValueSet(const AString &keyname, const AString &valuename, const AString &defValue="")=0
Gets the value; if not found, write the default to the repository.
void Stop(void)
Signals the thread to terminate and waits until it's finished.
void Stop(void)
Stops the authenticator thread.
virtual void Execute(void) override
cIsThread override:
void GenerateOfflineUUID(void)
Generates an UUID based on the username stored for this client, and stores it in the m_UUID member...
virtual bool GetValueSetB(const AString &keyname, const AString &valuename, const bool defValue=false)=0
AString m_Address
The URL to use for auth, without server part.
AString & CreateHexDump(AString &a_Out, const void *a_Data, size_t a_Size, size_t a_BytesPerLine)
format binary data this way: 00001234: 31 32 33 34 35 36 37 38 39 30 61 62 63 64 65 66 1234567890abcd...
RAII for cCriticalSection - locks the CS on creation, unlocks on destruction.
bool Start(void)
Starts the thread; returns without waiting for the actual start.
AString m_Server
The server that is to be contacted for auth / UUID conversions.