Skip to content

Commit

Permalink
Issue 00 - fix circular ref resulting in segFault
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasLdev committed Apr 7, 2024
1 parent 667981a commit b2499d7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 74 deletions.
4 changes: 2 additions & 2 deletions config/packages/google_apiclient.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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']]
4 changes: 1 addition & 3 deletions src/Controller/GoogleAuthorizeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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',
Expand Down
10 changes: 3 additions & 7 deletions src/Service/Google/Drive/GoogleDriveClientService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -26,8 +24,7 @@ public function __construct(
}

/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception
*/
public function getFilesForUser(UserSettings $userSettings): GoogleDriveResponse
{
Expand Down Expand Up @@ -64,8 +61,7 @@ public function getFilesForUser(UserSettings $userSettings): GoogleDriveResponse
}

/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception
*/
private function getFiles(UserSettings $userSettings, string $folderId): array
{
Expand Down
81 changes: 19 additions & 62 deletions src/Service/Google/GoogleClientService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit b2499d7

Please sign in to comment.