2017 © Pedro Peláez
 

library http-hmac-php

An implementation of the HTTP HMAC Spec in PHP that integrates with popular libraries such as Symfony and Guzzle. Different namespace to avoid version conflicts with content hub. Temporary!

image

nickveenhof/http-hmac-php

An implementation of the HTTP HMAC Spec in PHP that integrates with popular libraries such as Symfony and Guzzle. Different namespace to avoid version conflicts with content hub. Temporary!

  • Monday, September 5, 2016
  • by nickveenhof
  • Repository
  • 1 Watchers
  • 0 Stars
  • 15,534 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 22 Forks
  • 0 Open issues
  • 19 Versions
  • 12 % Grown

The README.md

HTTP HMAC Signer for PHP

Build Status Code Coverage HHVM Status Scrutinizer Code Quality Total Downloads Latest Stable Version License, (*1)

HMAC Request Signer is a PHP library that implements the version 2.0 of the HTTP HMAC Spec to sign and verify RESTful Web API requests. It integrates with popular libraries such as Symfony and Guzzle and can be used on both the server and client., (*2)

Installation

HMAC Request Signer can be installed with Composer by adding it as a dependency to your project's composer.json file., (*3)

{
    "require": {
        "nickveenhof/http-hmac-php": "~3.1.0"
    }
}

Please refer to Composer's documentation for more detailed installation and usage instructions., (*4)

Usage

Sign an API request sent via Guzzle


use NickVeenhof\Hmac\Guzzle\HmacAuthMiddleware; use NickVeenhof\Hmac\Key; use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; // Optionally, you can provide signed headers to generate the digest. The header keys need to be provided to the middleware below. $options = [ 'headers' => [ 'X-Custom-1' => 'value1', 'X-Custom-2' => 'value2', ], ]; // A key consists of your UUID and a MIME base64 encoded shared secret. $key = new Key('e7fe97fa-a0c8-4a42-ab8e-2c26d52df059', base64_encode('secret')); // Provide your key, realm and optional signed headers. $middleware = new HmacAuthMiddleware($key, 'CIStore', array_keys($options['headers'])); // Register the middleware. $stack = HandlerStack::create(); $stack->push($middleware); // Create a client. $client = new Client([ 'handler' => $stack, ]); // Request. $result = $client->get('https://service.acquia.io/api/v1/widget', $options); var_dump($result);

Authenticate the request using PSR-7-compatible requests

use NickVeenhof\Hmac\RequestAuthenticator;
use NickVeenhof\Hmac\ResponseSigner;

// $keyLoader implements \NickVeenhof\Hmac\KeyLoaderInterface
$authenticator = new RequestAuthenticator($keyLoader);

// $request implements PSR-7's \Psr\Http\Message\RequestInterface
// An exception will be thrown if it cannot authenticate.
$key = $authenticator->authenticate($request);

$signer = new ResponseSigner($key, $request)
$signedResponse = $signer->signResponse($response);

Authenticate using Silex's SecurityServiceProvider

In order to use the provided Silex security provider, you will need to include the following optional libraries in your project's composer.json:, (*5)

{
    "require": {
        "symfony/psr-http-message-bridge": "~0.1",
        "symfony/security": "~3.0",
        "zendframework/zend-diactoros": "~1.3.5"
    }
}

Sample implementation:, (*6)

use NickVeenhof\Hmac\HmacSecurityProvider;
use Silex\Application;
use Silex\Provider\SecurityServiceProvider;

$app = new Application();

// $keyLoader implements \NickVeenhof\Hmac\KeyLoaderInterface
$app->register(new SecurityServiceProvider());
$app->register(new HmacSecurityProvider($keyLoader));

$app['security.firewalls'] = [
    'hmac-auth' => array(
        'pattern' => '^/api/',
        'hmac' => true,
    ),
];

$app->boot();

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, (*7)

{
    "require": {
        "symfony/psr-http-message-bridge": "~0.1",
        "symfony/security": "~3.0",
        "zendframework/zend-diactoros": "~1.3.5"
    }
}

Sammple implementation:, (*8)

