/** * Tests the "isConnectedToNosto" method for the NostoAccount class. */ public function testAccountIsConnected() { $account = new NostoAccount('platform-test'); $this->specify('account is not connected', function () use($account) { $this->assertFalse($account->isConnectedToNosto()); }); $token = new NostoApiToken('sso', '123'); $account->addApiToken($token); $token = new NostoApiToken('products', '123'); $account->addApiToken($token); $this->specify('account is connected', function () use($account) { $this->assertTrue($account->isConnectedToNosto()); }); }
/** * Returns the url for the account administration iframe. * If the passed account is null, then the url will point to the start page where a new account can be created. * * @param NostoAccountMetaDataIframeInterface $meta the iframe meta data. * @param NostoAccount|null $account the account to return the url for. * @param array $params additional parameters to add to the iframe url. * @return string the iframe url. * @throws NostoException if the url cannot be created. */ public function getUrl(NostoAccountMetaDataIframeInterface $meta, NostoAccount $account = null, array $params = array()) { $queryParams = http_build_query(array_merge(array('lang' => strtolower($meta->getLanguageIsoCode()), 'ps_version' => $meta->getVersionPlatform(), 'nt_version' => $meta->getVersionModule(), 'product_pu' => $meta->getPreviewUrlProduct(), 'category_pu' => $meta->getPreviewUrlCategory(), 'search_pu' => $meta->getPreviewUrlSearch(), 'cart_pu' => $meta->getPreviewUrlCart(), 'front_pu' => $meta->getPreviewUrlFront(), 'shop_lang' => strtolower($meta->getLanguageIsoCodeShop()), 'shop_name' => $meta->getShopName(), 'unique_id' => $meta->getUniqueId(), 'fname' => $meta->getFirstName(), 'lname' => $meta->getLastName(), 'email' => $meta->getEmail()), $params)); if ($account !== null && $account->isConnectedToNosto()) { try { $url = $account->ssoLogin($meta) . '?' . $queryParams; } catch (NostoException $e) { // If the SSO fails, we show a "remove account" page to the user in order to // allow to remove Nosto and start over. // The only case when this should happen is when the api token for some // reason is invalid, which is the case when switching between environments. $url = NostoHttpRequest::buildUri($this->getBaseUrl() . self::IFRAME_URI_UNINSTALL . '?' . $queryParams, array('{platform}' => $meta->getPlatform())); } } else { $url = NostoHttpRequest::buildUri($this->getBaseUrl() . self::IFRAME_URI_INSTALL . '?' . $queryParams, array('{platform}' => $meta->getPlatform())); } return $url; }
/** * Syncs an existing Nosto account via OAuth. * * Requires that the OAuth cycle has already completed the first step in getting the authorization code. * * @param NostoOauthClientMetaInterface $meta the OAuth client meta data to use for connection to Nosto. * @param string $authCode the authorization code that grants access to transfer data from Nosto. * @return NostoAccount the synced account. * * @throws NostoException on failure. */ public function sync(NostoOauthClientMetaInterface $meta, $authCode) { $oauthClient = new NostoOAuthClient($meta); $token = $oauthClient->authenticate($authCode); $request = new NostoHttpRequest(); // The request is currently not made according the the OAuth2 spec with the access token in the // Authorization header. This is due to the authentication server not implementing the full OAuth2 spec yet. $request->setUrl(NostoOAuthClient::$baseUrl . '/exchange'); $request->setQueryParams(array('access_token' => $token->getAccessToken())); $response = $request->get(); if ($response->getCode() !== 200) { throw Nosto::createHttpException('Failed to send account sync to Nosto.', $request, $response); } $result = $response->getJsonResult(true); $account = new NostoAccount($token->getMerchantName()); $tokens = NostoApiToken::parseTokens($result, 'api_'); foreach ($tokens as $token) { $account->addApiToken($token); } if (!$account->isConnectedToNosto()) { throw new NostoException('Failed to sync all account details from Nosto. Unknown error.'); } return $account; }