@@ -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}
0 commit comments