11 #include "json/json.h"
22 constexpr
char DEFAULT_AUTH_ADDRESS[] =
"/session/minecraft/hasJoined?username=%USERNAME%&serverId=%SERVERID%";
29 Super(
"Authenticator"),
32 m_ShouldAuthenticate(true)
56 constexpr std::string_view HttpPrefix =
"http://";
57 constexpr std::string_view HttpsPrefix =
"https://";
60 (std::string_view(
m_Server).substr(0, HttpPrefix.size()) != HttpPrefix) &&
61 (std::string_view(
m_Server).substr(0, HttpsPrefix.size()) != HttpsPrefix)
71 LOGWARNING(
"%s %d: Supplied invalid URL for configuration value [Authentication: Server]: \"%s\", using default! Error: %s", __FUNCTION__, __LINE__,
m_Server.c_str(), ErrorMessage.c_str());
80 LOGWARNING(
"%s %d: Supplied invalid URL for configuration value [Authentication: Address]: \"%s\", using default! Error: %s", __FUNCTION__, __LINE__,
m_Address.c_str(), ErrorMessage.c_str());
95 std::string OfflineUsername(a_Username);
106 m_Queue.emplace_back(a_ClientID, a_Username, a_ServerHash);
159 Json::Value Properties;
178 LOGD(
"Trying to authenticate user %s", a_UserName.c_str());
182 ReplaceURL(ActualAddress,
"%USERNAME%", a_UserName);
183 ReplaceURL(ActualAddress,
"%SERVERID%", a_ServerId);
193 if (Response.empty())
200 LOGWARNING(
"%s: Cannot parse received data (authentication) to JSON!", __FUNCTION__);
203 a_UserName = root.get(
"name",
"Unknown").asString();
204 a_Properties = root[
"properties"];
205 if (!a_UUID.
FromString(root.get(
"id",
"").asString()))
207 LOGWARNING(
"%s: Received invalid UUID format", __FUNCTION__);
219 #ifdef ENABLE_PROPERTIES
223 #define DEFAULT_PROPERTIES_ADDRESS "/session/minecraft/profile/%UUID%"
226 bool GetPlayerProperties(
const AString & a_UUID, Json::Value & a_Properties);
228 bool cAuthenticator::GetPlayerProperties(
const AString & a_UUID, Json::Value & a_Properties)
230 LOGD(
"Trying to get properties for user %s", a_UUID.c_str());
240 if (Response.empty())
247 if (!reader.parse(Response, root,
false))
249 LOGWARNING(
"cAuthenticator: Cannot parse received properties data to JSON!");
253 a_Properties = root[
"properties"];
void LOGWARNING(std::string_view a_Format, const Args &... args)
void LOGINFO(std::string_view a_Format, const Args &... args)
constexpr char DEFAULT_AUTH_ADDRESS[]
constexpr char DEFAULT_AUTH_SERVER[]
void ReplaceURL(AString &iHayStack, const AString &iNeedle, const AString &iReplaceWith)
Replaces each occurence of iNeedle in iHayStack with iReplaceWith, after URL-encoding iReplaceWith.
bool ParseString(const AString &a_JsonStr, Json::Value &a_Root, AString *a_ErrorMsg)
static cUUID GenerateOfflineUUID(const AString &a_Username)
Generates an UUID based on the player name provided.
static std::pair< bool, AString > BlockingGet(const AString &a_URL, AStringMap a_Headers={}, const AString &a_Body={}, const AStringMap &a_Options={})
Alias for BlockingRequest("GET", ...)
static std::pair< bool, AString > Validate(const AString &a_Url)
Checks if the supplied URL is valid.
RAII for cCriticalSection - locks the CS on creation, unlocks on destruction.
Temporary RAII unlock for a cCSLock.
void Wait(void)
Waits until the event has been set.
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 Stop(void)
Signals the thread to terminate and waits until it's finished.
void Start(void)
Starts the thread; returns without waiting for the actual start.
bool m_ShouldAuthenticate
void ReadSettings(cSettingsRepositoryInterface &a_Settings)
(Re-)read server and address from INI:
void Stop(void)
Stops the authenticator thread.
AString m_Server
The server that is to be contacted for auth / UUID conversions.
virtual ~cAuthenticator() override
virtual void Execute(void) override
cIsThread override:
AString m_Address
The URL to use for auth, without server part.
bool AuthWithYggdrasil(AString &a_UserName, const AString &a_ServerId, cUUID &a_UUID, Json::Value &a_Properties) const
Returns true if the user authenticated okay, false on error Returns the case-corrected username,...
void Authenticate(int a_ClientID, std::string_view a_Username, std::string_view a_ServerHash)
Queues a request for authenticating a user.
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.
cServer * GetServer(void)
void KickUser(int a_ClientID, const AString &a_Reason)
Kicks the user, no matter in what world they are.
cMojangAPI & GetMojangAPI(void)
void AuthenticateUser(int a_ClientID, AString &&a_Username, const cUUID &a_UUID, Json::Value &&a_Properties)
Authenticates the specified user, called by cAuthenticator supplying player details from Mojang.
virtual bool GetValueSetB(const AString &keyname, const AString &valuename, const bool defValue=false)=0
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.
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.