Skip to content

Commit 73675fb

Browse files
committed
Fix bug with scoped key creation, add example for keys
1 parent 83b921e commit 73675fb

4 files changed

Lines changed: 182 additions & 6 deletions

File tree

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
"ext-json": "*"
3636
},
3737
"scripts": {
38-
"typesenseServer": "docker run -i -p 8108:8108 -v/tmp/typesense-server-data-1c/:/data typesense/typesense:0.15.0 --data-dir /data --api-key=xyz --listen-port 8108 --enable-cors"
38+
"typesenseServer": [
39+
"Composer\\Config::disableProcessTimeout",
40+
"docker run -i -p 8108:8108 -v/tmp/typesense-server-data-1c/:/data typesense/typesense:0.15.0 --data-dir /data --api-key=xyz --listen-port 8108 --enable-cors"
41+
]
3942
}
4043
}

examples/keys_operations.php

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
<?php
2+
3+
include '../vendor/autoload.php';
4+
5+
use \Typesense\Client;
6+
7+
try {
8+
$client = new Client(
9+
[
10+
'api_key' => 'xyz',
11+
'nodes' => [
12+
[
13+
'host' => 'localhost',
14+
'port' => '8108',
15+
'protocol' => 'http',
16+
],
17+
],
18+
'connection_timeout_seconds' => 2,
19+
]
20+
);
21+
echo '<pre>';
22+
try {
23+
print_r($client->collections['users']->delete());
24+
} catch (Exception $e) {
25+
// Don't error out if the collection was not found
26+
}
27+
echo "--------Create Collection-------\n";
28+
print_r(
29+
$client->collections->create(
30+
[
31+
'name' => 'users',
32+
'fields' => [
33+
[
34+
'name' => 'company_id',
35+
'type' => 'int32',
36+
'facet' => false
37+
],
38+
[
39+
'name' => 'user_name',
40+
'type' => 'string',
41+
'facet' => false
42+
],
43+
[
44+
'name' => 'login_count',
45+
'type' => 'int32',
46+
'facet' => false
47+
],
48+
[
49+
'name' => 'country',
50+
'type' => 'string',
51+
'facet' => true
52+
]
53+
],
54+
'default_sorting_field' => 'company_id'
55+
]
56+
)
57+
);
58+
echo "--------Create Collection-------\n";
59+
echo "\n";
60+
echo "--------Create Documents-------\n";
61+
print_r(
62+
$client->collections['users']->documents->createMany([
63+
[
64+
'company_id' => 124,
65+
'user_name' => 'Hilary Bradford',
66+
'login_count' => 10,
67+
'country' => 'USA'
68+
],
69+
[
70+
'company_id' => 124,
71+
'user_name' => 'Nile Carty',
72+
'login_count' => 100,
73+
'country' => 'USA'
74+
],
75+
[
76+
'company_id' => 126,
77+
'user_name' => 'Tahlia Maxwell',
78+
'login_count' => 1,
79+
'country' => 'France'
80+
],
81+
[
82+
'company_id' => 126,
83+
'user_name' => 'Karl Roy',
84+
'login_count' => 2,
85+
'country' => 'Germany'
86+
]
87+
])
88+
);
89+
echo "--------Create Documents-------\n";
90+
echo "\n";
91+
echo "--------Create a search only API key-------\n";
92+
$searchOnlyApiKeyResponse = $client->keys->create([
93+
'description' => 'Search-only key.',
94+
'actions' => ['documents:search'],
95+
'collections' => ['*']
96+
]);
97+
print_r($searchOnlyApiKeyResponse);
98+
echo "--------Create a search only API key-------\n";
99+
echo "\n";
100+
echo "--------Get All Keys-------\n";
101+
print_r($client->keys->retrieve());
102+
echo "--------Get All Keys-------\n";
103+
echo "\n";
104+
echo "--------Get Single Key-------\n";
105+
print_r(
106+
$client->keys[$searchOnlyApiKeyResponse['id']]->retrieve()
107+
);
108+
echo "--------Get Single Key-------\n";
109+
echo "\n";
110+
echo "--------Generate Scoped API Key-------\n";
111+
$scopedAPIKey = $client->keys->generateScopedSearchKey($searchOnlyApiKeyResponse['value'], ['filter_by' => 'company_id:124']);
112+
print_r($scopedAPIKey);
113+
echo "\n";
114+
echo "--------Generate Scoped API Key-------\n";
115+
echo "\n";
116+
echo "--------Search Documents with scoped Key-------\n";
117+
$scopedClient = new Client(
118+
[
119+
'api_key' => $scopedAPIKey,
120+
'nodes' => [
121+
[
122+
'host' => 'localhost',
123+
'port' => '8108',
124+
'protocol' => 'http',
125+
],
126+
],
127+
'connection_timeout_seconds' => 2,
128+
]
129+
);
130+
131+
print_r(
132+
$scopedClient->collections['users']->documents->search(
133+
[
134+
'q' => 'Hilary',
135+
'query_by' => 'user_name'
136+
]
137+
)
138+
);
139+
echo "--------Search Documents with scoped Key-------\n";
140+
echo "\n";
141+
echo "--------Search for document outside of scope for scoped Key-------\n";
142+
print_r(
143+
$scopedClient->collections['users']->documents->search(
144+
[
145+
'q' => 'Maxwell',
146+
'query_by' => 'user_name'
147+
]
148+
)
149+
);
150+
echo "--------Search for document outside of scope for scoped Key-------\n";
151+
echo "\n";
152+
echo "--------Delete Key-------\n";
153+
print_r(
154+
$client->keys[$searchOnlyApiKeyResponse['id']]->delete()
155+
);
156+
echo "--------Delete Key-------\n";
157+
echo "\n";
158+
} catch (Exception $e) {
159+
echo $e->getMessage();
160+
}

