2019-08-01 14:47:12 +05:30
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace api\components\Tokens;
|
|
|
|
|
2019-08-02 05:59:20 +05:30
|
|
|
use Carbon\Carbon;
|
2019-08-01 14:47:12 +05:30
|
|
|
use Exception;
|
|
|
|
use Lcobucci\JWT\Builder;
|
|
|
|
use Lcobucci\JWT\Parser;
|
|
|
|
use Lcobucci\JWT\Token;
|
2019-08-02 21:46:34 +05:30
|
|
|
use Webmozart\Assert\Assert;
|
2019-08-01 14:47:12 +05:30
|
|
|
use yii\base\Component as BaseComponent;
|
|
|
|
|
|
|
|
class Component extends BaseComponent {
|
|
|
|
|
|
|
|
private const PREFERRED_ALGORITHM = 'ES256';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $hmacKey;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $publicKeyPath;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $privateKeyPath;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
public $privateKeyPass;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var AlgorithmsManager|null
|
|
|
|
*/
|
|
|
|
private $algorithmManager;
|
|
|
|
|
2019-08-02 21:46:34 +05:30
|
|
|
public function init(): void {
|
|
|
|
parent::init();
|
|
|
|
Assert::notEmpty($this->hmacKey, 'hmacKey must be set');
|
|
|
|
Assert::notEmpty($this->privateKeyPath, 'privateKeyPath must be set');
|
|
|
|
Assert::notEmpty($this->publicKeyPath, 'publicKeyPath must be set');
|
|
|
|
}
|
|
|
|
|
2019-08-01 14:47:12 +05:30
|
|
|
public function create(array $payloads = [], array $headers = []): Token {
|
2019-08-02 05:59:20 +05:30
|
|
|
$now = Carbon::now();
|
2019-08-01 14:47:12 +05:30
|
|
|
$builder = (new Builder())
|
2019-08-02 05:59:20 +05:30
|
|
|
->issuedAt($now->getTimestamp())
|
|
|
|
->expiresAt($now->addHour()->getTimestamp());
|
2019-08-01 14:47:12 +05:30
|
|
|
foreach ($payloads as $claim => $value) {
|
|
|
|
$builder->withClaim($claim, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($headers as $claim => $value) {
|
|
|
|
$builder->withHeader($claim, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */
|
|
|
|
$algorithm = $this->getAlgorithmManager()->get(self::PREFERRED_ALGORITHM);
|
|
|
|
|
|
|
|
return $builder->getToken($algorithm->getSigner(), $algorithm->getPrivateKey());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $jwt
|
|
|
|
*
|
|
|
|
* @return Token
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function parse(string $jwt): Token {
|
|
|
|
return (new Parser())->parse($jwt);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function verify(Token $token): bool {
|
|
|
|
try {
|
|
|
|
$algorithm = $this->getAlgorithmManager()->get($token->getHeader('alg'));
|
|
|
|
return $token->verify($algorithm->getSigner(), $algorithm->getPublicKey());
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getAlgorithmManager(): AlgorithmsManager {
|
|
|
|
if ($this->algorithmManager === null) {
|
|
|
|
$this->algorithmManager = new AlgorithmsManager([
|
|
|
|
new Algorithms\HS256($this->hmacKey),
|
|
|
|
new Algorithms\ES256(
|
|
|
|
"file://{$this->privateKeyPath}",
|
|
|
|
$this->privateKeyPass,
|
|
|
|
"file://{$this->publicKeyPath}"
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->algorithmManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|