-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathkeys.rb
127 lines (112 loc) · 4.87 KB
/
keys.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# frozen_string_literal: true
##
# These examples walk you through operations to manage API Keys
require_relative 'client_initialization'
# Let's setup some test data for this example
schema = {
'name' => 'users',
'fields' => [
{
'name' => 'company_id',
'type' => 'int32',
'facet' => false
},
{
'name' => 'user_name',
'type' => 'string',
'facet' => false
},
{
'name' => 'login_count',
'type' => 'int32',
'facet' => false
},
{
'name' => 'country',
'type' => 'string',
'facet' => true
}
],
'default_sorting_field' => 'company_id'
}
# We have four users, belonging to two companies: 124 and 126
documents = [
{
'company_id' => 124,
'user_name' => 'Hilary Bradford',
'login_count' => 10,
'country' => 'USA'
},
{
'company_id' => 124,
'user_name' => 'Nile Carty',
'login_count' => 100,
'country' => 'USA'
},
{
'company_id' => 126,
'user_name' => 'Tahlia Maxwell',
'login_count' => 1,
'country' => 'France'
},
{
'company_id' => 126,
'user_name' => 'Karl Roy',
'login_count' => 2,
'country' => 'Germany'
}
]
# Delete if the collection already exists from a previous example run
begin
@typesense.collections['users'].delete
rescue Typesense::Error::ObjectNotFound
end
# create a collection
@typesense.collections.create(schema)
# Index documents
documents.each do |document|
@typesense.collections['users'].documents.create(document)
end
# Generate an API key and restrict it to only allow searches
# You want to use this API Key in the browser instead of the master API Key
unscoped_search_only_api_key_response = @typesense.keys.create({
'description' => 'Search-only key.',
'actions' => ['documents:search'],
'collections' => ['*']
})
ap unscoped_search_only_api_key_response
# Save the key returned, since this will be the only time the full API Key is returned, for security purposes
unscoped_search_only_api_key = unscoped_search_only_api_key_response['value']
# Side note: you can also retrieve metadata of API keys using the ID returned in the above response
unscoped_search_only_api_key_response = @typesense.keys[unscoped_search_only_api_key_response['id']].retrieve
ap unscoped_search_only_api_key_response
# We'll now use this search-only API key to generate a scoped search API key that can only access documents that have company_id:124
# This is useful when you store multi-tenant data in a single Typesense server, but you only want
# a particular tenant to access their own data. You'd generate one scoped search key per tenant.
# IMPORTANT: scoped search keys should only be generated *server-side*, so as to not leak the unscoped main search key to clients
scoped_search_only_api_key = @typesense.keys.generate_scoped_search_key(unscoped_search_only_api_key, { filter_by: 'company_id:124' })
ap "scoped_search_only_api_key: #{scoped_search_only_api_key}"
# Now let's search the data using the scoped API Key for company_id:124
# You can do searches with this scoped_search_only_api_key from the server-side or client-side
scoped_typesense_client = Typesense::Client.new({
nodes: [{
host: 'localhost',
port: '8108',
protocol: 'http'
}],
api_key: scoped_search_only_api_key
})
search_results = scoped_typesense_client.collections['users'].documents.search({
'q' => 'Hilary',
'query_by' => 'user_name'
})
ap search_results
# Search for a user that exists, but is outside the current key's scope
search_results = scoped_typesense_client.collections['users'].documents.search({
q: 'Maxwell',
query_by: 'user_name'
})
ap search_results # Will return empty result set
# Now let's delete the unscoped_search_only_api_key. You'd want to do this when you need to rotate keys for example.
results = @typesense.keys[unscoped_search_only_api_key_response['id']].delete
ap results