public function invalidateApplicationResources(array $uris, SecurityToken $token)
 {
     foreach ($uris as $uri) {
         $request = new RemoteContentRequest($uri);
         $this->cache->invalidate($request->toHash());
         // GET
         $request = new RemoteContentRequest($uri);
         $request->createRemoteContentRequestWithUri($uri);
         $this->cache->invalidate($request->toHash());
         // GET & SIGNED
         $request = new RemoteContentRequest($uri);
         $request->setAuthType(RemoteContentRequest::$AUTH_SIGNED);
         $request->setNotSignedUri($uri);
         $this->cache->invalidate($request->toHash());
     }
     if (Doctrine::getTable('SnsConfig')->get('is_use_outer_shindig', false) && Doctrine::getTable('SnsConfig')->get('is_relay_invalidation_notice', true)) {
         require_once 'OAuth.php';
         $shindigUrl = Doctrine::getTable('SnsConfig')->get('shindig_url');
         if (substr($shindigUrl, -1) !== '/') {
             $shindigUrl .= '/';
         }
         $invalidateUrl = $shindigUrl . 'gadgets/api/rest/cache';
         $key = Doctrine::getTable('SnsConfig')->get('shindig_backend_key');
         $secret = Doctrine::getTable('SnsConfig')->get('shindig_backend_secret');
         $consumer = new OAuthConsumer($key, $secret);
         $oauthRequest = OAuthRequest::from_consumer_and_token($consumer, null, 'POST', $invalidateUrl);
         $oauthRequest->set_parameter('xoauth_requestor_id', 1);
         $oauthRequest->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, null);
         $request = new RemoteContentRequest($invalidateUrl . '?xoauth_requestor_id=1');
         $request->setMethod('POST');
         $request->setContentType('application/json');
         $request->setPostBody(json_encode(array('invalidationKeys' => $uris)));
         $request->setHeaders($oauthRequest->to_header());
         $request->getOptions()->ignoreCache = true;
         $remoteContent = Shindig_Config::get('remote_content');
         $fetcher = new $remoteContent();
         $fetcher->fetch($request);
     }
 }
 /**
  * Get honest-to-goodness user data.
  */
 private function fetchData()
 {
     try {
         // TODO: it'd be better using $this->realRequest->getContentType(), but not set before hand. Temporary hack.
         $postBody = $this->realRequest->getPostBody();
         $url = $this->realRequest->getUrl();
         $msgParams = array();
         if (ShindigOAuthUtil::isFormEncoded($this->realRequest->getHeader("Content-Type")) && strlen($postBody) > 0) {
             $entries = explode('&', $postBody);
             foreach ($entries as $entry) {
                 $parts = explode('=', $entry);
                 if (count($parts) == 2) {
                     $msgParams[ShindigOAuthUtil::urldecode_rfc3986($parts[0])] = ShindigOAuthUtil::urldecode_rfc3986($parts[1]);
                 }
             }
         }
         $method = $this->realRequest->getMethod();
         $msgParams[self::$XOAUTH_APP_URL] = $this->authToken->getAppUrl();
         // Build and sign the message.
         $oauthRequest = $this->newRequestMessageMethod($method, $url, $msgParams);
         $oauthParams = $this->filterOAuthParams($oauthRequest);
         $newHeaders = array();
         switch ($method) {
             case 'POST':
                 if (empty($postBody) || count($postBody) == 0) {
                     $postBody = ShindigOAuthUtil::getPostBodyString($oauthParams);
                 } else {
                     $postBody = $postBody . "&" . ShindigOAuthUtil::getPostBodyString($oauthParams);
                 }
                 // To avoid 417 Response from server, adding empty "Expect" header
                 $newHeaders['Expect'] = '';
                 break;
             case 'GET':
                 $url = ShindigOAuthUtil::addParameters($url, $oauthParams);
                 break;
         }
         // To choose HTTP method client requested, we don't use $this->createRemoteContentRequest() here.
         $rcr = new RemoteContentRequest($url);
         $rcr->createRemoteContentRequest($method, $url, $newHeaders, null, $this->realRequest->getOptions());
         $rcr->setPostBody($postBody);
         $remoteFetcherClass = Config::get('remote_content_fetcher');
         $fetcher = new $remoteFetcherClass();
         $content = $fetcher->fetchRequest($rcr);
         $statusCode = $content->getHttpCode();
         //TODO is there a better way to detect an SP error? For example: http://wiki.oauth.net/ProblemReporting
         if ($statusCode == 401) {
             $tokenKey = $this->buildTokenKey();
             $this->tokenStore->removeTokenAndSecret($tokenKey);
         } else {
             if ($statusCode >= 400 && $statusCode < 500) {
                 $message = $this->parseAuthHeader(null, $content);
                 if ($message->get_parameter(ShindigOAuth::$OAUTH_PROBLEM) != null) {
                     throw new ShindigOAuthProtocolException($message);
                 }
             }
         }
         // Track metadata on the response
         $this->addResponseMetadata($content);
         return $content;
     } catch (Exception $e) {
         throw new GadgetException("INTERNAL SERVER ERROR: " . $e);
     }
 }
 private function signRequest(RemoteContentRequest $request)
 {
     $url = $request->getUrl();
     $method = $request->getMethod();
     try {
         // Parse the request into parameters for OAuth signing, stripping out
         // any OAuth or OpenSocial parameters injected by the client
         $parsedUri = parse_url($url);
         $resource = $url;
         $contentType = $request->getHeader('Content-Type');
         $signBody = stripos($contentType, 'application/x-www-form-urlencoded') !== false || $contentType == null;
         $msgParams = array();
         $postParams = array();
         if ($request->getPostBody()) {
             if ($signBody) {
                 // on normal application/x-www-form-urlencoded type post's encode and parse the post vars
                 parse_str($request->getPostBody(), $postParams);
                 $postParams = $this->sanitize($postParams);
             } else {
                 // on any other content-type of post (application/{json,xml,xml+atom}) use the body signing hash
                 // see http://oauth.googlecode.com/svn/spec/ext/body_hash/1.0/drafts/4/spec.html for details
                 $msgParams['oauth_body_hash'] = base64_encode(sha1($request->getPostBody(), true));
             }
         }
         if ($signBody && isset($postParams)) {
             $msgParams = array_merge($msgParams, $postParams);
         }
         $this->addOpenSocialParams($msgParams, $request->getToken(), $request->getOptions()->ownerSigned, $request->getOptions()->viewerSigned);
         $this->addOAuthParams($msgParams, $request->getToken());
         $consumer = new OAuthConsumer(NULL, NULL, NULL);
         $signatureMethod = new ShindigRsaSha1SignatureMethod($this->privateKeyObject, null);
         $req_req = OAuthRequest::from_consumer_and_token($consumer, NULL, $method, $resource, $msgParams);
         $req_req->sign_request($signatureMethod, $consumer, NULL);
         // Rebuild the query string, including all of the parameters we added.
         // We have to be careful not to copy POST parameters into the query.
         // If post and query parameters share a name, they end up being removed
         // from the query.
         $forPost = array();
         $postData = false;
         if ($method == 'POST' && $signBody) {
             foreach ($postParams as $key => $param) {
                 $forPost[$key] = $param;
                 if ($postData === false) {
                     $postData = array();
                 }
                 $postData[] = OAuthUtil::urlencode_rfc3986($key) . "=" . OAuthUtil::urlencode_rfc3986($param);
             }
             if ($postData !== false) {
                 $postData = implode("&", $postData);
             }
         }
         $newQueryParts = array();
         foreach ($req_req->get_parameters() as $key => $param) {
             if (!isset($forPost[$key])) {
                 if (!is_array($param)) {
                     $newQueryParts[] = urlencode($key) . '=' . urlencode($param);
                 } else {
                     foreach ($param as $elem) {
                         $newQueryParts[] = urlencode($key) . '=' . urlencode($elem);
                     }
                 }
             }
             $newQuery = implode('&', $newQueryParts);
         }
         // Careful here; the OAuth form encoding scheme is slightly different than
         // the normal form encoding scheme, so we have to use the OAuth library
         // formEncode method.
         $url = $parsedUri['scheme'] . '://' . $parsedUri['host'] . (isset($parsedUri['port']) ? ':' . $parsedUri['port'] : '') . (isset($parsedUri['path']) ? $parsedUri['path'] : '') . '?' . $newQuery;
         $request->setUri($url);
         if ($signBody) {
             $request->setPostBody($postData);
         }
     } catch (Exception $e) {
         throw new GadgetException($e);
     }
 }
 /**
  * Tests SigningFetcher->fetchRequest
  */
 public function testFetchRequestWithEmptyPath()
 {
     $request = new RemoteContentRequest('http://example.org');
     $request->setAuthType(RemoteContentRequest::$AUTH_SIGNED);
     $request->setToken(BasicSecurityToken::createFromValues('owner', 'viewer', 'app', 'domain', 'appUrl', '1', 'default'));
     $request->setPostBody('key=value&anotherkey=value');
     $this->signingFetcher->fetchRequest($request);
     $this->verifySignedRequest($request);
 }