From 40a009c67ffe467f4c5bc0d38729a574a38c6d8a Mon Sep 17 00:00:00 2001 From: Russ Date: Fri, 5 Apr 2024 13:35:24 -0400 Subject: [PATCH 1/4] Update getCredential to only refresh creds once per request Signed-off-by: Russ --- src/Guards/AuthenticationGuard.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Guards/AuthenticationGuard.php b/src/Guards/AuthenticationGuard.php index 23f8c7a..a5dc698 100644 --- a/src/Guards/AuthenticationGuard.php +++ b/src/Guards/AuthenticationGuard.php @@ -32,6 +32,11 @@ final class AuthenticationGuard extends GuardAbstract implements AuthenticationG */ protected const TELESCOPE = '\Laravel\Telescope\Telescope'; + /** + * @var bool + */ + private bool $didCredRefresh = false; + public function find(): ?CredentialEntityContract { if ($this->isImpersonating()) { @@ -92,10 +97,15 @@ public function getCredential(): ?CredentialEntityContract return $this->getImposter(); } + if (!empty($this->didCredRefresh)) { + return $this->credential; + } + if ($this->credential instanceof CredentialEntityContract) { $updated = $this->findSession(); $this->setCredential($updated); $this->pushState($updated); + $this->didCredRefresh = true; } return $this->credential; From bd2cdd82d161cae137e7b78ff2d0d061ca74a416 Mon Sep 17 00:00:00 2001 From: Russell Kenny Date: Tue, 9 Apr 2024 16:23:17 -0400 Subject: [PATCH 2/4] Update PHP syntax to fix phpstan, psalm, phpcs, and rector tests --- src/Guards/AuthenticationGuard.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Guards/AuthenticationGuard.php b/src/Guards/AuthenticationGuard.php index a5dc698..e2ab7f6 100644 --- a/src/Guards/AuthenticationGuard.php +++ b/src/Guards/AuthenticationGuard.php @@ -32,9 +32,6 @@ final class AuthenticationGuard extends GuardAbstract implements AuthenticationG */ protected const TELESCOPE = '\Laravel\Telescope\Telescope'; - /** - * @var bool - */ private bool $didCredRefresh = false; public function find(): ?CredentialEntityContract @@ -97,7 +94,7 @@ public function getCredential(): ?CredentialEntityContract return $this->getImposter(); } - if (!empty($this->didCredRefresh)) { + if ($this->didCredRefresh) { return $this->credential; } From 0afefb1561a7c6d2991d9afb2f7178f0760546c9 Mon Sep 17 00:00:00 2001 From: Russell Kenny Date: Wed, 10 Apr 2024 12:04:45 -0400 Subject: [PATCH 3/4] Implement initial thumbprint logic in getCredential --- src/Guards/AuthenticationGuard.php | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/Guards/AuthenticationGuard.php b/src/Guards/AuthenticationGuard.php index e2ab7f6..591ef6b 100644 --- a/src/Guards/AuthenticationGuard.php +++ b/src/Guards/AuthenticationGuard.php @@ -32,7 +32,7 @@ final class AuthenticationGuard extends GuardAbstract implements AuthenticationG */ protected const TELESCOPE = '\Laravel\Telescope\Telescope'; - private bool $didCredRefresh = false; + private ?string $credThumbprint = null; public function find(): ?CredentialEntityContract { @@ -94,15 +94,14 @@ public function getCredential(): ?CredentialEntityContract return $this->getImposter(); } - if ($this->didCredRefresh) { - return $this->credential; - } - if ($this->credential instanceof CredentialEntityContract) { $updated = $this->findSession(); - $this->setCredential($updated); - $this->pushState($updated); - $this->didCredRefresh = true; + $updatedCredThumbprint = $this->getCredThumbprint($updated); + if ($updatedCredThumbprint !== $this->credThumbprint) { + $this->setCredential($updated); + $this->pushState($updated); + $this->credThumbprint = $updatedCredThumbprint; + } } return $this->credential; @@ -247,6 +246,7 @@ public function setCredential( $this->stopImpersonating(); $this->credential = $credential; + $this->credThumbprint = $this->getCredThumbprint($credential); return $this; } @@ -338,6 +338,15 @@ public function user(): ?Authenticatable return $lastResponse = null; } + private function getCredThumbprint(?CredentialEntityContract $credential): null | string + { + if (! $credential instanceof CredentialEntityContract) { + return null; + } + + return md5(serialize($credential)); + } + private function pullState(): ?CredentialEntityContract { $sdk = $this->sdk(); From 8610e6cd6be5d22ea37db6e77972627a6ad1d7fc Mon Sep 17 00:00:00 2001 From: Russell Kenny Date: Wed, 10 Apr 2024 16:10:51 -0400 Subject: [PATCH 4/4] Make thumbprinting more performant by using sdk->getCredentials instead of this->findSession --- src/Guards/AuthenticationGuard.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Guards/AuthenticationGuard.php b/src/Guards/AuthenticationGuard.php index 591ef6b..e3cc4ff 100644 --- a/src/Guards/AuthenticationGuard.php +++ b/src/Guards/AuthenticationGuard.php @@ -95,12 +95,12 @@ public function getCredential(): ?CredentialEntityContract } if ($this->credential instanceof CredentialEntityContract) { - $updated = $this->findSession(); - $updatedCredThumbprint = $this->getCredThumbprint($updated); - if ($updatedCredThumbprint !== $this->credThumbprint) { + $currThumbprint = $this->getCredThumbprint($this->sdk()->getCredentials()); + if ($currThumbprint !== $this->credThumbprint) { + $updated = $this->findSession(); $this->setCredential($updated); $this->pushState($updated); - $this->credThumbprint = $updatedCredThumbprint; + $this->credThumbprint = $currThumbprint; } } @@ -246,7 +246,6 @@ public function setCredential( $this->stopImpersonating(); $this->credential = $credential; - $this->credThumbprint = $this->getCredThumbprint($credential); return $this; } @@ -338,9 +337,9 @@ public function user(): ?Authenticatable return $lastResponse = null; } - private function getCredThumbprint(?CredentialEntityContract $credential): null | string + private function getCredThumbprint(?object $credential): null | string { - if (! $credential instanceof CredentialEntityContract) { + if (null === $credential) { return null; }