Guzzle 6 support for http-signatures library

Michael Tibben 1ff43cd60b Allow http-signatures v5 7 years ago
examples 84239d4762 Update package to support Guzzle 6 and stop supporting old versions. 8 years ago
src 84239d4762 Update package to support Guzzle 6 and stop supporting old versions. 8 years ago
tests 84239d4762 Update package to support Guzzle 6 and stop supporting old versions. 8 years ago
.gitignore a0f24861fe Initial commit 10 years ago
.travis.yml 84239d4762 Update package to support Guzzle 6 and stop supporting old versions. 8 years ago
LICENSE 2de6cbc2a4 Initial commit 10 years ago
LICENSE.txt a0f24861fe Initial commit 10 years ago
README.md 84239d4762 Update package to support Guzzle 6 and stop supporting old versions. 8 years ago
composer.json 1ff43cd60b Allow http-signatures v5 7 years ago
phpunit.xml.dist ffeb67b4f0 Add phpunit.xml.dist 10 years ago

README.md

HTTP Signatures Guzzle 6

Guzzle 6 support for 99designs http-signatures library

Build Status

Adds 99designs/http-signatures support to Guzzle 6.

Older Guzzle Versions

For Guzzle 4 & 5 use the v1.x release of this repo. For Guzzle 3 see the 99designs/http-signatures-guzzle repo.

Signing with Guzzle 6

This library includes support for automatically signing Guzzle requests using Middleware.

You can use GuzzleHttpSignatures::defaultHandlerFromContext to easily create the default Guzzle handler with the middleware added to sign every request.

use GuzzleHttp\Client;
use HttpSignatures\Context;
use HttpSignatures\GuzzleHttpSignatures;

require __DIR__ . "/../vendor/autoload.php";

$context = new Context([
    'keys' => ['examplekey' => 'secret-key-here'],
    'algorithm' => 'hmac-sha256',
    'headers' => ['(request-target)', 'date'],
]);

$handlerStack = GuzzleHttpSignatures::defaultHandlerFromContext($context);
$client = new Client(['handler' => $handlerStack]);

// The below will now send a signed request to: http://example.org/path?query=123
$response = $client->get("http://www.example.com/path?query=123", ['headers' => ['date' => 'today']]);

Or if you're creating a custom HandlerStack you can add the Middleware yourself:

<?php

use GuzzleHttp\Client;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use HttpSignatures\Context;
use HttpSignatures\GuzzleHttpSignatures;

require __DIR__ . "/../vendor/autoload.php";

$context = new Context([
    'keys' => ['examplekey' => 'secret-key-here'],
    'algorithm' => 'hmac-sha256',
    'headers' => ['(request-target)', 'date'],
]);

$handlerStack = new HandlerStack();
$stack->setHandler(new CurlHandler());
$stack->push(GuzzleHttpSignatures::middlewareFromContext($this->context));
$stack->push(Middleware::history($this->history));
$client = new Client(['handler' => $handlerStack]);

// The below will now send a signed request to: http://example.org/path?query=123
$response = $client->get("http://www.example.com/path?query=123", ['headers' => ['date' => 'today']]);

Contributing

Pull Requests are welcome.