2020-04-07 03:28:14 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Nodes;
|
|
|
|
|
2021-01-23 20:09:16 +00:00
|
|
|
use DateTimeImmutable;
|
2020-04-07 03:28:14 +00:00
|
|
|
use Carbon\CarbonImmutable;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
use Pterodactyl\Models\Node;
|
2021-01-23 20:09:16 +00:00
|
|
|
use Lcobucci\JWT\Configuration;
|
2020-04-07 03:28:14 +00:00
|
|
|
use Lcobucci\JWT\Signer\Hmac\Sha256;
|
2021-01-23 20:09:16 +00:00
|
|
|
use Lcobucci\JWT\Signer\Key\InMemory;
|
|
|
|
use Pterodactyl\Extensions\Lcobucci\JWT\Encoding\TimestampDates;
|
2020-04-07 03:28:14 +00:00
|
|
|
|
|
|
|
class NodeJWTService
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $claims = [];
|
|
|
|
|
|
|
|
/**
|
2021-01-23 20:09:16 +00:00
|
|
|
* @var \DateTimeImmutable|null
|
2020-04-07 03:28:14 +00:00
|
|
|
*/
|
|
|
|
private $expiresAt;
|
|
|
|
|
2020-12-24 18:10:40 +00:00
|
|
|
/**
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
private $subject;
|
|
|
|
|
2020-04-07 03:28:14 +00:00
|
|
|
/**
|
|
|
|
* Set the claims to include in this JWT.
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setClaims(array $claims)
|
|
|
|
{
|
|
|
|
$this->claims = $claims;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-12-24 18:10:40 +00:00
|
|
|
/**
|
|
|
|
* @return $this
|
|
|
|
*/
|
2021-01-23 20:09:16 +00:00
|
|
|
public function setExpiresAt(DateTimeImmutable $date)
|
2020-04-07 03:28:14 +00:00
|
|
|
{
|
2021-01-23 20:09:16 +00:00
|
|
|
$this->expiresAt = $date;
|
2020-04-07 03:28:14 +00:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-12-24 18:10:40 +00:00
|
|
|
/**
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setSubject(string $subject)
|
|
|
|
{
|
|
|
|
$this->subject = $subject;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-04-07 03:28:14 +00:00
|
|
|
/**
|
|
|
|
* Generate a new JWT for a given node.
|
|
|
|
*
|
2021-01-23 20:09:16 +00:00
|
|
|
* @return \Lcobucci\JWT\Token\Plain
|
2020-04-07 03:28:14 +00:00
|
|
|
*/
|
2020-12-24 18:10:40 +00:00
|
|
|
public function handle(Node $node, string $identifiedBy, string $algo = 'md5')
|
2020-04-07 03:28:14 +00:00
|
|
|
{
|
2021-01-23 20:09:16 +00:00
|
|
|
$identifier = hash($algo, $identifiedBy);
|
2021-01-23 20:33:34 +00:00
|
|
|
$config = Configuration::forSymmetricSigner(new Sha256(), InMemory::plainText($node->getDecryptedKey()));
|
2020-04-07 03:28:14 +00:00
|
|
|
|
2021-01-23 20:33:34 +00:00
|
|
|
$builder = $config->builder(new TimestampDates())
|
2021-01-23 20:09:16 +00:00
|
|
|
->issuedBy(config('app.url'))
|
2020-04-07 03:28:14 +00:00
|
|
|
->permittedFor($node->getConnectionAddress())
|
2021-01-23 20:09:16 +00:00
|
|
|
->identifiedBy($identifier)
|
|
|
|
->withHeader('jti', $identifier)
|
|
|
|
->issuedAt(CarbonImmutable::now())
|
|
|
|
->canOnlyBeUsedAfter(CarbonImmutable::now()->subMinutes(5));
|
2020-04-07 03:28:14 +00:00
|
|
|
|
|
|
|
if ($this->expiresAt) {
|
|
|
|
$builder = $builder->expiresAt($this->expiresAt);
|
|
|
|
}
|
|
|
|
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!empty($this->subject)) {
|
2021-01-23 20:09:16 +00:00
|
|
|
$builder = $builder->relatedTo($this->subject)->withHeader('sub', $this->subject);
|
2020-12-24 18:10:40 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 03:28:14 +00:00
|
|
|
foreach ($this->claims as $key => $value) {
|
|
|
|
$builder = $builder->withClaim($key, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $builder
|
|
|
|
->withClaim('unique_id', Str::random(16))
|
2021-01-23 20:09:16 +00:00
|
|
|
->getToken($config->signer(), $config->signingKey());
|
2020-04-07 03:28:14 +00:00
|
|
|
}
|
|
|
|
}
|