/** * @covers PusherSignature::signRequest */ public function testCanSignRequest() { $request = new HttpRequest('POST', '/apps/3/events'); // We set variables in query to have always the same result $request->getQuery()->replace(array('auth_key' => $this->credentials->getKey(), 'auth_timestamp' => '1353088179', 'auth_version' => '1.0', 'body_md5' => 'ec365a775a4cd0599faeb73354201b6f')); $request->setResponseBody('{"name":"foo","channels":["project-3"],"data":"{\\"some\\":\\"data\\"}"}'); $this->pusherSignature->signRequest($request, $this->credentials); $this->assertEquals('auth_key=278d425bdf160c739803&auth_timestamp=1353088179&auth_version=1.0&body_md5=ec365a775a4cd0599faeb73354201b6f&auth_signature=da454824c97ba181a32ccc17a72625ba02771f50b50e1e7430e47a1f3f457e6c', $request->getQuery('auth_signature')); }
/** * Sign the Pusher request * * @link http://pusher.com/docs/rest_api#authentication * @param RequestInterface $request * @param Credentials $credentials */ public function signRequest(RequestInterface $request, Credentials $credentials) { $queryParameters = array('auth_key' => $credentials->getKey(), 'auth_timestamp' => time(), 'auth_version' => self::AUTH_VERSION); if ($request instanceof EntityEnclosingRequestInterface) { $body = $request->getBody(); $queryParameters['body_md5'] = $body->getContentLength() ? $body->getContentMd5() : ''; } // The signature algorithm asks that keys are all lowercased $queryParameters = array_change_key_case($request->getQuery()->toArray()) + $queryParameters; $queryParameters = array_filter($queryParameters); ksort($queryParameters); $method = strtoupper($request->getMethod()); $requestPath = $request->getPath(); $query = urldecode(http_build_query($queryParameters)); $signature = $this->signString(implode("\n", array($method, $requestPath, $query)), $credentials); $queryParameters['auth_signature'] = $signature; $request->getQuery()->replace($queryParameters); }