diff --git a/Minecraft.World/AuthModule.cpp b/Minecraft.World/AuthModule.cpp new file mode 100644 index 00000000..0d5ffb0f --- /dev/null +++ b/Minecraft.World/AuthModule.cpp @@ -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> &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 ElyByAuthModule::supportedVariations() +{ + return {L"java"}; +} + +vector> ElyByAuthModule::getSettings(const wstring &variation) +{ + return {{L"endpoint", endpoint}}; +} + +bool ElyByAuthModule::onAuthData(const vector> &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 MojangAuthModule::supportedVariations() +{ + return {L"java", L"bedrock"}; +} + +// --- KeypairOfflineAuthModule --- + +const wchar_t *KeypairOfflineAuthModule::schemeName() { return L"mcconsoles:keypair_offline"; } + +vector KeypairOfflineAuthModule::supportedVariations() +{ + return {L"rsa2048", L"ed25519"}; +} + +vector> KeypairOfflineAuthModule::getSettings(const wstring &variation) +{ + return {{L"key_type", variation}}; +} + +bool KeypairOfflineAuthModule::onAuthData(const vector> &fields, wstring &outUid, wstring &outUsername) +{ + return extractIdentity(fields, outUid, outUsername); +} + +const wchar_t *OfflineAuthModule::schemeName() { return L"mcconsoles:offline"; } + +vector OfflineAuthModule::supportedVariations() +{ + return {}; +} + +vector> OfflineAuthModule::getSettings(const wstring &variation) +{ + return {}; +} + +bool OfflineAuthModule::onAuthData(const vector> &fields, wstring &outUid, wstring &outUsername) +{ + return extractIdentity(fields, outUid, outUsername); +} diff --git a/Minecraft.World/AuthModule.h b/Minecraft.World/AuthModule.h new file mode 100644 index 00000000..4de6efa1 --- /dev/null +++ b/Minecraft.World/AuthModule.h @@ -0,0 +1,63 @@ +#pragma once +using namespace std; + +#include +#include +#include + +class AuthModule +{ +public: + virtual ~AuthModule() = default; + + virtual const wchar_t *schemeName() = 0; + virtual vector supportedVariations() = 0; + virtual vector> getSettings(const wstring &variation) = 0; + virtual bool onAuthData(const vector> &fields, wstring &outUid, wstring &outUsername) = 0; + + bool validate(const wstring &uid, const wstring &username); + +protected: + bool extractIdentity(const vector> &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 supportedVariations() override; + vector> getSettings(const wstring &variation) override; + bool onAuthData(const vector> &fields, wstring &outUid, wstring &outUsername) override; +}; + +class MojangAuthModule : public ElyByAuthModule +{ +public: + MojangAuthModule(); + + const wchar_t *schemeName() override; + vector supportedVariations() override; +}; + +class KeypairOfflineAuthModule : public AuthModule +{ +public: + const wchar_t *schemeName() override; + vector supportedVariations() override; + vector> getSettings(const wstring &variation) override; + bool onAuthData(const vector> &fields, wstring &outUid, wstring &outUsername) override; +}; + +class OfflineAuthModule : public AuthModule +{ +public: + const wchar_t *schemeName() override; + vector supportedVariations() override; + vector> getSettings(const wstring &variation) override; + bool onAuthData(const vector> &fields, wstring &outUid, wstring &outUsername) override; +}; diff --git a/Minecraft.World/cmake/sources/Common.cmake b/Minecraft.World/cmake/sources/Common.cmake index 544d1e57..eb4a6379 100644 --- a/Minecraft.World/cmake/sources/Common.cmake +++ b/Minecraft.World/cmake/sources/Common.cmake @@ -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"