Skip to content

Commit

Permalink
Merge pull request #11 from peckadesign/lazy-vytvoreni-klicu
Browse files Browse the repository at this point in the history
Lazy vytvoření klíčů
  • Loading branch information
tomasfoltyn authored Jan 24, 2024
2 parents 5ddf88d + fc683c1 commit 25243f5
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 9 deletions.
3 changes: 2 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
parameters:
bootstrapFiles:
- tests/phpstan/php7-compatibility.php
ignoreErrors:

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
{

}
66 changes: 58 additions & 8 deletions src/Tokenizer/AsymetricJwtTokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,88 @@ final class AsymetricJwtTokenizer implements Tokenizer


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

private string $privateKeyFile;

/**
* @var mixed
* @var \OpenSSLAsymmetricKey|resource|NULL
*/
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 \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;
}

}
11 changes: 11 additions & 0 deletions tests/phpstan/php7-compatibility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types = 1);

if (PHP_VERSION_ID < 80000) {
class OpenSSLAsymmetricKey
{

}

}

0 comments on commit 25243f5

Please sign in to comment.