Botcraft 1.21.10
Loading...
Searching...
No Matches
Botcraft::Authentifier Class Reference

#include <Authentifier.hpp>

Public Member Functions

 Authentifier ()
 
 ~Authentifier ()
 
bool AuthMicrosoft (const std::string &cache_key)
 Authentication using a Microsoft account, storing the credentials in the cache file.
 
bool AuthMCToken (const std::string &mc_token)
 Authentication using a minecraft token.
 
bool JoinServer (const std::string &server_id, const std::vector< unsigned char > &shared_secret, const std::vector< unsigned char > &server_public_key) const
 
const std::string & GetPlayerDisplayName () const
 
const std::array< unsigned char, 16 > & GetPlayerUUID () const
 
RSAGetPrivateKey () const
 
const std::string & GetPublicKey () const
 
const std::string & GetKeySignature () const
 
const long long int GetKeyTimestamp () const
 
std::vector< unsigned char > GetMessageSignature (const std::string &message, const int message_sent_index, const ProtocolCraft::UUID &chat_session_uuid, const std::vector< std::vector< unsigned char > > &last_seen, long long int &salt, long long int &timestamp)
 Compute the signature of a message.
 

Private Member Functions

void UpdateUUIDBytes ()
 Compute the UUID bytes from the string one.
 
ProtocolCraft::Json::Value GetAllCachedAccounts () const
 Get the content of the whole cache file.
 
ProtocolCraft::Json::Value GetCachedAccountOrDefault (const std::optional< std::string > &cache_key) const
 Get the cached credentials for a key.
 
void WriteCacheFile (const ProtocolCraft::Json::Value &profiles) const
 Save a profiles list to cache file.
 
std::string GetMSAToken (const std::optional< std::string > &cache_key) const
 Check if there is a saved credentials file and if the token is still valid.
 
std::string MSAAuthDeviceFlow (const std::optional< std::string > &cache_key) const
 Try to authenticate with microsoft account using device flow.
 
std::string GetXBLToken (const std::string &msa_token) const
 Try to get XBox Live token from Microsoft token.
 
std::pair< std::string, std::string > GetXSTSToken (const std::string &xbl_token) const
 Try to get XSTS token from XBL token.
 
bool GetMCToken (const std::string &xsts_token, const std::string &user_hash, const std::optional< std::string > &cache_key)
 Try to get MC token from XSTS token and user hash.
 
bool GetMCProfile (const std::optional< std::string > &cache_key)
 Try to get Minecraft profile from Minecraft token.
 
bool GetPlayerCertificates ()
 Try to get player certificates using Minecraft token.
 
const WebRequestResponse WebRequest (const std::string &host, const std::string &raw_request) const
 Send a web request with ssl stuff.
 
const WebRequestResponse POSTRequest (const std::string &host, const std::string &endpoint, const std::string &content_type, const std::string &accept, const std::string &authorization, const std::string &data) const
 Send a POST request with ssl stuff.
 
const WebRequestResponse GETRequest (const std::string &host, const std::string &endpoint, const std::string &authorization="") const
 Send a GET request with ssl stuff.
 

Private Attributes

std::string player_display_name
 
std::string mc_access_token
 
std::string mc_player_uuid
 
std::array< unsigned char, 16 > mc_player_uuid_bytes
 
RSAprivate_key = nullptr
 
std::string public_key
 
std::string key_signature
 
long long int key_timestamp
 
std::mt19937 rnd
 

Static Private Attributes

static const std::string cached_credentials_path = "botcraft_cached_credentials.json"
 Path to cache the credentials.
 
static const std::string botcraft_app_id = "a0ad834d-e78a-4881-87f6-390aa0f4b283"
 Botcraft app ID for microsoft auth.
 
static const ProtocolCraft::Json::Value defaultCachedCredentials
 Default cached credentials JSON.
 

Detailed Description

Definition at line 29 of file Authentifier.hpp.

Constructor & Destructor Documentation

◆ Authentifier()

Botcraft::Authentifier::Authentifier ( )

Definition at line 41 of file Authentifier.cpp.

References key_timestamp, mc_player_uuid_bytes, and rnd.

◆ ~Authentifier()

Botcraft::Authentifier::~Authentifier ( )

Definition at line 50 of file Authentifier.cpp.

References private_key.

Member Function Documentation

◆ AuthMCToken()

bool Botcraft::Authentifier::AuthMCToken ( const std::string &  mc_token)

Authentication using a minecraft token.

Parameters
mc_tokenMinecraft token, must be valid
Returns
True if successfully authenticated, false otherwise

Definition at line 169 of file Authentifier.cpp.

References GetMCProfile(), GetPlayerCertificates(), LOG_ERROR, LOG_INFO, mc_access_token, and UpdateUUIDBytes().

