Skip to content

Commit c8a98ed

Browse files
committed
Added an optional timestamp verification to add additional security level (expiring requests)
1 parent e5baad7 commit c8a98ed

2 files changed

Lines changed: 64 additions & 7 deletions

File tree

src/Client.php

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,26 @@ class Client
4949
*/
5050
protected $httpClient;
5151

52+
/**
53+
* @var bool
54+
*/
55+
protected $useTimestamp = false;
56+
5257
/**
5358
* Client constructor.
5459
*
5560
* @param string $key
5661
* @param string $secret
5762
* @param string $workspace
5863
* @param integer $timeout
64+
* @param boolean $useTimestamp
5965
*/
60-
public function __construct($key, $secret, $workspace = null, $timeout = null)
66+
public function __construct($key, $secret, $workspace = null, $timeout = null, $useTimestamp = false)
6167
{
6268
$this->key = $key;
6369
$this->secret = $secret;
6470
$this->workspace = $workspace;
71+
$this->useTimestamp = $useTimestamp;
6572

6673
if ($timeout)
6774
{
@@ -82,6 +89,14 @@ public function setWorkspace($workspace)
8289
return $this;
8390
}
8491

92+
/**
93+
* @param bool $status
94+
*/
95+
public function setUseTimestamp($status = true)
96+
{
97+
$this->useTimestamp = $status;
98+
}
99+
85100
/**
86101
* Set request timeout
87102
*
@@ -106,11 +121,12 @@ public function setBaseUrl($url)
106121

107122
/**
108123
* @param string $resource
124+
* @param integer $timestamp
109125
*
110126
* @return string
111127
* @throws Exception
112128
*/
113-
protected function createSignature($resource)
129+
protected function createSignature($resource, $timestamp)
114130
{
115131
if (!$this->key)
116132
{
@@ -131,6 +147,11 @@ protected function createSignature($resource)
131147
'resource' => $resource
132148
];
133149

150+
if ($this->useTimestamp)
151+
{
152+
$data['timestamp'] = $timestamp;
153+
}
154+
134155
ksort($data);
135156

136157
return hash_hmac('sha256', implode('', $data), $this->secret);
@@ -154,18 +175,25 @@ protected function getHttpClient()
154175

155176
/**
156177
* @param string $method
157-
* @param string $resource
178+
* @param $resource
158179
* @param array $params
159180
* @param array $headers
160181
*
161-
* @return \stdClass
182+
* @return null|\stdClass
162183
* @throws \ActualReports\PDFGeneratorAPI\Exception
184+
* @throws \GuzzleHttp\Exception\GuzzleException
163185
*/
164186
public function request($method = self::REQUEST_POST, $resource, array $params = [], array $headers = [])
165187
{
166-
$signature = $this->createSignature($resource);
188+
$timestamp = time();
189+
$signature = $this->createSignature($resource, $timestamp);
167190
$method = strtoupper($method);
168191

192+
if ($this->useTimestamp)
193+
{
194+
$headers['X-Auth-Timestamp'] = $timestamp;
195+
}
196+
169197
$options = [
170198
'headers' => array_merge($headers, [
171199
'X-Auth-Key' => $this->key,
@@ -202,8 +230,9 @@ public function request($method = self::REQUEST_POST, $resource, array $params =
202230
* @param $resource
203231
* @param $options
204232
*
205-
* @return \stdClass|null
233+
* @return null|\stdClass
206234
* @throws \ActualReports\PDFGeneratorAPI\Exception
235+
* @throws \GuzzleHttp\Exception\GuzzleException
207236
*/
208237
protected function handleRequest($method, $resource, $options)
209238
{
@@ -279,6 +308,7 @@ protected function handleResponse(ResponseInterface $response)
279308
*
280309
* @return array
281310
* @throws \ActualReports\PDFGeneratorAPI\Exception
311+
* @throws \GuzzleHttp\Exception\GuzzleException
282312
*/
283313
public function getAll(array $access = [], array $tags = [])
284314
{
@@ -297,6 +327,7 @@ public function getAll(array $access = [], array $tags = [])
297327
*
298328
* @return \stdClass
299329
* @throws \ActualReports\PDFGeneratorAPI\Exception
330+
* @throws \GuzzleHttp\Exception\GuzzleException
300331
*/
301332
public function get($template)
302333
{
@@ -310,6 +341,7 @@ public function get($template)
310341
*
311342
* @return \stdClass
312343
* @throws \ActualReports\PDFGeneratorAPI\Exception
344+
* @throws \GuzzleHttp\Exception\GuzzleException
313345
*/
314346
public function create($name)
315347
{
@@ -347,6 +379,7 @@ public function create($name)
347379
*
348380
* @return \stdClass
349381
* @throws \ActualReports\PDFGeneratorAPI\Exception
382+
* @throws \GuzzleHttp\Exception\GuzzleException
350383
*/
351384
public function copy($template, $newName = null)
352385
{
@@ -368,6 +401,7 @@ public function copy($template, $newName = null)
368401
*
369402
* @return \stdClass
370403
* @throws \ActualReports\PDFGeneratorAPI\Exception
404+
* @throws \GuzzleHttp\Exception\GuzzleException
371405
*/
372406
public function output($template, $data, $format = self::FORMAT_PDF, $name = null, array $params = [])
373407
{
@@ -390,13 +424,19 @@ public function output($template, $data, $format = self::FORMAT_PDF, $name = nul
390424
*/
391425
public function editor($template, $data = null, array $params = [])
392426
{
427+
$timestamp = time();
393428
$resource = 'templates/'.$template.'/editor';
394429
$params = array_merge([
395430
'key' => $this->key,
396431
'workspace' => $this->workspace,
397-
'signature' => $this->createSignature($resource)
432+
'signature' => $this->createSignature($resource, $timestamp)
398433
], $params);
399434

435+
if ($this->useTimestamp)
436+
{
437+
$params['timestamp'] = $timestamp;
438+
}
439+
400440
if ($data)
401441
{
402442
$params['data'] = self::dataToString($data);
@@ -412,6 +452,7 @@ public function editor($template, $data = null, array $params = [])
412452
*
413453
* @return \stdClass
414454
* @throws \ActualReports\PDFGeneratorAPI\Exception
455+
* @throws \GuzzleHttp\Exception\GuzzleException
415456
*/
416457
public function delete($template)
417458
{

tests/Client.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@ public function testGetTemplates()
3535
$this->assertTrue(count($result) > 10);
3636
}
3737

38+
public function testGetTemplatesWithTimestamp()
39+
{
40+
$this->client->setUseTimestamp(true);
41+
$result = $this->client->getAll();
42+
$this->client->setUseTimestamp(false);
43+
$this->assertTrue(count($result) > 10);
44+
}
45+
46+
public function testGetTemplatesWithExpiredTimestamp()
47+
{
48+
$this->expectException(Exception::class);
49+
$this->client->request('GET', 'templates', [
50+
'timestamp' => time() - 3601
51+
]);
52+
}
53+
3854
public function testGetTemplate()
3955
{
4056
$result = $this->client->get($this->templateId);

0 commit comments

Comments
 (0)