2019-08-01 14:47:12 +05:30
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace api\components\Tokens\Algorithms;
|
|
|
|
|
|
|
|
use Lcobucci\JWT\Signer;
|
|
|
|
use Lcobucci\JWT\Signer\Hmac\Sha256;
|
|
|
|
use Lcobucci\JWT\Signer\Key;
|
2024-12-02 15:40:55 +05:30
|
|
|
use Lcobucci\JWT\Signer\Key\InMemory;
|
2019-08-01 14:47:12 +05:30
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
final class HS256 implements AlgorithmInterface {
|
2019-08-01 14:47:12 +05:30
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
private ?InMemory $loadedKey = null;
|
2019-08-01 14:47:12 +05:30
|
|
|
|
|
|
|
/**
|
2024-12-02 15:40:55 +05:30
|
|
|
* @param non-empty-string $key
|
2019-08-01 14:47:12 +05:30
|
|
|
*/
|
2024-12-02 15:40:55 +05:30
|
|
|
public function __construct(
|
|
|
|
private readonly string $key,
|
|
|
|
) {
|
2019-08-01 14:47:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function getSigner(): Signer {
|
|
|
|
return new Sha256();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPrivateKey(): Key {
|
|
|
|
return $this->loadKey();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPublicKey(): Key {
|
|
|
|
return $this->loadKey();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function loadKey(): Key {
|
|
|
|
if ($this->loadedKey === null) {
|
2024-12-02 15:40:55 +05:30
|
|
|
$this->loadedKey = InMemory::plainText($this->key);
|
2019-08-01 14:47:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return $this->loadedKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|