◆ AuthMicrosoft()

bool Botcraft::Authentifier::AuthMicrosoft ( const std::string &  cache_key)

Authentication using a Microsoft account, storing the credentials in the cache file.

Parameters
cache_keyused as key to identify the credentials in cache file
Returns
True if successfully authenticated, false otherwise

Definition at line 63 of file Authentifier.cpp.

References ProtocolCraft::Json::Value::contains(), ProtocolCraft::Json::Value::get_string(), GetCachedAccountOrDefault(), GetMCProfile(), GetMCToken(), GetMSAToken(), GetPlayerCertificates(), GetXBLToken(), GetXSTSToken(), ProtocolCraft::Json::Value::is_number(), ProtocolCraft::Json::Value::is_string(), LOG_ERROR, LOG_INFO, LOG_WARNING, mc_access_token, mc_player_uuid, player_display_name, and UpdateUUIDBytes().

◆ GetAllCachedAccounts()

Json::Value Botcraft::Authentifier::GetAllCachedAccounts ( ) const
private

Get the content of the whole cache file.

Returns
The content in JSON

Definition at line 459 of file Authentifier.cpp.

References cached_credentials_path.

Referenced by GetCachedAccountOrDefault(), GetMCProfile(), GetMCToken(), GetMSAToken(), and MSAAuthDeviceFlow().

◆ GetCachedAccountOrDefault()

Json::Value Botcraft::Authentifier::GetCachedAccountOrDefault ( const std::optional< std::string > &  cache_key) const
private

Get the cached credentials for a key.

Returns
Cached credentials for the given account, or default if not found in cached

Definition at line 473 of file Authentifier.cpp.

References ProtocolCraft::Json::Value::contains(), defaultCachedCredentials, GetAllCachedAccounts(), ProtocolCraft::Json::Value::is_object(), and ProtocolCraft::Json::Value::size().

Referenced by AuthMicrosoft(), GetMCProfile(), GetMCToken(), and GetMSAToken().

◆ GetKeySignature()

const std::string & Botcraft::Authentifier::GetKeySignature ( ) const

Definition at line 312 of file Authentifier.cpp.

References key_signature.

◆ GetKeyTimestamp()

const long long int Botcraft::Authentifier::GetKeyTimestamp ( ) const

Definition at line 317 of file Authentifier.cpp.

References key_timestamp.

◆ GetMCProfile()

bool Botcraft::Authentifier::GetMCProfile ( const std::optional< std::string > &  cache_key)
private

Try to get Minecraft profile from Minecraft token.

Parameters
cache_keyIf present, the values will be stored in the cache file under the given key
Returns
True if the profile was correctly updated, false otherwise

Definition at line 872 of file Authentifier.cpp.

References ProtocolCraft::Json::Value::contains(), ProtocolCraft::Json::Value::get_object(), ProtocolCraft::Json::Value::get_string(), GetAllCachedAccounts(), GetCachedAccountOrDefault(), GETRequest(), LOG_ERROR, mc_access_token, mc_player_uuid, player_display_name, Botcraft::WebRequestResponse::response, Botcraft::WebRequestResponse::status_code, Botcraft::WebRequestResponse::status_message, UpdateUUIDBytes(), and WriteCacheFile().

Referenced by AuthMCToken(), and AuthMicrosoft().

◆ GetMCToken()

bool Botcraft::Authentifier::GetMCToken ( const std::string &  xsts_token,
const std::string &  user_hash,
const std::optional< std::string > &  cache_key 
)
private

Try to get MC token from XSTS token and user hash.

Parameters
xsts_tokenXSTS Token
user_hashUser hash
cache_keyIf present, the value will be stored in the cache file under the given key
Returns
True if minecraft token was correctly updated, false otherwise

Definition at line 811 of file Authentifier.cpp.

References ProtocolCraft::Json::Value::contains(), ProtocolCraft::Json::Value::Dump(), ProtocolCraft::Json::Value::get(), ProtocolCraft::Json::Value::get_object(), ProtocolCraft::Json::Value::get_string(), GetAllCachedAccounts(), GetCachedAccountOrDefault(), LOG_ERROR, LOG_WARNING, mc_access_token, POSTRequest(), Botcraft::WebRequestResponse::response, Botcraft::WebRequestResponse::status_code, Botcraft::WebRequestResponse::status_message, and WriteCacheFile().

Referenced by AuthMicrosoft().

◆ GetMessageSignature()

std::vector< unsigned char > Botcraft::Authentifier::GetMessageSignature ( const std::string &  message,
const int  message_sent_index,
const ProtocolCraft::UUID chat_session_uuid,
const std::vector< std::vector< unsigned char > > &  last_seen,
long long int &  salt,
long long int &  timestamp 
)

Compute the signature of a message.

