Skip to content

Commit b75182a

Browse files
committed
Refactors our generator to depend on a configuration adapter
Previously the generator was aware of the illuminate configuration contract, which made it difficult to use multiple signed request configurations in a single project. With the addition of the configuration adapters we can now have many implementations of configurations.
1 parent 72ef338 commit b75182a

File tree

6 files changed

+174
-35
lines changed

6 files changed

+174
-35
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace SoapBox\SignedRequests\Configurations;
4+
5+
use Illuminate\Contracts\Config\Repository;
6+
7+
class RepositoryConfiguration implements Configuration
8+
{
9+
/**
10+
* An instance of the configuration repository.
11+
*
12+
* @var \Illuminate\Contracts\Config\Repository
13+
*/
14+
protected $repository;
15+
16+
/**
17+
* Sets up our configuration with an instance of the laravel configuration
18+
* repository.
19+
*
20+
* @param \Illuminate\Contracts\Config\Repository $repository
21+
* A configuration repository.
22+
*/
23+
public function __construct(Repository $repository)
24+
{
25+
$this->repository = $repository;
26+
}
27+
28+
/**
29+
* Returns the name of the header that will contain the algorithm used to
30+
* sign the request.
31+
*
32+
* @return string
33+
*/
34+
public function getAlgorithmHeader(): string
35+
{
36+
return $this->repository->get('signed-requests.headers.algorithm');
37+
}
38+
39+
/**
40+
* Returns the name of the header that will contain the generated signature.
41+
*
42+
* @return string
43+
*/
44+
public function getSignatureHeader(): string
45+
{
46+
return $this->repository->get('signed-requests.headers.signature');
47+
}
48+
49+
/**
50+
* Returns the algorithm to use to generate the signature.
51+
*
52+
* @return string
53+
*/
54+
public function getSigningAlgorithm(): string
55+
{
56+
return $this->repository->get('signed-requests.algorithm');
57+
}
58+
59+
/**
60+
* Returns the key to sign the request with.
61+
*
62+
* @return string
63+
*/
64+
public function getSigningKey(): string
65+
{
66+
return $this->repository->get('signed-requests.key');
67+
}
68+
}

src/Middlewares/Guzzle/GenerateSignature.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Psr\Http\Message\RequestInterface;
66
use SoapBox\SignedRequests\Requests\Generator;
7+
use SoapBox\SignedRequests\Configurations\Configuration;
78

