/**
  * @return Configuration
  */
 public function getConfiguration()
 {
     if ($this->configuration == null) {
         $this->configuration = $this->container->get('ex_google_drive.configuration');
         $this->configuration->setStorageApi($this->storageApi);
     }
     return $this->configuration;
 }
Exemplo n.º 2
0
 protected function setUp()
 {
     self::$client = static::createClient();
     $container = self::$client->getContainer();
     $sapiToken = $container->getParameter('storage_api.test.token');
     $sapiUrl = $container->getParameter('storage_api.test.url');
     self::$client->setServerParameters(array('HTTP_X-StorageApi-Token' => $sapiToken));
     $this->storageApi = new SapiClient(['token' => $sapiToken, 'url' => $sapiUrl, 'userAgent' => $this->componentName]);
     $this->encryptor = $container->get('syrup.encryptor');
     $this->configuration = $container->get('ex_google_drive.configuration');
     $this->configuration->setStorageApi($this->storageApi);
     try {
         $this->configuration->create();
     } catch (\Exception $e) {
         // bucket exists
     }
     // Cleanup
     $sysBucketId = $this->configuration->getSysBucketId();
     $tableId = $sysBucketId . '.' . $this->accountId;
     if ($this->storageApi->tableExists($tableId)) {
         $this->storageApi->dropTable($tableId);
     }
 }
Exemplo n.º 3
0
 protected function initConfiguration()
 {
     $this->configuration->setStorageApi($this->storageApi);
     return $this->configuration;
 }
 public function oauthCallbackAction()
 {
     $session = $this->get('session');
     $token = $session->get('token');
     $accountId = $session->get('account');
     $referrer = $session->get('referrer');
     if ($token == null) {
         throw new UserException("Your session expired, please try again");
     }
     $code = $this->get('request')->query->get('code');
     if (empty($code)) {
         throw new SyrupComponentException(400, 'Could not read from Google API');
     }
     $googleApi = $this->getGoogleApi();
     try {
         $storageApi = new StorageApi(['token' => $token, 'url' => null, 'userAgent' => $this->componentName]);
         $tokenData = $storageApi->verifyToken();
         /** @var EncryptorInterface $encryptor */
         $encryptor = $this->get('syrup.encryptor');
         $configuration = new Configuration($this->componentName, $encryptor);
         $configuration->setStorageApi($storageApi);
         $tokens = $googleApi->authorize($code, $this->container->get('router')->generate('keboola_google_drive_oauth_callback', array(), UrlGeneratorInterface::ABSOLUTE_URL));
         $googleApi->setCredentials($tokens['access_token'], $tokens['refresh_token']);
         $userData = json_decode($googleApi->request('https://www.googleapis.com/oauth2/v2/userinfo')->getBody(), true);
         $account = $configuration->getAccountBy('accountId', $accountId);
         if (null == $account) {
             throw new ConfigurationException("Account doesn't exist");
         }
         $account->setGoogleId($userData['id'])->setGoogleName($userData['name'])->setEmail($userData['email'])->setAccessToken($tokens['access_token'])->setRefreshToken($tokens['refresh_token'])->setOwner($tokenData['description']);
         if ($account->isExternal()) {
             if ($userData['email'] == $tokenData['description'] || !isset($tokenData['creatorToken'])) {
                 // user generated an external link for himself or is reauthorizing himself into his config which was external before
                 $account->setExternal(false);
             } else {
                 $account->setOwner($tokenData['creatorToken']['description']);
             }
         }
         $account->save();
         $this->container->get('session')->clear();
         if ($referrer) {
             return new RedirectResponse($referrer);
         } else {
             return new JsonResponse(array('status' => 'ok'));
         }
     } catch (\Exception $e) {
         throw new SyrupComponentException(500, 'Could not save API tokens', $e);
     }
 }