Parameters
messageMessage to send
message_sent_indexIndex of the message in this message chain
chat_session_uuidUUID of the chat session, as sent in ServerboundChatSessionUpdatePacket
last_seenVector of signatures of previously received messages
saltOutput salt used to generate the signature
timestampOutput timestamp in ms used to generate the signature
Returns
The message signature

Definition at line 329 of file Authentifier.cpp.

References LOG_ERROR.

◆ GetMSAToken()

std::string Botcraft::Authentifier::GetMSAToken ( const std::optional< std::string > &  cache_key) const
private

◆ GetPlayerCertificates()

bool Botcraft::Authentifier::GetPlayerCertificates ( )
private

◆ GetPlayerDisplayName()

const std::string & Botcraft::Authentifier::GetPlayerDisplayName ( ) const

Definition at line 291 of file Authentifier.cpp.

References player_display_name.

◆ GetPlayerUUID()

const std::array< unsigned char, 16 > & Botcraft::Authentifier::GetPlayerUUID ( ) const

Definition at line 296 of file Authentifier.cpp.

References mc_player_uuid_bytes.

◆ GetPrivateKey()

RSA * Botcraft::Authentifier::GetPrivateKey ( ) const

Definition at line 302 of file Authentifier.cpp.

References private_key.

◆ GetPublicKey()

const std::string & Botcraft::Authentifier::GetPublicKey ( ) const

Definition at line 307 of file Authentifier.cpp.

References public_key.

◆ GETRequest()

const WebRequestResponse Botcraft::Authentifier::GETRequest ( const std::string &  host,
const std::string &  endpoint,
const std::string &  authorization = "" 
) const
private

Send a GET request with ssl stuff.

