Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy vytvoření klíčů #11

Merged
merged 2 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
{

}

}
Loading