From 807d12a2b577650e316bb1f22dab27b5645d2b60 Mon Sep 17 00:00:00 2001 From: Maxime Renou Date: Tue, 12 May 2020 15:57:23 +0200 Subject: [PATCH] Command and tokens trait --- src/Commands/Sync.php | 43 +++++++++++ src/Connect.php | 165 +++++++++++++++++++++------------------- src/ConnectServiceProvider.php | 41 +++++++--- src/Traits/HasConnectTokens.php | 8 ++ 4 files changed, 167 insertions(+), 90 deletions(-) create mode 100644 src/Commands/Sync.php create mode 100644 src/Traits/HasConnectTokens.php diff --git a/src/Commands/Sync.php b/src/Commands/Sync.php new file mode 100644 index 0000000..daa21eb --- /dev/null +++ b/src/Commands/Sync.php @@ -0,0 +1,43 @@ +syncAll(); + } +} diff --git a/src/Connect.php b/src/Connect.php index 235d51a..5a94772 100644 --- a/src/Connect.php +++ b/src/Connect.php @@ -3,6 +3,7 @@ namespace Bluesquare\Connect; use Bluesquare\Connect\Traits\HasConnectSync; +use Bluesquare\Connect\Traits\HasConnectTokens; use GuzzleHttp\Client; use Illuminate\Http\Request; use Illuminate\Routing\Router; @@ -112,62 +113,6 @@ class Connect return redirect()->to($url); } - public function getAccessTokenFromAuhtorizationCode($code) - { - $data = $this->request('post', 'oauth/token', [ - 'grant_type' => 'authorization_code', - 'client_id' => config('bconnect.client_id'), - 'client_secret' => config('bconnect.client_secret'), - 'scope' => config('bconnect.user_scopes'), - 'redirect_uri' => config('bconnect.redirect_url'), - 'code' => $code - ], false); - - return $data; - } - - public function getAccessTokenFromRefreshToken($refresh_token) - { - $data = $this->request('post', 'oauth/token', [ - 'grant_type' => 'refresh_token', - 'client_id' => config('bconnect.client_id'), - 'client_secret' => config('bconnect.client_secret'), - 'scope' => config('bconnect.user_scopes'), - 'redirect_uri' => config('bconnect.redirect_url'), - 'refresh_token' => $refresh_token - ], false); - - return $data; - } - - public function getUserData($access_token) - { - return $this->request('get', 'api/user', null, $access_token); - } - - public function updateUserConnectData($model, $data) - { - $model->connect_access_token = $data['access_token']; - $model->connect_refresh_token = $data['refresh_token']; - $model->connect_expires_at = $data['expires_at']; - $model->save(); - } - - public function getUserAccessToken($model) - { - if (empty($model->connect_access_token) || empty($model->connect_refresh_token) || empty($model->connect_expires_at)) { - throw new ConnectException("Missing Bluesquare Connect attributes on model: connect_access_token, connect_refresh_token, connect_expires_at"); - } - - if ($model->connect_expires_at <= now()) { - $connect_data = $this->getAccessTokenFromRefreshToken($model->connect_refresh_token); - $this->updateUserConnectData($model, $connect_data); - return $connect_data['access_token']; - } - - return $model->connect_access_token; - } - public function loginFromCallback(Request $request) { // State check @@ -202,7 +147,7 @@ class Connect // Access token $expires_at = now(); - $connect_data = $this->getAccessTokenFromAuhtorizationCode($request->code); + $connect_data = $this->getAccessTokenFromAuthorizationCode($request->code); $connect_data['expires_at'] = $expires_at->addSeconds($connect_data['expires_in']); $model_data = $this->getUserData($connect_data['access_token']); @@ -223,6 +168,68 @@ class Connect return redirect('/'); } + public function getAccessTokenFromAuthorizationCode($code) + { + $data = $this->request('post', 'oauth/token', [ + 'grant_type' => 'authorization_code', + 'client_id' => config('bconnect.client_id'), + 'client_secret' => config('bconnect.client_secret'), + 'scope' => config('bconnect.user_scopes'), + 'redirect_uri' => config('bconnect.redirect_url'), + 'code' => $code + ], false); + + return $data; + } + + public function getAccessTokenFromRefreshToken($refresh_token) + { + $data = $this->request('post', 'oauth/token', [ + 'grant_type' => 'refresh_token', + 'client_id' => config('bconnect.client_id'), + 'client_secret' => config('bconnect.client_secret'), + 'scope' => config('bconnect.user_scopes'), + 'redirect_uri' => config('bconnect.redirect_url'), + 'refresh_token' => $refresh_token + ], false); + + return $data; + } + + public function getUserData($access_token) + { + return $this->request('get', 'api/user', null, $access_token); + } + + public function updateUserConnectData($user, $data) + { + if (!in_array(HasConnectTokens::class, class_uses(get_class($user)))) + return false; + + $user->connect_access_token = $data['access_token']; + $user->connect_refresh_token = $data['refresh_token']; + $user->connect_expires_at = $data['expires_at']; + + return $user->save(); + } + + public function getUserAccessToken($user) + { + $has_fields = in_array(HasConnectTokens::class, class_uses(get_class($user))); + + if (!$has_fields) { + throw new ConnectException("User class does not implement HasConnectTokens"); + } + + if ($user->connect_expires_at <= now()) { + $connect_data = $this->getAccessTokenFromRefreshToken($user->connect_refresh_token); + $this->updateUserConnectData($user, $connect_data); + return $connect_data['access_token']; + } + + return $user->connect_access_token; + } + // OAuth (client) public function getAccessToken() @@ -253,7 +260,7 @@ class Connect cache()->delete('bconnect.access_token_expiration'); } - // Webhooks handler + // Webhook handler /** * @param Request $request @@ -291,7 +298,7 @@ class Connect return true; } - // Resources endpoints + // Resources getters public function getAll($resourceType) { @@ -303,7 +310,7 @@ class Connect return $this->request('get', "api/resources/$resourceType/$resourceId"); } - // Resources sync + // Resources syncing public function syncAll($resourceTypes = null) { @@ -348,7 +355,26 @@ class Connect $model::$method($resourceId, $resourceData); } - // + // Routing + + public function routes() + { + Route::namespace('\Bluesquare\Connect\Controllers') + ->group(function () { + Route::get('connect/authorize', 'ConnectController@authorize'); + Route::get('connect/callback', 'ConnectController@callback'); + }); + } + + public function apiRoutes() + { + Route::namespace('\Bluesquare\Connect\Controllers') + ->group(function () { + Route::post('connect/webhook', 'ConnectController@webhook'); + }); + } + + // Misc protected function resolveResourceType($class) { @@ -375,21 +401,4 @@ class Connect { return config('bconnect.url') ?? 'https://connect.bluesquare.io'; } - - public function routes() - { - Route::namespace('\Bluesquare\Connect\Controllers') - ->group(function () { - Route::get('connect/authorize', 'ConnectController@authorize'); - Route::get('connect/callback', 'ConnectController@callback'); - }); - } - - public function apiRoutes() - { - Route::namespace('\Bluesquare\Connect\Controllers') - ->group(function () { - Route::post('connect/webhook', 'ConnectController@webhook'); - }); - } } diff --git a/src/ConnectServiceProvider.php b/src/ConnectServiceProvider.php index 545b349..a28e5e6 100644 --- a/src/ConnectServiceProvider.php +++ b/src/ConnectServiceProvider.php @@ -2,6 +2,7 @@ namespace Bluesquare\Connect; +use Bluesquare\Connect\Commands\Sync; use Bluesquare\Connect\View\Components\Button; use Illuminate\Support\ServiceProvider; @@ -14,14 +15,34 @@ class ConnectServiceProvider extends ServiceProvider */ public function register() { + // Config + $this->mergeConfigFrom( __DIR__ . '/../config/bconnect.php', - 'bmail' + 'bconnect' ); + // Singletons + $this->app->singleton(Connect::class, function ($app) { return new Connect($app); }); + } + + /** + * Bootstrap any application services. + * + * @return void + */ + public function boot() + { + // Config + + $this->publishes([ + __DIR__ . '/../config/bconnect.php' => config_path('bconnect.php') + ]); + + // Views $this->loadViewsFrom(__DIR__.'/../resources/views/connect', 'connect'); @@ -32,17 +53,13 @@ class ConnectServiceProvider extends ServiceProvider $this->loadViewComponentsAs('connect', [ Button::class ]); - } - /** - * Bootstrap any application services. - * - * @return void - */ - public function boot() - { - $this->publishes([ - __DIR__ . '/../config/bconnect.php' => config_path('bconnect.php') - ], 'config'); + // Commands + + if ($this->app->runningInConsole()) { + $this->commands([ + Sync::class + ]); + } } } diff --git a/src/Traits/HasConnectTokens.php b/src/Traits/HasConnectTokens.php new file mode 100644 index 0000000..d2a044c --- /dev/null +++ b/src/Traits/HasConnectTokens.php @@ -0,0 +1,8 @@ +