Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: hyperf-ext/jwt
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v2.1.3
Choose a base ref
...
head repository: hyperf-ext/jwt
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
  • 4 commits
  • 5 files changed
  • 2 contributors

Commits on Jul 30, 2021

  1. support for hyperf 2.2

    ericyzhu committed Jul 30, 2021
    Copy the full SHA
    b95bf2a View commit details
  2. Copy the full SHA
    5dae39c View commit details

Commits on Aug 10, 2021

  1. Update composer.json

    支持hyperf2.1以上
    ljyljy0211 authored Aug 10, 2021
    Copy the full SHA
    8586fe5 View commit details

Commits on Aug 11, 2021

  1. Merge pull request #21 from ljyljy0211/ljyljy0211-patch-1

    Update composer.json
    ericyzhu authored Aug 11, 2021
    Copy the full SHA
    ef9d893 View commit details
Showing with 196 additions and 56 deletions.
  1. +2 −2 .php_cs → .php-cs-fixer.php
  2. +9 −9 composer.json
  3. +1 −2 src/Claims/IssuedAt.php
  4. +94 −12 src/Codec.php
  5. +90 −31 tests/CodecTest.php
4 changes: 2 additions & 2 deletions .php_cs → .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -8,15 +8,15 @@
@license https://github.com/hyperf-ext/jwt/blob/master/LICENSE
EOF;

