Initial revision supporting obtaining bearer authentication tokens

This commit is contained in:
Deon George 2022-08-12 14:41:22 +10:00
commit bbbdff040f
5 changed files with 177 additions and 0 deletions

29
composer.json Normal file
View File

@ -0,0 +1,29 @@
{
"name": "leenooks/intuit",
"description": "Intuit API",
"keywords": ["laravel","leenooks","intuit"],
"authors": [
{
"name": "Deon George",
"email": "deon@leenooks.net"
}
],
"require": {
"jenssegers/model": "^1.5"
},
"require-dev": {
},
"autoload": {
"psr-4": {
"Intuit\\": "src"
}
},
"extra": {
"laravel": {
"providers": [
"Intuit\\Providers\\IntuitServiceProvider"
]
}
},
"minimum-stability": "dev"
}

View File

@ -0,0 +1,36 @@
<?php
namespace Intuit\Providers;
use Illuminate\Routing\Router;
use Illuminate\Support\ServiceProvider;
/**
* Class IntuitServiceProvider.
*/
class IntuitServiceProvider extends ServiceProvider
{
private string $_path = '';
/**
* Bootstrap the application services.
*
* @param Router $router
*/
public function boot(Router $router)
{
$this->mergeConfigFrom($this->_path.'/config/intuit.php','intuit');
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
if (! $this->_path) {
$this->_path = realpath(__DIR__.'/../../src');
}
}
}

View File

@ -0,0 +1,82 @@
<?php
namespace Intuit\Providers\Socialite;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth;
use Laravel\Socialite\Two\InvalidStateException;
use Laravel\Socialite\Two\ProviderInterface;
use Laravel\Socialite\Two\AbstractProvider;
use Laravel\Socialite\Two\User as SocialUser;
class IntuitProvider extends AbstractProvider implements ProviderInterface
{
private const hosts = [
'authorise' => 'https://appcenter.intuit.com/connect/oauth2',
'accesstoken' => 'https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer',
'local' => 'https://sandbox-quickbooks.api.intuit.com',
'production' => 'https://quickbooks.api.intuit.com',
];
protected $scopes = ['com.intuit.quickbooks.accounting'];
/* ABSTRACT */
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase(
Arr::get(self::hosts,'authorise'),
$state
);
}
protected function getUserByToken($token)
{
$response = [];
$access = $this->getAccessTokenResponse($token);
if ((! $x=Arr::get($access,'token_type')) || ($x !== 'bearer')) {
$response['reject'] = TRUE;
$response['reject_reason'] = 'No bearer token?';
return $response;
}
return $response ?: $access;
}
protected function getTokenUrl()
{
return Arr::get(self::hosts,'accesstoken');
}
protected function mapUserToObject(array $user)
{
return (new SocialUser)->setRaw(Auth::user()->toArray());
}
/* OVERRIDES */
public function user()
{
if ($this->hasInvalidState()) {
throw new InvalidStateException;
}
if (! $this->getCode())
return NULL;
$details = $this->getUserByToken($this->getCode());
if ((! $details) || Arr::get($details,'reject'))
abort(403, sprintf('Authentication Failed with %s',Arr::get($details,'reject_reason','*no reason given*')));
$user = $this->mapUserToObject($details);
$user->refresh_token_expires_in = Arr::get($details,'x_refresh_token_expires_in');
$user->realmid = $this->request->get('realmId');
return $user->setToken(Arr::get($details,'access_token'))
->setRefreshToken(Arr::get($details,'refresh_token'))
->setExpiresIn(Arr::get($details,'expires_in'));
}
}

View File

@ -0,0 +1,23 @@
<?php
/**
* Setup for Intuit authentication
*/
namespace Intuit\Traits;
use Illuminate\Support\Arr;
use Intuit\Providers\Socialite\IntuitProvider;
trait IntuitSocialite
{
private function bootIntuitSocialite()
{
$socialite = $this->app->make('Laravel\Socialite\Contracts\Factory');
$socialite->extend('intuit', function ($app) use ($socialite) {
$config = Arr::get($app,'config.intuit');
return $socialite->buildProvider(IntuitProvider::class, $config);
});
}
}

7
src/config/intuit.php Normal file
View File

@ -0,0 +1,7 @@
<?php
return [
'client_id' => env('AUTH_INTUIT_CLIENT_ID'),
'client_secret' => env('AUTH_INTUIT_SECRET_KEY'),
'redirect' => '/auth/intuit/token',
];