Compare commits

...

2 Commits

Author SHA1 Message Date
947e1358b5 Better handle responses missing expected keys 2024-07-14 13:46:04 +10:00
403d2168c1 Some extra debug logging 2024-07-14 11:21:30 +10:00
3 changed files with 30 additions and 14 deletions

View File

@ -198,7 +198,7 @@ final class API
$x = $this->execute('query',$parameters); $x = $this->execute('query',$parameters);
if ((! $x->QueryResponse) || (! $x->QueryResponse->Customer) || (count($x->QueryResponse->Customer) !== 1)) if ((! $x->QueryResponse) || (! object_get($x->QueryResponse,'Customer')) || (count($x->QueryResponse->Customer) !== 1))
throw new InvalidQueryResultException(sprintf('%s:Query response malformed',self::LOGKEY)); throw new InvalidQueryResultException(sprintf('%s:Query response malformed',self::LOGKEY));
return new Customer($x->QueryResponse); return new Customer($x->QueryResponse);
@ -251,8 +251,8 @@ final class API
$x = $this->execute('query',$parameters); $x = $this->execute('query',$parameters);
if ((! $x->QueryResponse) || (! $x->QueryResponse->Invoice) || (count($x->QueryResponse->Invoice) !== 1)) if ((! $x->QueryResponse) || (! object_get($x->QueryResponse,'Invoice')) || (count($x->QueryResponse->Invoice) !== 1))
throw new InvalidQueryResultException(sprintf('%s:Query response malformed',self::LOGKEY)); throw new InvalidQueryResultException(sprintf('%s:Query response malformed [%s]',self::LOGKEY,serialize($x)));
return new Invoice($x->QueryResponse); return new Invoice($x->QueryResponse);
} }

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()));