/** * Tests that existing accounts can be synced from Nosto. * Accounts are synced using OAuth2 Authorization Code method. * We are only testing that we can start and act on the steps in the OAuth request cycle. */ public function testSyncingExistingAccount() { $meta = new NostoOAuthClientMetaData(); $client = new NostoOAuthClient($meta); $this->specify('oauth authorize url can be created', function () use($client) { $this->assertEquals('http://localhost:3000?client_id=client-id&redirect_uri=http%3A%2F%2Fmy.shop.com%2Fnosto%2Foauth&response_type=code&scope=sso products&lang=en', $client->getAuthorizationUrl()); }); $account = NostoAccount::syncFromNosto($meta, 'test123'); $this->specify('account was created', function () use($account, $meta) { $this->assertInstanceOf('NostoAccount', $account); $this->assertEquals('platform-00000000', $account->getName()); }); $this->specify('account has api token sso', function () use($account, $meta) { $token = $account->getApiToken('sso'); $this->assertInstanceOf('NostoApiToken', $token); $this->assertEquals('sso', $token->getName()); $this->assertNotEmpty($token->getValue()); }); $this->specify('account has api token products', function () use($account, $meta) { $token = $account->getApiToken('products'); $this->assertInstanceOf('NostoApiToken', $token); $this->assertEquals('products', $token->getName()); $this->assertNotEmpty($token->getValue()); }); $this->specify('account is connected to nosto', function () use($account, $meta) { $this->assertTrue($account->isConnectedToNosto()); }); }
/** * Handles the redirect from Nosto oauth2 authorization server when an * existing account is connected to a store. * This is handled in the front end as the oauth2 server validates the * "return_url" sent in the first step of the authorization cycle, and * requires it to be from the same domain that the account is configured * for and only redirects to that domain. */ public function indexAction() { // If the "Add Store Code to Urls" setting is set to "No" under // System -> Configuration -> Web -> Url Options, then Magento won't // set the store context based on the "___store" GET parameter if the // store does NOT belong to the default website. When this setting is // "Yes", then the store code will be a part of the url path and then // the correct context is set by Magento, regardless of the website the // store belongs to. // If the "___store" parameter is present in the url in the current // store context is not that store, then switch the store context. if (($storeCode = $this->getRequest()->getParam('___store')) !== null) { $store = Mage::app()->getStore($storeCode); if ($store && $store->getId() !== Mage::app()->getStore()->getId()) { Mage::app()->setCurrentStore($store->getCode()); } } $request = $this->getRequest(); $store = Mage::app()->getStore(); if (($code = $request->getParam('code')) !== null) { try { /** @var Nosto_Tagging_Helper_Oauth $oauthHelper */ $oauthHelper = Mage::helper('nosto_tagging/oauth'); $account = NostoAccount::syncFromNosto($oauthHelper->getMetaData($store), $code); /** @var Nosto_Tagging_Helper_Account $accountHelper */ $accountHelper = Mage::helper('nosto_tagging/account'); if ($accountHelper->save($account, $store)) { $params = array('message_type' => NostoMessage::TYPE_SUCCESS, 'message_code' => NostoMessage::CODE_ACCOUNT_CONNECT, 'store' => (int) $store->getId(), '_store' => Mage_Core_Model_App::ADMIN_STORE_ID); } else { throw new NostoException('Failed to connect account'); } } catch (NostoException $e) { Mage::log("\n" . $e->__toString(), Zend_Log::ERR, Nosto_Tagging_Model_Base::LOG_FILE_NAME); $params = array('message_type' => NostoMessage::TYPE_ERROR, 'message_code' => NostoMessage::CODE_ACCOUNT_CONNECT, 'store' => (int) $store->getId(), '_store' => Mage_Core_Model_App::ADMIN_STORE_ID); } $this->_redirect('adminhtml/nosto/redirectProxy', $params); } elseif (($error = $request->getParam('error')) !== null) { $logMsg = $error; if (($reason = $request->getParam('error_reason')) !== null) { $logMsg .= ' - ' . $reason; } if (($desc = $request->getParam('error_description')) !== null) { $logMsg .= ' - ' . $desc; } Mage::log("\n" . $logMsg, Zend_Log::ERR, Nosto_Tagging_Model_Base::LOG_FILE_NAME); $this->_redirect('adminhtml/nosto/redirectProxy', array('message_type' => NostoMessage::TYPE_ERROR, 'message_code' => NostoMessage::CODE_ACCOUNT_CONNECT, 'message_text' => $desc, 'store' => (int) $store->getId(), '_store' => Mage_Core_Model_App::ADMIN_STORE_ID)); } else { $this->norouteAction(); } }
/** * @inheritdoc */ public function initContent() { $id_lang = (int) Tools::getValue('language_id', $this->module->getContext()->language->id); if (($code = Tools::getValue('code')) !== false) { // The user accepted the authorization request. // The authorization server responded with a code that can be used to exchange for the access token. try { $meta = new NostoTaggingMetaOauth(); $meta->setModuleName($this->module->name); $meta->setModulePath($this->module->getPath()); $meta->loadData($this->module->getContext(), $id_lang); $account = NostoAccount::syncFromNosto($meta, $code); if (!Nosto::helper('nosto_tagging/account')->save($account, $id_lang)) { throw new NostoException('Failed to save account.'); } $msg = $this->module->l('Account %s successfully connected to Nosto.', 'oauth2'); $this->redirectToModuleAdmin(array('language_id' => $id_lang, 'oauth_success' => sprintf($msg, $account->getName()))); } catch (NostoException $e) { Nosto::helper('nosto_tagging/logger')->error(__CLASS__ . '::' . __FUNCTION__ . ' - ' . $e->getMessage(), $e->getCode()); $msg = $this->module->l('Account could not be connected to Nosto. Please contact Nosto support.', 'oauth2'); $this->redirectToModuleAdmin(array('language_id' => $id_lang, 'oauth_error' => $msg)); } } elseif (($error = Tools::getValue('error')) !== false) { $message_parts = array($error); if (($error_reason = Tools::getValue('error_reason')) !== false) { $message_parts[] = $error_reason; } if (($error_description = Tools::getValue('error_description')) !== false) { $message_parts[] = urldecode($error_description); } Nosto::helper('nosto_tagging/logger')->error(__CLASS__ . '::' . __FUNCTION__ . ' - ' . implode(' - ', $message_parts), 200); // Prefer to show the error description sent from Nosto to the user when something is wrong. // These messages are localized to users current back office language. if (!empty($error_description)) { $msg = urldecode($error_description); } elseif (!empty($error_reason) && $error_reason === 'user_denied') { $msg = $this->module->l('Account could not be connected to Nosto. You rejected the connection request.', 'oauth2'); } else { $msg = $this->module->l('Account could not be connected to Nosto. Please contact Nosto support.', 'oauth2'); } $this->redirectToModuleAdmin(array('language_id' => $id_lang, 'oauth_error' => $msg)); } $this->notFound(); }