Skip to content

Commit 4480578

Browse files
author
Justin Hayes
committed
Added the guzzle middleware to generate a signature
1 parent 1c062a4 commit 4480578

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace SoapBox\SignedRequests\Middlewares\Guzzle;
4+
5+
use Psr\Http\Message\RequestInterface;
6+
use SoapBox\SignedRequests\Requests\Generator;
7+
8+
class GenerateSignature
9+
{
10+
/**
11+
* An instance of the signed request generator
12+
*
13+
* @var \SoapBox\SignedRequests\Requests\Generator
14+
*/
15+
protected $generator;
16+
17+
/**
18+
* Expect an instance of the generator so we can sign the request
19+
*
20+
* @param \SoapBox\SignedRequests\Requests\Generator $generator
21+
* An instance of the signed request generator
22+
*/
23+
public function __construct(Generator $generator)
24+
{
25+
$this->generator = $generator;
26+
}
27+
28+
/**
29+
* Return the middleware callable. This callable function signs the request.
30+
*
31+
* @param callable $handler
32+
* The next handler to invoke
33+
*
34+
* @return callable
35+
*/
36+
public function __invoke(callable $handler) : callable
37+
{
38+
return function (RequestInterface $request, array $options) use ($handler) {
39+
$request = $this->generator->sign($request);
40+
return $handler($request, $options);
41+
};
42+
}
43+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace Tests\Midlewares\Guzzle;
4+
5+
use Mockery;
6+
use Tests\TestCase;
7+
use GuzzleHttp\Psr7\Request;
8+
use Illuminate\Config\Repository;
9+
use Psr\Http\Message\RequestInterface;
10+
use SoapBox\SignedRequests\Requests\Generator;
11+
use SoapBox\SignedRequests\Middlewares\Guzzle\GenerateSignature;
12+
13+
class GenerateSignatureTest extends TestCase
14+
{
15+
/**
16+
* @test
17+
*/
18+
public function it_can_be_constructed()
19+
{
20+
$middleware = new GenerateSignature(new Generator(Mockery::mock(Repository::class)));
21+
$this->assertInstanceOf(GenerateSignature::class, $middleware);
22+
}
23+
24+
/**
25+
* @test
26+
*/
27+
public function it_signs_the_request()
28+
{
29+
$configurations = Mockery::mock(Repository::class);
30+
$configurations->shouldReceive('get')
31+
->with('signed-requests.cache-prefix')
32+
->andReturn('prefix');
33+
$configurations->shouldReceive('get')
34+
->with('signed-requests.request-replay.tolerance')
35+
->andReturn(60);
36+
$configurations->shouldReceive('get')
37+
->with('signed-requests.headers.algorithm')
38+
->andReturn('Test-Algorithm');
39+
$configurations->shouldReceive('get')
40+
->with('signed-requests.headers.signature')
41+
->andReturn('Test-Signature');
42+
$algorithm = $configurations->shouldReceive('get')
43+
->with('signed-requests.algorithm')
44+
->andReturn('sha256');
45+
$key = $configurations->shouldReceive('get')
46+
->with('signed-requests.key')
47+
->andReturn('bigsecretrighthereitellyouwhat');
48+
$middleware = new GenerateSignature(new Generator($configurations));
49+
50+
$nextHandler = function (RequestInterface $request, array $options) {
51+
return $request;
52+
};
53+
54+
$request = new Request('get', 'tested');
55+
56+
$handler = call_user_func_array($middleware, [$nextHandler]);
57+
$request = $handler($request, []);
58+
59+
$this->assertTrue($request->hasHeader('Test-Signature'));
60+
$this->assertTrue($request->hasHeader('Test-Algorithm'));
61+
$this->assertSame('sha256', $request->getHeader('Test-Algorithm')[0]);
62+
}
63+
}

0 commit comments

Comments
 (0)