89
class GenerateSignature
910
{
@@ -15,14 +16,14 @@ class GenerateSignature
1516
protected $generator;
1617

1718
/**
18-
* Expect an instance of the generator so we can sign the request
19+
* Expect a configuration to build our generator with.
1920
*
20-
* @param \SoapBox\SignedRequests\Requests\Generator $generator
21-
* An instance of the signed request generator
21+
* @param \SoapBox\SignedRequests\Configurations\Configuration $configuration
22+
* The configuration to use for generating signed requests.
2223
*/
23-
public function __construct(Generator $generator)
24+
public function __construct(Configuration $configuration)
2425
{
25-
$this->generator = $generator;
26+
$this->generator = new Generator($configuration);
2627
}
2728

2829
/**

src/Requests/Generator.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,27 @@
88
use SoapBox\SignedRequests\Signature;
99
use Illuminate\Support\Facades\Config;
1010
use Illuminate\Contracts\Config\Repository;
11+
use SoapBox\SignedRequests\Configurations\Configuration;
1112

1213
class Generator
1314
{
1415
/**
15-
* An instance of the configuration repository.
16+
* A configuration to use for generating signatures.
1617
*
17-
* @var \Illuminate\Contracts\Config\Repository
18+
* @var \SoapBox\SignedRequests\Configurations\Configuration
1819
*/
19-
private $repository;
20+
private $configuration;
2021

2122
/**
2223
* Constructs our signed request generator with an instance of the
2324
* configurations.
2425
*
25-
* @param \Illuminate\Contracts\Config\Repository $repository
26-
* A configuration repository.
26+
* @param \SoapBox\SignedRequests\Configurations\Configuration $configuration
27+
* The configuration to use for generating the signed request.
2728
*/
28-
public function __construct(Repository $repository)
29+
public function __construct(Configuration $configuration)
2930
{
30-
$this->repository = $repository;
31+
$this->configuration = $configuration;
3132
}
3233

3334
/**
@@ -37,15 +38,15 @@ public function __construct(Repository $repository)
3738
* The request to sign.
3839
*
3940
* @return \GuzzleHttp\Psr7\Request
40-
* The request with an id, algorith, and signature.
41+
* The request with an id, algorithm, and signature.
4142
*/
4243
public function sign(Request $request) : Request
4344
{
44-
$algorithmHeader = $this->repository->get('signed-requests.headers.algorithm');
45-
$signatureHeader = $this->repository->get('signed-requests.headers.signature');
45+
$algorithmHeader = $this->configuration->getAlgorithmHeader();
46+
$signatureHeader = $this->configuration->getSignatureHeader();
4647

47-
$algorithm = $this->repository->get('signed-requests.algorithm');
48-
$key = $this->repository->get('signed-requests.key');
48+
$algorithm = $this->configuration->getSigningAlgorithm();
49+
$key = $this->configuration->getSigningKey();
4950

5051
$request = $request->withHeader('X-SIGNED-ID', (string) Uuid::uuid4());
5152
$request = $request->withHeader('X-SIGNED-TIMESTAMP', (string) Carbon::now());
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace Tests\Configurations;
4+
5+
use Mockery;
6+
use Tests\TestCase;
7+
use Illuminate\Contracts\Config\Repository;
8+
use SoapBox\SignedRequests\Configurations\RepositoryConfiguration;
9+
10+
class RepositoryConfigurationTest extends TestCase
11+
{
12+
protected $repository;
13+
protected $configuration;
14+
15+
/**
16+
* @before
17+
*/
18+
function setup_repository()
19+
{
20+
$this->repository = Mockery::mock(Repository::class);
21+
$this->configuration = new RepositoryConfiguration($this->repository);
22+
}
23+
24+
/**
25+
* @test
26+
*/
27+
function get_algorithm_header_returns_the_algorithm_header_value_from_the_configuration_repository()
28+
{
29+
$this->repository->shouldReceive('get')
30+
->with('signed-requests.headers.algorithm')
31+
->andReturn('algorithm');
32+
33+
$this->assertSame('algorithm', $this->configuration->getAlgorithmHeader());
34+
}
35+
36+
/**
37+
* @test
38+
*/
39+
function get_signature_header_returns_the_signature_header_value_from_the_configuration_repository()
40+
{
41+
$this->repository->shouldReceive('get')
42+
->with('signed-requests.headers.signature')
43+
->andReturn('signature');
44+
45+
$this->assertSame('signature', $this->configuration->getSignatureHeader());
46+
}
47+
48+
/**
49+
* @test
50+
*/
51+
function get_signing_algorithm_returns_the_signing_algorithm_to_use_from_the_configuration_repository()
52+
{
53+
$this->repository->shouldReceive('get')
54+
->with('signed-requests.algorithm')
55+
->andReturn('sha256');
56+
57+
$this->assertSame('sha256', $this->configuration->getSigningAlgorithm());
58+
}
59+
60+
/**
61+
* @test
62+
*/
63+
function get_signing_key_returns_the_key_used_for_signing_the_request_from_the_configuration_repository()
64+
{
65+
$this->repository->shouldReceive('get')
66+
->with('signed-requests.key')
67+
->andReturn('key');
68+
69+
$this->assertSame('key', $this->configuration->getSigningKey());
70+
}
71+
}

tests/Middlewares/Guzzle/GenerateSignatureTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
use Illuminate\Config\Repository;
99
use Psr\Http\Message\RequestInterface;
1010
use SoapBox\SignedRequests\Requests\Generator;
11+
use SoapBox\SignedRequests\Configurations\Configuration;
1112
use SoapBox\SignedRequests\Middlewares\Guzzle\GenerateSignature;
13+
use SoapBox\SignedRequests\Configurations\RepositoryConfiguration;
1214

1315
class GenerateSignatureTest extends TestCase
1416
{
@@ -17,7 +19,7 @@ class GenerateSignatureTest extends TestCase
1719
*/
1820
public function it_can_be_constructed()
1921
{
20-
$middleware = new GenerateSignature(new Generator(Mockery::mock(Repository::class)));
22+
$middleware = new GenerateSignature(Mockery::mock(Configuration::class));
2123
$this->assertInstanceOf(GenerateSignature::class, $middleware);
2224
}
2325

@@ -45,7 +47,7 @@ public function it_signs_the_request()
4547
$key = $configurations->shouldReceive('get')
4648
->with('signed-requests.key')
4749
->andReturn('bigsecretrighthereitellyouwhat');
48-
$middleware = new GenerateSignature(new Generator($configurations));
50+
$middleware = new GenerateSignature(new RepositoryConfiguration($configurations));
4951

5052
$nextHandler = function (RequestInterface $request, array $options) {
5153
return $request;

tests/Requests/GeneratorTest.php

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,36 @@
66
use Tests\TestCase;
77
use GuzzleHttp\Psr7\Request;
88
use SoapBox\SignedRequests\Signature;
9-
use Illuminate\Contracts\Config\Repository;
109
use SoapBox\SignedRequests\Requests\Payload;
1110
use SoapBox\SignedRequests\Requests\Generator;
11+
use SoapBox\SignedRequests\Configurations\Configuration;
1212

1313
class GeneratorTest extends TestCase
1414
{
15-
private $repository;
15+
private $configuration;
1616
private $generator;
1717

1818
/**
1919
* @before
2020
*/
2121
public function setup_a_local_generator()
2222
{
23-
$this->repository = Mockery::mock(Repository::class);
23+
$this->configuration = Mockery::mock(Configuration::class);
2424

25-
$this->repository
26-
->shouldReceive('get')
27-
->with('signed-requests.headers.algorithm')
25+
$this->configuration
26+
->shouldReceive('getAlgorithmHeader')
2827
->andReturn('X-ALGORITHM');
29-
$this->repository
30-
->shouldReceive('get')
31-
->with('signed-requests.headers.signature')
28+
$this->configuration
29+
->shouldReceive('getSignatureHeader')
3230
->andReturn('X-SIGNATURE');
33-
$this->repository
34-
->shouldReceive('get')
35-
->with('signed-requests.algorithm')
31+
$this->configuration
32+
->shouldReceive('getSigningAlgorithm')
3633
->andReturn('sha256');
37-
$this->repository
38-
->shouldReceive('get')
39-
->with('signed-requests.key')
34+
$this->configuration
35+
->shouldReceive('getSigningKey')
4036
->andReturn('key');
4137

42-
$this->generator = new Generator($this->repository);
38+
$this->generator = new Generator($this->configuration);
4339
}
4440

4541
/**

0 commit comments

Comments
 (0)