Skip to content

Commit a1aa7b9

Browse files
authored
feat(remaining-validity): add getSecuredApiKeyRemainingValidity method (#581)
1 parent ee6e3be commit a1aa7b9

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Exceptions;
4+
5+
final class ValidUntilNotFoundException extends AlgoliaException
6+
{
7+
}

src/SearchClient.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Algolia\AlgoliaSearch;
44

5+
use Algolia\AlgoliaSearch\Exceptions\ValidUntilNotFoundException;
56
use Algolia\AlgoliaSearch\Response\DeleteApiKeyResponse;
67
use Algolia\AlgoliaSearch\Response\IndexingResponse;
78
use Algolia\AlgoliaSearch\Response\MultipleIndexBatchIndexingResponse;
@@ -355,4 +356,28 @@ public function custom($method, $path, $requestOptions = array(), $hosts = null)
355356
{
356357
return $this->api->send($method, $path, $requestOptions, $hosts);
357358
}
359+
360+
/**
361+
* Returns the time the given securedAPIKey remains valid in seconds.
362+
*
363+
* @param string $securedAPIKey the key to check
364+
*
365+
* @return int remaining validity in seconds
366+
*
367+
* @throws ValidUntilNotFoundException
368+
*/
369+
public static function getSecuredApiKeyRemainingValidity($securedAPIKey)
370+
{
371+
$decodedKey = base64_decode($securedAPIKey);
372+
$regex = '/validUntil=(\d+)/';
373+
preg_match($regex, $decodedKey, $matches);
374+
375+
if (0 === count($matches)) {
376+
throw new ValidUntilNotFoundException("The SecuredAPIKey doesn't have a validUntil parameter.");
377+
}
378+
379+
$validUntil = (int) $matches[1];
380+
381+
return $validUntil - time();
382+
}
358383
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Tests\Integration;
4+
5+
use Algolia\AlgoliaSearch\SearchClient;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class SearchClientTest extends TestCase
9+
{
10+
public function testGetSecuredApiKeyRemainingValidity()
11+
{
12+
$now = time();
13+
14+
$newSecuredKey = SearchClient::generateSecuredApiKey('foo',
15+
array('validUntil' => $now - (10 * 60))
16+
);
17+
18+
$this->assertLessThan(0, SearchClient::getSecuredApiKeyRemainingValidity($newSecuredKey));
19+
20+
$newSecuredKey = SearchClient::generateSecuredApiKey('foo',
21+
array('validUntil' => $now + (10 * 60))
22+
);
23+
24+
$this->assertGreaterThan(0, SearchClient::getSecuredApiKeyRemainingValidity($newSecuredKey));
25+
26+
try {
27+
$newSecuredKey = SearchClient::generateSecuredApiKey('foo', array());
28+
SearchClient::getSecuredApiKeyRemainingValidity($newSecuredKey);
29+
} catch (\Exception $e) {
30+
$this->assertInstanceOf('Algolia\AlgoliaSearch\Exceptions\ValidUntilNotFoundException', $e);
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)