|
Botcraft 1.21.10
|
#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 |
| RSA * | GetPrivateKey () 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 ×tamp) |
| 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 |
| RSA * | private_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. | |
Definition at line 29 of file Authentifier.hpp.
| Botcraft::Authentifier::Authentifier | ( | ) |
Definition at line 41 of file Authentifier.cpp.
References key_timestamp, mc_player_uuid_bytes, and rnd.
| Botcraft::Authentifier::~Authentifier | ( | ) |
Definition at line 50 of file Authentifier.cpp.
References private_key.
| bool Botcraft::Authentifier::AuthMCToken | ( | const std::string & | mc_token | ) |
Authentication using a minecraft token.
| mc_token | Minecraft token, must be valid |
Definition at line 169 of file Authentifier.cpp.
References GetMCProfile(), GetPlayerCertificates(), LOG_ERROR, LOG_INFO, mc_access_token, and UpdateUUIDBytes().
| bool Botcraft::Authentifier::AuthMicrosoft | ( | const std::string & | cache_key | ) |
Authentication using a Microsoft account, storing the credentials in the cache file.
| cache_key | used as key to identify the credentials in cache file |
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().
|
private |
Get the content of the whole cache file.
Definition at line 459 of file Authentifier.cpp.
References cached_credentials_path.
Referenced by GetCachedAccountOrDefault(), GetMCProfile(), GetMCToken(), GetMSAToken(), and MSAAuthDeviceFlow().
|
private |
Get the cached credentials for a key.
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().
| const std::string & Botcraft::Authentifier::GetKeySignature | ( | ) | const |
Definition at line 312 of file Authentifier.cpp.
References key_signature.
| const long long int Botcraft::Authentifier::GetKeyTimestamp | ( | ) | const |
Definition at line 317 of file Authentifier.cpp.
References key_timestamp.
|
private |
Try to get Minecraft profile from Minecraft token.
| cache_key | If present, the values will be stored in the cache file under the given key |
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().
|
private |
Try to get MC token from XSTS token and user hash.
| xsts_token | XSTS Token |
| user_hash | User hash |
| cache_key | If present, the value will be stored in the cache file under the given key |
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().
| 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.
| message | Message to send |
| message_sent_index | Index of the message in this message chain |
| chat_session_uuid | UUID of the chat session, as sent in ServerboundChatSessionUpdatePacket |
| last_seen | Vector of signatures of previously received messages |
| salt | Output salt used to generate the signature |
| timestamp | Output timestamp in ms used to generate the signature |
Definition at line 329 of file Authentifier.cpp.
References LOG_ERROR.
|
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
| cache_key | If present, the value will be stored in the cache file under the given key |
Definition at line 503 of file Authentifier.cpp.
References botcraft_app_id, ProtocolCraft::Json::Value::contains(), ProtocolCraft::Json::Value::Dump(), ProtocolCraft::Json::Value::get(), ProtocolCraft::Json::Value::get_object(), ProtocolCraft::Json::Value::get_string(), GetAllCachedAccounts(), GetCachedAccountOrDefault(), ProtocolCraft::Json::Value::is_number(), ProtocolCraft::Json::Value::is_object(), ProtocolCraft::Json::Value::is_string(), LOG_ERROR, LOG_INFO, MSAAuthDeviceFlow(), POSTRequest(), Botcraft::WebRequestResponse::response, Botcraft::WebRequestResponse::status_code, Botcraft::WebRequestResponse::status_message, and WriteCacheFile().
Referenced by AuthMicrosoft().
|
private |
Try to get player certificates using Minecraft token.
Definition at line 936 of file Authentifier.cpp.
References ProtocolCraft::Json::Value::contains(), ProtocolCraft::Json::Value::Dump(), ProtocolCraft::Json::Value::get_string(), key_signature, key_timestamp, LOG_ERROR, LOG_INFO, mc_access_token, POSTRequest(), private_key, public_key, Botcraft::WebRequestResponse::response, Botcraft::WebRequestResponse::status_code, Botcraft::WebRequestResponse::status_message, and Botcraft::Utilities::TimestampMilliFromISO8601().
Referenced by AuthMCToken(), and AuthMicrosoft().
| const std::string & Botcraft::Authentifier::GetPlayerDisplayName | ( | ) | const |
Definition at line 291 of file Authentifier.cpp.
References player_display_name.
| const std::array< unsigned char, 16 > & Botcraft::Authentifier::GetPlayerUUID | ( | ) | const |
Definition at line 296 of file Authentifier.cpp.
References mc_player_uuid_bytes.
| RSA * Botcraft::Authentifier::GetPrivateKey | ( | ) | const |
Definition at line 302 of file Authentifier.cpp.
References private_key.
| const std::string & Botcraft::Authentifier::GetPublicKey | ( | ) | const |
Definition at line 307 of file Authentifier.cpp.
References public_key.
|
private |
Send a GET request with ssl stuff.
| host | The host address (after https:// and before the first /) |
| endpoint | The endpoint (after the first /) |
| authorization | Optional authorization header, only set if not empty |
Definition at line 1137 of file Authentifier.cpp.
References WebRequest().
Referenced by GetMCProfile().
|
private |
Try to get XBox Live token from Microsoft token.
| msa_token | Microsoft access token |
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().
|
private |
Try to get XSTS token from XBL token.
| xbl_token | XBL token |
Definition at line 769 of file Authentifier.cpp.
References ProtocolCraft::Json::Value::contains(), ProtocolCraft::Json::Value::Dump(), ProtocolCraft::Json::Value::get_string(), ProtocolCraft::Json::Value::is_array(), LOG_ERROR, POSTRequest(), Botcraft::WebRequestResponse::response, ProtocolCraft::Json::Value::size(), Botcraft::WebRequestResponse::status_code, and Botcraft::WebRequestResponse::status_message.
Referenced by AuthMicrosoft().
| 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 |
Definition at line 201 of file Authentifier.cpp.
References ProtocolCraft::Json::Value::Dump(), LOG_ERROR, mc_access_token, mc_player_uuid, POSTRequest(), Botcraft::WebRequestResponse::response, Botcraft::WebRequestResponse::status_code, and Botcraft::WebRequestResponse::status_message.
|
private |
Try to authenticate with microsoft account using device flow.
| cache_key | If present, the values will be stored in the cache file under the given key |
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().
|
private |
Send a POST request with ssl stuff.
| host | The host address (after https:// and before the first /) |
| endpoint | The endpoint (after the first /) |
| content_type | Data type |
| accept | Accept header value |
| data | Actual data to send |
| authorization | Optional authorization header, only if not empty |
Definition at line 1113 of file Authentifier.cpp.
References WebRequest().
Referenced by GetMCToken(), GetMSAToken(), GetPlayerCertificates(), GetXBLToken(), GetXSTSToken(), JoinServer(), and MSAAuthDeviceFlow().
|
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().
|
private |
Send a web request with ssl stuff.
| host | The host address |
| raw_request | The full request (header + content) as it should be sent |
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().
|
private |
Save a profiles list to cache file.
| profiles | A 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().
|
staticprivate |
Botcraft app ID for microsoft auth.
Definition at line 181 of file Authentifier.hpp.
Referenced by GetMSAToken(), and MSAAuthDeviceFlow().
|
staticprivate |
Path to cache the credentials.
Definition at line 178 of file Authentifier.hpp.
Referenced by GetAllCachedAccounts(), and WriteCacheFile().
|
staticprivate |
Default cached credentials JSON.
Definition at line 184 of file Authentifier.hpp.
Referenced by GetCachedAccountOrDefault().
|
private |
Definition at line 194 of file Authentifier.hpp.
Referenced by GetKeySignature(), and GetPlayerCertificates().
|
private |
Definition at line 195 of file Authentifier.hpp.
Referenced by Authentifier(), GetKeyTimestamp(), and GetPlayerCertificates().
|
private |
Definition at line 187 of file Authentifier.hpp.
Referenced by AuthMCToken(), AuthMicrosoft(), GetMCProfile(), GetMCToken(), GetPlayerCertificates(), and JoinServer().
|
private |
Definition at line 188 of file Authentifier.hpp.
Referenced by AuthMicrosoft(), GetMCProfile(), JoinServer(), and UpdateUUIDBytes().
|
private |
Definition at line 189 of file Authentifier.hpp.
Referenced by Authentifier(), GetPlayerUUID(), and UpdateUUIDBytes().
|
private |
Definition at line 186 of file Authentifier.hpp.
Referenced by AuthMicrosoft(), GetMCProfile(), and GetPlayerDisplayName().
|
private |
Definition at line 192 of file Authentifier.hpp.
Referenced by GetPlayerCertificates(), GetPrivateKey(), and ~Authentifier().
|
private |
Definition at line 193 of file Authentifier.hpp.
Referenced by GetPlayerCertificates(), and GetPublicKey().
|
private |
Definition at line 197 of file Authentifier.hpp.
Referenced by Authentifier().