Skip to content

Commit

Permalink
Merge pull request #9 from jeremykendall/develop
Browse files Browse the repository at this point in the history
Merge develop into master
  • Loading branch information
jeremykendall committed Aug 19, 2013
2 parents dc8475e + 956358b commit 23ed1cb
Show file tree
Hide file tree
Showing 13 changed files with 235 additions and 74 deletions.
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ API secret generation.
### Request Signing

``` php
$collection = new QueryAuth\NormalizedParameterCollection();
$signer = new QueryAuth\Signer($collection);
$client = new QueryAuth\Client($signer);
$factory = new QueryAuth\Factory();
$client = $factory->newClient();

$key = 'API_KEY';
$secret = 'API_SECRET';
Expand All @@ -60,9 +59,8 @@ those provided to the method (if any), plus `timestamp`, `key`, and `signature`.
### Signature Validation

``` php
$collection = new QueryAuth\NormalizedParameterCollection();
$signer = new QueryAuth\Signer($collection);
$server = new QueryAuth\Server($signer);
$factory = new QueryAuth\Factory();
$server = $factory->newServer();

$secret = 'API_SECRET_FROM_PERSISTENCE_LAYER';
$method = 'GET';
Expand Down Expand Up @@ -90,8 +88,8 @@ request is valid. The default value can be modified using `Server::setDrift()`.
You can generate API keys and secrets in the following manner.

