HTTP HMAC Signer for PHP

This library implements version 2.0 of the HTTP HMAC Spec to sign and verify RESTful Web API requests. It integrates with popular frameworks and libraries, like Symfony and Guzzle, and can be used on both the server and client.
Installation
Use Composer and add it as a dependency to your project's composer.json file:
{
"require": {
"acquia/http-hmac-php": "^5.0"
}
}
Please refer to Composer's documentation for more detailed installation and usage instructions.
Usage
Sign an API request sent via Guzzle
require_once 'vendor/autoload.php';
use Acquia\Hmac\Guzzle\HmacAuthMiddleware;
use Acquia\Hmac\Key;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
$key_id = 'e7fe97fa-a0c8-4a42-ab8e-2c26d52df059';
$key_secret = base64_encode('secret');
$key = new Key($key_id, $key_secret);
$headers = [
'X-Custom-1' => 'value1',
'X-Custom-2' => 'value2',
];
$realm = 'Acquia';
$middleware = new HmacAuthMiddleware($key, $realm, array_keys($headers));
$stack = HandlerStack::create();
$stack->push($middleware);
$client = new Client([
'handler' => $stack,
]);
try {
$result = $client->get('https://service.acquia.io/api/v1/widget', [
'headers' => $headers,
]);
} catch (ClientException $e) {
print $e->getMessage();
$response = $e->getResponse();
}
print $response->getBody();
Authenticate the request using PSR-7-compatible requests
use Acquia\Hmac\RequestAuthenticator;
use Acquia\Hmac\ResponseSigner;
$authenticator = new RequestAuthenticator($keyLoader);
$key = $authenticator->authenticate($request);
$signer = new ResponseSigner($key, $request);
$signedResponse = $signer->signResponse($response);
Authenticate using Symfony's Security component
In order to use the provided Symfony integration, you will need to include the following optional libraries in your project's composer.json
{
"require": {
"symfony/psr-http-message-bridge": "~0.1",
"symfony/security": "~3.0",
"zendframework/zend-diactoros": "~1.3.5"
}
}
Sample implementation:
parameters:
hmac_keys: {"key": "secret"}
services:
hmac.keyloader:
class: Acquia\Hmac\KeyLoader
arguments:
$keys: '%hmac_keys%'
hmac.request.authenticator:
class: Acquia\Hmac\RequestAuthenticator
arguments:
- '@hmac.keyloader'
public: false
hmac.response.signer:
class: Acquia\Hmac\Symfony\HmacResponseListener
tags:
- { name: kernel.event_listener, event: kernel.response, method: onKernelResponse }
hmac.entry-point:
class: Acquia\Hmac\Symfony\HmacAuthenticationEntryPoint
hmac.security.authentication.provider:
class: Acquia\Hmac\Symfony\HmacAuthenticationProvider
arguments:
- '@hmac.request.authenticator'
public: false
hmac.security.authentication.listener:
class: Acquia\Hmac\Symfony\HmacAuthenticationListener
arguments: ['@security.token_storage', '@security.authentication.manager', '@hmac.entry-point']
public: false
security:
firewalls:
hmac_auth:
pattern: ^/api/
stateless: true
hmac_auth: true
namespace AppBundle;
use Acquia\Hmac\Symfony\HmacFactory;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class AppBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
parent::build($container);
$extension = $container->getExtension('security');
$extension->addSecurityListenerFactory(new HmacFactory());
}
}
PHPUnit testing a controller using HMAC HTTP authentication in Symfony:
- Add the service declaration:
services:
test.client.hmac:
class: Acquia\Hmac\Test\Mocks\Symfony\HmacClient
arguments: ['@kernel', '%test.client.parameters%', '@test.client.history', '@test.client.cookiejar']
namespace MyApp\Bundle\AppBundle\Tests;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Client;
use Acquia\Hmac\Key;
class HmacTestCase extends WebTestCase
{
private $client;
protected static function createClient(array $options = array(), array $server = array())
{
$kernel = static::bootKernel($options);
$client = $kernel->getContainer()->get('test.client.hmac');
$client->setServerParameters($server);
return $client;
}
protected function setUp()
{
$this->client = static::createClient();
$this->client->setKey(new Key('my-key', 'my-not-really-secret'));
}
Contributing and Development
GNU Make and Composer are used to manage development dependencies and testing:
make install
make test
All code should adhere to the following standards:
Submit changes using GitHub's standard pull request workflow.