feat: add basic authentication packet handling.

This commit is contained in:
Matthew Toro 2026-03-28 00:07:18 -04:00
parent 7447fabe0d
commit 186982b458
7 changed files with 96 additions and 0 deletions

View file

@ -0,0 +1,47 @@
#include "stdafx.h"
#include "InputOutputStream.h"
#include "PacketListener.h"
#include "AuthPackets.h"
AuthPacket::AuthPacket(AuthStage stage, vector<pair<wstring, wstring>> fields)
: stage(stage), fields(std::move(fields))
{
}
void AuthPacket::read(DataInputStream *dis)
{
stage = static_cast<AuthStage>(dis->readByte());
short count = dis->readShort();
fields.clear();
fields.reserve(count);
for (short i = 0; i < count; i++)
{
wstring key = readUtf(dis, 256);
wstring value = readUtf(dis, 4096);
fields.emplace_back(std::move(key), std::move(value));
}
}
void AuthPacket::write(DataOutputStream *dos)
{
dos->writeByte(static_cast<byte>(stage));
dos->writeShort(static_cast<short>(fields.size()));
for (const auto &field : fields)
{
writeUtf(field.first, dos);
writeUtf(field.second, dos);
}
}
void AuthPacket::handle(PacketListener *listener)
{
listener->handleAuth(shared_from_this());
}
int AuthPacket::getEstimatedSize()
{
int size = 1 + 2;
for (const auto &field : fields)
size += 4 + static_cast<int>((field.first.length() + field.second.length()) * sizeof(wchar_t));
return size;
}

View file

@ -0,0 +1,36 @@
#pragma once
using namespace std;
#include "Packet.h"
enum class AuthStage : uint8_t
{
ANNOUNCE_VERSION,
DECLARE_SCHEME,
ACCEPT_SCHEME,
SCHEME_SETTINGS,
BEGIN_AUTH,
AUTH_DATA,
AUTH_DONE,
ASSIGN_IDENTITY,
CONFIRM_IDENTITY,
AUTH_SUCCESS,
AUTH_FAILURE
};
class AuthPacket : public Packet, public enable_shared_from_this<AuthPacket>
{
public:
AuthStage stage;
vector<pair<wstring, wstring>> fields;
AuthPacket(AuthStage stage = AuthStage::ANNOUNCE_VERSION, vector<pair<wstring, wstring>> fields = {});
virtual void read(DataInputStream *dis);
virtual void write(DataOutputStream *dos);
virtual void handle(PacketListener *listener);
virtual int getEstimatedSize();
static shared_ptr<Packet> create() { return std::make_shared<AuthPacket>(); }
virtual int getId() { return 72; }
};

View file

@ -150,6 +150,7 @@ void Packet::staticCtor()
//map(253, true, false, ServerAuthDataPacket.class);
map(254, false, true, false, false, typeid(GetInfoPacket), GetInfoPacket::create); // TODO New for 1.8.2 - Needs sendToAny?
map(255, true, true, true, false, typeid(DisconnectPacket), DisconnectPacket::create);
map(72, true, true, true, false, typeid(AuthPacket), AuthPacket::create);
}
IllegalArgumentException::IllegalArgumentException(const wstring& information)

View file

@ -491,3 +491,8 @@ void PacketListener::handleGameCommand(shared_ptr<GameCommandPacket> packet)
{
onUnhandledPacket( (shared_ptr<Packet> ) packet);
}
void PacketListener::handleAuth(shared_ptr<AuthPacket> packet)
{
onUnhandledPacket( (shared_ptr<Packet> ) packet);
}

View file

@ -111,6 +111,7 @@ class KickPlayerPacket;
class AdditionalModelPartsPacket;
class XZPacket;
class GameCommandPacket;
class AuthPacket;
class PacketListener
{
@ -227,4 +228,7 @@ public:
virtual void handleKickPlayer(shared_ptr<KickPlayerPacket> packet);
virtual void handleXZ(shared_ptr<XZPacket> packet);
virtual void handleGameCommand(shared_ptr<GameCommandPacket> packet);
// MCConsoles Auth
virtual void handleAuth(shared_ptr<AuthPacket> packet);
};

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}/AuthPackets.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/AuthPackets.h"
"${CMAKE_CURRENT_SOURCE_DIR}/AwardStatPacket.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/AwardStatPacket.h"
"${CMAKE_CURRENT_SOURCE_DIR}/BlockRegionUpdatePacket.cpp"

View file

@ -108,4 +108,5 @@
#include "KickPlayerPacket.h"
#include "XZPacket.h"
#include "GameCommandPacket.h"
#include "AuthPackets.h"