warrant 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +34 -6
- data/lib/warrant/models/{userset_warrant.rb → warrant.rb} +1 -1
- data/lib/warrant/util.rb +20 -0
- data/lib/warrant/version.rb +1 -1
- data/lib/warrant/warrant_client.rb +49 -21
- data/lib/warrant.rb +3 -3
- metadata +8 -8
- data/lib/warrant/models/user_warrant.rb +0 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a62cf14af95db8e5b31f60059a2a826a67c067239edb5f6c63cf02426df3ae21
|
4
|
+
data.tar.gz: bdc8aa083993566c57760d484e043ad3ed64abe2bf1945ebad2428c2eb497551
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 36cf1f6120b776293513e49d5554aa70a8ff22e671105072e2f04115d1e00bc852fd4cc5326672ae9ad246f35f069718272f2abf957ff9abe27ebeed0b564efc
|
7
|
+
data.tar.gz: 4377ab4955b67e922622ef8c10c50c17d3281d097ec62df85ce31779df3554d8326121622024ffde252abf1a3c13655d0e3f6251d898fc78f722a1578b091daa
|
data/README.md
CHANGED
@@ -28,19 +28,19 @@ require 'warrant'
|
|
28
28
|
Warrant.api_key = 'api_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E='
|
29
29
|
```
|
30
30
|
|
31
|
-
### `
|
31
|
+
### `create_user(email, user_id = '', tenant_id = '')`
|
32
32
|
|
33
|
-
This method creates a user entity in Warrant with the specified `
|
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.
|
34
34
|
|
35
35
|
```ruby
|
36
|
-
# Create user with
|
36
|
+
# Create user with user email and id
|
37
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
|
-
### `
|
43
|
+
### `create_warrant(objectType, objectId, relation, user)`
|
44
44
|
|
45
45
|
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
46
|
|
@@ -49,7 +49,7 @@ This method creates a warrant which specifies that the provided `user` (or users
|
|
49
49
|
Warrant::WarrantClient.create_warrant('store', store.id, 'view', { userId: user.id })
|
50
50
|
```
|
51
51
|
|
52
|
-
### `
|
52
|
+
### `create_session(userId)`
|
53
53
|
|
54
54
|
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
55
|
|
@@ -58,7 +58,7 @@ This method creates a session in Warrant for the user with the specified `userId
|
|
58
58
|
Warrant::WarrantClient.create_session(user.id)
|
59
59
|
```
|
60
60
|
|
61
|
-
### `
|
61
|
+
### `is_authorized(objectType, objectId, relation, userId)`
|
62
62
|
|
63
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.
|
64
64
|
|
@@ -68,6 +68,34 @@ Warrant::WarrantClient.is_authorized('store', '824', 'view', '123') # true
|
|
68
68
|
Warrant::WarrantClient.is_authorized('store', '824', 'edit', '123') # false
|
69
69
|
```
|
70
70
|
|
71
|
+
### `list_warrants(filters = {})`
|
72
|
+
This method returns all warrants that match the filters provided, or all warrants for your organization if none are provided.
|
73
|
+
|
74
|
+
#### **Filter Parameters**
|
75
|
+
---
|
76
|
+
#### **objectType**
|
77
|
+
Only return warrants with the given object type.
|
78
|
+
|
79
|
+
#### **objectId**
|
80
|
+
Only return warrants with the given object id.
|
81
|
+
|
82
|
+
#### **relation**
|
83
|
+
Only return warrants with the given relation.
|
84
|
+
|
85
|
+
#### **userId**
|
86
|
+
Only return warrants with the given user id
|
87
|
+
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
# List all warrants for an organization
|
91
|
+
Warrant::WarrantClient.list_warrants
|
92
|
+
|
93
|
+
# List all warrants with object type of store
|
94
|
+
Warrant::WarrantClient.list_warrants(object_type: 'store')
|
95
|
+
```
|
96
|
+
|
97
|
+
---
|
98
|
+
|
71
99
|
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
100
|
test this code through your own Warrant account.
|
73
101
|
|
data/lib/warrant/util.rb
ADDED
@@ -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
|
data/lib/warrant/version.rb
CHANGED
@@ -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
|
}
|
@@ -20,7 +20,7 @@ module Warrant
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def create_user(email, user_id = '', tenant_id = '')
|
23
|
-
uri = URI.parse("#{Warrant.config.api_base}/v1/users")
|
23
|
+
uri = URI.parse("#{::Warrant.config.api_base}/v1/users")
|
24
24
|
params = {
|
25
25
|
tenantId: tenant_id,
|
26
26
|
userId: user_id,
|
@@ -38,7 +38,7 @@ module Warrant
|
|
38
38
|
end
|
39
39
|
|
40
40
|
def create_role(role_id)
|
41
|
-
uri = URI.parse("#{Warrant.config.api_base}/v1/roles")
|
41
|
+
uri = URI.parse("#{::Warrant.config.api_base}/v1/roles")
|
42
42
|
params = {
|
43
43
|
roleId: role_id
|
44
44
|
}
|
@@ -54,7 +54,7 @@ module Warrant
|
|
54
54
|
end
|
55
55
|
|
56
56
|
def delete_role(role_id)
|
57
|
-
uri = URI.parse("#{Warrant.config.api_base}/v1/roles/#{role_id}")
|
57
|
+
uri = URI.parse("#{::Warrant.config.api_base}/v1/roles/#{role_id}")
|
58
58
|
res = delete(uri)
|
59
59
|
|
60
60
|
case res
|
@@ -66,7 +66,7 @@ module Warrant
|
|
66
66
|
end
|
67
67
|
|
68
68
|
def create_permission(permission_id)
|
69
|
-
uri = URI.parse("#{Warrant.config.api_base}/v1/permissions")
|
69
|
+
uri = URI.parse("#{::Warrant.config.api_base}/v1/permissions")
|
70
70
|
params = {
|
71
71
|
permissionId: permission_id
|
72
72
|
}
|
@@ -82,7 +82,7 @@ module Warrant
|
|
82
82
|
end
|
83
83
|
|
84
84
|
def delete_permission(permission_id)
|
85
|
-
uri = URI.parse("#{Warrant.config.api_base}/v1/permissions/#{permission_id}")
|
85
|
+
uri = URI.parse("#{::Warrant.config.api_base}/v1/permissions/#{permission_id}")
|
86
86
|
res = delete(uri)
|
87
87
|
|
88
88
|
case res
|
@@ -94,7 +94,7 @@ module Warrant
|
|
94
94
|
end
|
95
95
|
|
96
96
|
def create_warrant(object_type, object_id, relation, user)
|
97
|
-
uri = URI.parse("#{Warrant.config.api_base}/v1/warrants")
|
97
|
+
uri = URI.parse("#{::Warrant.config.api_base}/v1/warrants")
|
98
98
|
params = {
|
99
99
|
objectType: object_type,
|
100
100
|
objectId: object_id,
|
@@ -106,11 +106,7 @@ module Warrant
|
|
106
106
|
|
107
107
|
case res
|
108
108
|
when Net::HTTPSuccess
|
109
|
-
|
110
|
-
UserWarrant.new(res_json['id'], res_json['objectType'], res_json['objectId'], res_json['relation'], res_json['user']['userId'])
|
111
|
-
elsif res_json['user']['objectType']
|
112
|
-
UsersetWarrant.new(res_json['id'], res_json['objectType'], res_json['objectId'], res_json['relation'], res_json['user'])
|
113
|
-
end
|
109
|
+
Warrant.new(res_json['id'], res_json['objectType'], res_json['objectId'], res_json['relation'], res_json['user'])
|
114
110
|
else
|
115
111
|
res_json
|
116
112
|
end
|
@@ -128,8 +124,31 @@ module Warrant
|
|
128
124
|
end
|
129
125
|
end
|
130
126
|
|
127
|
+
def list_warrants(filters = {})
|
128
|
+
query_string = ""
|
129
|
+
unless filters.empty?
|
130
|
+
new_filters = Util.normalize_options(filters.compact)
|
131
|
+
|
132
|
+
query_string = URI.encode_www_form(new_filters)
|
133
|
+
end
|
134
|
+
|
135
|
+
uri = URI.parse("#{::Warrant.config.api_base}/v1/warrants?#{query_string}")
|
136
|
+
|
137
|
+
res = get(uri)
|
138
|
+
res_json = JSON.parse(res.body)
|
139
|
+
|
140
|
+
case res
|
141
|
+
when Net::HTTPSuccess
|
142
|
+
res_json.map do |warrant|
|
143
|
+
Warrant.new(warrant['id'], warrant['objectType'], warrant['objectId'], warrant['relation'], warrant['user'])
|
144
|
+
end
|
145
|
+
else
|
146
|
+
res_json
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
131
150
|
def assign_role_to_user(user_id, role_id)
|
132
|
-
uri = URI.parse("#{Warrant.config.api_base}/v1/users/#{user_id}/roles/#{role_id}")
|
151
|
+
uri = URI.parse("#{::Warrant.config.api_base}/v1/users/#{user_id}/roles/#{role_id}")
|
133
152
|
res = post(uri)
|
134
153
|
res_json = JSON.parse(res.body)
|
135
154
|
|
@@ -142,7 +161,7 @@ module Warrant
|
|
142
161
|
end
|
143
162
|
|
144
163
|
def remove_role_from_user(user_id, role_id)
|
145
|
-
uri = URI.parse("#{Warrant.config.api_base}/v1/users/#{user_id}/roles/#{role_id}")
|
164
|
+
uri = URI.parse("#{::Warrant.config.api_base}/v1/users/#{user_id}/roles/#{role_id}")
|
146
165
|
res = delete(uri)
|
147
166
|
|
148
167
|
case res
|
@@ -154,7 +173,7 @@ module Warrant
|
|
154
173
|
end
|
155
174
|
|
156
175
|
def assign_permission_to_user(user_id, permission_id)
|
157
|
-
uri = URI.parse("#{Warrant.config.api_base}/v1/users/#{user_id}/permissions/#{permission_id}")
|
176
|
+
uri = URI.parse("#{::Warrant.config.api_base}/v1/users/#{user_id}/permissions/#{permission_id}")
|
158
177
|
res = post(uri)
|
159
178
|
res_json = JSON.parse(res.body)
|
160
179
|
|
@@ -167,7 +186,7 @@ module Warrant
|
|
167
186
|
end
|
168
187
|
|
169
188
|
def remove_permission_from_user(user_id, permission_id)
|
170
|
-
uri = URI.parse("#{Warrant.config.api_base}/v1/users/#{user_id}/permissions/#{permission_id}")
|
189
|
+
uri = URI.parse("#{::Warrant.config.api_base}/v1/users/#{user_id}/permissions/#{permission_id}")
|
171
190
|
res = delete(uri)
|
172
191
|
|
173
192
|
case res
|
@@ -179,7 +198,7 @@ module Warrant
|
|
179
198
|
end
|
180
199
|
|
181
200
|
def create_session(user_id)
|
182
|
-
uri = URI.parse("#{Warrant.config.api_base}/v1/users/#{user_id}/sessions")
|
201
|
+
uri = URI.parse("#{::Warrant.config.api_base}/v1/users/#{user_id}/sessions")
|
183
202
|
res = post(uri)
|
184
203
|
res_json = JSON.parse(res.body)
|
185
204
|
|
@@ -192,7 +211,7 @@ module Warrant
|
|
192
211
|
end
|
193
212
|
|
194
213
|
def create_self_service_session(user_id, redirect_url)
|
195
|
-
uri = URI.parse("#{Warrant.config.api_base}/v1/sessions")
|
214
|
+
uri = URI.parse("#{::Warrant.config.api_base}/v1/sessions")
|
196
215
|
params = {
|
197
216
|
type: "ssdash",
|
198
217
|
userId: user_id,
|
@@ -210,7 +229,7 @@ module Warrant
|
|
210
229
|
end
|
211
230
|
|
212
231
|
def is_authorized(object_type, object_id, relation, user_id)
|
213
|
-
uri = URI.parse("#{Warrant.config.api_base}/v1/authorize")
|
232
|
+
uri = URI.parse("#{::Warrant.config.api_base}/v1/authorize")
|
214
233
|
params = {
|
215
234
|
objectType: object_type,
|
216
235
|
objectId: object_id,
|
@@ -239,7 +258,7 @@ module Warrant
|
|
239
258
|
http = Net::HTTP.new(uri.host, uri.port)
|
240
259
|
http.use_ssl = true
|
241
260
|
headers = {
|
242
|
-
"Authorization": "ApiKey #{Warrant.config.api_key}"
|
261
|
+
"Authorization": "ApiKey #{::Warrant.config.api_key}"
|
243
262
|
}
|
244
263
|
http.post(uri.path, params.to_json, headers)
|
245
264
|
end
|
@@ -248,10 +267,19 @@ module Warrant
|
|
248
267
|
http = Net::HTTP.new(uri.host, uri.port)
|
249
268
|
http.use_ssl = true
|
250
269
|
headers = {
|
251
|
-
"Authorization": "ApiKey #{Warrant.config.api_key}"
|
270
|
+
"Authorization": "ApiKey #{::Warrant.config.api_key}"
|
252
271
|
}
|
253
272
|
http.delete(uri.path, headers)
|
254
273
|
end
|
274
|
+
|
275
|
+
def get(uri, params = {})
|
276
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
277
|
+
http.use_ssl = true
|
278
|
+
headers = {
|
279
|
+
"Authorization": "ApiKey #{::Warrant.config.api_key}"
|
280
|
+
}
|
281
|
+
http.get(uri, headers)
|
282
|
+
end
|
255
283
|
end
|
256
284
|
end
|
257
285
|
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/
|
15
|
-
require "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.
|
4
|
+
version: 0.1.3
|
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-05-
|
11
|
+
date: 2022-05-09 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/
|
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.
|
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,15 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Warrant
|
4
|
-
class UserWarrant
|
5
|
-
attr_reader :id, :object_type, :object_id, :relation, :user
|
6
|
-
|
7
|
-
def initialize(id, object_type, object_id, relation, user_id)
|
8
|
-
@id = id
|
9
|
-
@object_type = object_type
|
10
|
-
@object_id = object_id
|
11
|
-
@relation = relation
|
12
|
-
@user = User.new(user_id)
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|