public function testSignatureGeneration() { $signature = new Signature('test-key'); $actual = $signature->generate('test-string'); $data = $signature->getRequestTime() . $signature->getNonce() . 'test-string'; $expected = hash_hmac('sha1', $data, 'test-key'); $this->assertEquals($expected, $actual); }
public function testPostHash() { $indexId = 'ABCD-12345'; $derivedKey = 'secretKey'; $index = AcquiaSearchClient::factory(array('index_id' => $indexId, 'derived_key' => $derivedKey)); $request = $index->post('/update?wt=json', null, 'Some Body'); $signature = new Signature($derivedKey); $signature->setRequestTime(12345); $auth = new AcquiaSearchAuthPlugin($indexId, $signature); $auth->signRequest($request); $cookie = $request->getHeader('Cookie'); $expected = 'acquia_solr_time=12345; acquia_solr_nonce=12345678; acquia_solr_hmac=e006724d186805fdc0999cd1b132a9f78786b7f4;'; $this->assertEquals($expected, (string) $cookie); }
/** * @param \Guzzle\Http\Message\Request $request */ public function signRequest(Request $request) { $url = $request->getPath(); if ('POST' == $request->getMethod() && $request instanceof EntityEnclosingRequest) { $body = (string) $request->getBody(); $hash = $this->signature->generate($body); } else { $url .= '?' . $request->getQuery(); $hash = $this->signature->generate($url); } $request->addCookie('acquia_solr_time', $this->signature->getRequestTime()); $request->addCookie('acquia_solr_nonce', $this->signature->getNonce()); $request->addCookie('acquia_solr_hmac', $hash . ';'); // The timestamp should be current for each request. $this->signature->unsetRequestTime(); }