/** * Tests the "getApiToken" method for the NostoAccount class. */ public function testAccountApiToken() { $account = new NostoAccount('platform-test'); $this->specify('account does not have sso token', function () use($account) { $this->assertNull($account->getApiToken('sso')); }); $token = new NostoApiToken('sso', '123'); $account->addApiToken($token); $this->specify('account has sso token', function () use($account) { $this->assertEquals('123', $account->getApiToken('sso')->getValue()); }); }
/** * Deletes a Nosto account from the PS config. * Also sends a notification to Nosto that the account has been deleted. * * @param NostoAccount $account the account to delete. * @param int $id_lang the ID of the language model to delete the account for. * @param null|int $id_shop_group the ID of the shop context. * @param null|int $id_shop the ID of the shop. * @return bool true if successful, false otherwise. */ public function delete(NostoAccount $account, $id_lang, $id_shop_group = null, $id_shop = null) { /** @var NostoTaggingHelperConfig $helper_config */ $helper_config = Nosto::helper('nosto_tagging/config'); $success = $helper_config->deleteAllFromContext($id_lang, $id_shop_group, $id_shop); if ($success) { $token = $account->getApiToken('sso'); if ($token) { try { $account->delete(); } catch (NostoException $e) { Nosto::helper('nosto_tagging/logger')->error(__CLASS__ . '::' . __FUNCTION__ . ' - ' . $e->getMessage(), $e->getCode()); } } } return $success; }
/** * Encrypts and returns the data. * * @param NostoAccount $account the account to export the data for. * @param NostoExportCollectionInterface $collection the data collection to export. * @return string the encrypted data. */ public static function export(NostoAccount $account, NostoExportCollectionInterface $collection) { $data = ''; // Use the first 16 chars of the SSO token as secret for encryption. $token = $account->getApiToken(NostoApiToken::API_SSO); if (!empty($token)) { $tokenValue = $token->getValue(); $secret = substr($tokenValue, 0, 16); if (!empty($secret)) { $iv = phpseclib_Crypt_Random::string(16); $cipher = new NostoCipher(); $cipher->setSecret($secret); $cipher->setIV($iv); $cipherText = $cipher->encrypt($collection->getJson()); // Prepend the IV to the cipher string so that nosto can parse and use it. // There is no security concern with sending the IV as plain text. $data = $iv . $cipherText; } } return $data; }
/** * Signs the user in to Nosto via SSO. * * Requires that the account has a valid sso token associated with it. * * @param NostoAccount $account the account to sign into. * @param NostoAccountMetaSingleSignOnInterface $meta the SSO meta-data. * @return string a secure login url. * * @throws NostoException on failure. */ public function sso(NostoAccount $account, NostoAccountMetaSingleSignOnInterface $meta) { $token = $account->getApiToken(NostoApiToken::API_SSO); if (is_null($token)) { throw new NostoException(sprintf('No `%s` API token found for account "%s".', NostoApiToken::API_SSO, $account->getName())); } $request = new NostoHttpRequest(); $request->setUrl(NostoHttpRequest::$baseUrl . NostoHttpRequest::PATH_SSO_AUTH); $request->setReplaceParams(array('{platform}' => $meta->getPlatform(), '{email}' => $meta->getEmail())); $request->setContentType('application/x-www-form-urlencoded'); $request->setAuthBasic('', $token->getValue()); $response = $request->post(http_build_query(array('fname' => $meta->getFirstName(), 'lname' => $meta->getLastName()))); if ($response->getCode() !== 200) { throw Nosto::createHttpException('Failed to sign into Nosto using Single Sign On.', $request, $response); } $result = $response->getJsonResult(); if (empty($result->login_url)) { throw new NostoException('No "login_url" returned when logging in employee to Nosto'); } return $result->login_url; }