Skip to content

Commit b2a464a

Browse files
authored
feat(clients): add generate_secured_api_key to ruby (#3166)
1 parent 2181ca5 commit b2a464a

File tree

3 files changed

+61
-8
lines changed

3 files changed

+61
-8
lines changed

clients/algoliasearch-client-ruby/lib/algolia/transport/transport.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ def self.encode_uri(uri)
99
CGI.escape(uri).gsub('+', '%20')
1010
end
1111

12+
def self.stringify_query_params(query_params)
13+
query_params.to_h do |key, value|
14+
value = value.join(',') if value.is_a?(Array)
15+
[encode_uri(key.to_s).to_sym, encode_uri(value.to_s)]
16+
end
17+
end
18+
1219
class Transport
1320
include RetryOutcomeType
1421
include CallType
@@ -79,7 +86,7 @@ def build_request(method, path, body, request_options)
7986
request[:method] = method.downcase
8087
request[:path] = path
8188
request[:body] = build_body(body, request_options)
82-
request[:query_params] = stringify_query_params(request_options.query_params)
89+
request[:query_params] = Algolia::Transport.stringify_query_params(request_options.query_params)
8390
request[:header_params] = generate_header_params(body, request_options)
8491
request[:timeout] = request_options.timeout
8592
request[:connect_timeout] = request_options.connect_timeout
@@ -128,13 +135,6 @@ def get_timeout(call_type)
128135
@config.write_timeout
129136
end
130137
end
131-
132-
def stringify_query_params(query_params)
133-
query_params.to_h do |key, value|
134-
value = value.join(',') if value.is_a?(Array)
135-
[Algolia::Transport.encode_uri(key.to_s).to_sym, Algolia::Transport.encode_uri(value.to_s)]
136-
end
137-
end
138138
end
139139
end
140140
end

templates/ruby/api.mustache

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# {{{generationBanner}}}
22

3+
{{#isSearchClient}}
4+
require 'openssl'
5+
require 'base64'
6+
{{/isSearchClient}}
7+
38
module {{moduleName}}
49
{{#operations}}
510
class {{classname}}

templates/ruby/search_helpers.mustache

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,51 @@ def browse_synonyms(index_name, search_synonyms_params = Search::SearchSynonymsP
157157

158158
synonyms unless block_given?
159159
end
160+
161+
# Helper: Generates a secured API key based on the given `parent_api_key` and given `restrictions`.
162+
#
163+
# @param parent_api_key [String] Parent API key used the generate the secured key
164+
# @param restrictions [SecuredApiKeyRestrictions] Restrictions to apply on the secured key
165+
#
166+
# @return [String]
167+
#
168+
def generate_secured_api_key(parent_api_key, restrictions = {})
169+
restrictions = restrictions.to_hash
170+
if restrictions.key?(:searchParams)
171+
# merge searchParams with the root of the restrictions
172+
173+
restrictions.merge!(restrictions[:searchParams])
174+
restrictions.delete(:searchParams)
175+
end
176+
177+
url_encoded_restrictions = Algolia::Transport.stringify_query_params(restrictions).sort.to_h.map do |key, value|
178+
"#{key}=#{value}"
179+
end.join('&')
180+
181+
hmac = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), parent_api_key, url_encoded_restrictions)
182+
183+
puts "hmac: #{hmac}"
184+
puts "url_encoded_restrictions: #{url_encoded_restrictions}"
185+
Base64.encode64("#{hmac}#{url_encoded_restrictions}").gsub("\n", '')
186+
end
187+
188+
# Helper: Retrieves the remaining validity of the previous generated `secured_api_key`, the `validUntil` parameter must have been provided.
189+
#
190+
# @param secured_api_key [String]
191+
#
192+
# @return [Integer]
193+
#
194+
def get_secured_api_key_remaining_validity(secured_api_key)
195+
now = Time.now.to_i
196+
decoded_key = Base64.decode64(secured_api_key)
197+
regex = 'validUntil=(\d+)'
198+
matches = decoded_key.match(regex)
199+
200+
if matches.nil?
201+
raise AlgoliaError, 'The SecuredApiKey doesn\'t have a validUntil parameter.'
202+
end
203+
204+
valid_until = matches[1].to_i
205+
206+
valid_until - now
207+
end

0 commit comments

Comments
 (0)