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

#include <Authentifier.hpp>

Public Member Functions

 Authentifier ()
 
 ~Authentifier ()
 
const bool AuthMicrosoft (const std::string &login)
 Authentication using a Microsoft account.
 
const bool JoinServer (const std::string &server_id, const std::vector< unsigned char > &shared_secret, const std::vector< unsigned char > &public_key) const
 
const std::string & GetPlayerDisplayName () const
 
const std::array< unsigned char, 16 > & GetPlayerUUID () const
 
const std::string & GetPrivateKey () const
 
const std::string & GetPublicKey () const
 
const std::string & GetKeySignature () const
 
const long long int GetKeyTimestamp () const
 
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 GetCachedProfiles () const
 Get the content of the whole cache file.
 
ProtocolCraft::Json::Value GetCachedCredentials (const std::string &login) const
 Try to find a cached account corresponding to login.
 
const std::tuple< std::string, std::string, std::string > ExtractMCFromResponse (const ProtocolCraft::Json::Value &response) const
 Extract the token, the name and the uuid from a server response.
 
const bool IsTokenExpired (const long long int &t) const
 Check if a validity time is in the present or in the future.
 
void WriteCacheFile (const ProtocolCraft::Json::Value &profiles) const
 Save a profiles list to cache file.
 
void UpdateCachedMSA (const std::string &login, const std::string &access_token, const std::string &refresh_token, const long long int &expiration) const
 Update the cached MSA data for the given login.
 
void UpdateCachedMCToken (const std::string &login, const std::string &mc_token, const long long int &expiration) const
 Update the cached MC token data for the given login.
 
void UpdateCachedMCProfile (const std::string &login, const std::string &name, const std::string &id) const
 Update the cached MC profile data for the given login.
 
void UpdateCachedPlayerCertificates (const std::string &login, const std::string &private_k, const std::string &public_k, const std::string &signature_v1, const std::string &signature_v2, const long long int &expiration) const
 Update the cached player certificates for the given login.
 
const std::string GetMSAToken (const std::string &login) const
 Check if there is a saved credentials file and if the token is still valid.
 
const std::string MSAAuthDeviceFlow (const std::string &login) const
 Try to authenticate with microsoft account using device flow.
 
const std::string GetXBLToken (const std::string &msa_token) const
 Try to get XBox Live token from Microsoft token.
 
const std::pair< std::string, std::string > GetXSTSToken (const std::string &xbl_token) const
 Try to get XSTS token from XBL token.
 
const std::string GetMCToken (const std::string &login, const std::string &xsts_token, const std::string &user_hash) const
 Try to get MC token from XSTS token and user hash.
 
const std::pair< std::string, std::string > GetMCProfile (const std::string &login, const std::string &mc_token) const
 Try to get Minecraft profile from Minecraft token.
 
const std::tuple< std::string, std::string, std::string, long long int > GetPlayerCertificates (const std::string &login, const std::string &mc_token) const
 Try to get player certificates from 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
 
std::string private_key
 
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 23 of file Authentifier.hpp.

Constructor & Destructor Documentation

◆ Authentifier()

Botcraft::Authentifier::Authentifier ( )

Definition at line 49 of file Authentifier.cpp.

References key_timestamp, mc_player_uuid_bytes, and rnd.

◆ ~Authentifier()

Botcraft::Authentifier::~Authentifier ( )

Definition at line 58 of file Authentifier.cpp.

Member Function Documentation

◆ AuthMicrosoft()

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

Authentication using a Microsoft account.

If login is set, use it to identify the cached credentials.

Parameters
loginLogin is used 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(), GetCachedCredentials(), GetMCProfile(), GetMCToken(), GetMSAToken(), GetPlayerCertificates(), GetXBLToken(), GetXSTSToken(), ProtocolCraft::Json::Value::is_number(), ProtocolCraft::Json::Value::is_string(), IsTokenExpired(), key_signature, key_timestamp, LOG_ERROR, LOG_INFO, LOG_WARNING, mc_access_token, mc_player_uuid, player_display_name, private_key, public_key, and UpdateUUIDBytes().

◆ ExtractMCFromResponse()

const std::tuple< std::string, std::string, std::string > Botcraft::Authentifier::ExtractMCFromResponse ( const ProtocolCraft::Json::Value response) const
private

Extract the token, the name and the uuid from a server response.

Parameters
responseThe json response sent by the auth server
Returns
A tuple containing <the new token, the new name, the new uuid>, all empty if failed

Definition at line 469 of file Authentifier.cpp.

References ProtocolCraft::Json::Value::contains(), ProtocolCraft::Json::Value::get_object(), ProtocolCraft::Json::Value::get_string(), and LOG_ERROR.

◆ GetCachedCredentials()

Json::Value Botcraft::Authentifier::GetCachedCredentials ( const std::string &  login) const
private