src/Client.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ class Client
2929
*/
3030
public Aliases $aliases;
3131

32+
/**
33+
* @var \Typesense\Keys
34+
*/
35+
public Keys $keys;
36+
3237
/**
3338
* Client constructor.
3439
*
@@ -41,6 +46,7 @@ public function __construct(array $config)
4146
$this->config = new Configuration($config);
4247
$this->collections = new Collections($this->config);
4348
$this->aliases = new Aliases($this->config);
49+
$this->keys = new Keys($this->config);
4450
}
4551

4652
/**
@@ -59,4 +65,12 @@ public function getAliases(): Aliases
5965
return $this->aliases;
6066
}
6167

68+
/**
69+
* @return \Typesense\Keys
70+
*/
71+
public function getKeys(): Keys
72+
{
73+
return $this->keys;
74+
}
75+
6276
}

src/Keys.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@ class Keys implements \ArrayAccess
3737
* @param \Typesense\Lib\Configuration $config
3838
* @param \Typesense\ApiCall $apiCall
3939
*/
40-
public function __construct(
41-
Configuration $config, ApiCall $apiCall
42-
) {
40+
public function __construct(Configuration $config)
41+
{
4342
$this->config = $config;
44-
$this->apiCall = $apiCall;
43+
$this->apiCall = new ApiCall($config);
4544
}
4645

4746
/**
@@ -66,7 +65,7 @@ public function generateScopedSearchKey(
6665
string $searchKey, array $parameters
6766
): string {
6867
$paramStr = json_encode($parameters, JSON_THROW_ON_ERROR);
69-
$digest = base64_encode(hash_hmac('sha256', utf8_encode($paramStr), utf8_encode($searchKey)));
68+
$digest = base64_encode(hash_hmac('sha256', utf8_encode($paramStr), utf8_encode($searchKey), TRUE));
7069
$keyPrefix = substr($searchKey, 0, 4);
7170
$rawScopedKey = sprintf('%s%s%s', utf8_decode($digest), $keyPrefix, $paramStr);
7271
return base64_encode(utf8_encode($rawScopedKey));

0 commit comments

Comments
 (0)