return PhpCsFixer\Config::create()
return (new PhpCsFixer\Config())
->setRiskyAllowed(true)
->setRules([
'@PSR2' => true,
'@Symfony' => true,
'@DoctrineAnnotation' => true,
'@PhpCsFixer' => true,
'header_comment' => [
'commentType' => 'PHPDoc',
'comment_type' => 'PHPDoc',
'header' => $header,
'separate' => 'none',
'location' => 'after_declare_strict',
18 changes: 9 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
@@ -36,17 +36,17 @@
"ext-swoole": ">=4.5",
"ext-json": "*",
"ext-openssl": "*",
"hyperf/cache": "~2.1.0",
"hyperf/command": "~2.1.0",
"hyperf/config": "~2.1.0",
"hyperf/di": "~2.1.0",
"hyperf/framework": "~2.1.0",
"lcobucci/jwt": "~3.3.0",
"hyperf/cache": "^2.1",
"hyperf/command": "^2.1",
"hyperf/config": "^2.1",
"hyperf/di": "^2.1",
"hyperf/framework": "^2.1",
"lcobucci/jwt": "~4.1.0",
"nesbot/carbon": "^2.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.14",
"hyperf/testing": "~2.1.0",
"friendsofphp/php-cs-fixer": "^3.0",
"hyperf/testing": "^2.1",
"phpstan/phpstan": "^0.12",
"swoole/ide-helper": "dev-master",
"mockery/mockery": "^1.0"
@@ -55,7 +55,7 @@
"sort-packages": true
},
"scripts": {
"test": "co-phpunit -c phpunit.xml --colors=always",
"test": "co-phpunit --prepend tests/bootstrap.php -c phpunit.xml --colors=always",
"analyse": "phpstan analyse --memory-limit 1024M -l 0 ./src",
"cs-fix": "php-cs-fixer fix $1"
},
3 changes: 1 addition & 2 deletions src/Claims/IssuedAt.php
Original file line number Diff line number Diff line change
@@ -40,8 +40,7 @@ public function validate(bool $ignoreExpired = false): bool
}

if (
($refreshTtl = $this->getFactory()->getRefreshTtl()) !== null and
$this->isPast($value + $refreshTtl)
($refreshTtl = $this->getFactory()->getRefreshTtl()) !== null && $this->isPast($value + $refreshTtl)
) {
throw new TokenExpiredException('Token has expired and can no longer be refreshed');
}
106 changes: 94 additions & 12 deletions src/Codec.php
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@
use HyperfExt\Jwt\Exceptions\JwtException;
use HyperfExt\Jwt\Exceptions\TokenInvalidException;
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\Parser;
use Lcobucci\JWT\Signer;
use Lcobucci\JWT\Signer\Ecdsa\Sha256 as ES256;
@@ -25,9 +26,12 @@
use Lcobucci\JWT\Signer\Hmac\Sha256 as HS256;
use Lcobucci\JWT\Signer\Hmac\Sha384 as HS384;
use Lcobucci\JWT\Signer\Hmac\Sha512 as HS512;
use Lcobucci\JWT\Signer\Key\InMemory;
use Lcobucci\JWT\Signer\Rsa\Sha256 as RS256;
use Lcobucci\JWT\Signer\Rsa\Sha384 as RS384;
use Lcobucci\JWT\Signer\Rsa\Sha512 as RS512;
use Lcobucci\JWT\Token\RegisteredClaims;
use Lcobucci\JWT\Validation\Constraint\SignedWith;

class Codec implements CodecInterface
{
@@ -81,18 +85,46 @@ class Codec implements CodecInterface
*/
protected $algo;

/**
* The Configuration instance.
*
* @var \Lcobucci\JWT\Configuration
*/
protected $config;

/**
* The Signer instance.
*
* @var \Lcobucci\JWT\Signer
*/
protected $signer;

public function __construct(string $secret, string $algo, array $keys)
/**
* @param null|\Lcobucci\JWT\Configuration $config
*
* @throws \HyperfExt\Jwt\Exceptions\JwtException
*/
public function __construct(string $secret, string $algo, array $keys, $config = null)
{
$this->secret = $secret;
$this->algo = $algo;
$this->keys = $keys;
$this->config = $config;

$this->signer = $this->getSigner();

if (! is_null($config)) {
$this->config = $config;
} elseif ($this->isAsymmetric()) {
$this->config = Configuration::forAsymmetricSigner($this->signer, $this->getSigningKey(), $this->getVerificationKey());
} else {
$this->config = Configuration::forSymmetricSigner($this->signer, InMemory::plainText($this->getSecret()));
}
if (! count($this->config->validationConstraints())) {
$this->config->setValidationConstraints(
new SignedWith($this->signer, $this->getVerificationKey()),
);
}
}

/**
@@ -200,9 +232,9 @@ public function encode(array $payload): string

try {
foreach ($payload as $key => $value) {
$builder->withClaim($key, $value);
$this->addClaim($builder, $key, $value);
}
return (string) $builder->getToken($this->getSigner(), $this->getSigningKey());
return $builder->getToken($this->config->signer(), $this->config->signingKey())->toString();
} catch (Exception $e) {
throw new JwtException('Could not create token: ' . $e->getMessage(), $e->getCode(), $e);
}
@@ -223,15 +255,65 @@ public function decode(string $token): array
throw new TokenInvalidException('Could not decode token: ' . $e->getMessage(), $e->getCode(), $e);
}

if (! $jwt->verify($this->getSigner(), $this->getVerificationKey())) {
if (! $this->config->validator()->validate($jwt, ...$this->config->validationConstraints())) {
throw new TokenInvalidException('Token Signature could not be verified.');
}
return (new Collection($jwt->claims()->all()))->map(function ($claim) {
if (is_a($claim, \DateTimeImmutable::class)) {
return $claim->getTimestamp();
}
if (is_object($claim) && method_exists($claim, 'getValue')) {
return $claim->getValue();
}

return (new Collection($jwt->getClaims()))->map(function ($claim) {
return is_object($claim) ? $claim->getValue() : $claim;
return $claim;
})->toArray();
}

/**
* Gets the {@see $config} attribute.
*
* @return \Lcobucci\JWT\Configuration
*/
public function getConfig()
{
return $this->config;
}

/**
* Adds a claim to the {@see $config}.
*
* @param mixed $value
*/
protected function addClaim(Builder $builder, string $key, $value)
{
switch ($key) {
case RegisteredClaims::ID:
$builder->identifiedBy((string) $value);
break;
case RegisteredClaims::EXPIRATION_TIME:
$builder->expiresAt(\DateTimeImmutable::createFromFormat('U', (string) $value));
break;
case RegisteredClaims::NOT_BEFORE:
$builder->canOnlyBeUsedAfter(\DateTimeImmutable::createFromFormat('U', (string) $value));
break;
case RegisteredClaims::ISSUED_AT:
$builder->issuedAt(\DateTimeImmutable::createFromFormat('U', (string) $value));
break;
case RegisteredClaims::ISSUER:
$builder->issuedBy((string) $value);
break;
case RegisteredClaims::AUDIENCE:
$builder->permittedFor((string) $value);
break;
case RegisteredClaims::SUBJECT:
$builder->relatedTo((string) $value);
break;
default:
$builder->withClaim($key, $value);
}
}

/**
* Get the signer instance.
*
@@ -255,15 +337,15 @@ protected function getSigner(): Signer
*/
protected function getBuilder(): Builder
{
return new Builder();
return $this->config->builder();
}

/**
* Get the parser instance.
*/
protected function getParser(): Parser
{
return new Parser();
return $this->config->parser();
}

/**
@@ -281,8 +363,8 @@ protected function isAsymmetric(): bool
protected function getSigningKey(): Signer\Key
{
return $this->isAsymmetric()
? new Signer\Key($this->getPrivateKey(), $this->getPassphrase())
: new Signer\Key($this->getSecret());
? InMemory::plainText($this->getPrivateKey(), $this->getPassphrase() ?? '')
: InMemory::plainText($this->getSecret());
}

/**
@@ -291,7 +373,7 @@ protected function getSigningKey(): Signer\Key
protected function getVerificationKey(): Signer\Key
{
return $this->isAsymmetric()
? new Signer\Key($this->getPublicKey())
: new Signer\Key($this->getSecret());
? InMemory::plainText($this->getPublicKey())
: InMemory::plainText($this->getSecret());
}
}
Loading