Path: blob/1.0-develop/app/Services/Nodes/NodeJWTService.php
10262 views
<?php12namespace Pterodactyl\Services\Nodes;34use Carbon\CarbonImmutable;5use Illuminate\Support\Str;6use Pterodactyl\Models\Node;7use Pterodactyl\Models\User;8use Lcobucci\JWT\Configuration;9use Lcobucci\JWT\UnencryptedToken;10use Lcobucci\JWT\Signer\Hmac\Sha256;11use Lcobucci\JWT\Signer\Key\InMemory;12use Pterodactyl\Extensions\Lcobucci\JWT\Encoding\TimestampDates;1314class NodeJWTService15{16private array $claims = [];1718private ?User $user = null;1920private \DateTimeImmutable $expiresAt;2122private ?string $subject = null;2324/**25* Set the claims to include in this JWT.26*/27public function setClaims(array $claims): self28{29$this->claims = $claims;3031return $this;32}3334/**35* Attaches a user to the JWT being created and will automatically inject the36* "user_uuid" key into the final claims array with the user's UUID.37*/38public function setUser(User $user): self39{40$this->user = $user;4142return $this;43}4445public function setExpiresAt(\DateTimeImmutable $date): self46{47$this->expiresAt = $date;4849return $this;50}5152public function setSubject(string $subject): self53{54$this->subject = $subject;5556return $this;57}5859/**60* Generate a new JWT for a given node.61*/62public function handle(Node $node, ?string $identifiedBy, string $algo = 'md5'): UnencryptedToken63{64$identifier = hash($algo, $identifiedBy);65$config = Configuration::forSymmetricSigner(new Sha256(), InMemory::plainText($node->getDecryptedKey()));6667$builder = $config->builder(new TimestampDates())68->issuedBy(config('app.url'))69->permittedFor($node->getConnectionAddress())70->identifiedBy($identifier)71->withHeader('jti', $identifier)72->issuedAt(CarbonImmutable::now())73->canOnlyBeUsedAfter(CarbonImmutable::now()->subMinutes(5));7475if (isset($this->expiresAt)) {76$builder = $builder->expiresAt($this->expiresAt);77}7879if (!empty($this->subject)) {80$builder = $builder->relatedTo($this->subject)->withHeader('sub', $this->subject);81}8283foreach ($this->claims as $key => $value) {84$builder = $builder->withClaim($key, $value);85}8687if (!is_null($this->user)) {88$builder = $builder89->withClaim('user_uuid', $this->user->uuid)90// The "user_id" claim is deprecated and should not be referenced — it remains91// here solely to ensure older versions of Wings are unaffected when the Panel92// is updated.93//94// This claim will be removed in [email protected] or later.95->withClaim('user_id', $this->user->id);96}9798return $builder99->withClaim('unique_id', Str::random())100->getToken($config->signer(), $config->signingKey());101}102}103104105