warrant 0.1.1 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5243205fa7c1a3a21d681d66ab0071c2b71c95fb018a9812b7844919611c8b31
4
- data.tar.gz: f36c01ea86c7cb028b142fc30eae84824e6ef861915d3f71038052b704cedb8e
3
+ metadata.gz: a67a5a1dd2b6e9b95b675395d1337acd9509f166f5e22d224987260191b5090d
4
+ data.tar.gz: '08013f834166288f8a0ba21753d7a11d0a89e1aa8b7189894e0d80146f47081e'
5
5
  SHA512:
6
- metadata.gz: 4675af03e0ca8a149e5b01c0a58a6e7f72064597c167a4a6ddef76e98975d321dcb0efe1feafe85df558207f070cbae2969dd3c84bafeaad35a99ad97aa9c31c
7
- data.tar.gz: 188ddcc865f033900b0f1b11e4ba49d11ce5a6ff01a0cc67387a07e0600e048ccbbbe75c0f6297a9b4eeefb9e759f71c3104d4fe5a8d2221610b649b56fad630
6
+ metadata.gz: 8fd775c0e3bf27a5e2eebfa06992c2b09e6dc8d1337740ddbab46e328ea9ccc16b48f1da20e0214ba450d2428d0ac43370ae58ab7f698de7a3ed4b00abab646d
7
+ data.tar.gz: 04d89c962874fbd667d3f695f2913552e4dd855fa436737b387e05498736f4557549cd64958ab6dbce92d37f9ffd114549f498140cce5f7823e3f93e0e39c4c9
data/README.md CHANGED
@@ -28,28 +28,43 @@ require 'warrant'
28
28
  Warrant.api_key = 'api_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E='
29
29
  ```
30
30
 
31
- ### `createUser(user_id, username)`
31
+ ### `create_user(email, user_id = '')`
32
32
 
33
- This method creates a user entity in Warrant with the specified `userId`. Provide an optional `username` to make it easier to identify users in the Warrant dashboard.
33
+ This method creates a user entity in Warrant with the specified `user_d`. Provide an optional `username` to make it easier to identify users in the Warrant dashboard.
34
34
 
35
35
  ```ruby
36
- # Create user with `user.id` as the user_id and `user.email` as the username
37
- Warrant::WarrantClient.create_user(user.id, user.email)
36
+ # Create user with user email and id
37
+ Warrant::WarrantClient.create_user(user.email, user.id)
38
38
 
39
39
  # Create user with generated id
40
40
  Warrant::WarrantClient.create_user()
41
41
  ```
42
42
 
43
- ### `createWarrant(objectType, objectId, relation, user)`
43
+ ### `create_warrant(object_type, object_id, relation, user)`
44
+
45
+ #### **User parameters**
46
+ Can provide either a user id, or a combination of object type, object id, and relation
47
+ ---
48
+ #### **user_id**
49
+ Creates a warrant for the user specified by user_id
50
+
51
+ #### **object_type**
52
+ #### **object_id**
53
+ #### **relation**
54
+ Creates a warrant for the given userset specified by object type, object id, and relation
55
+
44
56
 
45
57
  This method creates a warrant which specifies that the provided `user` (or userset) has `relation` on the object of type `objectType` with id `objectId`.
46
58
 
47
59
  ```ruby
48
60
  # Create a warrant allowing user.id to "view" the store with id store.id
49
- Warrant::WarrantClient.create_warrant('store', store.id, 'view', { userId: user.id })
61
+ Warrant::WarrantClient.create_warrant('store', store.id, 'view', { user_id: user.id })
62
+
63
+ # Create a warrant specifying all members of the manager role to "view" store of id store.id
64
+ Warrant::WarrantClient.create_warrant('store', store.id, 'view', { object_type: 'role', object_id: 'manager', relation: 'member' })
50
65
  ```
51
66
 
52
- ### `createSession(userId)`
67
+ ### `create_session(userId)`
53
68
 
54
69
  This method creates a session in Warrant for the user with the specified `userId` and returns a session token which can be used to make authorized requests to the Warrant API only for the specified user. This session token can safely be used to make requests to the Warrant API's authorization endpoint to determine user access in web and mobile client applications.
55
70
 
@@ -58,9 +73,9 @@ This method creates a session in Warrant for the user with the specified `userId
58
73
  Warrant::WarrantClient.create_session(user.id)
