Skip to content
This repository has been archived by the owner on Feb 10, 2023. It is now read-only.

Commit

Permalink
Merge pull request #24 from lepiaf/add-requester-header
Browse files Browse the repository at this point in the history
add header requester name via middleware
  • Loading branch information
lepiaf committed May 23, 2018
2 parents ddfe8ba + 6253323 commit 5f3089b
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 2 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -24,7 +24,8 @@
},
"require-dev": {
"roave/security-advisories": "dev-master",
"phpunit/phpunit": "^7.0"
"phpunit/phpunit": "^7.0",
"guzzlehttp/guzzle": "^6.3"
},
"suggest": {
"csa/guzzle-bundle": "Allow to unseal and verify response from sapient api automatically."
Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/Configuration.php
Expand Up @@ -33,6 +33,7 @@ public function getConfigTreeBuilder()
->children()
->booleanNode('unseal')->defaultFalse()->end()
->booleanNode('verify')->defaultFalse()->end()
->scalarNode('requester_name')->defaultFalse()->end()
->end()
->end()
->arrayNode('sealing_public_keys')
Expand Down
5 changes: 5 additions & 0 deletions src/DependencyInjection/SapientExtension.php
Expand Up @@ -45,6 +45,11 @@ public function load(array $configs, ContainerBuilder $container)
if ($config['guzzle_middleware']['unseal']) {
$loader->load('guzzle_middleware/unseal_response.yml');
}

if ($config['guzzle_middleware']['requester_name']) {
$container->setParameter('sapient.guzzle_middleware.requester_name', $config['guzzle_middleware']['requester_name']);
$loader->load('guzzle_middleware/requester_header.yml');
}
}
}
}
33 changes: 33 additions & 0 deletions src/GuzzleHttp/Middleware/RequesterHeaderMiddleware.php
@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);

namespace lepiaf\SapientBundle\GuzzleHttp\Middleware;

use lepiaf\SapientBundle\Service\PublicKeyGetter;
use Psr\Http\Message\RequestInterface;

class RequesterHeaderMiddleware
{
/**
* @var string
*/
private $requesterName;

/**
* @param string $requesterName
*/
public function __construct(string $requesterName)
{
$this->requesterName = $requesterName;
}

public function __invoke(callable $handler): callable
{
return function(RequestInterface $request, array $options) use ($handler) {
return $handler(
$request->withHeader(PublicKeyGetter::HEADER_REQUESTER, $this->requesterName),
$options
);
};
}
}
6 changes: 6 additions & 0 deletions src/Resources/config/guzzle_middleware/requester_header.yml
@@ -0,0 +1,6 @@
services:
lepiaf\SapientBundle\GuzzleHttp\Middleware\RequesterHeaderMiddleware:
arguments:
- '%sapient.guzzle_middleware.requester_name%'
tags:
- { name: 'csa_guzzle.middleware', alias: 'requester_name' }
11 changes: 11 additions & 0 deletions src/Resources/doc/reference.rst
Expand Up @@ -16,6 +16,7 @@ Reference
guzzle_middleware:
unseal: boolean
verify: boolean
requester_name: string
sealing_public_keys:
-
name: string
Expand Down Expand Up @@ -104,6 +105,16 @@ If enable, it will activate Guzzle middleware that verify signature in response.

Before enabling this option, you must configure verifying_public_keys_ array.

.. guzzle_middleware.requester_name:
guzzle_middleware.requester_name
------------------------

This Guzzle middleware will add a header ``Sapient-Requester`` automatically on each request. This
header is used by recipient to choose the right key to encrypt response.

It is optional but highly recommended. If not enable, you must add header manually in
Guzzle client configuration.

.. sealing_public_keys:
sealing_public_keys
-------------------
Expand Down
2 changes: 1 addition & 1 deletion src/Service/PublicKeyGetter.php
Expand Up @@ -14,7 +14,7 @@
class PublicKeyGetter
{
public const HEADER_SIGNER = 'Sapient-Signer';
private const HEADER_REQUESTER = 'Sapient-Requester';
public const HEADER_REQUESTER = 'Sapient-Requester';

/**
* @var array
Expand Down
31 changes: 31 additions & 0 deletions tests/GuzzleHttp/Middleware/RequesterHeaderMiddlewareTest.php
@@ -0,0 +1,31 @@
<?php

namespace Tests\lepiaf\SapientBundle\EventSubscriber;

use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use lepiaf\SapientBundle\GuzzleHttp\Middleware\RequesterHeaderMiddleware;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestInterface;

class RequesterHeaderMiddlewareTest extends TestCase
{
public function testRequestWithHeader()
{
$mockHandler = new MockHandler([
function (RequestInterface $request) {
$this->assertTrue($request->hasHeader('Sapient-Requester'));
$this->assertSame(['client-bob'], $request->getHeader('Sapient-Requester'));

return new Response(200);
},
]);
$handler = HandlerStack::create($mockHandler);
$handler->push(new RequesterHeaderMiddleware('client-bob'));
$client = new Client(['handler' => $handler]);

$client->request('GET', 'http://api.example.com/api/ping');
}
}

0 comments on commit 5f3089b

Please sign in to comment.