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 0194292
Showing 1 changed file with 44 additions and 6 deletions.
50 changes: 44 additions & 6 deletions src/Tokenizer/AsymetricJwtTokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,76 @@ final class AsymetricJwtTokenizer implements Tokenizer


/**
* @var mixed
* @var \OpenSSLAsymmetricKey|resource
*/
private $privateKey;

Check failure on line 14 in src/Tokenizer/AsymetricJwtTokenizer.php

View workflow job for this annotation

GitHub Actions / Checks (7.4)

Property Pd\PublicAccess\Tokenizer\AsymetricJwtTokenizer::$privateKey has unknown class OpenSSLAsymmetricKey as its type.

private string $privateKeyFile;

/**
* @var mixed
* @var \OpenSSLAsymmetricKey|resource
*/
private $publicKey;

Check failure on line 21 in src/Tokenizer/AsymetricJwtTokenizer.php

View workflow job for this annotation

GitHub Actions / Checks (7.4)

Property Pd\PublicAccess\Tokenizer\AsymetricJwtTokenizer::$publicKey has unknown class OpenSSLAsymmetricKey as its type.

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;
}


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);
}


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 \OpenSSLAsymmetricKey|resource
*/
private function privateKey()

Check failure on line 54 in src/Tokenizer/AsymetricJwtTokenizer.php

View workflow job for this annotation

GitHub Actions / Checks (7.4)

Return typehint of method Pd\PublicAccess\Tokenizer\AsymetricJwtTokenizer::privateKey() has invalid type OpenSSLAsymmetricKey.
{
if ( ! isset($this->privateKey)) {
$this->privateKey = \openssl_pkey_get_private('file://' . $this->privateKeyFile);

Check failure on line 57 in src/Tokenizer/AsymetricJwtTokenizer.php

View workflow job for this annotation

GitHub Actions / Checks (7.4)

Property Pd\PublicAccess\Tokenizer\AsymetricJwtTokenizer::$privateKey (OpenSSLAsymmetricKey|resource) does not accept resource|false.

Check failure on line 57 in src/Tokenizer/AsymetricJwtTokenizer.php

View workflow job for this annotation

GitHub Actions / Checks (8)

Property Pd\PublicAccess\Tokenizer\AsymetricJwtTokenizer::$privateKey (OpenSSLAsymmetricKey|resource) does not accept OpenSSLAsymmetricKey|false.

if ($this->privateKey === FALSE) {
throw new \RuntimeException('Invalid private key for JWT tokenizer');
}
}

return $this->privateKey;
}


/**
* @return \OpenSSLAsymmetricKey|resource
*/
private function publicKey()

Check failure on line 71 in src/Tokenizer/AsymetricJwtTokenizer.php

View workflow job for this annotation

GitHub Actions / Checks (7.4)

Return typehint of method Pd\PublicAccess\Tokenizer\AsymetricJwtTokenizer::publicKey() has invalid type OpenSSLAsymmetricKey.
{
if ( ! isset($this->publicKey)) {
$this->publicKey = \openssl_pkey_get_public('file://' . $this->publicKeyFile);

Check failure on line 74 in src/Tokenizer/AsymetricJwtTokenizer.php

View workflow job for this annotation

GitHub Actions / Checks (7.4)

Property Pd\PublicAccess\Tokenizer\AsymetricJwtTokenizer::$publicKey (OpenSSLAsymmetricKey|resource) does not accept resource|false.

Check failure on line 74 in src/Tokenizer/AsymetricJwtTokenizer.php

View workflow job for this annotation

GitHub Actions / Checks (8)

Property Pd\PublicAccess\Tokenizer\AsymetricJwtTokenizer::$publicKey (OpenSSLAsymmetricKey|resource) does not accept OpenSSLAsymmetricKey|false.

if ($this->publicKey === FALSE) {
throw new \RuntimeException('Invalid public key for JWT tokenizer');
}
}

return $this->publicKey;
}

}

0 comments on commit 0194292

Please sign in to comment.