Initial commit

This commit is contained in:
Adrian Palmer 2014-08-27 17:45:39 +10:00
commit a0f24861fe
10 changed files with 232 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/composer.lock
/vendor/

12
.travis.yml Normal file
View File

@ -0,0 +1,12 @@
language: php
php:
- 5.6
- 5.5
- 5.4
- 5.3
- hhvm
install: composer install
script: vendor/bin/phpunit

22
LICENSE.txt Normal file
View File

@ -0,0 +1,22 @@
Copyright (c) 2014 99designs
MIT License
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

28
README.md Normal file
View File

@ -0,0 +1,28 @@
HTTP Signatures Guzzle 3
===
Adds Guzzle 3 support to [99designs/http-signatures][99signatures]
Signing with Guzzle 3
---
This library includes support for automatically signing Guzzle requests using an event subscriber.
```php
use HttpSignatures\Guzzle\CreateRequestSubscriber;
$client = new \Guzzle\Http\Client('http://example.org');
$client->addSubscriber(new CreateRequestSubscriber($context));
// The below will now send a signed request to: http://example.org/path?query=123
$client->get('/path?query=123', array(
'Date' => 'Wed, 30 Jul 2014 16:40:19 -0700',
'Accept' => 'llamas',
))->send();
```
## Contributing
Pull Requests are welcome.
[99signatures]: https://github.com/99designs/http-signatures-php

26
composer.json Normal file
View File

@ -0,0 +1,26 @@
{
"name": "99designs/http-signatures-guzzle",
"description": "Sign and verify HTTP messages with Guzzle",
"homepage": "https://github.com/99designs/http-signatures-php",
"keywords": ["http", "https", "signing", "signed", "signature", "hmac", "guzzle"],
"license": "MIT",
"authors": [
{
"name": "Adrian Palmer",
"email": "adrian.palmer@99designs.com"
}
],
"autoload": {
"psr-4": {
"HttpSignatures\\": "src/"
}
},
"require": {
"php": ">=5.3.0",
"99designs/http-signatures": "~1.1",
"guzzle/guzzle": ">=3.9"
},
"require-dev": {
"phpunit/phpunit": "~4.1"
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace HttpSignatures\Guzzle;
use Guzzle\Common\Event;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CreateRequestSubscriber implements EventSubscriberInterface
{
private $context;
function __construct($context)
{
$this->context = $context;
}
public static function getSubscribedEvents()
{
return array(
'client.create_request' => 'signRequest'
);
}
public function signRequest($e)
{
$this->context->signer()->sign(new Message($e['request']));
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace HttpSignatures\Guzzle;
class Message
{
private $request;
public $headers;
public function __construct($request)
{
$this->request = $request;
$this->headers = new MessageHeaders($request);
}
public function getQueryString()
{
return $this->request->getQuery(true);
}
public function getMethod()
{
return $this->request->getMethod();
}
public function getPathInfo()
{
return $this->request->getPath();
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace HttpSignatures\Guzzle;
class MessageHeaders
{
private $request;
public function __construct($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);
}
}

View File

@ -0,0 +1,53 @@
<?php
namespace HttpSignatures\Test;
use HttpSignatures\Guzzle\CreateRequestSubscriber;
use HttpSignatures\Context;
use HttpSignatures\Guzzle\Message;
class GuzzleSignerTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->context = new Context(array(
'keys' => array('pda' => 'secret'),
'algorithm' => 'hmac-sha256',
'headers' => array('(request-target)', 'date'),
));
$this->client = new \Guzzle\Http\Client();
$this->client->addSubscriber(new CreateRequestSubscriber($this->context));
}
public function testGuzzleRequestHasExpectedHeaders()
{
$message = $this->client->get('/path?query=123', array('date' => 'today', 'accept' => 'llamas'));
$expectedString = implode(
',',
array(
'keyId="pda"',
'algorithm="hmac-sha256"',
'headers="(request-target) date"',
'signature="SFlytCGpsqb/9qYaKCQklGDvwgmrwfIERFnwt+yqPJw="',
)
);
$this->assertEquals(
$expectedString,
(string) $message->getHeader('Signature')
);
$this->assertEquals(
'Signature ' . $expectedString,
(string) $message->getHeader('Authorization')
);
}
public function testVerifyGuzzleRequest()
{
$message = $this->client->get('/path?query=123', array('date' => 'today', 'accept' => 'dogs'));
$this->assertTrue($this->context->verifier()->isValid(new Message($message)));
}
}

3
tests/bootstrap.php Normal file
View File

@ -0,0 +1,3 @@
<?php
error_reporting(E_ALL);
require(__DIR__ . '/../vendor/autoload.php');