Skip to content

Commit d50a023

Browse files
committed
Update the verifier to verify signed requests genenerated by the updated payload
1 parent b5a423e commit d50a023

File tree

4 files changed

+378
-10
lines changed

4 files changed

+378
-10
lines changed

src/Requests/Payload.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,28 @@ protected function generateFromIlluminateRequest(IlluminateRequest $request) : s
8282
$id = $this->request->headers->get('X-SIGNED-ID', '');
8383
$timestamp = $this->request->headers->get('X-SIGNED-TIMESTAMP', '');
8484

85+
$string = json_decode((string) $this->request->getContent());
86+
87+
if (is_null($string)) {
88+
return json_encode([
89+
'id' => (string) $id,
90+
'method' => strtoupper($this->request->getMethod()),
91+
'timestamp' => $timestamp,
92+
'uri' => rtrim((string) $this->request->fullUrl(), '/'),
93+
'content' => (string) $this->request->getContent()
94+
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
95+
}
96+
8597
return json_encode([
86-
'id' => (string) $id,
98+
'id' => (string)$id,
8799
'method' => strtoupper($this->request->getMethod()),
88100
'timestamp' => $timestamp,
89-
'uri' => (string) $this->request->fullUrl(),
90-
'content' => $this->request->getContent()
91-
], JSON_UNESCAPED_SLASHES);
101+
'uri' => rtrim((string) $this->request->fullUrl(), '/'),
102+
'content' => json_encode(
103+
json_decode((string) $this->request->getContent()),
104+
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
105+
)
106+
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
92107
}
93108

94109
/**

src/Requests/Verifier.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public function getContent($asResource = false)
142142
json_decode($content);
143143

144144
if (json_last_error() == JSON_ERROR_NONE) {
145-
return json_encode(json_decode($content), JSON_UNESCAPED_SLASHES);
145+
return json_encode(json_decode($content), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
146146
}
147147

148148
return $content;

tests/ClientTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,4 +241,50 @@ public function it_generates_a_signature_with_a_complex_json_payload_after_strip
241241
]
242242
);
243243
}
244+
245+
/**
246+
* @test
247+
*/
248+
public function it_generates_a_signature_with_a_simple_json_get_payload()
249+
{
250+
Carbon::setTestNow('2001-01-01 00:00:00');
251+
$this->expectUuid4('303103f5-3dca-4704-96ad-860717769ec9');
252+
253+
$uri = 'https://localhost';
254+
255+
$this->handler->expects('GET', $uri)
256+
->inspectRequest(function ($request) use ($uri) {
257+
$this->assertTrue($request->hasHeader('Algorithm'));
258+
$this->assertTrue($request->hasHeader('Signature'));
259+
$this->assertSame(
260+
'939ada016b60aa267980a73f62e6dc583b03b35a2abf0dea5b054871d6c6a306',
261+
$request->getHeader('Signature')[0]
262+
);
263+
});
264+
265+
$this->client->get($uri, ['json' => ['payload' => 'payload']]);
266+
}
267+
268+
/**
269+
* @test
270+
*/
271+
public function it_generates_a_signature_with_a_simple_get_payload()
272+
{
273+
Carbon::setTestNow('2001-01-01 00:00:00');
274+
$this->expectUuid4('303103f5-3dca-4704-96ad-860717769ec9');
275+
276+
$uri = 'https://localhost';
277+
278+
$this->handler->expects('GET', $uri)
279+
->inspectRequest(function ($request) use ($uri) {
280+
$this->assertTrue($request->hasHeader('Algorithm'));
281+
$this->assertTrue($request->hasHeader('Signature'));
282+
$this->assertSame(
283+
'9feb58dfece796627b16f7865fc19ee6bfc5b231d49b12d83170d74d22bf9641',
284+
$request->getHeader('Signature')[0]
285+
);
286+
});
287+
288+
$this->client->get($uri, ['body' => 'payload']);
289+
}
244290
}

0 commit comments

Comments
 (0)