Skip to content

Commit

Permalink
perf: Update getCredential to only refresh credential once per request (
Browse files Browse the repository at this point in the history
#453)

* Update getCredential to only refresh creds once per request

Signed-off-by: Russ <[email protected]>

* Update PHP syntax to fix phpstan, psalm, phpcs, and rector tests

* Implement initial thumbprint logic in getCredential

* Make thumbprinting more performant by using sdk->getCredentials instead of this->findSession

---------

Signed-off-by: Russ <[email protected]>
Co-authored-by: Russell Kenny <[email protected]>
  • Loading branch information
ComputerTinker and Russell Kenny authored Jun 4, 2024
1 parent 908ff13 commit b4d099a
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/Guards/AuthenticationGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ final class AuthenticationGuard extends GuardAbstract implements AuthenticationG
*/
protected const TELESCOPE = '\Laravel\Telescope\Telescope';

private ?string $credThumbprint = null;

public function find(): ?CredentialEntityContract
{
if ($this->isImpersonating()) {
Expand Down Expand Up @@ -93,9 +95,13 @@ public function getCredential(): ?CredentialEntityContract
}

if ($this->credential instanceof CredentialEntityContract) {
$updated = $this->findSession();
$this->setCredential($updated);
$this->pushState($updated);
$currThumbprint = $this->getCredThumbprint($this->sdk()->getCredentials());
if ($currThumbprint !== $this->credThumbprint) {
$updated = $this->findSession();
$this->setCredential($updated);
$this->pushState($updated);
$this->credThumbprint = $currThumbprint;
}
}

return $this->credential;
Expand Down Expand Up @@ -331,6 +337,15 @@ public function user(): ?Authenticatable
return $lastResponse = null;
}

private function getCredThumbprint(?object $credential): null | string
{
if (null === $credential) {
return null;
}

return md5(serialize($credential));
}

private function pullState(): ?CredentialEntityContract
{
$sdk = $this->sdk();
Expand Down

0 comments on commit b4d099a

Please sign in to comment.