From 403d2168c17a7515e5777d121fabd7596ddc87d9 Mon Sep 17 00:00:00 2001 From: Deon George Date: Sun, 14 Jul 2024 11:21:30 +1000 Subject: [PATCH] Some extra debug logging --- src/Models/ProviderToken.php | 14 ++++++++++--- src/Providers/Socialite/IntuitProvider.php | 24 ++++++++++++++-------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/Models/ProviderToken.php b/src/Models/ProviderToken.php index 5ac8de8..e1fcac6 100644 --- a/src/Models/ProviderToken.php +++ b/src/Models/ProviderToken.php @@ -3,10 +3,13 @@ namespace Intuit\Models; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Facades\Log; use Laravel\Socialite\Facades\Socialite; class ProviderToken extends Model { + private const LOGKEY = 'IPT'; + protected $casts = [ 'access_token_expires_at' => 'datetime:Y-m-d H:i:s', 'refresh_token_expires_at' => 'datetime:Y-m-d H:i:s', @@ -14,7 +17,7 @@ class ProviderToken extends Model /* ATTRIBUTES */ - public function getAccessTokenAttribute($value): ?string + public function getAccessTokenAttribute(string $value): ?string { if (! $this->hasAccessTokenExpired()) return $value; @@ -29,11 +32,16 @@ class ProviderToken extends Model public function hasAccessTokenExpired(): bool { - return $this->access_token_expires_at->isPast(); + return $this + ->access_token_expires_at + ->isPast(); } public function refreshToken(): bool { - return Socialite::with($this->provider->name)->refreshtoken($this); + Log::debug(sprintf('%s:= Refreshing token for [%s]',self::LOGKEY,$this->provider->name)); + + return Socialite::with($this->provider->name) + ->refreshToken($this); } } \ No newline at end of file diff --git a/src/Providers/Socialite/IntuitProvider.php b/src/Providers/Socialite/IntuitProvider.php index 1444192..35b88f9 100644 --- a/src/Providers/Socialite/IntuitProvider.php +++ b/src/Providers/Socialite/IntuitProvider.php @@ -6,6 +6,7 @@ use Carbon\Carbon; use GuzzleHttp\RequestOptions; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Log; use Laravel\Passport\Exceptions\InvalidAuthTokenException; use Laravel\Socialite\Two\InvalidStateException; use Laravel\Socialite\Two\ProviderInterface; @@ -16,6 +17,8 @@ use App\Models\{ProviderOauth,ProviderToken}; class IntuitProvider extends AbstractProvider implements ProviderInterface { + private const LOGKEY = 'SIP'; + private const hosts = [ 'authorise' => 'https://appcenter.intuit.com/connect/oauth2', 'tokenendpoint' => 'https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer', @@ -104,12 +107,16 @@ class IntuitProvider extends AbstractProvider implements ProviderInterface ]; } - public function refreshtoken($to) + public function refreshToken($to) { - $response = $this->getHttpClient()->post($this->getTokenUrl(), [ - RequestOptions::HEADERS => $this->getAuthorisationHeader($to->provider), - RequestOptions::FORM_PARAMS => $this->getRefreshTokenFields($to->refresh_token), - ]); + Log::debug(sprintf('%s:= Refreshing token for [%d]',self::LOGKEY,$to->id)); + + $response = $this + ->getHttpClient() + ->post($this->getTokenUrl(),[ + RequestOptions::HEADERS => $this->getAuthorisationHeader($to->provider), + RequestOptions::FORM_PARAMS => $this->getRefreshTokenFields($to->refresh_token), + ]); switch ($response->getStatusCode()) { case '200': @@ -125,10 +132,11 @@ class IntuitProvider extends AbstractProvider implements ProviderInterface if (($x=Arr::get($body,'refresh_token')) !== $to->refresh_token) { $to->refresh_token = $x; $to->refresh_token_expires_at = Carbon::now()->addSeconds(Arr::get($body,'x_refresh_token_expires_in')); - } - $to->save(); - return TRUE; + Log::debug(sprintf('%s:= Refresh token updated.',self::LOGKEY)); + } + + return $to->save(); default: throw new InvalidAuthTokenException(sprintf('Invalid response [%d] refreshing token for [%s] (%s)',$response->getStatusCode(),$to->user->email,$response->getBody()));