Skip to content

Commit 25243f5

Browse files
authored
Merge pull request #11 from peckadesign/lazy-vytvoreni-klicu
Lazy vytvoření klíčů
2 parents 5ddf88d + fc683c1 commit 25243f5

File tree

5 files changed

+87
-9
lines changed

5 files changed

+87
-9
lines changed

phpstan.neon

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
parameters:
2+
bootstrapFiles:
3+
- tests/phpstan/php7-compatibility.php
24
ignoreErrors:
3-
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Pd\PublicAccess\Exception;
4+
5+
class CreateKeyException extends PublicAccessException
6+
{
7+
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Pd\PublicAccess\Exception;
4+
5+
class PublicAccessException extends \Exception
6+
{
7+
8+
}

src/Tokenizer/AsymetricJwtTokenizer.php

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,88 @@ final class AsymetricJwtTokenizer implements Tokenizer
99

1010

1111
/**
12-
* @var mixed
12+
* @var \OpenSSLAsymmetricKey|resource|NULL
1313
*/
14-
private $privateKey;
14+
private $privateKey = NULL;
15+
16+
private string $privateKeyFile;
1517

1618
/**
17-
* @var mixed
19+
* @var \OpenSSLAsymmetricKey|resource|NULL
1820
*/
19-
private $publicKey;
21+
private $publicKey = NULL;
22+
23+
private string $publicKeyFile;
2024

2125

2226
public function __construct(
2327
string $privateKey,
2428
string $publicKey
2529
)
2630
{
27-
$this->privateKey = \openssl_pkey_get_private('file://' . $privateKey);
28-
$this->publicKey = \openssl_pkey_get_public('file://' . $publicKey);
31+
$this->privateKeyFile = $privateKey;
32+
$this->publicKeyFile = $publicKey;
2933
}
3034

3135

36+
/**
37+
* @throws \Pd\PublicAccess\Exception\CreateKeyException
38+
*/
3239
public function create(\Pd\PublicAccess\PublicAccess $object): string
3340
{
34-
return \Firebase\JWT\JWT::encode($object->jsonSerialize(), $this->privateKey, self::ALGORITHM);
41+
return \Firebase\JWT\JWT::encode($object->jsonSerialize(), $this->privateKey(), self::ALGORITHM);
3542
}
3643

3744

45+
/**
46+
* @throws \Pd\PublicAccess\Exception\CreateKeyException
47+
*/
3848
public function decode(string $token): \stdClass
3949
{
4050
/** @var \stdClass $decode */
41-
$decode = \Firebase\JWT\JWT::decode($token, new \Firebase\JWT\Key($this->publicKey, self::ALGORITHM));
51+
$decode = \Firebase\JWT\JWT::decode($token, new \Firebase\JWT\Key($this->publicKey(), self::ALGORITHM));
4252

4353
return $decode;
4454
}
4555

56+
57+
/**
58+
* @return \OpenSSLAsymmetricKey|resource
59+
* @throws \Pd\PublicAccess\Exception\CreateKeyException
60+
*/
61+
private function privateKey()
62+
{
63+
if ($this->privateKey === NULL) {
64+
$privateKey = \openssl_pkey_get_private('file://' . $this->privateKeyFile);
65+
66+
if ($privateKey === FALSE) {
67+
throw new \Pd\PublicAccess\Exception\CreateKeyException('Invalid private key for JWT tokenizer');
68+
}
69+
70+
$this->privateKey = $privateKey;
71+
}
72+
73+
return $this->privateKey;
74+
}
75+
76+
77+
/**
78+
* @return \OpenSSLAsymmetricKey|resource
79+
* @throws \Pd\PublicAccess\Exception\CreateKeyException
80+
*/
81+
private function publicKey()
82+
{
83+
if ($this->publicKey === NULL) {
84+
$publicKey = \openssl_pkey_get_public('file://' . $this->publicKeyFile);
85+
86+
if ($publicKey === FALSE) {
87+
throw new \Pd\PublicAccess\Exception\CreateKeyException('Invalid public key for JWT tokenizer');
88+
}
89+
90+
$this->publicKey = $publicKey;
91+
}
92+
93+
return $this->publicKey;
94+
}
95+
4696
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
if (PHP_VERSION_ID < 80000) {
6+
class OpenSSLAsymmetricKey
7+
{
8+
9+
}
10+
11+
}

0 commit comments

Comments
 (0)