Botcraft 1.21.10
Loading...
Searching...
No Matches
Authentifier.hpp
Go to the documentation of this file.
1#pragma once
2#include <array>
3#include <optional>
4#include <string>
5
7
8#if PROTOCOL_VERSION > 758 /* > 1.18.2 */
9#include <random>
10#endif
11#if PROTOCOL_VERSION > 759 /* > 1.19 */
13#endif
14
15#if PROTOCOL_VERSION > 758 /* > 1.18.2 */
16struct rsa_st;
17typedef struct rsa_st RSA;
18#endif
19
20namespace Botcraft
21{
28
30 {
31 public:
34
35 /// @brief Authentication using a Microsoft account, storing the credentials in the cache file
36 /// @param cache_key used as key to identify the credentials in cache file
37 /// @return True if successfully authenticated, false otherwise
38 bool AuthMicrosoft(const std::string& cache_key);
39
40 /// @brief Authentication using a minecraft token
41 /// @param mc_token Minecraft token, must be valid
42 /// @return True if successfully authenticated, false otherwise
43 bool AuthMCToken(const std::string& mc_token);
44
45 // Join a server after encryption request
46 bool JoinServer(const std::string& server_id, const std::vector<unsigned char>& shared_secret, const std::vector<unsigned char>& server_public_key) const;
47
48 const std::string& GetPlayerDisplayName() const;
49 const std::array<unsigned char, 16>& GetPlayerUUID() const;
50
51#if PROTOCOL_VERSION > 758 /* > 1.18.2 */
52 RSA* GetPrivateKey() const;
53 const std::string& GetPublicKey() const;
54 const std::string& GetKeySignature() const;
55 const long long int GetKeyTimestamp() const;
56
57#if PROTOCOL_VERSION == 759 /* 1.19 */
58 /// @brief Compute the signature of a message
59 /// @param message Message to send
60 /// @param salt Output salt used to generate the signature
61 /// @param timestamp Output timestamp in ms used to generate the signature
62 /// @return The message signature
63 std::vector<unsigned char> GetMessageSignature(const std::string& message, long long int& salt, long long int& timestamp);
64#elif PROTOCOL_VERSION == 760 /* 1.19.1/2 */
65 /// @brief Compute the signature of a message
66 /// @param message Message to send
67 /// @param previous_signature Signature of the previous message sent
68 /// @param last_seen Vector of previously received messages from players
69 /// @param salt Output salt used to generate the signature
70 /// @param timestamp Output timestamp in ms used to generate the signature
71 /// @return The message signature
72 std::vector<unsigned char> GetMessageSignature(const std::string& message,
73 const std::vector<unsigned char>& previous_signature, const std::vector<ProtocolCraft::LastSeenMessagesEntry>& last_seen,
74 long long int& salt, long long int& timestamp);
75#else
76 /// @brief Compute the signature of a message
77 /// @param message Message to send
78 /// @param message_sent_index Index of the message in this message chain
79 /// @param chat_session_uuid UUID of the chat session, as sent in ServerboundChatSessionUpdatePacket
80 /// @param last_seen Vector of signatures of previously received messages
81 /// @param salt Output salt used to generate the signature
82 /// @param timestamp Output timestamp in ms used to generate the signature
83 /// @return The message signature
84 std::vector<unsigned char> GetMessageSignature(const std::string& message,
85 const int message_sent_index, const ProtocolCraft::UUID& chat_session_uuid,
86 const std::vector<std::vector<unsigned char>>& last_seen,
87 long long int& salt, long long int& timestamp);
88#endif
89#endif
90
91 private:
92 /// @brief Compute the UUID bytes from the string one
93 void UpdateUUIDBytes();
94
95#ifdef USE_ENCRYPTION
96 /// @brief Get the content of the whole cache file
97 /// @return The content in JSON
99
100 /// @brief Get the cached credentials for a key
101 /// @return Cached credentials for the given account, or default if not found in cached
102 ProtocolCraft::Json::Value GetCachedAccountOrDefault(const std::optional<std::string>& cache_key) const;
103
104 /// @brief Save a profiles list to cache file
105 /// @param profiles A json object with logins as keys and cache credentials as values
106 void WriteCacheFile(const ProtocolCraft::Json::Value& profiles) const;
107
108 /// @brief Check if there is a saved credentials file and
109 /// if the token is still valid. Refresh it if not.
110 /// If file doesn't exist, launch auth device flow
111 /// @param cache_key If present, the value will be stored in the cache file under the given key
112 /// @return The microsoft access token, empty if failed.
113 std::string GetMSAToken(const std::optional<std::string>& cache_key) const;
114
115 /// @brief Try to authenticate with microsoft account using device flow.
116 /// @param cache_key If present, the values will be stored in the cache file under the given key
117 /// @return The microsoft access token, empty if failed.
118 std::string MSAAuthDeviceFlow(const std::optional<std::string>& cache_key) const;
119
120 /// @brief Try to get XBox Live token from Microsoft token.
121 /// @param msa_token Microsoft access token
122 /// @return XBL token, empty if failed.
123 std::string GetXBLToken(const std::string& msa_token) const;
124
125 /// @brief Try to get XSTS token from XBL token.
126 /// @param xbl_token XBL token
127 /// @return Pair of {XSTS token, userhash}, empty if failed.
128 std::pair<std::string, std::string> GetXSTSToken(const std::string& xbl_token) const;
129
130 /// @brief Try to get MC token from XSTS token and user hash
131 /// @param xsts_token XSTS Token
132 /// @param user_hash User hash
133 /// @param cache_key If present, the value will be stored in the cache file under the given key
134 /// @return True if minecraft token was correctly updated, false otherwise
135 bool GetMCToken(const std::string& xsts_token, const std::string& user_hash, const std::optional<std::string>& cache_key);
136
137 /// @brief Try to get Minecraft profile from Minecraft token
138 /// @param cache_key If present, the values will be stored in the cache file under the given key
139 /// @return True if the profile was correctly updated, false otherwise
140 bool GetMCProfile(const std::optional<std::string>& cache_key);
141
142#if PROTOCOL_VERSION > 758 /* > 1.18.2 */
143 /// @brief Try to get player certificates using Minecraft token
144 /// @return True if the certificates were correctly fetched, false otherwise
146#endif
147
148 /// @brief Send a web request with ssl stuff
149 /// @param host The host address
150 /// @param raw_request The full request (header + content) as it should be sent
151 /// @return A WebRequestResponse returned by the server
152 const WebRequestResponse WebRequest(const std::string& host, const std::string& raw_request) const;
153
154 /// @brief Send a POST request with ssl stuff
155 /// @param host The host address (after https:// and before the first /)
156 /// @param endpoint The endpoint (after the first /)
157 /// @param content_type Data type
158 /// @param accept Accept header value
159 /// @param data Actual data to send
160 /// @param authorization Optional authorization header, only if not empty
161 /// @return A WebRequestResponse returned by the server
162 const WebRequestResponse POSTRequest(const std::string& host, const std::string& endpoint,
163 const std::string& content_type, const std::string& accept,
164 const std::string& authorization, const std::string& data) const;
165
166 /// @brief Send a GET request with ssl stuff
167 /// @param host The host address (after https:// and before the first /)
168 /// @param endpoint The endpoint (after the first /)
169 /// @param authorization Optional authorization header, only set if not empty
170 /// @return A WebRequestResponse returned by the server
171 const WebRequestResponse GETRequest(const std::string& host, const std::string& endpoint,
172 const std::string& authorization = "") const;
173#endif
174
175 private:
176
177 /// @brief Path to cache the credentials
178 static const std::string cached_credentials_path;
179
180 /// @brief Botcraft app ID for microsoft auth
181 static const std::string botcraft_app_id;
182
183 /// @brief Default cached credentials JSON
185
187 std::string mc_access_token;
188 std::string mc_player_uuid;
189 std::array<unsigned char, 16> mc_player_uuid_bytes;
190
191#if PROTOCOL_VERSION > 758 /* > 1.18.2 */
192 RSA* private_key = nullptr;
193 std::string public_key;
194 std::string key_signature;
195 long long int key_timestamp;
196
197 std::mt19937 rnd;
198#endif
199
200 };
201}
struct rsa_st RSA
bool GetPlayerCertificates()
Try to get player certificates using Minecraft token.
bool GetMCProfile(const std::optional< std::string > &cache_key)
Try to get Minecraft profile from 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
std::string GetXBLToken(const std::string &msa_token) const
Try to get XBox Live token from Microsoft token.
bool AuthMicrosoft(const std::string &cache_key)
Authentication using a Microsoft account, storing the credentials in the cache file.
const WebRequestResponse WebRequest(const std::string &host, const std::string &raw_request) const
Send a web request with ssl stuff.
ProtocolCraft::Json::Value GetAllCachedAccounts() const
Get the content of the whole cache file.
std::pair< std::string, std::string > GetXSTSToken(const std::string &xbl_token) const
Try to get XSTS token from XBL token.
std::string player_display_name
static const std::string cached_credentials_path
Path to cache the credentials.
static const std::string botcraft_app_id
Botcraft app ID for microsoft auth.
const std::string & GetPlayerDisplayName() const
long long int key_timestamp
void UpdateUUIDBytes()
Compute the UUID bytes from the string one.
static const ProtocolCraft::Json::Value defaultCachedCredentials
Default cached credentials JSON.
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.
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.
const std::string & GetKeySignature() const
const long long int GetKeyTimestamp() const
const WebRequestResponse GETRequest(const std::string &host, const std::string &endpoint, const std::string &authorization="") const
Send a GET request with ssl stuff.
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.
const std::array< unsigned char, 16 > & GetPlayerUUID() const
RSA * GetPrivateKey() const
std::array< unsigned char, 16 > mc_player_uuid_bytes
std::string MSAAuthDeviceFlow(const std::optional< std::string > &cache_key) const
Try to authenticate with microsoft account using device flow.
const std::string & GetPublicKey() 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.
void WriteCacheFile(const ProtocolCraft::Json::Value &profiles) const
Save a profiles list to cache file.
ProtocolCraft::Json::Value GetCachedAccountOrDefault(const std::optional< std::string > &cache_key) const
Get the cached credentials for a key.
bool AuthMCToken(const std::string &mc_token)
Authentication using a minecraft token.
Main class, basically a JsonVariant with extra utility functions it doesn't inherit JsonVariant direc...
Definition Json.hpp:45
std::array< unsigned char, 16 > UUID
ProtocolCraft::Json::Value response