/** * @inheritdoc */ public static function syncFromNosto(NostoOAuthClientMetaDataInterface $meta, $code) { $oauthClient = new NostoOAuthClient($meta); $token = $oauthClient->authenticate($code); if (empty($token->accessToken)) { throw new NostoException('No access token found when trying to sync account from Nosto'); } if (empty($token->merchantName)) { throw new NostoException('No merchant name found when trying to sync account from Nosto'); } $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->accessToken)); $response = $request->get(); $result = $response->getJsonResult(true); if ($response->getCode() !== 200) { Nosto::throwHttpException('Failed to sync account from Nosto.', $request, $response); } if (empty($result)) { throw new NostoException('Received invalid data from Nosto when trying to sync account'); } $account = new self($token->merchantName); $account->tokens = NostoApiToken::parseTokens($result, 'api_'); if (!$account->isConnectedToNosto()) { throw new NostoException('Failed to sync all account details from Nosto'); } return $account; }
/** * 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; }
/** * Authenticates the application with the given code to receive an access token. * * @param string $code code sent by the authorization server to exchange for an access token. * @return NostoOAuthToken * @throws NostoException */ public function authenticate($code) { if (empty($code)) { throw new NostoException('Invalid authentication token'); } $request = new NostoHttpRequest(); $request->setUrl(self::$baseUrl . self::PATH_TOKEN); $request->setReplaceParams(array('{cid}' => $this->clientId, '{sec}' => $this->clientSecret, '{uri}' => $this->redirectUrl, '{cod}' => $code)); $response = $request->get(); $result = $response->getJsonResult(true); if ($response->getCode() !== 200) { Nosto::throwHttpException('Failed to authenticate with code.', $request, $response); } if (empty($result['access_token'])) { throw new NostoException('No "access_token" returned after authenticating with code'); } if (empty($result['merchant_name'])) { throw new NostoException('No "merchant_name" returned after authenticating with code'); } return NostoOAuthToken::create($result); }
/** * Tests the http request socket adapter. */ public function testHttpRequestSocketAdapter() { $request = new NostoHttpRequest(new NostoHttpRequestAdapterSocket(NostoHttpRequest::$userAgent)); $request->setUrl('http://localhost:3000'); $response = $request->get(); $this->assertEquals(404, $response->getCode()); $response = $request->post('test'); $this->assertEquals(404, $response->getCode()); }