Skip to content

Commit

Permalink
Lazy vytvoření klíčů
Browse files Browse the repository at this point in the history
 - je to celkem náročná operace
 - nevytváříme tak zbytečně klíče, když je AsymetricJwtTokenizer uvedený jako závislost, ale při aktuálním běhu aplikace ho nevyužijeme
  • Loading branch information
tomasfoltyn committed Jan 19, 2024
1 parent 5ddf88d commit 704c0d2
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/Exception/CreateKeyException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types = 1);

namespace Pd\PublicAccess\Exception;

class CreateKeyException extends PublicAccessException
{

}
8 changes: 8 additions & 0 deletions src/Exception/PublicAccessException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types = 1);

namespace Pd\PublicAccess\Exception;

class PublicAccessException extends \Exception
{

}
62 changes: 56 additions & 6 deletions src/Tokenizer/AsymetricJwtTokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,86 @@ final class AsymetricJwtTokenizer implements Tokenizer
/**
* @var mixed
*/
private $privateKey;
private $privateKey = NULL;

private string $privateKeyFile;

/**
* @var mixed
*/
private $publicKey;
private $publicKey = NULL;

private string $publicKeyFile;


public function __construct(
string $privateKey,
string $publicKey
)
{
$this->privateKey = \openssl_pkey_get_private('file://' . $privateKey);
$this->publicKey = \openssl_pkey_get_public('file://' . $publicKey);
$this->privateKeyFile = $privateKey;
$this->publicKeyFile = $publicKey;
}


/**
* @throws \Pd\PublicAccess\Exception\CreateKeyException
*/
public function create(\Pd\PublicAccess\PublicAccess $object): string
{
return \Firebase\JWT\JWT::encode($object->jsonSerialize(), $this->privateKey, self::ALGORITHM);
return \Firebase\JWT\JWT::encode($object->jsonSerialize(), $this->privateKey(), self::ALGORITHM);
}


/**
* @throws \Pd\PublicAccess\Exception\CreateKeyException
*/
public function decode(string $token): \stdClass
{
/** @var \stdClass $decode */
$decode = \Firebase\JWT\JWT::decode($token, new \Firebase\JWT\Key($this->publicKey, self::ALGORITHM));
$decode = \Firebase\JWT\JWT::decode($token, new \Firebase\JWT\Key($this->publicKey(), self::ALGORITHM));

return $decode;
}


/**
* @return mixed
* @throws \Pd\PublicAccess\Exception\CreateKeyException
*/
private function privateKey()
{
if ($this->privateKey === NULL) {
$privateKey = \openssl_pkey_get_private('file://' . $this->privateKeyFile);

if ($privateKey === FALSE) {
throw new \Pd\PublicAccess\Exception\CreateKeyException('Invalid private key for JWT tokenizer');
}

$this->privateKey = $privateKey;
}

return $this->privateKey;
}


/**
* @return mixed
* @throws \Pd\PublicAccess\Exception\CreateKeyException
*/
private function publicKey()
{
if ($this->publicKey === NULL) {
$publicKey = \openssl_pkey_get_public('file://' . $this->publicKeyFile);

if ($publicKey === FALSE) {
throw new \Pd\PublicAccess\Exception\CreateKeyException('Invalid public key for JWT tokenizer');
}

$this->publicKey = $publicKey;
}

return $this->publicKey;
}

}

0 comments on commit 704c0d2

Please sign in to comment.