59
74
  ```
60
75
 
61
- ### `isAuthorized(objectType, objectId, relation, userId)`
76
+ ### `is_authorized(object_type, object_id, relation, user_id)`
62
77
 
63
- This method returns `true` if the user with the specified `userId` has the specified `relation` to the object of type `objectType` with id `objectId` and `false` otherwise.
78
+ This method returns `true` if the user with the specified `user_id` has the specified `relation` to the object of type `object_type` with id `objectId` and `false` otherwise.
64
79
 
65
80
  ```ruby
66
81
  # Example: user 123 can only view store 824
@@ -68,6 +83,34 @@ Warrant::WarrantClient.is_authorized('store', '824', 'view', '123') # true
68
83
  Warrant::WarrantClient.is_authorized('store', '824', 'edit', '123') # false
69
84
  ```
70
85
 
86
+ ### `list_warrants(filters = {})`
87
+ This method returns all warrants that match the filters provided, or all warrants for your organization if none are provided.
88
+
89
+ #### **Filter Parameters**
90
+ ---
91
+ #### **object_type**
92
+ Only return warrants with the given object type.
93
+
94
+ #### **object_id**
95
+ Only return warrants with the given object id.
96
+
97
+ #### **relation**
98
+ Only return warrants with the given relation.
99
+
100
+ #### **user_id**
101
+ Only return warrants with the given user id
102
+
103
+
104
+ ```ruby
105
+ # List all warrants for an organization
106
+ Warrant::WarrantClient.list_warrants
107
+
108
+ # List all warrants with object type of store
109
+ Warrant::WarrantClient.list_warrants(object_type: 'store')
110
+ ```
111
+
112
+ ---
113
+
71
114
  We’ve used a random API key in these code examples. Replace it with your [actual publishable API keys](https://app.warrant.dev) to
72
115
  test this code through your own Warrant account.
73
116
 
@@ -1,10 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Warrant
4
- class UsersetWarrant
5
- attr_reader :object_type, :object_id, :relation, :user
4
+ class Warrant
5
+ attr_reader :id, :object_type, :object_id, :relation, :user
6
6
 
7
- def initialize(object_type, object_id, relation, user)
7
+ def initialize(id, object_type, object_id, relation, user)
8
+ @id = id
8
9
  @object_type = object_type
9
10
  @object_id = object_id
10
11
  @relation = relation
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Warrant
4
+ class Util
5
+ class << self
6
+ def camelcase(str)
7
+ str = str.split('_').collect(&:capitalize).join
8
+ str.sub(str[0], str[0].downcase)
9
+ end
10
+
11
+ def normalize_options(opts)
12
+ new_opts = opts.each_with_object({}) do |(k, v), new_opts|
13
+ new_key = Util.camelcase(k.to_s)
14
+
15
+ new_opts[new_key] = v
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Warrant
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.4"
5
5
  end
@@ -4,7 +4,7 @@ module Warrant
4
4
  class WarrantClient
5
5
  class << self
6
6
  def create_tenant(tenant_id = '')
7
- uri = URI.parse("#{Warrant.config.api_base}/v1/tenants")
7
+ uri = URI.parse("#{::Warrant.config.api_base}/v1/tenants")
8
8
  params = {
9
9
  tenantId: tenant_id
10
10
  }
@@ -19,10 +19,9 @@ module Warrant
19
19
  end
20
20
  end
21
21
 
22
- def create_user(email, user_id = '', tenant_id = '')
23
- uri = URI.parse("#{Warrant.config.api_base}/v1/users")
22
+ def create_user(email, user_id = '')
23
+ uri = URI.parse("#{::Warrant.config.api_base}/v1/users")
24
24
  params = {
25
- tenantId: tenant_id,
26
25
  userId: user_id,
27
26
  email: email
28
27
  }
@@ -38,7 +37,7 @@ module Warrant
38
37
  end
39
38
 
40
39
  def create_role(role_id)
41
- uri = URI.parse("#{Warrant.config.api_base}/v1/roles")
40
+ uri = URI.parse("#{::Warrant.config.api_base}/v1/roles")
42
41
  params = {
43
42
  roleId: role_id
44
43
  }
@@ -54,7 +53,7 @@ module Warrant
54
53
  end
55
54
 
56
55
  def delete_role(role_id)
57
- uri = URI.parse("#{Warrant.config.api_base}/v1/roles/#{role_id}")
56
+ uri = URI.parse("#{::Warrant.config.api_base}/v1/roles/#{role_id}")
58
57
  res = delete(uri)
59
58
 
60
59
  case res
@@ -66,7 +65,7 @@ module Warrant
66
65
  end
67
66
 
68
67
  def create_permission(permission_id)
69
- uri = URI.parse("#{Warrant.config.api_base}/v1/permissions")
68
+ uri = URI.parse("#{::Warrant.config.api_base}/v1/permissions")
70
69
  params = {
71
70
  permissionId: permission_id
72
71
  }
@@ -82,7 +81,7 @@ module Warrant
82
81
  end
83
82
 
84
83
  def delete_permission(permission_id)
85
- uri = URI.parse("#{Warrant.config.api_base}/v1/permissions/#{permission_id}")
84
+ uri = URI.parse("#{::Warrant.config.api_base}/v1/permissions/#{permission_id}")
86
85
  res = delete(uri)
87
86
 
88
87
  case res
@@ -94,22 +93,53 @@ module Warrant
94
93
  end
95
94
 
96
95
  def create_warrant(object_type, object_id, relation, user)
97
- uri = URI.parse("#{Warrant.config.api_base}/v1/warrants")
96
+ uri = URI.parse("#{::Warrant.config.api_base}/v1/warrants")
98
97
  params = {
99
98
  objectType: object_type,
100
99
  objectId: object_id,
101
100
  relation: relation,
102
- user: user
101
+ user: Util.normalize_options(user)
103
102
  }
104
103
  res = post(uri, params)
105
104
  res_json = JSON.parse(res.body)
106
105
 
107
106
  case res
108
107
  when Net::HTTPSuccess
109
- if res_json['user']['userId']
110
- UserWarrant.new(res_json['objectType'], res_json['objectId'], res_json['relation'], res_json['user']['userId'])
111
- elsif res_json['user']['objectType']
112
- UsersetWarrant.new(res_json['objectType'], res_json['objectId'], res_json['relation'], res_json['user'])
108
+ Warrant.new(res_json['id'], res_json['objectType'], res_json['objectId'], res_json['relation'], res_json['user'])
109
+ else
110
+ res_json
111
+ end
112
+ end
113
+
114
+ def delete_warrant(warrant_id)
115
+ uri = URI.parse("#{::Warrant.config.api_base}/v1/warrants/#{warrant_id}")
116
+ res = delete(uri)
117
+
118
+ case res
119
+ when Net::HTTPSuccess
120
+ return
121
+ else
122
+ res_json
123
+ end
124
+ end
125
+
126
+ def list_warrants(filters = {})
127
+ query_string = ""
128
+ unless filters.empty?
129
+ new_filters = Util.normalize_options(filters.compact)
130
+
131
+ query_string = URI.encode_www_form(new_filters)
132
+ end
133
+
134
+ uri = URI.parse("#{::Warrant.config.api_base}/v1/warrants?#{query_string}")
135
+
136
+ res = get(uri)
137
+ res_json = JSON.parse(res.body)
138
+
139
+ case res
140
+ when Net::HTTPSuccess
141
+ res_json.map do |warrant|
142
+ Warrant.new(warrant['id'], warrant['objectType'], warrant['objectId'], warrant['relation'], warrant['user'])
113
143
  end
114
144
  else
115
145
  res_json
@@ -117,7 +147,7 @@ module Warrant
117
147
  end
118
148
 
119
149
  def assign_role_to_user(user_id, role_id)
120
- uri = URI.parse("#{Warrant.config.api_base}/v1/users/#{user_id}/roles/#{role_id}")
150
+ uri = URI.parse("#{::Warrant.config.api_base}/v1/users/#{user_id}/roles/#{role_id}")
121
151
  res = post(uri)
122
152
  res_json = JSON.parse(res.body)
123
153
 
@@ -130,7 +160,7 @@ module Warrant
130
160
  end
131
161
 
132
162
  def remove_role_from_user(user_id, role_id)
133
- uri = URI.parse("#{Warrant.config.api_base}/v1/users/#{user_id}/roles/#{role_id}")
163
+ uri = URI.parse("#{::Warrant.config.api_base}/v1/users/#{user_id}/roles/#{role_id}")
134
164
  res = delete(uri)
135
165
 
136
166
  case res
@@ -142,7 +172,7 @@ module Warrant
142
172
  end
143
173
 
144
174
  def assign_permission_to_user(user_id, permission_id)
145
- uri = URI.parse("#{Warrant.config.api_base}/v1/users/#{user_id}/permissions/#{permission_id}")
175
+ uri = URI.parse("#{::Warrant.config.api_base}/v1/users/#{user_id}/permissions/#{permission_id}")
146
176
  res = post(uri)
147
177
  res_json = JSON.parse(res.body)
148
178
 
@@ -155,7 +185,7 @@ module Warrant
155
185
  end
156
186
 
157
187
  def remove_permission_from_user(user_id, permission_id)
158
- uri = URI.parse("#{Warrant.config.api_base}/v1/users/#{user_id}/permissions/#{permission_id}")
188
+ uri = URI.parse("#{::Warrant.config.api_base}/v1/users/#{user_id}/permissions/#{permission_id}")
159
189
  res = delete(uri)
160
190
 
161
191
  case res
@@ -167,7 +197,7 @@ module Warrant
167
197
  end
168
198
 
169
199
  def create_session(user_id)
170
- uri = URI.parse("#{Warrant.config.api_base}/v1/users/#{user_id}/sessions")
200
+ uri = URI.parse("#{::Warrant.config.api_base}/v1/users/#{user_id}/sessions")
171
201
  res = post(uri)
172
202
  res_json = JSON.parse(res.body)
173
203
 
@@ -180,7 +210,7 @@ module Warrant
180
210
  end
181
211
 
182
212
  def create_self_service_session(user_id, redirect_url)
183
- uri = URI.parse("#{Warrant.config.api_base}/v1/sessions")
213
+ uri = URI.parse("#{::Warrant.config.api_base}/v1/sessions")
184
214
  params = {
185
215
  type: "ssdash",
186
216
  userId: user_id,
@@ -198,7 +228,7 @@ module Warrant
198
228
  end
199
229
 
200
230
  def is_authorized(object_type, object_id, relation, user_id)
201
- uri = URI.parse("#{Warrant.config.api_base}/v1/authorize")
231
+ uri = URI.parse("#{::Warrant.config.api_base}/v1/authorize")
202
232
  params = {
203
233
  objectType: object_type,
204
234
  objectId: object_id,
@@ -227,7 +257,7 @@ module Warrant
227
257
  http = Net::HTTP.new(uri.host, uri.port)
228
258
  http.use_ssl = true
229
259
  headers = {
230
- "Authorization": "ApiKey #{Warrant.config.api_key}"
260
+ "Authorization": "ApiKey #{::Warrant.config.api_key}"
231
261
  }
232
262
  http.post(uri.path, params.to_json, headers)
233
263
  end
@@ -236,10 +266,19 @@ module Warrant
236
266
  http = Net::HTTP.new(uri.host, uri.port)
237
267
  http.use_ssl = true
238
268
  headers = {
239
- "Authorization": "ApiKey #{Warrant.config.api_key}"
269
+ "Authorization": "ApiKey #{::Warrant.config.api_key}"
240
270
  }
241
271
  http.delete(uri.path, headers)
242
272
  end
273
+
274
+ def get(uri, params = {})
275
+ http = Net::HTTP.new(uri.host, uri.port)
276
+ http.use_ssl = true
277
+ headers = {
278
+ "Authorization": "ApiKey #{::Warrant.config.api_key}"
279
+ }
280
+ http.get(uri, headers)
281
+ end
243
282
  end
244
283
  end
245
284
  end
data/lib/warrant.rb CHANGED
@@ -11,13 +11,13 @@ require "warrant/models/role"
11
11
  require "warrant/models/tenant"
12
12
  require "warrant/models/user"
13
13
  require "warrant/models/userset"
14
- require "warrant/models/user_warrant"
15
- require "warrant/models/userset_warrant"
14
+ require "warrant/models/warrant"
15
+ require "warrant/util"
16
16
  require "warrant/warrant_configuration"
17
17
  require "warrant/warrant_client"
18
18
 
19
19
  module Warrant
20
- @config = Warrant::WarrantConfiguration.new
20
+ @config = ::Warrant::WarrantConfiguration.new
21
21
 
22
22
  class << self
23
23
  extend Forwardable
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: warrant
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Warrant
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-01-26 00:00:00.000000000 Z
11
+ date: 2022-05-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Ruby library for the Warrant API at https://warrant.dev.
14
14
  email: hello@warrant.dev
@@ -30,9 +30,9 @@ files:
30
30
  - lib/warrant/models/role.rb
31
31
  - lib/warrant/models/tenant.rb
32
32
  - lib/warrant/models/user.rb
33
- - lib/warrant/models/user_warrant.rb
34
33
  - lib/warrant/models/userset.rb
35
- - lib/warrant/models/userset_warrant.rb
34
+ - lib/warrant/models/warrant.rb
35
+ - lib/warrant/util.rb
36
36
  - lib/warrant/version.rb
37
37
  - lib/warrant/warrant_client.rb
38
38
  - lib/warrant/warrant_configuration.rb
@@ -44,7 +44,7 @@ metadata:
44
44
  source_code_uri: https://github.com/warrant-dev/warrant-ruby
45
45
  changelog_uri: https://github.com/warrant-dev/warrant-ruby/CHANGELOG.md
46
46
  documentation_uri: https://docs.warrant.dev/
47
- post_install_message:
47
+ post_install_message:
48
48
  rdoc_options: []
49
49
  require_paths:
50
50
  - lib
@@ -59,8 +59,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
59
59
  - !ruby/object:Gem::Version
60
60
  version: '0'
61
61
  requirements: []
62
- rubygems_version: 3.2.14
63
- signing_key:
62
+ rubygems_version: 3.3.11
63
+ signing_key:
64
64
  specification_version: 4
65
65
  summary: Warrant Ruby Library
66
66
  test_files: []
@@ -1,14 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Warrant
4
- class UserWarrant
5
- attr_reader :object_type, :object_id, :relation, :user
6
-
7
- def initialize(object_type, object_id, relation, user_id)
8
- @object_type = object_type
9
- @object_id = object_id
10
- @relation = relation
11
- @user = User.new(user_id)
12
- end
13
- end
14
- end