Parameters
hostThe host address (after https:// and before the first /)
endpointThe endpoint (after the first /)
authorizationOptional authorization header, only set if not empty
Returns
A WebRequestResponse returned by the server

Definition at line 1137 of file Authentifier.cpp.

References WebRequest().

Referenced by GetMCProfile().

◆ GetXBLToken()

std::string Botcraft::Authentifier::GetXBLToken ( const std::string &  msa_token) const
private

Try to get XBox Live token from Microsoft token.

Parameters
msa_tokenMicrosoft access token
Returns
XBL token, empty if failed.

Definition at line 734 of file Authentifier.cpp.

References ProtocolCraft::Json::Value::contains(), ProtocolCraft::Json::Value::Dump(), ProtocolCraft::Json::Value::get_string(), LOG_ERROR, POSTRequest(), Botcraft::WebRequestResponse::response, Botcraft::WebRequestResponse::status_code, and Botcraft::WebRequestResponse::status_message.

Referenced by AuthMicrosoft().

◆ GetXSTSToken()

std::pair< std::string, std::string > Botcraft::Authentifier::GetXSTSToken ( const std::string &  xbl_token) const
private

◆ JoinServer()

bool Botcraft::Authentifier::JoinServer ( const std::string &  server_id,
const std::vector< unsigned char > &  shared_secret,
const std::vector< unsigned char > &  server_public_key 
) const

◆ MSAAuthDeviceFlow()

std::string Botcraft::Authentifier::MSAAuthDeviceFlow ( const std::optional< std::string > &  cache_key) const
private

Try to authenticate with microsoft account using device flow.

Parameters
cache_keyIf present, the values will be stored in the cache file under the given key
Returns
The microsoft access token, empty if failed.

Definition at line 599 of file Authentifier.cpp.

References botcraft_app_id, ProtocolCraft::Json::Value::contains(), ProtocolCraft::Json::Value::Dump(), ProtocolCraft::Json::Value::get(), ProtocolCraft::Json::Value::get_number(), ProtocolCraft::Json::Value::get_string(), GetAllCachedAccounts(), LOG_ALWAYS, LOG_ERROR, LOG_INFO, POSTRequest(), Botcraft::WebRequestResponse::response, Botcraft::WebRequestResponse::status_code, Botcraft::WebRequestResponse::status_message, and WriteCacheFile().

Referenced by GetMSAToken().

◆ POSTRequest()

const WebRequestResponse Botcraft::Authentifier::POSTRequest ( const std::string &  host,
const std::string &  endpoint,
const std::string &  content_type,
const std::string &  accept,
const std::string &  authorization,
const std::string &  data 
) const
private

Send a POST request with ssl stuff.

Parameters
hostThe host address (after https:// and before the first /)
endpointThe endpoint (after the first /)
content_typeData type
acceptAccept header value
dataActual data to send
authorizationOptional authorization header, only if not empty
Returns
A WebRequestResponse returned by the server

Definition at line 1113 of file Authentifier.cpp.

References WebRequest().

Referenced by GetMCToken(), GetMSAToken(), GetPlayerCertificates(), GetXBLToken(), GetXSTSToken(), JoinServer(), and MSAAuthDeviceFlow().

◆ UpdateUUIDBytes()

void Botcraft::Authentifier::UpdateUUIDBytes ( )
private

Compute the UUID bytes from the string one.

Definition at line 449 of file Authentifier.cpp.

References mc_player_uuid, and mc_player_uuid_bytes.

Referenced by AuthMCToken(), AuthMicrosoft(), and GetMCProfile().

◆ WebRequest()

const WebRequestResponse Botcraft::Authentifier::WebRequest ( const std::string &  host,
const std::string &  raw_request 
) const
private

Send a web request with ssl stuff.

Parameters
hostThe host address
raw_requestThe full request (header + content) as it should be sent
Returns
A WebRequestResponse returned by the server

Definition at line 1011 of file Authentifier.cpp.

References LOG_ERROR, ProtocolCraft::Json::Parse(), Botcraft::WebRequestResponse::response, Botcraft::WebRequestResponse::status_code, and Botcraft::WebRequestResponse::status_message.

Referenced by GETRequest(), and POSTRequest().

◆ WriteCacheFile()

void Botcraft::Authentifier::WriteCacheFile ( const ProtocolCraft::Json::Value profiles) const
private

Save a profiles list to cache file.

Parameters
profilesA json object with logins as keys and cache credentials as values

Definition at line 492 of file Authentifier.cpp.

References cached_credentials_path, and ProtocolCraft::Json::Value::Dump().

Referenced by GetMCProfile(), GetMCToken(), GetMSAToken(), and MSAAuthDeviceFlow().

Member Data Documentation

◆ botcraft_app_id

const std::string Botcraft::Authentifier::botcraft_app_id = "a0ad834d-e78a-4881-87f6-390aa0f4b283"
staticprivate

Botcraft app ID for microsoft auth.

Definition at line 181 of file Authentifier.hpp.

Referenced by GetMSAToken(), and MSAAuthDeviceFlow().

◆ cached_credentials_path

const std::string Botcraft::Authentifier::cached_credentials_path = "botcraft_cached_credentials.json"
staticprivate

Path to cache the credentials.

Definition at line 178 of file Authentifier.hpp.

Referenced by GetAllCachedAccounts(), and WriteCacheFile().

◆ defaultCachedCredentials

const Json::Value Botcraft::Authentifier::defaultCachedCredentials
staticprivate
Initial value:
= {
{ "msa", {
{ "access_token", nullptr },
{ "expires_date", nullptr },
{ "refresh_token", nullptr }
}},
{ "name", nullptr },
{ "id", nullptr },
{ "mc_token", nullptr },
{ "expires_date", nullptr }
}

Default cached credentials JSON.

Definition at line 184 of file Authentifier.hpp.

Referenced by GetCachedAccountOrDefault().

◆ key_signature

std::string Botcraft::Authentifier::key_signature
private

Definition at line 194 of file Authentifier.hpp.

Referenced by GetKeySignature(), and GetPlayerCertificates().

◆ key_timestamp

long long int Botcraft::Authentifier::key_timestamp
private

Definition at line 195 of file Authentifier.hpp.

Referenced by Authentifier(), GetKeyTimestamp(), and GetPlayerCertificates().

◆ mc_access_token

std::string Botcraft::Authentifier::mc_access_token
private

◆ mc_player_uuid

std::string Botcraft::Authentifier::mc_player_uuid
private

Definition at line 188 of file Authentifier.hpp.

Referenced by AuthMicrosoft(), GetMCProfile(), JoinServer(), and UpdateUUIDBytes().

◆ mc_player_uuid_bytes

std::array<unsigned char, 16> Botcraft::Authentifier::mc_player_uuid_bytes
private

Definition at line 189 of file Authentifier.hpp.

Referenced by Authentifier(), GetPlayerUUID(), and UpdateUUIDBytes().

◆ player_display_name

std::string Botcraft::Authentifier::player_display_name
private

Definition at line 186 of file Authentifier.hpp.

Referenced by AuthMicrosoft(), GetMCProfile(), and GetPlayerDisplayName().

◆ private_key

RSA* Botcraft::Authentifier::private_key = nullptr
private

Definition at line 192 of file Authentifier.hpp.

Referenced by GetPlayerCertificates(), GetPrivateKey(), and ~Authentifier().

◆ public_key

std::string Botcraft::Authentifier::public_key
private

Definition at line 193 of file Authentifier.hpp.

Referenced by GetPlayerCertificates(), and GetPublicKey().

◆ rnd

std::mt19937 Botcraft::Authentifier::rnd
private

Definition at line 197 of file Authentifier.hpp.

Referenced by Authentifier().


The documentation for this class was generated from the following files: