Guzzle4 support
This commit is contained in:
47
src/HttpSignatures/GuzzleHttp/Message.php
Normal file
47
src/HttpSignatures/GuzzleHttp/Message.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace HttpSignatures\GuzzleHttp;
|
||||
|
||||
use GuzzleHttp\Message\RequestInterface;
|
||||
|
||||
/**
|
||||
* Class RequestMessage
|
||||
*
|
||||
* wrapper around the Guzzle Request instance to have a consistent API for the HttpSignatures classes to consume
|
||||
*
|
||||
* @package HttpSignatures\Guzzle
|
||||
*/
|
||||
class Message
|
||||
{
|
||||
/**
|
||||
* @var RequestInterface
|
||||
*/
|
||||
private $request;
|
||||
|
||||
/**
|
||||
* @var MessageHeaders
|
||||
*/
|
||||
public $headers;
|
||||
|
||||
public function __construct(RequestInterface $request)
|
||||
{
|
||||
$this->request = $request;
|
||||
$this->headers = new MessageHeaders($request);
|
||||
}
|
||||
|
||||
public function getQueryString()
|
||||
{
|
||||
$qs = $this->request->getQuery();
|
||||
return $qs->count() ? $qs : null;
|
||||
}
|
||||
|
||||
public function getMethod()
|
||||
{
|
||||
return $this->request->getMethod();
|
||||
}
|
||||
|
||||
public function getPathInfo()
|
||||
{
|
||||
return $this->request->getPath();
|
||||
}
|
||||
}
|
||||
40
src/HttpSignatures/GuzzleHttp/MessageHeaders.php
Normal file
40
src/HttpSignatures/GuzzleHttp/MessageHeaders.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace HttpSignatures\GuzzleHttp;
|
||||
|
||||
use GuzzleHttp\Message\MessageInterface;
|
||||
|
||||
/**
|
||||
* Class MessageHeaders
|
||||
*
|
||||
* wrapper around the Guzzle Request instance to have a consistent API for the HttpSignatures classes to consume
|
||||
*
|
||||
* @package HttpSignatures\Guzzle
|
||||
*/
|
||||
class MessageHeaders
|
||||
{
|
||||
/**
|
||||
* @var MessageInterface
|
||||
*/
|
||||
private $request;
|
||||
|
||||
public function __construct(MessageInterface $request)
|
||||
{
|
||||
$this->request = $request;
|
||||
}
|
||||
|
||||
public function has($header)
|
||||
{
|
||||
return $this->request->hasHeader($header);
|
||||
}
|
||||
|
||||
public function get($header)
|
||||
{
|
||||
return $this->request->getHeader($header);
|
||||
}
|
||||
|
||||
public function set($header, $value)
|
||||
{
|
||||
$this->request->setHeader($header, $value);
|
||||
}
|
||||
}
|
||||
37
src/HttpSignatures/GuzzleHttp/RequestSubscriber.php
Normal file
37
src/HttpSignatures/GuzzleHttp/RequestSubscriber.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace HttpSignatures\GuzzleHttp;
|
||||
|
||||
use GuzzleHttp\Event\BeforeEvent;
|
||||
use GuzzleHttp\Event\RequestEvents;
|
||||
use GuzzleHttp\Event\SubscriberInterface;
|
||||
use HttpSignatures\Context;
|
||||
|
||||
class RequestSubscriber implements SubscriberInterface
|
||||
{
|
||||
/**
|
||||
* @var \HttpSignatures\Context
|
||||
*/
|
||||
private $context;
|
||||
|
||||
public function __construct(Context $context)
|
||||
{
|
||||
$this->context = $context;
|
||||
}
|
||||
|
||||
public function getEvents()
|
||||
{
|
||||
return ['before' => ['onBefore', RequestEvents::SIGN_REQUEST]];
|
||||
}
|
||||
|
||||
public function onBefore(BeforeEvent $event)
|
||||
{
|
||||
$request = $event->getRequest();
|
||||
|
||||
if ($request->getConfig()['auth'] != 'http-signatures') {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->context->signer()->sign(new Message($request));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user