Try to find a cached account corresponding to login.

Only one Microsoft account can be cached using an empty login.

Parameters
loginLogin of the account
Returns
Cached credentials for the corresponding account

Definition at line 455 of file Authentifier.cpp.

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

Referenced by AuthMicrosoft(), GetMSAToken(), and GetPlayerCertificates().

◆ GetCachedProfiles()

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

Get the content of the whole cache file.

Returns
The content in JSON

Definition at line 441 of file Authentifier.cpp.

References cached_credentials_path.

Referenced by GetCachedCredentials(), UpdateCachedMCProfile(), UpdateCachedMCToken(), UpdateCachedMSA(), and UpdateCachedPlayerCertificates().

◆ GetKeySignature()

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

Definition at line 283 of file Authentifier.cpp.

References key_signature.

◆ GetKeyTimestamp()

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

Definition at line 288 of file Authentifier.cpp.

References key_timestamp.

◆ GetMCProfile()

const std::pair< std::string, std::string > Botcraft::Authentifier::GetMCProfile ( const std::string &  login,
const std::string &  mc_token 
) const
private

Try to get Minecraft profile from Minecraft token.

Parameters
loginLogin used to store credentials in cache
mc_tokenMinecraft token
Returns
Pair of {MC UUID, MC name}, empty if failed.

Definition at line 1017 of file Authentifier.cpp.

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

Referenced by AuthMicrosoft().

◆ GetMCToken()

const std::string Botcraft::Authentifier::GetMCToken ( const std::string &  login,
const std::string &  xsts_token,
const std::string &  user_hash 
) const
private

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

Parameters
loginLogin used to store credentials in cache
xsts_tokenXSTS Token
user_hashUser hash
Returns
Minecraft token, empty if failed.

Definition at line 977 of file Authentifier.cpp.

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

Referenced by AuthMicrosoft().

◆ GetMessageSignature()

const 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 301 of file Authentifier.cpp.

References LOG_ERROR.

◆ GetMSAToken()

const std::string Botcraft::Authentifier::GetMSAToken ( const std::string &  login) const
private

Check if there is a saved credentials file and if the token is still valid.

Refresh it if not. If file doesn't exist, launch auth device flow

Parameters
loginLogin used as key for the cached credentials
Returns
The microsoft access token, empty if failed.

Definition at line 685 of file Authentifier.cpp.

References botcraft_app_id, ProtocolCraft::Json::Value::contains(), ProtocolCraft::Json::Value::Dump(), ProtocolCraft::Json::Value::get_string(), GetCachedCredentials(), ProtocolCraft::Json::Value::is_number(), ProtocolCraft::Json::Value::is_object(), ProtocolCraft::Json::Value::is_string(), IsTokenExpired(), LOG_ERROR, LOG_INFO, LOG_WARNING, MSAAuthDeviceFlow(), POSTRequest(), Botcraft::WebRequestResponse::response, Botcraft::WebRequestResponse::status_code, Botcraft::WebRequestResponse::status_message, and UpdateCachedMSA().

Referenced by AuthMicrosoft().

◆ GetPlayerCertificates()

const std::tuple< std::string, std::string, std::string, long long int > Botcraft::Authentifier::GetPlayerCertificates ( const std::string &  login,
const std::string &  mc_token 
) const
private

◆ GetPlayerDisplayName()

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

Definition at line 262 of file Authentifier.cpp.

References player_display_name.

◆ GetPlayerUUID()

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

Definition at line 267 of file Authentifier.cpp.

References mc_player_uuid_bytes.

◆ GetPrivateKey()

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

Definition at line 273 of file Authentifier.cpp.

References private_key.

◆ GetPublicKey()

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

Definition at line 278 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 1298 of file Authentifier.cpp.

References WebRequest().

Referenced by GetMCProfile().

◆ GetXBLToken()

const 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 900 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()

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

◆ IsTokenExpired()

const bool Botcraft::Authentifier::IsTokenExpired ( const long long int &  t) const
private

Check if a validity time is in the present or in the future.

Parameters
tThe expiration timestamp
Returns
True if expired, false if valid

Definition at line 506 of file Authentifier.cpp.

Referenced by AuthMicrosoft(), GetMSAToken(), and GetPlayerCertificates().

◆ JoinServer()

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

◆ MSAAuthDeviceFlow()

const std::string Botcraft::Authentifier::MSAAuthDeviceFlow ( const std::string &  login) const
private

Try to authenticate with microsoft account using device flow.

Save the credentials to cached file if success.

Returns
The microsoft access token, empty if failed.

Definition at line 767 of file Authentifier.cpp.

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

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 1274 of file Authentifier.cpp.

References WebRequest().

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

◆ UpdateCachedMCProfile()