``` php
$randomFactory = new \RandomLib\Factory();
$keyGenerator = new QueryAuth\KeyGenerator($randomFactory);
$factory = new QueryAuth\Factory();
$keyGenerator = $factory->newKeyGenerator();

// 40 character random alphanumeric string
$key = $keyGenerator->generateKey();
Expand Down Expand Up @@ -131,7 +129,7 @@ Package installation is handled by Composer.

## Credits

* The Client, Signer, and NormalizedParameterCollection code are my own implementation of
* The Client, Signer, and ParameterCollection code are my own implementation of
the [Signature Version 2
implementation](https://github.com/aws/aws-sdk-php/blob/master/src/Aws/Common/Signature/SignatureV2.php)
from the [AWS SDK for PHP
Expand Down
37 changes: 21 additions & 16 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 92 additions & 0 deletions src/QueryAuth/Factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
* Query Auth: Signature generation and validation for REST API query authentication
*
* @copyright 2013 Jeremy Kendall
* @license https://github.com/jeremykendall/query-auth/blob/master/LICENSE MIT
* @link https://github.com/jeremykendall/query-auth
*/

namespace QueryAuth;

use QueryAuth\Client;
use QueryAuth\ParameterCollection;
use QueryAuth\Server;
use QueryAuth\Signer;
use RandomLib\Factory as RandomFactory;

/**
* Creates QueryAuth classes
*/
class Factory
{
/**
* @var RandomFactory RandomLib Factory
*/
private $randomFactory;

/**
* Creates a client instance
*
* @return Client Client instance
*/
public function newClient()
{
return new Client($this->newSigner());
}

/**
* Creates a server instance
*
* @return Server Server instance
*/
public function newServer()
{
return new Server($this->newSigner());
}

/**
* Creates new KeyGenerator created with medium strength RandomLib\Generator
*/
public function newKeyGenerator()
{
return new KeyGenerator(
$this->getRandomFactory()->getMediumStrengthGenerator()
);
}

/**
* Creates a signer for either server or client
*
* @return Signer Signer instance
*/
protected function newSigner()
{
return new Signer(new ParameterCollection());
}

/**
* Get an instance of RandomFactory. If property is null, creates a new
* instance.
*
* @return RandomFactory Instance of RandomFactory
*/
public function getRandomFactory()
{
if ($this->randomFactory === null) {
$this->randomFactory = new RandomFactory();
}

return $this->randomFactory;
}

/**
* Set randomFactory
*
* @param RandomFactory $randomFactory Instance of RandomFactory
*/
public function setRandomFactory(RandomFactory $randomFactory)
{
$this->randomFactory = $randomFactory;
}
}
19 changes: 8 additions & 11 deletions src/QueryAuth/KeyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@

namespace QueryAuth;

use RandomLib\Factory as RandomFactory;
use RandomLib\Generator as Generator;

/**
* Creates API keys and secrets
*/
class KeyGenerator
{
/**
* @var RandomFactory Random factory
* @var Generator RandomLib Generator
*/
private $randomFactory;
private $generator;

/**
* Public constructor
*
* @var RandomFactory $randomFactory RandomLib factory
* @var Generator $generator RandomLib factory
*/
public function __construct(RandomFactory $randomFactory)
public function __construct(Generator $generator)
{
$this->randomFactory = $randomFactory;
$this->generator = $generator;
}

/**
Expand All @@ -39,9 +39,8 @@ public function __construct(RandomFactory $randomFactory)
public function generateKey()
{
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$generator = $this->randomFactory->getMediumStrengthGenerator();

return $generator->generateString(40, $chars);
return $this->generator->generateString(40, $chars);
}

/**
Expand All @@ -51,8 +50,6 @@ public function generateKey()
*/
public function generateSecret()
{
$generator = $this->randomFactory->getMediumStrengthGenerator();

return $generator->generateString(60);
return $this->generator->generateString(60);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* Collection class
*/
class NormalizedParameterCollection implements \IteratorAggregate, \ArrayAccess, \Countable
class ParameterCollection implements \IteratorAggregate, \ArrayAccess, \Countable
{
/**
* @var array Holds parameters
Expand Down Expand Up @@ -52,12 +52,12 @@ public function normalize()
}

/**
* Adds value
* Sets a value
*
* @param mixed $key Key
* @param mixed $value Value
*/
public function add($key, $value)
public function set($key, $value)
{
$this->container[$key] = $value;
}
Expand Down
8 changes: 4 additions & 4 deletions src/QueryAuth/Signer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@

namespace QueryAuth;

use QueryAuth\NormalizedParameterCollection;
use QueryAuth\ParameterCollection;

/**
* Creates signature
*/
class Signer
{
/**
* @var NormalizedParameterCollection Request parameter collection
* @var ParameterCollection Request parameter collection
*/
private $collection;

/**
* Public constructor
*
* @param NormalizedParameterCollection $collection Parameter collection
* @param ParameterCollection $collection Parameter collection
*/
public function __construct(NormalizedParameterCollection $collection)
public function __construct(ParameterCollection $collection)
{
$this->collection = $collection;
}
Expand Down
9 changes: 5 additions & 4 deletions tests/QueryAuth/Tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
namespace QueryAuthTests;

use QueryAuth\Client;
use QueryAuth\NormalizedParameterCollection;
use QueryAuth\Factory;
use QueryAuth\ParameterCollection;
use QueryAuth\Signer;

class ClientTest extends \PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -35,8 +36,8 @@ class ClientTest extends \PHPUnit_Framework_TestCase

protected function setUp()
{
$signer = new Signer(new NormalizedParameterCollection());
$this->client = new Client($signer);
$factory = new Factory();
$this->client = $factory->newClient();
$this->key = md5(time());
$this->secret = base64_encode(time() . 'secret');
$this->host = 'www.example.com';
Expand Down Expand Up @@ -83,7 +84,7 @@ public function testGetSignedRequestParamsForPostRequestWithParams()
public function testGetSetSigner()
{
$this->assertInstanceOf('QueryAuth\Signer', $this->client->getSigner());
$signature = new Signer(new NormalizedParameterCollection());
$signature = new Signer(new ParameterCollection());
$this->client->setSigner($signature);
$this->assertSame($signature, $this->client->getSigner());
}
Expand Down
Loading

0 comments on commit 23ed1cb

Please sign in to comment.