Botcraft 1.21.4
Loading...
Searching...
No Matches
Authentifier.hpp
Go to the documentation of this file.
1#pragma once
2#include <string>
3#include <array>
4
6
7#if PROTOCOL_VERSION > 758 /* > 1.18.2 */
8#include <random>
9#endif
10#if PROTOCOL_VERSION > 759 /* > 1.19 */
12#endif
13
14namespace Botcraft
15{
22
24 {
25 public:
28
29 /// @brief Authentication using a Microsoft account. If
30 /// login is set, use it to identify the cached credentials.
31 /// @param login Login is used as key to identify the credentials in cache file
32 /// @return True if successfully authenticated, false otherwise
33 const bool AuthMicrosoft(const std::string& login);
34
35 // Join a server after encryption request arrived
36 const bool JoinServer(const std::string& server_id, const std::vector<unsigned char>& shared_secret, const std::vector<unsigned char>& public_key) const;
37
38 const std::string& GetPlayerDisplayName() const;
39 const std::array<unsigned char, 16>& GetPlayerUUID() const;
40
41#if PROTOCOL_VERSION > 758 /* > 1.18.2 */
42 const std::string& GetPrivateKey() const;
43 const std::string& GetPublicKey() const;
44 const std::string& GetKeySignature() const;
45 const long long int GetKeyTimestamp() const;
46
47#if PROTOCOL_VERSION == 759 /* 1.19 */
48 /// @brief Compute the signature of a message
49 /// @param message Message to send
50 /// @param salt Output salt used to generate the signature
51 /// @param timestamp Output timestamp in ms used to generate the signature
52 /// @return The message signature
53 const std::vector<unsigned char> GetMessageSignature(const std::string& message,
54 long long int& salt, long long int& timestamp);
55#elif PROTOCOL_VERSION == 760 /* 1.19.1/2 */
56 /// @brief Compute the signature of a message
57 /// @param message Message to send
58 /// @param previous_signature Signature of the previous message sent
59 /// @param last_seen Vector of previously received messages from players
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 const std::vector<unsigned char> GetMessageSignature(const std::string& message,
64 const std::vector<unsigned char>& previous_signature, const std::vector<ProtocolCraft::LastSeenMessagesEntry>& last_seen,
65 long long int& salt, long long int& timestamp);
66#else
67 /// @brief Compute the signature of a message
68 /// @param message Message to send
69 /// @param message_sent_index Index of the message in this message chain
70 /// @param chat_session_uuid UUID of the chat session, as sent in ServerboundChatSessionUpdatePacket
71 /// @param last_seen Vector of signatures of previously received messages
72 /// @param salt Output salt used to generate the signature
73 /// @param timestamp Output timestamp in ms used to generate the signature
74 /// @return The message signature
75 const std::vector<unsigned char> GetMessageSignature(const std::string& message,
76 const int message_sent_index, const ProtocolCraft::UUID& chat_session_uuid,
77 const std::vector<std::vector<unsigned char>>& last_seen,
78 long long int& salt, long long int& timestamp);
79#endif
80#endif
81
82 private:
83 /// @brief Compute the UUID bytes from the string one
84 void UpdateUUIDBytes();
85
86#ifdef USE_ENCRYPTION
87 /// @brief Get the content of the whole cache file
88 /// @return The content in JSON
90
91 /// @brief Try to find a cached account corresponding to login.
92 /// Only one Microsoft account can be cached using an empty login.
93 /// @param login Login of the account
94 /// @return Cached credentials for the corresponding account
95 ProtocolCraft::Json::Value GetCachedCredentials(const std::string& login) const;
96
97 /// @brief Extract the token, the name and the uuid from a server response
98 /// @param response The json response sent by the auth server
99 /// @return A tuple containing <the new token, the new name, the new uuid>, all empty if failed
100 const std::tuple<std::string, std::string, std::string> ExtractMCFromResponse(const ProtocolCraft::Json::Value& response) const;
101
102 /// @brief Check if a validity time is in the present or in the future
103 /// @param t The expiration timestamp
104 /// @return True if expired, false if valid
105 const bool IsTokenExpired(const long long int& t) const;
106
107 /// @brief Save a profiles list to cache file
108 /// @param profiles A json object with logins as keys and cache credentials as values
109 void WriteCacheFile(const ProtocolCraft::Json::Value& profiles) const;
110
111 /// @brief Update the cached MSA data for the given login
112 /// @param login The login we want to update the data for
113 /// @param access_token New access token
114 /// @param refresh_token New refresh token
115 /// @param expiration New expiration date
116 void UpdateCachedMSA(const std::string& login, const std::string& access_token,
117 const std::string& refresh_token, const long long int& expiration) const;
118
119 /// @brief Update the cached MC token data for the given login
120 /// @param login The login we want to update the data for
121 /// @param mc_token New MC token
122 /// @param expiration New token expiration date
123 void UpdateCachedMCToken(const std::string& login, const std::string& mc_token,
124 const long long int& expiration) const;
125
126 /// @brief Update the cached MC profile data for the given login
127 /// @param login The login we want to update the data for
128 /// @param name New MC name
129 /// @param id New MC uuid
130 void UpdateCachedMCProfile(const std::string& login, const std::string& name,
131 const std::string& id) const;
132
133#if PROTOCOL_VERSION > 758 /* > 1.18.2 */
134 /// @brief Update the cached player certificates for the given login
135 /// @param login The login we want to update the data for
136 /// @param private_k New private key
137 /// @param public_k New public key
138 /// @param signature_v1 Signature v1 of the given login
139 /// @param signature_v2 Signature v2 of the given login
140 /// @param expiration New keys expiration date
141 void UpdateCachedPlayerCertificates(const std::string& login, const std::string& private_k,
142 const std::string& public_k, const std::string& signature_v1,
143 const std::string& signature_v2, const long long int& expiration) const;
144#endif
145
146 /// @brief Check if there is a saved credentials file and
147 /// if the token is still valid. Refresh it if not.
148 /// If file doesn't exist, launch auth device flow
149 /// @param login Login used as key for the cached credentials
150 /// @return The microsoft access token, empty if failed.
151 const std::string GetMSAToken(const std::string& login) const;
152
153 /// @brief Try to authenticate with microsoft account using device flow.
154 /// Save the credentials to cached file if success.
155 /// @return The microsoft access token, empty if failed.
156 const std::string MSAAuthDeviceFlow(const std::string& login) const;
157
158 /// @brief Try to get XBox Live token from Microsoft token.
159 /// @param msa_token Microsoft access token
160 /// @return XBL token, empty if failed.
161 const std::string GetXBLToken(const std::string& msa_token) const;
162
163 /// @brief Try to get XSTS token from XBL token.
164 /// @param xbl_token XBL token
165 /// @return Pair of {XSTS token, userhash}, empty if failed.
166 const std::pair<std::string, std::string> GetXSTSToken(const std::string& xbl_token) const;
167
168 /// @brief Try to get MC token from XSTS token and user hash.
169 /// @param login Login used to store credentials in cache
170 /// @param xsts_token XSTS Token
171 /// @param user_hash User hash
172 /// @return Minecraft token, empty if failed.
173 const std::string GetMCToken(const std::string& login,
174 const std::string& xsts_token, const std::string& user_hash) const;
175
176 /// @brief Try to get Minecraft profile from Minecraft token
177 /// @param login Login used to store credentials in cache
178 /// @param mc_token Minecraft token
179 /// @return Pair of {MC UUID, MC name}, empty if failed.
180 const std::pair<std::string, std::string> GetMCProfile(const std::string& login,
181 const std::string& mc_token) const;
182
183#if PROTOCOL_VERSION > 758 /* > 1.18.2 */
184 /// @brief Try to get player certificates from Minecraft token
185 /// @param login Login used to store credentials in cache
186 /// @param mc_token Minecraft token
187 /// @return Tuple of {private key, public key, signature, timestamp }, empty if failed
188 const std::tuple<std::string, std::string, std::string, long long int> GetPlayerCertificates(const std::string& login,
189 const std::string& mc_token) const;
190#endif
191
192 /// @brief Send a web request with ssl stuff
193 /// @param host The host address
194 /// @param raw_request The full request (header + content) as it should be sent
195 /// @return A WebRequestResponse returned by the server
196 const WebRequestResponse WebRequest(const std::string& host, const std::string& raw_request) const;
197
198 /// @brief Send a POST request with ssl stuff
199 /// @param host The host address (after https:// and before the first /)
200 /// @param endpoint The endpoint (after the first /)
201 /// @param content_type Data type
202 /// @param accept Accept header value
203 /// @param data Actual data to send
204 /// @param authorization Optional authorization header, only if not empty
205 /// @return A WebRequestResponse returned by the server
206 const WebRequestResponse POSTRequest(const std::string& host, const std::string& endpoint,
207 const std::string& content_type, const std::string& accept,
208 const std::string& authorization, const std::string& data) const;
209
210 /// @brief Send a GET request with ssl stuff
211 /// @param host The host address (after https:// and before the first /)
212 /// @param endpoint The endpoint (after the first /)
213 /// @param authorization Optional authorization header, only set if not empty
214 /// @return A WebRequestResponse returned by the server
215 const WebRequestResponse GETRequest(const std::string& host, const std::string& endpoint,
216 const std::string& authorization = "") const;
217#endif
218
219 private:
220
221 /// @brief Path to cache the credentials
222 static const std::string cached_credentials_path;
223
224 /// @brief Botcraft app ID for microsoft auth
225 static const std::string botcraft_app_id;
226
227 /// @brief Default cached credentials JSON
229
231 std::string mc_access_token;
232 std::string mc_player_uuid;
233 std::array<unsigned char, 16> mc_player_uuid_bytes;
234
235#if PROTOCOL_VERSION > 758 /* > 1.18.2 */
236 std::string private_key;
237 std::string public_key;
238 std::string key_signature;
239 long long int key_timestamp;
240
241 std::mt19937 rnd;
242#endif
243
244 };
245}
ProtocolCraft::Json::Value GetCachedProfiles() const
Get the content of the whole cache file.
const std::string & GetPrivateKey() const
ProtocolCraft::Json::Value GetCachedCredentials(const std::string &login) const
Try to find a cached account corresponding to 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.
const WebRequestResponse WebRequest(const std::string &host, const std::string &raw_request) const
Send a web request with ssl stuff.
const std::string GetXBLToken(const std::string &msa_token) const
Try to get XBox Live token from Microsoft token.
std::string player_display_name
const std::string MSAAuthDeviceFlow(const std::string &login) const
Try to authenticate with microsoft account using device flow.
const bool IsTokenExpired(const long long int &t) const
Check if a validity time is in the present or in the future.
const std::string GetMSAToken(const std::string &login) const
Check if there is a saved credentials file and if the token is still valid.
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.
const bool AuthMicrosoft(const std::string &login)
Authentication using a Microsoft account.
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.
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.
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 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.
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 > GetXSTSToken(const std::string &xbl_token) const
Try to get XSTS token from XBL token.
const std::array< unsigned char, 16 > & GetPlayerUUID() const
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.
std::array< unsigned char, 16 > mc_player_uuid_bytes
const std::string & GetPublicKey() const
void WriteCacheFile(const ProtocolCraft::Json::Value &profiles) const
Save a profiles list to cache file.
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.
const bool JoinServer(const std::string &server_id, const std::vector< unsigned char > &shared_secret, const std::vector< unsigned char > &public_key) const
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::pair< std::string, std::string > GetMCProfile(const std::string &login, const std::string &mc_token) const
Try to get Minecraft profile from Minecraft token.
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.
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