meilisearch 0.18.1 → 0.18.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6309d55fa663d9fd8126c1bbf290313ddab29e0e8e72ee690b0abe49887d6097
4
- data.tar.gz: 2386993d60f7fa52c38e10d58e191a0f0797a3672de31a36b5c1013440285584
3
+ metadata.gz: 9b69f3c0d8bf43393b59abe4e44aabf40ff673dd08bde8f6e60d1c996be8b7bf
4
+ data.tar.gz: d2ff90fe7e1cf738dc4e3297cf7e7215e80c7a63317d06144681fab39206d1b2
5
5
  SHA512:
6
- metadata.gz: d50aa62ffebcd4d707fc55471e8738d9dd53433c77af3f1bb08e3681395ee19eb35513bc14a1e769bf223a9e16622805dc1010d06b4b84c2252237d1eb5a8b97
7
- data.tar.gz: fff2a910e52b6c137037c3b011ddba3b0d11d28fd2427c1b4d45e925e2b28711c4e0fca269309de9b97dd99df980e4ed067b322c7148c0fde68a14ce4fe7d388
6
+ metadata.gz: 59dfc2b7ced1fec8a75c32b4b02b49d42a8a63af862bcb86931692be8e9c92b89657b93958c150937ddbe295d645eff5192a181ef439885462c2f6174c1519c3
7
+ data.tar.gz: 71b81d762f8c609a7e2d50ac0052e6c8e0b904c28b2cb7b98f0c55934122c97c1160a4c0b4a2659adcf474a59be85ea609a75183e69550ca4971d177afb8e2b0
data/README.md CHANGED
@@ -198,7 +198,7 @@ JSON output:
198
198
 
199
199
  ## 🤖 Compatibility with Meilisearch
200
200
 
201
- This package only guarantees the compatibility with the [version v0.25.0 of Meilisearch](https://github.com/meilisearch/meilisearch/releases/tag/v0.25.0).
201
+ This package only guarantees the compatibility with the [version v0.26.0 of Meilisearch](https://github.com/meilisearch/meilisearch/releases/tag/v0.26.0).
202
202
 
203
203
  ## 💡 Learn More
204
204
 
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'meilisearch/http_request'
4
-
5
3
  module MeiliSearch
6
4
  class Client < HTTPRequest
5
+ include MeiliSearch::TenantToken
6
+
7
7
  ### INDEXES
8
8
 
9
9
  def raw_indexes
@@ -62,4 +62,10 @@ module MeiliSearch
62
62
  super(@message)
63
63
  end
64
64
  end
65
+
66
+ module TenantToken
67
+ class ExpireOrInvalidSignature < StandardError; end
68
+ class InvalidApiKey < StandardError; end
69
+ class InvalidSearchRules < StandardError; end
70
+ end
65
71
  end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MeiliSearch
4
+ module TenantToken
5
+ HEADER = {
6
+ typ: 'JWT',
7
+ alg: 'HS256'
8
+ }.freeze
9
+
10
+ def generate_tenant_token(search_rules, api_key: nil, expires_at: nil)
11
+ signature = retrieve_valid_key!(api_key, @api_key)
12
+ expiration = validate_expires_at!(expires_at)
13
+ rules = validate_search_rules!(search_rules)
14
+ unsigned_data = build_payload(expiration, rules, signature)
15
+
16
+ combine(unsigned_data, to_base64(sign_data(signature, unsigned_data)))
17
+ end
18
+
19
+ private
20
+
21
+ def build_payload(expiration, rules, signature)
22
+ payload = {
23
+ searchRules: rules,
24
+ apiKeyPrefix: signature[0..7],
25
+ exp: expiration
26
+ }
27
+
28
+ combine(encode(HEADER), encode(payload))
29
+ end
30
+
31
+ def validate_expires_at!(expires_at)
32
+ return unless expires_at
33
+ return expires_at.to_i if expires_at.utc? && expires_at > Time.now.utc
34
+
35
+ raise
36
+ rescue StandardError
37
+ raise ExpireOrInvalidSignature
38
+ end
39
+
40
+ def validate_search_rules!(data)
41
+ return data if data
42
+
43
+ raise InvalidSearchRules
44
+ end
45
+
46
+ def retrieve_valid_key!(*keys)
47
+ key = keys.compact.find { |k| !k.empty? }
48
+
49
+ raise InvalidApiKey if key.nil?
50
+
51
+ key
52
+ end
53
+
54
+ def sign_data(key, msg)
55
+ OpenSSL::HMAC.digest('SHA256', key, msg)
56
+ end
57
+
58
+ def to_base64(data)
59
+ Base64.urlsafe_encode64(data, padding: false)
60
+ end
61
+
62
+ def encode(data)
63
+ to_base64(JSON.generate(data))
64
+ end
65
+
66
+ def combine(*parts)
67
+ parts.join('.')
68
+ end
69
+ end
70
+ end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MeiliSearch
4
- VERSION = '0.18.1'
4
+ VERSION = '0.18.2'
5
5
 
6
6
  def self.qualified_version
7
7
  "Meilisearch Ruby (v#{VERSION})"
data/lib/meilisearch.rb CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  require 'meilisearch/version'
4
4
  require 'meilisearch/utils'
5
+ require 'meilisearch/http_request'
6
+ require 'meilisearch/tenant_token'
5
7
  require 'meilisearch/task'
6
8
  require 'meilisearch/client'
7
9
  require 'meilisearch/index'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: meilisearch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.18.1
4
+ version: 0.18.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Meili
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-02-01 00:00:00.000000000 Z
11
+ date: 2022-03-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -44,6 +44,7 @@ files:
44
44
  - lib/meilisearch/http_request.rb
45
45
  - lib/meilisearch/index.rb
46
46
  - lib/meilisearch/task.rb
47
+ - lib/meilisearch/tenant_token.rb
47
48
  - lib/meilisearch/utils.rb
48
49
  - lib/meilisearch/version.rb
49
50
  homepage: https://github.com/meilisearch/meilisearch-ruby