add: implement authentication module with validation and identity extraction

This commit is contained in:
Matthew Toro 2026-03-28 00:52:17 -04:00
parent 186982b458
commit 7a24494d3c
3 changed files with 152 additions and 0 deletions

View file

@ -0,0 +1,87 @@
#include "stdafx.h"
#include "AuthModule.h"
bool AuthModule::validate(const wstring &uid, const wstring &username)
{
return !uid.empty() && !username.empty() && username.length() <= 16;
}
bool AuthModule::extractIdentity(const vector<pair<wstring, wstring>> &fields, wstring &outUid, wstring &outUsername)
{
for (const auto &[key, value] : fields)
{
if (key == L"uid") outUid = value;
else if (key == L"username") outUsername = value;
}
return validate(outUid, outUsername);
}
ElyByAuthModule::ElyByAuthModule(const wstring &endpoint)
: endpoint(endpoint)
{
}
const wchar_t *ElyByAuthModule::schemeName() { return L"mcconsoles:elyby"; }
vector<wstring> ElyByAuthModule::supportedVariations()
{
return {L"java"};
}
vector<pair<wstring, wstring>> ElyByAuthModule::getSettings(const wstring &variation)
{
return {{L"endpoint", endpoint}};
}
bool ElyByAuthModule::onAuthData(const vector<pair<wstring, wstring>> &fields, wstring &outUid, wstring &outUsername)
{
return extractIdentity(fields, outUid, outUsername);
}
MojangAuthModule::MojangAuthModule()
: ElyByAuthModule(L"https://sessionserver.mojang.com")
{
}
const wchar_t *MojangAuthModule::schemeName() { return L"mcconsoles:mojang"; }
vector<wstring> MojangAuthModule::supportedVariations()
{
return {L"java", L"bedrock"};
}
// --- KeypairOfflineAuthModule ---
const wchar_t *KeypairOfflineAuthModule::schemeName() { return L"mcconsoles:keypair_offline"; }
vector<wstring> KeypairOfflineAuthModule::supportedVariations()
{
return {L"rsa2048", L"ed25519"};
}
vector<pair<wstring, wstring>> KeypairOfflineAuthModule::getSettings(const wstring &variation)
{
return {{L"key_type", variation}};
}
bool KeypairOfflineAuthModule::onAuthData(const vector<pair<wstring, wstring>> &fields, wstring &outUid, wstring &outUsername)
{
return extractIdentity(fields, outUid, outUsername);
}
const wchar_t *OfflineAuthModule::schemeName() { return L"mcconsoles:offline"; }
vector<wstring> OfflineAuthModule::supportedVariations()
{
return {};
}
vector<pair<wstring, wstring>> OfflineAuthModule::getSettings(const wstring &variation)
{
return {};
}
bool OfflineAuthModule::onAuthData(const vector<pair<wstring, wstring>> &fields, wstring &outUid, wstring &outUsername)
{
return extractIdentity(fields, outUid, outUsername);
}

View file

@ -0,0 +1,63 @@
#pragma once
using namespace std;
#include <string>
#include <vector>
#include <utility>
class AuthModule
{
public:
virtual ~AuthModule() = default;
virtual const wchar_t *schemeName() = 0;
virtual vector<wstring> supportedVariations() = 0;
virtual vector<pair<wstring, wstring>> getSettings(const wstring &variation) = 0;
virtual bool onAuthData(const vector<pair<wstring, wstring>> &fields, wstring &outUid, wstring &outUsername) = 0;
bool validate(const wstring &uid, const wstring &username);
protected:
bool extractIdentity(const vector<pair<wstring, wstring>> &fields, wstring &outUid, wstring &outUsername);
};
class ElyByAuthModule : public AuthModule
{
protected:
wstring endpoint;
public:
ElyByAuthModule(const wstring &endpoint = L"https://authserver.ely.by");
const wchar_t *schemeName() override;
vector<wstring> supportedVariations() override;
vector<pair<wstring, wstring>> getSettings(const wstring &variation) override;
bool onAuthData(const vector<pair<wstring, wstring>> &fields, wstring &outUid, wstring &outUsername) override;
};
class MojangAuthModule : public ElyByAuthModule
{
public:
MojangAuthModule();
const wchar_t *schemeName() override;
vector<wstring> supportedVariations() override;
};
class KeypairOfflineAuthModule : public AuthModule
{
public:
const wchar_t *schemeName() override;
vector<wstring> supportedVariations() override;
vector<pair<wstring, wstring>> getSettings(const wstring &variation) override;
bool onAuthData(const vector<pair<wstring, wstring>> &fields, wstring &outUid, wstring &outUsername) override;
};
class OfflineAuthModule : public AuthModule
{
public:
const wchar_t *schemeName() override;
vector<wstring> supportedVariations() override;
vector<pair<wstring, wstring>> getSettings(const wstring &variation) override;
bool onAuthData(const vector<pair<wstring, wstring>> &fields, wstring &outUid, wstring &outUsername) override;
};

View file

@ -259,6 +259,8 @@ set(_MINECRAFT_WORLD_COMMON_NET_MINECRAFT_NETWORK_PACKET
"${CMAKE_CURRENT_SOURCE_DIR}/AddPlayerPacket.h"
"${CMAKE_CURRENT_SOURCE_DIR}/AnimatePacket.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/AnimatePacket.h"
"${CMAKE_CURRENT_SOURCE_DIR}/AuthModule.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/AuthModule.h"
"${CMAKE_CURRENT_SOURCE_DIR}/AuthPackets.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/AuthPackets.h"
"${CMAKE_CURRENT_SOURCE_DIR}/AwardStatPacket.cpp"