72 private \Closure $playerInfoConsumer,
73 private \Closure $authCallback
76 private static function calculateUuidFromXuid(
string $xuid) : UuidInterface{
77 $hash = md5(
"pocket-auth-1-xuid:" . $xuid, binary: true);
78 $hash[6] = chr((ord($hash[6]) & 0x0f) | 0x30);
79 $hash[8] = chr((ord($hash[8]) & 0x3f) | 0x80);
81 return Uuid::fromBytes($hash);
84 public function handleLogin(LoginPacket $packet) : bool{
85 $authInfo = $this->parseAuthInfo($packet->authInfoJson);
87 if($authInfo->AuthenticationType === AuthenticationType::FULL->value){
89 [$headerArray, $claimsArray,] = JwtUtils::parse($authInfo->Token);
91 throw PacketHandlingException::wrap($e,
"Error parsing authentication token");
93 $header = $this->mapXboxTokenHeader($headerArray);
94 $claims = $this->mapXboxTokenBody($claimsArray);
96 $legacyUuid = self::calculateUuidFromXuid($claims->xid);
97 $username = $claims->xname;
100 $authRequired = $this->processLoginCommon($packet, $username, $legacyUuid, $xuid);
101 if($authRequired ===
null){
105 $this->processOpenIdLogin($authInfo->Token, $header->kid, $packet->clientDataJwt, $authRequired);
107 }elseif($authInfo->AuthenticationType === AuthenticationType::SELF_SIGNED->value){
109 [, $claimsArray, ] = JwtUtils::parse($authInfo->Token);
110 }
catch(JwtException $e){
111 throw PacketHandlingException::wrap($e,
"Error parsing self-signed authentication token");
113 $claims = $this->mapSelfSignedTokenBody($claimsArray);
115 if(!Uuid::isValid($claims->leguuid)){
116 throw new PacketHandlingException(
"Invalid UUID string in self-signed certificate: " . $claims->leguuid);
118 $legacyUuid = Uuid::fromString($claims->leguuid);
119 $username = $claims->xname;
122 $selfSignedKey = base64_decode($claims->cpk, strict:
true);
123 if($selfSignedKey ===
false){
124 throw new PacketHandlingException(
"Invalid self-signed key");
127 $authRequired = $this->processLoginCommon($packet, $username, $legacyUuid, $xuid);
128 if($authRequired ===
null){
132 $this->processSelfSignedLogin($authInfo->Token, $selfSignedKey, $packet->clientDataJwt, $authRequired);
134 throw new PacketHandlingException(
"Unsupported authentication type: $authInfo->AuthenticationType");
140 private function processLoginCommon(LoginPacket $packet,
string $username, UuidInterface $legacyUuid,
string $xuid) : ?bool{
141 if(!Player::isValidUserName($username)){
142 $this->session->disconnectWithError(KnownTranslationFactory::disconnectionScreen_invalidName());
147 $clientData = $this->parseClientData($packet->clientDataJwt);
150 $skin = $this->session->getTypeConverter()->getSkinAdapter()->fromSkinData(ClientDataToSkinDataHelper::fromClientData($clientData));
151 }
catch(\InvalidArgumentException | InvalidSkinException $e){
152 $this->session->disconnectWithError(
153 reason:
"Invalid skin: " . $e->getMessage(),
154 disconnectScreenMessage: KnownTranslationFactory::disconnectionScreen_invalidSkin()
161 $playerInfo =
new XboxLivePlayerInfo(
166 $clientData->LanguageCode,
170 $playerInfo =
new PlayerInfo(
174 $clientData->LanguageCode,
178 ($this->playerInfoConsumer)($playerInfo);
180 $ev =
new PlayerPreLoginEvent(
182 $this->session->getIp(),
183 $this->session->getPort(),
184 $this->server->requiresAuthentication()
186 if($this->
server->getNetwork()->getValidConnectionCount() > $this->server->getMaxPlayers()){
187 $ev->setKickFlag(PlayerPreLoginEvent::KICK_FLAG_SERVER_FULL, KnownTranslationFactory::disconnectionScreen_serverFull());
189 if(!$this->
server->isWhitelisted($playerInfo->getUsername())){
190 $ev->setKickFlag(PlayerPreLoginEvent::KICK_FLAG_SERVER_WHITELISTED, KnownTranslationFactory::pocketmine_disconnect_whitelisted());
194 if(($banEntry = $this->
server->getNameBans()->getEntry($playerInfo->getUsername())) !==
null){
195 $banReason = $banEntry->getReason();
196 $banMessage = $banReason ===
"" ? KnownTranslationFactory::pocketmine_disconnect_ban_noReason() : KnownTranslationFactory::pocketmine_disconnect_ban($banReason);
197 }elseif(($banEntry = $this->
server->getIPBans()->getEntry($this->session->getIp())) !==
null){
198 $banReason = $banEntry->getReason();
199 $banMessage = KnownTranslationFactory::pocketmine_disconnect_ban($banReason !==
"" ? $banReason : KnownTranslationFactory::pocketmine_disconnect_ban_ip());
201 if($banMessage !==
null){
202 $ev->setKickFlag(PlayerPreLoginEvent::KICK_FLAG_BANNED, $banMessage);
206 if(!$ev->isAllowed()){
207 $this->session->disconnect($ev->getFinalDisconnectReason(), $ev->getFinalDisconnectScreenMessage());
211 return $ev->isAuthRequired();
219 $authInfoJson = json_decode($authInfo, associative: false, flags: JSON_THROW_ON_ERROR);
220 }
catch(\JsonException $e){
221 throw PacketHandlingException::wrap($e);
223 if(!is_object($authInfoJson)){
224 throw new PacketHandlingException(
"Unexpected type for auth info data: " . gettype($authInfoJson) .
", expected object");
227 $mapper = $this->defaultJsonMapper(
"Root authentication info JSON");
229 $clientData = $mapper->map($authInfoJson,
new AuthenticationInfo());
230 }
catch(\JsonMapper_Exception $e){
231 throw PacketHandlingException::wrap($e);
241 $mapper = $this->defaultJsonMapper(
"OpenID JWT header");
244 }
catch(\JsonMapper_Exception $e){
245 throw PacketHandlingException::wrap($e);
255 $mapper = $this->defaultJsonMapper(
"OpenID JWT body");
258 }
catch(\JsonMapper_Exception $e){
259 throw PacketHandlingException::wrap($e);
269 $mapper = $this->defaultJsonMapper(
"OpenID JWT body");
272 }
catch(\JsonMapper_Exception $e){
273 throw PacketHandlingException::wrap($e);
283 [, $clientDataClaims, ] =
JwtUtils::parse($clientDataJwt);
285 throw PacketHandlingException::wrap($e);
288 $mapper = $this->defaultJsonMapper(
"ClientData JWT body");
290 $clientData = $mapper->map($clientDataClaims,
new ClientData());
291 }
catch(\JsonMapper_Exception $e){
292 throw PacketHandlingException::wrap($e);
303 protected function processOpenIdLogin(
string $token,
string $keyId,
string $clientData,
bool $authRequired) : void{
304 $this->session->setHandler(null);
306 $authKeyProvider = $this->
server->getAuthKeyProvider();
308 $authKeyProvider->getKey($keyId)->onCompletion(
309 function(array $issuerAndKey) use ($token, $clientData, $authRequired) :
void{
310 [$issuer, $mojangPublicKeyPem] = $issuerAndKey;
311 $this->
server->getAsyncPool()->submitTask(
new ProcessOpenIdLoginTask($token, $issuer, $mojangPublicKeyPem, $clientData, $authRequired, $this->authCallback));
313 fn() => ($this->authCallback)(
false, $authRequired,
"Unrecognized authentication key ID: $keyId",
null)
317 protected function processSelfSignedLogin(
string $token,
string $clientPublicKey,
string $clientData,
bool $authRequired) : void{
318 $this->session->setHandler(null);
320 $this->
server->getAsyncPool()->submitTask(
new ProcessSelfSignedLoginTask($token, $clientPublicKey, $clientData, $authRequired, onCompletion: $this->authCallback));
323 private function defaultJsonMapper(
string $logContext) : \JsonMapper{
324 $mapper = new \JsonMapper();
325 $mapper->bExceptionOnMissingData =
true;
326 $mapper->undefinedPropertyHandler = $this->warnUndefinedJsonPropertyHandler($logContext);
327 $mapper->bStrictObjectTypeChecking =
true;
328 $mapper->bEnforceMapType =
false;
335 private function warnUndefinedJsonPropertyHandler(
string $context) : \Closure{
336 return function(object $object, string $name, mixed $value) use ($context) : void{
339 $this->session->getLogger()->warning(
340 "$context: Unexpected JSON property for " . (
new \ReflectionClass($object))->getShortName() .
": " . Utils::printable(substr($name, 0, 80))
343 throw new PacketHandlingException(
"$context: Too many unexpected JSON properties");