From b2fd62e284a8b4e8eb035125416cb84ff47381bd Mon Sep 17 00:00:00 2001 From: Christoph Reiter Date: Mon, 5 May 2025 16:09:20 +0200 Subject: [PATCH] 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. --- src/Keys.php | 2 +- tests/Feature/KeysTest.php | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Keys.php b/src/Keys.php index fc7b0008..4327f7dc 100644 --- a/src/Keys.php +++ b/src/Keys.php @@ -58,7 +58,7 @@ public function generateScopedSearchKey( string $searchKey, array $parameters ): string { - $paramStr = json_encode($parameters, JSON_THROW_ON_ERROR); + $paramStr = json_encode((object)$parameters, JSON_THROW_ON_ERROR); $digest = base64_encode( hash_hmac( 'sha256', diff --git a/tests/Feature/KeysTest.php b/tests/Feature/KeysTest.php index 658d0393..24783383 100644 --- a/tests/Feature/KeysTest.php +++ b/tests/Feature/KeysTest.php @@ -73,4 +73,13 @@ public function testCanGenerateScopedSearchKey(): void ]); $this->assertEquals($scopedSearchKey, $result); } + + public function testGenerateScopedSearchKeyWithoutParams(): void + { + $searchKey = "RN23GFr1s6jQ9kgSNg2O7fYcAUXU7127"; + $scopedSearchKey = + "R2pFZkxSemhGZmEvOXd2WWpYNWVSTzF2N2xRSk9jQmlpZ2NpdnloUTFGYz1STjIze30="; + $result = $this->client()->keys->generateScopedSearchKey($searchKey, []); + $this->assertEquals($scopedSearchKey, $result); + } }