![]() |
Botcraft 1.21.4
|
#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 ×tamp) |
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. | |
Definition at line 23 of file Authentifier.hpp.
Botcraft::Authentifier::Authentifier | ( | ) |
Definition at line 49 of file Authentifier.cpp.
References key_timestamp, mc_player_uuid_bytes, and rnd.
Botcraft::Authentifier::~Authentifier | ( | ) |
Definition at line 58 of file Authentifier.cpp.
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.
login | Login is 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(), 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().
|
private |
Extract the token, the name and the uuid from a server response.
response | The json response sent by the auth server |
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.
|
private |
Try to find a cached account corresponding to login.
Only one Microsoft account can be cached using an empty login.
login | Login of the 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().
|
private |
Get the content of the whole cache file.
Definition at line 441 of file Authentifier.cpp.
References cached_credentials_path.
Referenced by GetCachedCredentials(), UpdateCachedMCProfile(), UpdateCachedMCToken(), UpdateCachedMSA(), and UpdateCachedPlayerCertificates().
const std::string & Botcraft::Authentifier::GetKeySignature | ( | ) | const |
Definition at line 283 of file Authentifier.cpp.
References key_signature.
const long long int Botcraft::Authentifier::GetKeyTimestamp | ( | ) | const |
Definition at line 288 of file Authentifier.cpp.
References key_timestamp.
|
private |
Try to get Minecraft profile from Minecraft token.
login | Login used to store credentials in cache |
mc_token | Minecraft token |
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().
|
private |
Try to get MC token from XSTS token and user hash.
login | Login used to store credentials in cache |
xsts_token | XSTS Token |
user_hash | User hash |
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().
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.
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 301 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
login | Login used as key for the cached credentials |
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().
|
private |
Try to get player certificates from Minecraft token.
login | Login used to store credentials in cache |
mc_token | Minecraft token |
Definition at line 1056 of file Authentifier.cpp.
References ProtocolCraft::Json::Value::contains(), ProtocolCraft::Json::Value::Dump(), ProtocolCraft::Json::Value::get(), 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, POSTRequest(), Botcraft::WebRequestResponse::response, Botcraft::WebRequestResponse::status_code, Botcraft::WebRequestResponse::status_message, Botcraft::Utilities::TimestampMilliFromISO8601(), and UpdateCachedPlayerCertificates().
Referenced by AuthMicrosoft().
const std::string & Botcraft::Authentifier::GetPlayerDisplayName | ( | ) | const |
Definition at line 262 of file Authentifier.cpp.
References player_display_name.
const std::array< unsigned char, 16 > & Botcraft::Authentifier::GetPlayerUUID | ( | ) | const |
Definition at line 267 of file Authentifier.cpp.
References mc_player_uuid_bytes.
const std::string & Botcraft::Authentifier::GetPrivateKey | ( | ) | const |
Definition at line 273 of file Authentifier.cpp.
References private_key.
const std::string & Botcraft::Authentifier::GetPublicKey | ( | ) | const |
Definition at line 278 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 1298 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 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().
|
private |
Try to get XSTS token from XBL token.
xbl_token | XBL token |
Definition at line 935 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().
|
private |
Check if a validity time is in the present or in the future.
t | The expiration timestamp |
Definition at line 506 of file Authentifier.cpp.
Referenced by AuthMicrosoft(), GetMSAToken(), and GetPlayerCertificates().
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 |
Definition at line 172 of file Authentifier.cpp.
References ProtocolCraft::Json::Value::Dump(), LOG_ERROR, mc_access_token, mc_player_uuid, POSTRequest(), public_key, Botcraft::WebRequestResponse::response, Botcraft::WebRequestResponse::status_code, and Botcraft::WebRequestResponse::status_message.
|
private |
Try to authenticate with microsoft account using device flow.
Save the credentials to cached file if success.
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().
|
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 1274 of file Authentifier.cpp.
References WebRequest().
Referenced by GetMCToken(), GetMSAToken(), GetPlayerCertificates(), GetXBLToken(), GetXSTSToken(), JoinServer(), and MSAAuthDeviceFlow().
|
private |
Update the cached MC profile data for the given login.
login | The login we want to update the data for |
name | New MC name |
id | New MC uuid |
Definition at line 594 of file Authentifier.cpp.
References ProtocolCraft::Json::Value::contains(), defaultCachedCredentials, GetCachedProfiles(), and WriteCacheFile().
Referenced by GetMCProfile().
|
private |
Update the cached MC token data for the given login.
login | The login we want to update the data for |
mc_token | New MC token |
expiration | New token expiration date |
Definition at line 563 of file Authentifier.cpp.
References ProtocolCraft::Json::Value::contains(), defaultCachedCredentials, GetCachedProfiles(), and WriteCacheFile().
Referenced by GetMCToken().
|
private |
Update the cached MSA data for the given login.
login | The login we want to update the data for |
access_token | New access token |
refresh_token | New refresh token |
expiration | New expiration date |
Definition at line 522 of file Authentifier.cpp.
References ProtocolCraft::Json::Value::contains(), defaultCachedCredentials, GetCachedProfiles(), and WriteCacheFile().
Referenced by GetMSAToken(), and MSAAuthDeviceFlow().
|
private |
Update the cached player certificates for the given login.
login | The login we want to update the data for |
private_k | New private key |
public_k | New public key |
signature_v1 | Signature v1 of the given login |
signature_v2 | Signature v2 of the given login |
expiration | New keys expiration date |
Definition at line 625 of file Authentifier.cpp.
References ProtocolCraft::Json::Value::contains(), defaultCachedCredentials, GetCachedProfiles(), and WriteCacheFile().
Referenced by GetPlayerCertificates().
|
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().
|
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 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().
|
private |
Save a profiles list to cache file.
profiles | A 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().
|
staticprivate |
Botcraft app ID for microsoft auth.
Definition at line 225 of file Authentifier.hpp.
Referenced by GetMSAToken(), and MSAAuthDeviceFlow().
|
staticprivate |
Path to cache the credentials.
Definition at line 222 of file Authentifier.hpp.
Referenced by GetCachedProfiles(), and WriteCacheFile().
|
staticprivate |
Default cached credentials JSON.
Definition at line 228 of file Authentifier.hpp.
Referenced by GetCachedCredentials(), UpdateCachedMCProfile(), UpdateCachedMCToken(), UpdateCachedMSA(), and UpdateCachedPlayerCertificates().
|
private |
Definition at line 238 of file Authentifier.hpp.
Referenced by AuthMicrosoft(), and GetKeySignature().
|
private |
Definition at line 239 of file Authentifier.hpp.
Referenced by Authentifier(), AuthMicrosoft(), and GetKeyTimestamp().
|
private |
Definition at line 231 of file Authentifier.hpp.
Referenced by AuthMicrosoft(), and JoinServer().
|
private |
Definition at line 232 of file Authentifier.hpp.
Referenced by AuthMicrosoft(), JoinServer(), and UpdateUUIDBytes().
|
private |
Definition at line 233 of file Authentifier.hpp.
Referenced by Authentifier(), GetPlayerUUID(), and UpdateUUIDBytes().
|
private |
Definition at line 230 of file Authentifier.hpp.
Referenced by AuthMicrosoft(), and GetPlayerDisplayName().
|
private |
Definition at line 236 of file Authentifier.hpp.
Referenced by AuthMicrosoft(), and GetPrivateKey().
|
private |
Definition at line 237 of file Authentifier.hpp.
Referenced by AuthMicrosoft(), GetPublicKey(), and JoinServer().
|
private |
Definition at line 241 of file Authentifier.hpp.
Referenced by Authentifier().