Skip to content

Commit 14426f3

Browse files
author
Dave Wong
committed
allow specifying a different set of environment variables denoted by a prefix to the signed requests configuration keys
1 parent fe047b5 commit 14426f3

File tree

2 files changed

+61
-7
lines changed

2 files changed

+61
-7
lines changed

src/Middlewares/Laravel/VerifySignature.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,27 @@ public function __construct(Configurations $configurations, Cache $cache)
5858
* An instance of the request.
5959
* @param \Closure $next
6060
* A callback function of where to go next.
61+
* @param mixed $prefix
6162
*
6263
* @return mixed
6364
*/
64-
public function handle(Request $request, Closure $next)
65+
public function handle(Request $request, Closure $next, $prefix = '')
6566
{
67+
if (!empty($prefix)) {
68+
$prefix .= '-';
69+
}
70+
6671
$signed = new Verifier($request);
6772

6873
$key = sprintf(
6974
'%s.%s',
70-
$this->configurations->get('signed-requests.cache-prefix'),
75+
$this->configurations->get($prefix . 'signed-requests.cache-prefix'),
7176
$signed->getId()
7277
);
7378

74-
$tolerance = $this->configurations->get('signed-requests.request-replay.tolerance');
79+
$tolerance = $this->configurations->get($prefix . 'signed-requests.request-replay.tolerance');
7580

76-
if (true !== $this->configurations->get('signed-requests.request-replay.allow')) {
81+
if (true !== $this->configurations->get($prefix . 'signed-requests.request-replay.allow')) {
7782
$isExpired = $signed->isExpired($tolerance);
7883

7984
if ($isExpired || $this->cache->has($key)) {
@@ -82,10 +87,10 @@ public function handle(Request $request, Closure $next)
8287
}
8388

8489
$signed
85-
->setSignatureHeader($this->configurations->get('signed-requests.headers.signature'))
86-
->setAlgorithmHeader($this->configurations->get('signed-requests.headers.algorithm'));
90+
->setSignatureHeader($this->configurations->get($prefix . 'signed-requests.headers.signature'))
91+
->setAlgorithmHeader($this->configurations->get($prefix . 'signed-requests.headers.algorithm'));
8792

88-
if (!$signed->isValid($this->configurations->get('signed-requests.key'))) {
93+
if (!$signed->isValid($this->configurations->get($prefix . 'signed-requests.key'))) {
8994
throw new InvalidSignatureException();
9095
}
9196

tests/Middlewares/Laravel/VerifySignatureTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,55 @@ public function it_should_call_our_callback_if_the_request_is_valid()
132132
});
133133
}
134134

135+
/**
136+
* @test
137+
*/
138+
public function it_should_prefix_the_configuration_keys_if_a_prefix_is_supplied()
139+
{
140+
$id = (string) Uuid::uuid4();
141+
142+
$this->configurations->shouldReceive('get')
143+
->with('prefix-signed-requests.headers.signature')
144+
->andReturn('signature');
145+
146+
$this->configurations->shouldReceive('get')
147+
->with('prefix-signed-requests.headers.algorithm')
148+
->andReturn('algorithm');
149+
150+
$this->configurations->shouldReceive('get')
151+
->with('prefix-signed-requests.key')
152+
->andReturn('key');
153+
154+
$this->configurations->shouldReceive('get')
155+
->with('prefix-signed-requests.request-replay.allow')
156+
->andReturn(true);
157+
158+
$this->configurations->shouldReceive('get')
159+
->with('prefix-signed-requests.cache-prefix')
160+
->andReturn('prefix');
161+
$this->configurations->shouldReceive('get')
162+
->with('prefix-signed-requests.request-replay.tolerance')
163+
->andReturn(60);
164+
165+
$query = [];
166+
$request = [];
167+
$attributes = [];
168+
$cookies = [];
169+
$files = [];
170+
$server = [
171+
'HTTP_X-SIGNED-ID' => $id,
172+
'HTTP_ALGORITHM' => 'sha256'
173+
];
174+
175+
$request = new Request($query, $request, $attributes, $cookies, $files, $server, 'a');
176+
$request->headers->set('signature', (string) new Signature(new Payload($request), 'sha256', 'key'));
177+
178+
$this->middleware->handle($request, function () {
179+
// This should be called.
180+
$this->assertTrue(true);
181+
}, 'prefix');
182+
}
183+
135184
/**
136185
* @test
137186
* @expectedException \SoapBox\SignedRequests\Exceptions\ExpiredRequestException

0 commit comments

Comments
 (0)