diff --git a/src/Jeskew/Guzzle/Plugin/Hawk.php b/src/Jeskew/Guzzle/Plugin/Hawk.php index ce046fe..a750cfe 100644 --- a/src/Jeskew/Guzzle/Plugin/Hawk.php +++ b/src/Jeskew/Guzzle/Plugin/Hawk.php @@ -8,18 +8,28 @@ use GuzzleHttp\Event\BeforeEvent; use GuzzleHttp\Event\EmitterInterface; use GuzzleHttp\Event\SubscriberInterface; +use GuzzleHttp\Message\RequestInterface; class Hawk implements SubscriberInterface { private $key; private $secret; private $offset; + private $parsePayload; + private $extHeaders; - public function __construct($key, $secret, $offset = 0) - { + public function __construct( + $key, + $secret, + $offset = 0, + $parsePayload = false, + $extHeaders = [] + ) { $this->key = $key; $this->secret = $secret; $this->offset = $offset; + $this->parsePayload = $parsePayload; + $this->extHeaders = $extHeaders; } public function getEvents() @@ -38,7 +48,10 @@ public function signRequest(BeforeEvent $event) $this->secret, $request->getUrl(), $request->getMethod(), - $this->offset + $this->offset, + $this->gatherExtHeaders($request), + $this->parsePayload ? (string) $request->getBody() : '', + $this->parsePayload ? $request->getHeader('content-type') : '' ); $request->setHeader( @@ -73,6 +86,18 @@ public function generateHawkRequest( return $request; } + private function gatherExtHeaders(RequestInterface $request) + { + $headers = array_intersect_key( + $request->getHeaders(), + array_flip($this->extHeaders) + ); + + ksort($headers); + + return $headers; + } + private function buildClient($offset) { $builder = ClientBuilder::create();