# app/config/services.yml
services:
    hmac.security.authentication.provider:
        class: NickVeenhof\Hmac\Symfony\HmacAuthenticationProvider
        arguments:
            - '@hmac.request.authenticator' # Service should implement \NickVeenhof\Hmac\RequstAuthenticatorInterface
        public: false

    hmac.security.authentication.listener:
        class: NickVeenhof\Hmac\Symfony\HmacAuthenticationListener
        arguments: ['@security.token_storage', '@security.authentication.manager']
        public: false

# app/config/security.yml
security:
    # ...

    firewalls:
        hmac_auth:
            pattern:   ^/api/
            stateless: true
            wsse:      true
// src/AppBundle/AppBundle.php
namespace AppBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class AppBundle extends Bundle
{
    public function build(ContainerBuilder $container)
    {
        parent::build($container);

        // $hmacFactory should implement \Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\SecurityFactoryInterface
        // @see http://symfony.com/doc/current/cookbook/security/custom_authentication_provider.html#the-factory
        $extension = $container->getExtension('security');
        $extension->addSecurityListenerFactory($hmacFactory);
    }
}

Contributing and Development

Submit changes using GitHub's standard pull request workflow., (*9)

All code should adhere to the following standards:, (*10)

Use PHP_CodeSniffer to validate coding style and automatically fix problems according to the PSR-2 standard:, (*11)

$ vendor/bin/phpcs --standard=PSR2 --runtime-set ignore_warnings_on_exit true --colors src/.
$ vendor/bin/phpcs --standard=PSR2 --runtime-set ignore_warnings_on_exit true --colors test/.
$ vendor/bin/phpcbf --standard=PSR2 src/.
$ vendor/bin/phpcbf --standard=PSR2 test/.

Refer to PHP Project Starter's documentation for the Apache Ant targets supported by this project., (*12)

The Versions

24/06 2016

dev-request-response-objects-in-exceptions

dev-request-response-objects-in-exceptions https://github.com/acquia/http-hmac-php

An implementation of the HTTP HMAC Spec in PHP that integrates with popular libraries such as Symfony and Guzzle.

  Sources   Download

MIT

The Requires

 

The Development Requires

02/05 2016
18/04 2016

3.0.0-beta2

3.0.0.0-beta2 https://github.com/acquia/http-hmac-php

An implementation of the HTTP HMAC Spec in PHP that integrates with popular libraries such as Symfony and Guzzle.

  Sources   Download

MIT

The Requires

 

The Development Requires

14/04 2016

3.0.0-beta1

3.0.0.0-beta1 https://github.com/acquia/http-hmac-php

An implementation of the HTTP HMAC Spec in PHP that integrates with popular libraries such as Symfony and Guzzle.

  Sources   Download

MIT

The Requires

 

The Development Requires

12/04 2016

dev-3.0.0-beta

dev-3.0.0-beta https://github.com/acquia/http-hmac-php

An implementation of the HTTP HMAC Spec in PHP that integrates with popular libraries such as Symfony and Guzzle.

  Sources   Download

MIT

The Requires

 

The Development Requires

08/04 2016

dev-3.0.0+poc

dev-3.0.0+poc https://github.com/acquia/http-hmac-php

An implementation of the HTTP HMAC Spec in PHP that integrates with popular libraries such as Symfony and Guzzle.

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

28/01 2016

2.1.0

2.1.0.0 https://github.com/acquia/http-hmac-php

An implementation of the HTTP HMAC Spec in PHP that integrates with popular libraries such as Symfony and Guzzle.

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

23/11 2015
23/11 2015

2.0.1

2.0.1.0 https://github.com/acquia/http-hmac-php

An implementation of the HTTP HMAC Spec in PHP that integrates with popular libraries such as Symfony and Guzzle.

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

11/11 2015

2.0.0

2.0.0.0 https://github.com/acquia/http-hmac-php

An implementation of the HTTP HMAC Spec in PHP that integrates with popular libraries such as Symfony and Guzzle.

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

20/02 2015

1.0.x-dev

1.0.9999999.9999999-dev https://github.com/acquia/http-hmac-php

An implementation of the HTTP HMAC Spec in PHP that integrates with popular libraries such as Symfony and Guzzle.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

20/02 2015
15/02 2015

0.7.0

0.7.0.0 https://github.com/acquia/http-hmac-php

An implementation of the HTTP HMAC Spec in PHP that integrates with popular libraries such as Symfony and Guzzle.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires