From bbbdff040fa7d49e5acec38ce3a7c5a35af58a30 Mon Sep 17 00:00:00 2001 From: Deon George Date: Fri, 12 Aug 2022 14:41:22 +1000 Subject: [PATCH] Initial revision supporting obtaining bearer authentication tokens --- composer.json | 29 ++++++++ src/Providers/IntuitServiceProvider.php | 36 ++++++++++ src/Providers/Socialite/IntuitProvider.php | 82 ++++++++++++++++++++++ src/Traits/IntuitSocialite.php | 23 ++++++ src/config/intuit.php | 7 ++ 5 files changed, 177 insertions(+) create mode 100644 composer.json create mode 100644 src/Providers/IntuitServiceProvider.php create mode 100644 src/Providers/Socialite/IntuitProvider.php create mode 100644 src/Traits/IntuitSocialite.php create mode 100644 src/config/intuit.php diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..92939d0 --- /dev/null +++ b/composer.json @@ -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" +} \ No newline at end of file diff --git a/src/Providers/IntuitServiceProvider.php b/src/Providers/IntuitServiceProvider.php new file mode 100644 index 0000000..ccfab45 --- /dev/null +++ b/src/Providers/IntuitServiceProvider.php @@ -0,0 +1,36 @@ +mergeConfigFrom($this->_path.'/config/intuit.php','intuit'); + } + + /** + * Register the application services. + * + * @return void + */ + public function register() + { + if (! $this->_path) { + $this->_path = realpath(__DIR__.'/../../src'); + } + } +} \ No newline at end of file diff --git a/src/Providers/Socialite/IntuitProvider.php b/src/Providers/Socialite/IntuitProvider.php new file mode 100644 index 0000000..9909ced --- /dev/null +++ b/src/Providers/Socialite/IntuitProvider.php @@ -0,0 +1,82 @@ + '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')); + } +} \ No newline at end of file diff --git a/src/Traits/IntuitSocialite.php b/src/Traits/IntuitSocialite.php new file mode 100644 index 0000000..d83ec46 --- /dev/null +++ b/src/Traits/IntuitSocialite.php @@ -0,0 +1,23 @@ +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); + }); + } +} \ No newline at end of file diff --git a/src/config/intuit.php b/src/config/intuit.php new file mode 100644 index 0000000..81f980a --- /dev/null +++ b/src/config/intuit.php @@ -0,0 +1,7 @@ + env('AUTH_INTUIT_CLIENT_ID'), + 'client_secret' => env('AUTH_INTUIT_SECRET_KEY'), + 'redirect' => '/auth/intuit/token', +]; \ No newline at end of file