Skip to content

Commit

Permalink
bug #815 Fix compatibility for lcobucci/jwt v3.x (bis) (chalasr)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.x-dev branch.

Discussion
----------

Fix compatibility for lcobucci/jwt v3.x (bis)

Fixes #814

Commits
-------

5e127cd Fix compatibility for lcobucci/jwt v3.x
  • Loading branch information
chalasr committed Dec 19, 2020
2 parents 7d3e2eb + 5e127cd commit 9c29534
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions Services/JWSProvider/LcobucciJWSProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ class LcobucciJWSProvider implements JWSProviderInterface
*/
private $legacyJWTApi;

/**
* @var bool
*/
private $useDateObjects;

/**
* @param KeyLoaderInterface $keyLoader
* @param string $cryptoEngine
Expand Down Expand Up @@ -85,7 +90,8 @@ public function __construct(KeyLoaderInterface $keyLoader, $cryptoEngine, $signa
$this->signer = $this->getSignerForAlgorithm($signatureAlgorithm);
$this->ttl = $ttl;
$this->clockSkew = $clockSkew;
$this->legacyJWTApi = !class_exists(OpenSSL::class);
$this->legacyJWTApi = !class_exists(OpenSSL::class); // exists only on lcobucci/jwt 3.3+
$this->useDateObjects = method_exists(Token::class, 'payload') || class_exists(Plain::class); // exists only on lcobucci/jwt 3.4+
}

/**
Expand All @@ -108,7 +114,7 @@ public function create(array $payload, array $header = [])
if ($this->legacyJWTApi) {
$jws->setIssuedAt($now);
} else {
$jws->issuedAt(new \DateTimeImmutable("@{$now}"));
$jws->issuedAt($this->useDateObjects ? new \DateTimeImmutable("@{$now}") : $now);
}

if (null !== $this->ttl || isset($payload['exp'])) {
Expand All @@ -118,7 +124,7 @@ public function create(array $payload, array $header = [])
if ($this->legacyJWTApi) {
$jws->setExpiration($exp);
} else {
$jws->expiresAt($exp instanceof \DateTimeImmutable ? $exp : new \DateTimeImmutable("@$exp"));
$jws->expiresAt($exp instanceof \DateTimeImmutable ? $exp : ($this->useDateObjects ? new \DateTimeImmutable("@$exp") : $exp));
}
}

Expand Down Expand Up @@ -162,7 +168,7 @@ public function load($token)

$payload = [];

if ($this->legacyJWTApi) {
if ($this->legacyJWTApi || !$this->useDateObjects) {
foreach ($jws->getClaims() as $claim) {
$payload[$claim->getName()] = $claim->getValue();
}
Expand All @@ -179,7 +185,7 @@ public function load($token)
$payload,
$this->verify($jws),
null !== $this->ttl,
$this->legacyJWTApi ? $jws->getHeaders() : $jws->headers()->all(),
$this->legacyJWTApi || !$this->useDateObjects ? $jws->getHeaders() : $jws->headers()->all(),
$this->clockSkew
);

Expand Down Expand Up @@ -220,7 +226,7 @@ private function getSignedToken(Builder $jws)
}

if ($this->legacyJWTApi) {
$jws->sign($key, $this->signer);
$jws->sign($this->signer, $key);

return $jws->getToken();
}
Expand All @@ -236,7 +242,7 @@ private function getSignedToken(Builder $jws)

private function verify(Token $jwt)
{
if ($this->legacyJWTApi) {
if ($this->legacyJWTApi || !$this->useDateObjects) {
if (!$jwt->validate(new ValidationData(time() + $this->clockSkew))) {
return false;
}
Expand Down

0 comments on commit 9c29534

Please sign in to comment.