Some extra debug logging

This commit is contained in:
Deon George 2024-07-14 11:21:30 +10:00
parent b87550fae7
commit 403d2168c1
2 changed files with 27 additions and 11 deletions

View File

@ -3,10 +3,13 @@
namespace Intuit\Models; namespace Intuit\Models;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;
use Laravel\Socialite\Facades\Socialite; use Laravel\Socialite\Facades\Socialite;
class ProviderToken extends Model class ProviderToken extends Model
{ {
private const LOGKEY = 'IPT';
protected $casts = [ protected $casts = [
'access_token_expires_at' => 'datetime:Y-m-d H:i:s', 'access_token_expires_at' => 'datetime:Y-m-d H:i:s',
'refresh_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 */ /* ATTRIBUTES */
public function getAccessTokenAttribute($value): ?string public function getAccessTokenAttribute(string $value): ?string
{ {
if (! $this->hasAccessTokenExpired()) if (! $this->hasAccessTokenExpired())
return $value; return $value;
@ -29,11 +32,16 @@ class ProviderToken extends Model
public function hasAccessTokenExpired(): bool public function hasAccessTokenExpired(): bool
{ {
return $this->access_token_expires_at->isPast(); return $this
->access_token_expires_at
->isPast();
} }
public function refreshToken(): bool 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);
} }
} }

View File

@ -6,6 +6,7 @@ use Carbon\Carbon;
use GuzzleHttp\RequestOptions; use GuzzleHttp\RequestOptions;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Laravel\Passport\Exceptions\InvalidAuthTokenException; use Laravel\Passport\Exceptions\InvalidAuthTokenException;
use Laravel\Socialite\Two\InvalidStateException; use Laravel\Socialite\Two\InvalidStateException;
use Laravel\Socialite\Two\ProviderInterface; use Laravel\Socialite\Two\ProviderInterface;
@ -16,6 +17,8 @@ use App\Models\{ProviderOauth,ProviderToken};
class IntuitProvider extends AbstractProvider implements ProviderInterface class IntuitProvider extends AbstractProvider implements ProviderInterface
{ {
private const LOGKEY = 'SIP';
private const hosts = [ private const hosts = [
'authorise' => 'https://appcenter.intuit.com/connect/oauth2', 'authorise' => 'https://appcenter.intuit.com/connect/oauth2',
'tokenendpoint' => 'https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer', '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(), [ Log::debug(sprintf('%s:= Refreshing token for [%d]',self::LOGKEY,$to->id));
RequestOptions::HEADERS => $this->getAuthorisationHeader($to->provider),
RequestOptions::FORM_PARAMS => $this->getRefreshTokenFields($to->refresh_token), $response = $this
]); ->getHttpClient()
->post($this->getTokenUrl(),[
RequestOptions::HEADERS => $this->getAuthorisationHeader($to->provider),
RequestOptions::FORM_PARAMS => $this->getRefreshTokenFields($to->refresh_token),
]);
switch ($response->getStatusCode()) { switch ($response->getStatusCode()) {
case '200': case '200':
@ -125,10 +132,11 @@ class IntuitProvider extends AbstractProvider implements ProviderInterface
if (($x=Arr::get($body,'refresh_token')) !== $to->refresh_token) { if (($x=Arr::get($body,'refresh_token')) !== $to->refresh_token) {
$to->refresh_token = $x; $to->refresh_token = $x;
$to->refresh_token_expires_at = Carbon::now()->addSeconds(Arr::get($body,'x_refresh_token_expires_in')); $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: default:
throw new InvalidAuthTokenException(sprintf('Invalid response [%d] refreshing token for [%s] (%s)',$response->getStatusCode(),$to->user->email,$response->getBody())); throw new InvalidAuthTokenException(sprintf('Invalid response [%d] refreshing token for [%s] (%s)',$response->getStatusCode(),$to->user->email,$response->getBody()));