Skip to content

Commit 9037f94

Browse files
authored
Merge pull request #15 from SoapBox/feature/customizable-generators
[Feature] Decouple the Generators from the Illuminate Configuration Repository
2 parents ac25d94 + 0b7def8 commit 9037f94

14 files changed

+340
-43
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace SoapBox\SignedRequests\Configurations;
4+
5+
interface Configuration
6+
{
7+
/**
8+
* Returns the name of the header that will contain the algorithm used to
9+
* sign the request.
10+
*
11+
* @return string
12+
*/
13+
public function getAlgorithmHeader(): string;
14+
15+
/**
16+
* Returns the name of the header that will contain the generated signature.
17+
*
18+
* @return string
19+
*/
20+
public function getSignatureHeader(): string;
21+
22+
/**
23+
* Returns the algorithm to use to generate the signature.
24+
*
25+
* @return string
26+
*/
27+
public function getSigningAlgorithm(): string;
28+
29+
/**
30+
* Returns the key to sign the request with.
31+
*
32+
* @return string
33+
*/
34+
public function getSigningKey(): string;
35+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
namespace SoapBox\SignedRequests\Configurations;
4+
5+
class CustomConfiguration implements Configuration
6+
{
7+
/**
8+
* The algorithm header to return.
9+
*
10+
* @var string
11+
*/
12+
protected $algorithmHeader;
13+
14+
/**
15+
* The signature header to return.
16+
*
17+
* @var string
18+
*/
19+
protected $signatureHeader;
20+
21+
/**
22+
* The signing algorithm to return.
23+
*
24+
* @var string
25+
*/
26+
protected $signingAlgorithm;
27+
28+
/**
29+
* The signing key to return.
30+
*
31+
* @var string
32+
*/
33+
protected $signingKey;
34+
35+
/**
36+
* Sets up our custom configuration with various properties.
37+
*
38+
* @param string $algorithmHeader
39+
* The algorithm header to use.
40+
* @param string $signatureHeader
41+
* The signature header to use.
42+
* @param string $signingAlgorithm
43+
* The signing algorithm to use.
44+
* @param string $signingKey
45+
* The signing key to use.
46+
*/
47+
public function __construct(
48+
string $algorithmHeader,
49+
string $signatureHeader,
50+
string $signingAlgorithm,
51+
string $signingKey
52+
) {
53+
$this->algorithmHeader = $algorithmHeader;
54+
$this->signatureHeader = $signatureHeader;
55+
$this->signingAlgorithm = $signingAlgorithm;
56+
$this->signingKey = $signingKey;
57+
}
58+
59+
/**
60+
* Returns the name of the header that will contain the algorithm used to
61+
* sign the request.
62+
*
63+
* @return string
64+
*/
65+
public function getAlgorithmHeader(): string
66+
{
67+
return $this->algorithmHeader;
68+
}
69+
70+
/**
71+
* Returns the name of the header that will contain the generated signature.
72+
*
73+
* @return string
74+
*/
75+
public function getSignatureHeader(): string
76+
{
77+
return $this->signatureHeader;
78+
}
79+
80+
/**
81+
* Returns the algorithm to use to generate the signature.
82+
*
83+
* @return string
84+
*/
85+
public function getSigningAlgorithm(): string
86+
{
87+
return $this->signingAlgorithm;
88+
}
89+
90+
/**
91+
* Returns the key to sign the request with.
92+
*
93+
* @return string
94+
*/
95+
public function getSigningKey(): string
96+
{
97+
return $this->signingKey;
98+
}
99+
}
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/Exceptions/ExpiredRequestException.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace SoapBox\SignedRequests\Exceptions;
44

55
use Exception;
6-
use Throwable;
76
use Symfony\Component\HttpFoundation\Response;
87
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
98

src/Exceptions/InvalidSignatureException.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace SoapBox\SignedRequests\Exceptions;
44

55
use Exception;
6-
use Throwable;
76
use Symfony\Component\HttpFoundation\Response;
87
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
98

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 & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,27 @@
66
use Ramsey\Uuid\Uuid;
77
use GuzzleHttp\Psr7\Request;
88
use SoapBox\SignedRequests\Signature;
9-
use Illuminate\Support\Facades\Config;
10-
use Illuminate\Contracts\Config\Repository;
9+
use SoapBox\SignedRequests\Configurations\Configuration;
1110

1211
class Generator
1312
{
1413
/**
15-
* An instance of the configuration repository.
14+
* A configuration to use for generating signatures.
1615
*
17-
* @var \Illuminate\Contracts\Config\Repository
16+
* @var \SoapBox\SignedRequests\Configurations\Configuration
1817
*/
19-
private $repository;
18+
private $configuration;
2019

2120
/**
2221
* Constructs our signed request generator with an instance of the
2322
* configurations.
2423
*
25-
* @param \Illuminate\Contracts\Config\Repository $repository
26-
* A configuration repository.
24+
* @param \SoapBox\SignedRequests\Configurations\Configuration $configuration
25+
* The configuration to use for generating the signed request.
2726
*/
28-
public function __construct(Repository $repository)
27+
public function __construct(Configuration $configuration)
2928
{
30-
$this->repository = $repository;
29+
$this->configuration = $configuration;
3130
}
3231

3332
/**
@@ -37,15 +36,15 @@ public function __construct(Repository $repository)
3736
* The request to sign.
3837
*
3938
* @return \GuzzleHttp\Psr7\Request
40-
* The request with an id, algorith, and signature.
39+
* The request with an id, algorithm, and signature.
4140
*/
4241
public function sign(Request $request) : Request
4342
{
44-
$algorithmHeader = $this->repository->get('signed-requests.headers.algorithm');
45-
$signatureHeader = $this->repository->get('signed-requests.headers.signature');
43+
$algorithmHeader = $this->configuration->getAlgorithmHeader();
44+
$signatureHeader = $this->configuration->getSignatureHeader();
4645

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

5049
$request = $request->withHeader('X-SIGNED-ID', (string) Uuid::uuid4());
5150
$request = $request->withHeader('X-SIGNED-TIMESTAMP', (string) Carbon::now());

src/Requests/Verifier.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Carbon\Carbon;
66
use Illuminate\Http\Request;
77
use SoapBox\SignedRequests\Signature;
8-
use SoapBox\SignedRequests\Requests\Payload;
98

109
class Verifier
1110
{
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Tests\Configurations;
4+
5+
use Tests\TestCase;
6+
use SoapBox\SignedRequests\Configurations\CustomConfiguration;
7+
8+
class CustomConfigurationTest extends TestCase
9+
{
10+
/**
11+
* @test
12+
*/
13+
function it_returns_the_set_values_when_requested()
14+
{
15+
$algorithmHeader = 'a';
16+
$signatureHeader = 's';
17+
$signingAlgorithm = 'sa';
18+
$signingKey = 'sk';
19+
20+
$configuration = new CustomConfiguration(
21+
$algorithmHeader,
22+
$signatureHeader,
23+
$signingAlgorithm,
24+
$signingKey
25+
);
26+
27+
$this->assertSame($algorithmHeader, $configuration->getAlgorithmHeader());
28+
$this->assertSame($signatureHeader, $configuration->getSignatureHeader());
29+
$this->assertSame($signingAlgorithm, $configuration->getSigningAlgorithm());
30+
$this->assertSame($signingKey, $configuration->getSigningKey());
31+
}
32+
}

0 commit comments

Comments
 (0)