Skip to content

Commit

Permalink
feature #1046 feat: try to invalidate realpath cache if keypair loadi…
Browse files Browse the repository at this point in the history
…ng failed (lobodol)

This PR was squashed before being merged into the 2.x branch.

Discussion
----------

feat: try to invalidate realpath cache if keypair loading failed

Closes #1045

Commits
-------

f1d5c31 feat: try to invalidate realpath cache if keypair loading failed
  • Loading branch information
chalasr committed Jul 26, 2022
2 parents ef9d7fc + f1d5c31 commit 3e8c013
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
23 changes: 21 additions & 2 deletions Services/KeyLoader/AbstractKeyLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,18 @@ public function getAdditionalPublicKeys(): array
throw new \RuntimeException(sprintf('Additional public key "%s" does not exist or is not readable. Did you correctly set the "lexik_jwt_authentication.additional_public_keys" configuration key?', $key));
}

$contents[] = is_file($key) ? file_get_contents($key) : $key;
$rawKey = $key;

if (is_file($key)) {
$rawKey = @file_get_contents($key);

if (false === $rawKey) {
// Try invalidating the realpath cache
clearstatcache(true, $key);
$rawKey = file_get_contents($key);
}
}
$contents[] = $rawKey;
}

return $contents;
Expand Down Expand Up @@ -93,6 +104,14 @@ private function readKey($type)
throw new \RuntimeException(sprintf('Signature key "%s" does not exist or is not readable. Did you correctly set the "lexik_jwt_authentication.signature_key" configuration key?', $key));
}

return file_get_contents($key);
$rawKey = @file_get_contents($key);

if (false === $rawKey) {
// Try invalidating the realpath cache
clearstatcache(true, $key);
$rawKey = file_get_contents($key);
}

return $rawKey;
}
}
10 changes: 9 additions & 1 deletion Services/KeyLoader/OpenSSLKeyLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@ public function loadKey($type)
throw new \InvalidArgumentException(sprintf('The key type must be "public" or "private", "%s" given.', $type));
}

$rawKey = file_get_contents($this->getKeyPath($type));
$keyPath = $this->getKeyPath($type);
$rawKey = @file_get_contents($keyPath);

if (false === $rawKey) {
// Try invalidating the realpath cache
clearstatcache(true, $keyPath);
$rawKey = file_get_contents($keyPath);
}

$key = call_user_func_array("openssl_pkey_get_$type", self::TYPE_PRIVATE == $type ? [$rawKey, $this->getPassphrase()] : [$rawKey]);

if (!$key) {
Expand Down

0 comments on commit 3e8c013

Please sign in to comment.