From b2499d768b261d48b50e97ea8340163e6e132981 Mon Sep 17 00:00:00 2001 From: Thomas LEFEBVRE Date: Sun, 7 Apr 2024 13:53:07 +0200 Subject: [PATCH] Issue 00 - fix circular ref resulting in segFault --- config/packages/google_apiclient.yaml | 4 +- src/Controller/GoogleAuthorizeController.php | 4 +- .../Google/Drive/GoogleDriveClientService.php | 10 +-- src/Service/Google/GoogleClientService.php | 81 +++++-------------- 4 files changed, 25 insertions(+), 74 deletions(-) diff --git a/config/packages/google_apiclient.yaml b/config/packages/google_apiclient.yaml index 70b4b80..78ff17e 100644 --- a/config/packages/google_apiclient.yaml +++ b/config/packages/google_apiclient.yaml @@ -7,5 +7,5 @@ services: # Authentication with "OAuth 2.0" using Client ID & Secret - [setClientId, ['%env(GOOGLE_CLIENT_ID)%']] - [setClientSecret, ['%env(GOOGLE_CLIENT_SECRET)%']] - # Authentication with "OAuth 2.0" or "Service account" using JSON - - [setAuthConfig, ['%env(resolve:GOOGLE_AUTH_CONFIG)%']] + - [setApplicationName, ['Instabot']] + - [setAccessType, ['offline']] diff --git a/src/Controller/GoogleAuthorizeController.php b/src/Controller/GoogleAuthorizeController.php index f176846..bf55940 100644 --- a/src/Controller/GoogleAuthorizeController.php +++ b/src/Controller/GoogleAuthorizeController.php @@ -12,8 +12,6 @@ use App\Service\Google\OAuth\GoogleOAuthTokenService; use Doctrine\ORM\EntityManagerInterface; use Exception; -use Psr\Container\ContainerExceptionInterface; -use Psr\Container\NotFoundExceptionInterface; use Random\RandomException; use SodiumException; use Symfony\Component\HttpFoundation\RedirectResponse; @@ -32,7 +30,7 @@ public function index(GoogleClientService $clientService): RedirectResponse try { $client = $clientService->getClientForUser($settings); - } catch (NotFoundExceptionInterface | ContainerExceptionInterface | Exception $e) { + } catch (Exception $e) { $this->flashOnRedirect( 'error', 'errors.controller.google.authorization_failed', diff --git a/src/Service/Google/Drive/GoogleDriveClientService.php b/src/Service/Google/Drive/GoogleDriveClientService.php index 19222f1..d9f49a3 100644 --- a/src/Service/Google/Drive/GoogleDriveClientService.php +++ b/src/Service/Google/Drive/GoogleDriveClientService.php @@ -11,10 +11,8 @@ use App\Entity\UserSettings; use App\Model\GoogleDriveResponse; use App\Service\Google\GoogleClientService; +use Exception; use Google\Service\Drive; -use Google\Service\Exception; -use Psr\Container\ContainerExceptionInterface; -use Psr\Container\NotFoundExceptionInterface; class GoogleDriveClientService { @@ -26,8 +24,7 @@ public function __construct( } /** - * @throws ContainerExceptionInterface - * @throws NotFoundExceptionInterface + * @throws Exception */ public function getFilesForUser(UserSettings $userSettings): GoogleDriveResponse { @@ -64,8 +61,7 @@ public function getFilesForUser(UserSettings $userSettings): GoogleDriveResponse } /** - * @throws ContainerExceptionInterface - * @throws NotFoundExceptionInterface + * @throws Exception */ private function getFiles(UserSettings $userSettings, string $folderId): array { diff --git a/src/Service/Google/GoogleClientService.php b/src/Service/Google/GoogleClientService.php index 26b5e83..28a301d 100644 --- a/src/Service/Google/GoogleClientService.php +++ b/src/Service/Google/GoogleClientService.php @@ -9,97 +9,54 @@ namespace App\Service\Google; use App\Entity\UserSettings; -use App\Service\Google\OAuth\GoogleOAuthTokenService; use App\Service\Security\EncryptionService; use Exception; use Google\Client; use Google\Service\Drive; -use Psr\Container\ContainerExceptionInterface; -use Psr\Container\NotFoundExceptionInterface; -use RuntimeException; -use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface; +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; +use Symfony\Component\Routing\Router; +/** + * Initiate the connexion with Google SDK client. + */ class GoogleClientService { - public const GOOGLE_API_KEY = 'google_api_key'; - public const GOOGLE_CLIENT_ID = 'google_client_id'; - public const GOOGLE_CLIENT_SECRET = 'google_client_secret'; - public const GOOGLE_APP_NAME = 'Instabot'; - public function __construct( - private readonly ContainerBagInterface $containerBag, - private readonly GoogleOAuthTokenService $OAuthService, - private readonly EncryptionService $encryptionService + private readonly EncryptionService $encryptionService, + private readonly Router $router, ) { } /** - * @throws NotFoundExceptionInterface - * @throws ContainerExceptionInterface * @throws Exception */ public function getClientForUser(UserSettings $userSettings): Client { $client = new Client(); - - $this->setClientBaseData($client); - - $authCode = $userSettings->getGoogleDriveAuthCode(); $accessToken = $userSettings->getGoogleDriveToken(); - // first time authorization - if (!$accessToken && !$authCode) { - return $client; - } + $this->setClientExtraData($client); - $authResponse = $this->OAuthService->getAccessToken($userSettings, $client); - - // unsuccessful token retrieval or getting a new token - if (false === $authResponse->getSuccess() || !$authResponse->getAccessToken()) { + // first time authorization won't have token. + if (null === $accessToken | '' === $accessToken) { return $client; } - $client->setAccessToken($this->encryptionService->decrypt($authResponse->getAccessToken())); + $client->setAccessToken($this->encryptionService->decrypt($accessToken)); return $client; } - /** - * @throws ContainerExceptionInterface - * @throws NotFoundExceptionInterface - */ - private function setClientBaseData(Client $client): void + // the rest is set in google_apiclient.yaml. + private function setClientExtraData(Client $client): void { - $params = $this->getRequiredParameters(); + $client->setRedirectUri( + $this->router->generate( + 'app_google_authorize_response', [], + UrlGeneratorInterface::ABSOLUTE_URL + ) + ); - // TODO : try without settings env params here but use the yaml conf - $client->setApplicationName(self::GOOGLE_APP_NAME); - $client->setDeveloperKey($params[self::GOOGLE_API_KEY]); - $client->setClientId($params[self::GOOGLE_CLIENT_ID]); - $client->setClientSecret($params[self::GOOGLE_CLIENT_SECRET]); - $client->setAccessType('offline'); - $client->setRedirectUri('https://' . $_SERVER['HTTP_HOST'] . '/google/authorize-response'); $client->addScope(Drive::DRIVE); } - - /** - * @throws NotFoundExceptionInterface - * @throws ContainerExceptionInterface - */ - private function getRequiredParameters(): array - { - $envParams = [ - self::GOOGLE_API_KEY => $this->containerBag->get(self::GOOGLE_API_KEY), - self::GOOGLE_CLIENT_ID => $this->containerBag->get(self::GOOGLE_CLIENT_ID), - self::GOOGLE_CLIENT_SECRET => $this->containerBag->get(self::GOOGLE_CLIENT_SECRET), - ]; - - foreach ($envParams as $param) { - if (false === is_string($param)) { - throw new RuntimeException('Google API parameters must be strings.'); - } - } - - return $envParams; - } }