Skip to content

Commit b2fd62e

Browse files
committed
Fix scoped key generation without parameters
In case generateScopedSearchKey() is called with an empty parameters array the json encoding would result in '[]' which results in a broken scoped key and an unhelpful 401 permission error from typesense. Cast input to an object, so we end up with '{}' for that case.
1 parent 513270e commit b2fd62e

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/Keys.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function generateScopedSearchKey(
5858
string $searchKey,
5959
array $parameters
6060
): string {
61-
$paramStr = json_encode($parameters, JSON_THROW_ON_ERROR);
61+
$paramStr = json_encode((object)$parameters, JSON_THROW_ON_ERROR);
6262
$digest = base64_encode(
6363
hash_hmac(
6464
'sha256',

tests/Feature/KeysTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,13 @@ public function testCanGenerateScopedSearchKey(): void
7373
]);
7474
$this->assertEquals($scopedSearchKey, $result);
7575
}
76+
77+
public function testGenerateScopedSearchKeyWithoutParams(): void
78+
{
79+
$searchKey = "RN23GFr1s6jQ9kgSNg2O7fYcAUXU7127";
80+
$scopedSearchKey =
81+
"R2pFZkxSemhGZmEvOXd2WWpYNWVSTzF2N2xRSk9jQmlpZ2NpdnloUTFGYz1STjIze30=";
82+
$result = $this->client()->keys->generateScopedSearchKey($searchKey, []);
83+
$this->assertEquals($scopedSearchKey, $result);
84+
}
7685
}

0 commit comments

Comments
 (0)