From 1f5c27cab00b40b02aa49206275d7eda70b3940d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Folt=C3=BDn?= Date: Fri, 19 Jan 2024 09:36:26 +0100 Subject: [PATCH 1/2] =?UTF-8?q?Lazy=20vytvo=C5=99en=C3=AD=20kl=C3=AD=C4=8D?= =?UTF-8?q?=C5=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- src/Exception/CreateKeyException.php | 8 +++ src/Exception/PublicAccessException.php | 8 +++ src/Tokenizer/AsymetricJwtTokenizer.php | 66 ++++++++++++++++++++++--- 3 files changed, 74 insertions(+), 8 deletions(-) create mode 100644 src/Exception/CreateKeyException.php create mode 100644 src/Exception/PublicAccessException.php diff --git a/src/Exception/CreateKeyException.php b/src/Exception/CreateKeyException.php new file mode 100644 index 0000000..6ecb6c2 --- /dev/null +++ b/src/Exception/CreateKeyException.php @@ -0,0 +1,8 @@ +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 \OpenSSLAsymmetricKey|resource + * @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 \OpenSSLAsymmetricKey|resource + * @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; + } + } From fc683c14527ccc70d72433152f4facccc12cf21b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Folt=C3=BDn?= Date: Fri, 19 Jan 2024 10:43:47 +0100 Subject: [PATCH 2/2] =?UTF-8?q?Lazy=20vytvo=C5=99en=C3=AD=20kl=C3=AD=C4=8D?= =?UTF-8?q?=C5=AF=20-=20kompatibilita=20pro=20phpstan?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phpstan.neon | 3 ++- tests/phpstan/php7-compatibility.php | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 tests/phpstan/php7-compatibility.php diff --git a/phpstan.neon b/phpstan.neon index d9e4093..3608b41 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,3 +1,4 @@ parameters: + bootstrapFiles: + - tests/phpstan/php7-compatibility.php ignoreErrors: - diff --git a/tests/phpstan/php7-compatibility.php b/tests/phpstan/php7-compatibility.php new file mode 100644 index 0000000..cc878dc --- /dev/null +++ b/tests/phpstan/php7-compatibility.php @@ -0,0 +1,11 @@ +