From 57908586bdeb9f6840ff7493223a0aab933b1d19 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Wed, 17 Jun 2020 19:00:11 +0100 Subject: [PATCH] more jsonmapper models for login --- src/network/mcpe/auth/ProcessLoginTask.php | 36 ++++++++++++--- .../protocol/types/login/JwtBodyRfc7519.php | 45 +++++++++++++++++++ .../protocol/types/login/JwtChainLinkBody.php | 33 ++++++++++++++ .../mcpe/protocol/types/login/JwtHeader.php | 37 +++++++++++++++ 4 files changed, 146 insertions(+), 5 deletions(-) create mode 100644 src/network/mcpe/protocol/types/login/JwtBodyRfc7519.php create mode 100644 src/network/mcpe/protocol/types/login/JwtChainLinkBody.php create mode 100644 src/network/mcpe/protocol/types/login/JwtHeader.php diff --git a/src/network/mcpe/auth/ProcessLoginTask.php b/src/network/mcpe/auth/ProcessLoginTask.php index 9fd706582..b92015560 100644 --- a/src/network/mcpe/auth/ProcessLoginTask.php +++ b/src/network/mcpe/auth/ProcessLoginTask.php @@ -29,6 +29,8 @@ use Mdanter\Ecc\Serializer\PublicKey\DerPublicKeySerializer; use pocketmine\network\mcpe\JwtException; use pocketmine\network\mcpe\JwtUtils; use pocketmine\network\mcpe\protocol\LoginPacket; +use pocketmine\network\mcpe\protocol\types\login\JwtChainLinkBody; +use pocketmine\network\mcpe\protocol\types\login\JwtHeader; use pocketmine\scheduler\AsyncTask; use function base64_decode; use function time; @@ -106,18 +108,30 @@ class ProcessLoginTask extends AsyncTask{ */ private function validateToken(string $jwt, ?string &$currentPublicKey, bool $first = false) : void{ try{ - [$headers, $claims, ] = JwtUtils::parse($jwt); + [$headersArray, $claimsArray, ] = JwtUtils::parse($jwt); }catch(JwtException $e){ throw new VerifyLoginException("Failed to parse JWT: " . $e->getMessage(), 0, $e); } + $mapper = new \JsonMapper(); + $mapper->bExceptionOnMissingData = true; + $mapper->bExceptionOnUndefinedProperty = true; + $mapper->bEnforceMapType = false; + + try{ + /** @var JwtHeader $headers */ + $headers = $mapper->map($headersArray, new JwtHeader()); + }catch(\JsonMapper_Exception $e){ + throw new VerifyLoginException("Invalid JWT header: " . $e->getMessage(), 0, $e); + } + if($currentPublicKey === null){ if(!$first){ throw new VerifyLoginException("%pocketmine.disconnect.invalidSession.missingKey"); } //First link, check that it is self-signed - $currentPublicKey = $headers["x5u"]; + $currentPublicKey = $headers->x5u; } $derPublicKeySerializer = new DerPublicKeySerializer(); @@ -143,16 +157,28 @@ class ProcessLoginTask extends AsyncTask{ $this->authenticated = true; //we're signed into xbox live } + $mapper = new \JsonMapper(); + $mapper->bExceptionOnUndefinedProperty = false; //we only care about the properties we're using in this case + $mapper->bExceptionOnMissingData = true; + $mapper->bEnforceMapType = false; + $mapper->bRemoveUndefinedAttributes = true; + try{ + /** @var JwtChainLinkBody $claims */ + $claims = $mapper->map($claimsArray, new JwtChainLinkBody()); + }catch(\JsonMapper_Exception $e){ + throw new VerifyLoginException("Invalid chain link body: " . $e->getMessage(), 0, $e); + } + $time = time(); - if(isset($claims["nbf"]) and $claims["nbf"] > $time + self::CLOCK_DRIFT_MAX){ + if(isset($claims->nbf) and $claims->nbf > $time + self::CLOCK_DRIFT_MAX){ throw new VerifyLoginException("%pocketmine.disconnect.invalidSession.tooEarly"); } - if(isset($claims["exp"]) and $claims["exp"] < $time - self::CLOCK_DRIFT_MAX){ + if(isset($claims->exp) and $claims->exp < $time - self::CLOCK_DRIFT_MAX){ throw new VerifyLoginException("%pocketmine.disconnect.invalidSession.tooLate"); } - $currentPublicKey = $claims["identityPublicKey"] ?? null; //if there are further links, the next link should be signed with this + $currentPublicKey = $claims->identityPublicKey ?? null; //if there are further links, the next link should be signed with this } public function onCompletion() : void{ diff --git a/src/network/mcpe/protocol/types/login/JwtBodyRfc7519.php b/src/network/mcpe/protocol/types/login/JwtBodyRfc7519.php new file mode 100644 index 000000000..301779401 --- /dev/null +++ b/src/network/mcpe/protocol/types/login/JwtBodyRfc7519.php @@ -0,0 +1,45 @@ +