void Botcraft::Authentifier::UpdateCachedMCProfile ( const std::string &  login,
const std::string &  name,
const std::string &  id 
) const
private

Update the cached MC profile data for the given login.

Parameters
loginThe login we want to update the data for
nameNew MC name
idNew MC uuid

Definition at line 594 of file Authentifier.cpp.

References ProtocolCraft::Json::Value::contains(), defaultCachedCredentials, GetCachedProfiles(), and WriteCacheFile().

Referenced by GetMCProfile().

◆ UpdateCachedMCToken()

void Botcraft::Authentifier::UpdateCachedMCToken ( const std::string &  login,
const std::string &  mc_token,
const long long int &  expiration 
) const
private

Update the cached MC token data for the given login.

Parameters
loginThe login we want to update the data for
mc_tokenNew MC token
expirationNew token expiration date

Definition at line 563 of file Authentifier.cpp.

References ProtocolCraft::Json::Value::contains(), defaultCachedCredentials, GetCachedProfiles(), and WriteCacheFile().

Referenced by GetMCToken().

◆ UpdateCachedMSA()

void Botcraft::Authentifier::UpdateCachedMSA ( const std::string &  login,
const std::string &  access_token,
const std::string &  refresh_token,
const long long int &  expiration 
) const
private

Update the cached MSA data for the given login.

Parameters
loginThe login we want to update the data for
access_tokenNew access token
refresh_tokenNew refresh token
expirationNew expiration date

Definition at line 522 of file Authentifier.cpp.

References ProtocolCraft::Json::Value::contains(), defaultCachedCredentials, GetCachedProfiles(), and WriteCacheFile().

Referenced by GetMSAToken(), and MSAAuthDeviceFlow().

◆ UpdateCachedPlayerCertificates()

void Botcraft::Authentifier::UpdateCachedPlayerCertificates ( const std::string &  login,
const std::string &  private_k,
const std::string &  public_k,
const std::string &  signature_v1,
const std::string &  signature_v2,
const long long int &  expiration 
) const
private

Update the cached player certificates for the given login.

Parameters
loginThe login we want to update the data for
private_kNew private key
public_kNew public key
signature_v1Signature v1 of the given login
signature_v2Signature v2 of the given login
expirationNew keys expiration date

Definition at line 625 of file Authentifier.cpp.

References ProtocolCraft::Json::Value::contains(), defaultCachedCredentials, GetCachedProfiles(), and WriteCacheFile().

Referenced by GetPlayerCertificates().

◆ UpdateUUIDBytes()

void Botcraft::Authentifier::UpdateUUIDBytes ( )
private

Compute the UUID bytes from the string one.

Definition at line 431 of file Authentifier.cpp.

References mc_player_uuid, and mc_player_uuid_bytes.

Referenced by AuthMicrosoft().

◆ 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 1172 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 511 of file Authentifier.cpp.

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

Referenced by UpdateCachedMCProfile(), UpdateCachedMCToken(), UpdateCachedMSA(), and UpdateCachedPlayerCertificates().

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 225 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 222 of file Authentifier.hpp.

Referenced by GetCachedProfiles(), 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 },
{ "certificates", {
{ "private_key", nullptr },
{ "public_key", nullptr },
{ "expires_date", nullptr },
{ "signature_v1", nullptr },
{ "signature_v2", nullptr }
}}
}

Default cached credentials JSON.

Definition at line 228 of file Authentifier.hpp.

Referenced by GetCachedCredentials(), UpdateCachedMCProfile(), UpdateCachedMCToken(), UpdateCachedMSA(), and UpdateCachedPlayerCertificates().

◆ key_signature

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

Definition at line 238 of file Authentifier.hpp.

Referenced by AuthMicrosoft(), and GetKeySignature().

◆ key_timestamp

long long int Botcraft::Authentifier::key_timestamp
private

Definition at line 239 of file Authentifier.hpp.

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

◆ mc_access_token

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

Definition at line 231 of file Authentifier.hpp.

Referenced by AuthMicrosoft(), and JoinServer().

◆ mc_player_uuid

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

Definition at line 232 of file Authentifier.hpp.

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

◆ mc_player_uuid_bytes

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

Definition at line 233 of file Authentifier.hpp.

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

◆ player_display_name

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

Definition at line 230 of file Authentifier.hpp.

Referenced by AuthMicrosoft(), and GetPlayerDisplayName().

◆ private_key

std::string Botcraft::Authentifier::private_key
private

Definition at line 236 of file Authentifier.hpp.

Referenced by AuthMicrosoft(), and GetPrivateKey().

◆ public_key

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

Definition at line 237 of file Authentifier.hpp.

Referenced by AuthMicrosoft(), GetPublicKey(), and JoinServer().

◆ rnd

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

Definition at line 241 of file Authentifier.hpp.

Referenced by Authentifier().


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