PocketMine-MP 5.41.2 git-e73f84662e3363c758c5cd2380d3130518867133
Loading...
Searching...
No Matches
LoginPacketHandler.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\network\mcpe\handler;
25
37use pocketmine\network\mcpe\protocol\types\login\AuthenticationType;
50use Ramsey\Uuid\Uuid;
51use Ramsey\Uuid\UuidInterface;
52use function chr;
53use function count;
54use function gettype;
55use function is_array;
56use function is_object;
57use function json_decode;
58use function md5;
59use function ord;
60use function substr;
61use const JSON_THROW_ON_ERROR;
62
71 public function __construct(
72 private Server $server,
73 private NetworkSession $session,
74 private \Closure $playerInfoConsumer,
75 private \Closure $authCallback
76 ){}
77
78 private static function calculateUuidFromXuid(string $xuid) : UuidInterface{
79 $hash = md5("pocket-auth-1-xuid:" . $xuid, binary: true);
80 $hash[6] = chr((ord($hash[6]) & 0x0f) | 0x30); // set version to 3
81 $hash[8] = chr((ord($hash[8]) & 0x3f) | 0x80); // set variant to RFC 4122
82
83 return Uuid::fromBytes($hash);
84 }
85
86 public function handleLogin(LoginPacket $packet) : bool{
87 $authInfo = $this->parseAuthInfo($packet->authInfoJson);
88
89 if($authInfo->AuthenticationType === AuthenticationType::FULL->value){
90 try{
91 [$headerArray, $claimsArray,] = JwtUtils::parse($authInfo->Token);
92 }catch(JwtException $e){
93 throw PacketHandlingException::wrap($e, "Error parsing authentication token");
94 }
95 $header = $this->mapXboxTokenHeader($headerArray);
96 $claims = $this->mapXboxTokenBody($claimsArray);
97
98 $legacyUuid = self::calculateUuidFromXuid($claims->xid);
99 $username = $claims->xname;
100 $xuid = $claims->xid;
101
102 $authRequired = $this->processLoginCommon($packet, $username, $legacyUuid, $xuid);
103 if($authRequired === null){
104 //plugin cancelled
105 return true;
106 }
107 $this->processOpenIdLogin($authInfo->Token, $header->kid, $packet->clientDataJwt, $authRequired);
108
109 }elseif($authInfo->AuthenticationType === AuthenticationType::SELF_SIGNED->value){
110 try{
111 $chainData = json_decode($authInfo->Certificate, flags: JSON_THROW_ON_ERROR);
112 }catch(\JsonException $e){
113 throw PacketHandlingException::wrap($e, "Error parsing self-signed certificate chain");
114 }
115 if(!is_object($chainData)){
116 throw new PacketHandlingException("Unexpected type for self-signed certificate chain: " . gettype($chainData) . ", expected object");
117 }
118 try{
119 $chain = $this->defaultJsonMapper("Self-signed auth chain JSON")->map($chainData, new LegacyAuthChain());
120 }catch(\JsonMapper_Exception $e){
121 throw PacketHandlingException::wrap($e, "Error mapping self-signed certificate chain");
122 }
123 if(count($chain->chain) > 1 || !isset($chain->chain[0])){
124 throw new PacketHandlingException("Expected exactly one certificate in self-signed certificate chain, got " . count($chain->chain));
125 }
126
127 try{
128 [, $claimsArray, ] = JwtUtils::parse($chain->chain[0]);
129 }catch(JwtException $e){
130 throw PacketHandlingException::wrap($e, "Error parsing self-signed certificate");
131 }
132 if(!isset($claimsArray["extraData"]) || !is_array($claimsArray["extraData"])){
133 throw new PacketHandlingException("Expected \"extraData\" to be present in self-signed certificate");
134 }
135
136 try{
137 $claims = $this->defaultJsonMapper("Self-signed auth JWT 'extraData'")->map($claimsArray["extraData"], new LegacyAuthIdentityData());
138 }catch(\JsonMapper_Exception $e){
139 throw PacketHandlingException::wrap($e, "Error mapping self-signed certificate extraData");
140 }
141
142 if(!Uuid::isValid($claims->identity)){
143 throw new PacketHandlingException("Invalid UUID string in self-signed certificate: " . $claims->identity);
144 }
145 $legacyUuid = Uuid::fromString($claims->identity);
146 $username = $claims->displayName;
147 $xuid = "";
148
149 $authRequired = $this->processLoginCommon($packet, $username, $legacyUuid, $xuid);
150 if($authRequired === null){
151 //plugin cancelled
152 return true;
153 }
154 $this->processSelfSignedLogin($chain->chain, $packet->clientDataJwt, $authRequired);
155 }else{
156 throw new PacketHandlingException("Unsupported authentication type: $authInfo->AuthenticationType");
157 }
158
159 return true;
160 }
161
162 private function processLoginCommon(LoginPacket $packet, string $username, UuidInterface $legacyUuid, string $xuid) : ?bool{
163 if(!Player::isValidUserName($username)){
164 $this->session->disconnectWithError(KnownTranslationFactory::disconnectionScreen_invalidName());
165
166 return null;
167 }
168
169 $clientData = $this->parseClientData($packet->clientDataJwt);
170
171 try{
172 $skin = $this->session->getTypeConverter()->getSkinAdapter()->fromSkinData(ClientDataToSkinDataHelper::fromClientData($clientData));
173 }catch(\InvalidArgumentException | InvalidSkinException $e){
174 $this->session->disconnectWithError(
175 reason: "Invalid skin: " . $e->getMessage(),
176 disconnectScreenMessage: KnownTranslationFactory::disconnectionScreen_invalidSkin()
177 );
178
179 return null;
180 }
181
182 if($xuid !== ""){
183 $playerInfo = new XboxLivePlayerInfo(
184 $xuid,
185 $username,
186 $legacyUuid,
187 $skin,
188 $clientData->LanguageCode,
189 (array) $clientData
190 );
191 }else{
192 $playerInfo = new PlayerInfo(
193 $username,
194 $legacyUuid,
195 $skin,
196 $clientData->LanguageCode,
197 (array) $clientData
198 );
199 }
200 ($this->playerInfoConsumer)($playerInfo);
201
202 $ev = new PlayerPreLoginEvent(
203 $playerInfo,
204 $this->session->getIp(),
205 $this->session->getPort(),
206 $this->server->requiresAuthentication()
207 );
208 if($this->server->getNetwork()->getValidConnectionCount() > $this->server->getMaxPlayers()){
209 $ev->setKickFlag(PlayerPreLoginEvent::KICK_FLAG_SERVER_FULL, KnownTranslationFactory::disconnectionScreen_serverFull());
210 }
211 if(!$this->server->isWhitelisted($playerInfo->getUsername())){
212 $ev->setKickFlag(PlayerPreLoginEvent::KICK_FLAG_SERVER_WHITELISTED, KnownTranslationFactory::pocketmine_disconnect_whitelisted());
213 }
214
215 $banMessage = null;
216 if(($banEntry = $this->server->getNameBans()->getEntry($playerInfo->getUsername())) !== null){
217 $banReason = $banEntry->getReason();
218 $banMessage = $banReason === "" ? KnownTranslationFactory::pocketmine_disconnect_ban_noReason() : KnownTranslationFactory::pocketmine_disconnect_ban($banReason);
219 }elseif(($banEntry = $this->server->getIPBans()->getEntry($this->session->getIp())) !== null){
220 $banReason = $banEntry->getReason();
221 $banMessage = KnownTranslationFactory::pocketmine_disconnect_ban($banReason !== "" ? $banReason : KnownTranslationFactory::pocketmine_disconnect_ban_ip());
222 }
223 if($banMessage !== null){
224 $ev->setKickFlag(PlayerPreLoginEvent::KICK_FLAG_BANNED, $banMessage);
225 }
226
227 $ev->call();
228 if(!$ev->isAllowed()){
229 $this->session->disconnect($ev->getFinalDisconnectReason(), $ev->getFinalDisconnectScreenMessage());
230 return null;
231 }
232
233 return $ev->isAuthRequired();
234 }
235
239 protected function parseAuthInfo(string $authInfo) : AuthenticationInfo{
240 try{
241 $authInfoJson = json_decode($authInfo, associative: false, flags: JSON_THROW_ON_ERROR);
242 }catch(\JsonException $e){
243 throw PacketHandlingException::wrap($e);
244 }
245 if(!is_object($authInfoJson)){
246 throw new PacketHandlingException("Unexpected type for auth info data: " . gettype($authInfoJson) . ", expected object");
247 }
248
249 $mapper = $this->defaultJsonMapper("Root authentication info JSON");
250 try{
251 $clientData = $mapper->map($authInfoJson, new AuthenticationInfo());
252 }catch(\JsonMapper_Exception $e){
253 throw PacketHandlingException::wrap($e);
254 }
255 return $clientData;
256 }
257
262 protected function mapXboxTokenHeader(array $headerArray) : XboxAuthJwtHeader{
263 $mapper = $this->defaultJsonMapper("OpenID JWT header");
264 try{
265 $header = $mapper->map($headerArray, new XboxAuthJwtHeader());
266 }catch(\JsonMapper_Exception $e){
267 throw PacketHandlingException::wrap($e);
268 }
269 return $header;
270 }
271
276 protected function mapXboxTokenBody(array $bodyArray) : XboxAuthJwtBody{
277 $mapper = $this->defaultJsonMapper("OpenID JWT body");
278 try{
279 $header = $mapper->map($bodyArray, new XboxAuthJwtBody());
280 }catch(\JsonMapper_Exception $e){
281 throw PacketHandlingException::wrap($e);
282 }
283 return $header;
284 }
285
289 protected function parseClientData(string $clientDataJwt) : ClientData{
290 try{
291 [, $clientDataClaims, ] = JwtUtils::parse($clientDataJwt);
292 }catch(JwtException $e){
293 throw PacketHandlingException::wrap($e);
294 }
295
296 $mapper = $this->defaultJsonMapper("ClientData JWT body");
297 try{
298 $clientData = $mapper->map($clientDataClaims, new ClientData());
299 }catch(\JsonMapper_Exception $e){
300 throw PacketHandlingException::wrap($e);
301 }
302 return $clientData;
303 }
304
311 protected function processOpenIdLogin(string $token, string $keyId, string $clientData, bool $authRequired) : void{
312 $this->session->setHandler(null); //drop packets received during login verification
313
314 $authKeyProvider = $this->server->getAuthKeyProvider();
315
316 $authKeyProvider->getKey($keyId)->onCompletion(
317 function(array $issuerAndKey) use ($token, $clientData, $authRequired) : void{
318 [$issuer, $mojangPublicKeyPem] = $issuerAndKey;
319 $this->server->getAsyncPool()->submitTask(new ProcessOpenIdLoginTask($token, $issuer, $mojangPublicKeyPem, $clientData, $authRequired, $this->authCallback));
320 },
321 fn() => ($this->authCallback)(false, $authRequired, "Unrecognized authentication key ID: $keyId", null)
322 );
323 }
324
328 protected function processSelfSignedLogin(array $legacyCertificate, string $clientDataJwt, bool $authRequired) : void{
329 $this->session->setHandler(null); //drop packets received during login verification
330
331 $this->server->getAsyncPool()->submitTask(new ProcessLegacyLoginTask($legacyCertificate, $clientDataJwt, rootAuthKeyDer: null, authRequired: $authRequired, onCompletion: $this->authCallback));
332 }
333
334 private function defaultJsonMapper(string $logContext) : \JsonMapper{
335 $mapper = new \JsonMapper();
336 $mapper->bExceptionOnMissingData = true;
337 $mapper->undefinedPropertyHandler = $this->warnUndefinedJsonPropertyHandler($logContext);
338 $mapper->bStrictObjectTypeChecking = true;
339 $mapper->bEnforceMapType = false;
340 return $mapper;
341 }
342
346 private function warnUndefinedJsonPropertyHandler(string $context) : \Closure{
347 return fn(object $object, string $name, mixed $value) => $this->session->getLogger()->warning(
348 "$context: Unexpected JSON property for " . (new \ReflectionClass($object))->getShortName() . ": " . Utils::printable(substr($name, 0, 80))
349 );
350 }
351}
__construct(private Server $server, private NetworkSession $session, private \Closure $playerInfoConsumer, private \Closure $authCallback)
processOpenIdLogin(string $token, string $keyId, string $clientData, bool $authRequired)
processSelfSignedLogin(array $legacyCertificate, string $clientDataJwt, bool $authRequired)