warrant 3.1.0 → 5.0.0
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 +4 -4
- data/README.md +0 -7
- data/lib/warrant/api_operations.rb +9 -7
- data/lib/warrant/models/feature.rb +122 -93
- data/lib/warrant/models/list_response.rb +14 -0
- data/lib/warrant/models/object.rb +230 -0
- data/lib/warrant/models/permission.rb +72 -97
- data/lib/warrant/models/pricing_tier.rb +128 -93
- data/lib/warrant/models/query_result.rb +16 -0
- data/lib/warrant/models/role.rb +72 -91
- data/lib/warrant/models/session.rb +4 -4
- data/lib/warrant/models/tenant.rb +131 -123
- data/lib/warrant/models/user.rb +180 -147
- data/lib/warrant/models/warrant.rb +201 -110
- data/lib/warrant/util.rb +6 -0
- data/lib/warrant/version.rb +1 -1
- data/lib/warrant.rb +3 -0
- metadata +5 -2
@@ -2,24 +2,24 @@
|
|
2
2
|
|
3
3
|
module Warrant
|
4
4
|
class Warrant
|
5
|
-
attr_reader :id, :object_type, :object_id, :relation, :subject, :
|
5
|
+
attr_reader :id, :object_type, :object_id, :relation, :subject, :policy, :warrant_token
|
6
6
|
|
7
7
|
# @!visibility private
|
8
|
-
def initialize(object_type, object_id, relation, subject,
|
8
|
+
def initialize(object_type, object_id, relation, subject, policy = nil, warrant_token = nil)
|
9
9
|
@object_type = object_type
|
10
10
|
@object_id = object_id
|
11
11
|
@relation = relation
|
12
12
|
@subject = subject
|
13
|
-
@
|
14
|
-
@
|
13
|
+
@policy = policy
|
14
|
+
@warrant_token = warrant_token
|
15
15
|
end
|
16
16
|
|
17
17
|
# Create a new warrant that associates an object (object_type and object_id) to a subject via a relation.
|
18
18
|
#
|
19
|
-
# @param object [WarrantObject | Hash]
|
20
|
-
# @param relation [String] The relation
|
21
|
-
# @param subject [WarrantObject | Hash]
|
22
|
-
# @param
|
19
|
+
# @param object [WarrantObject | Hash] The object to which the warrant will apply. Can be a hash with object type and id or an instance of a class that implements the WarrantObject module and its methods (`warrant_object_type` and `warrant_object_id`). The object type must be one of your system's existing object type.
|
20
|
+
# @param relation [String] The relation for this object to subject association. The relation must be valid as per the object type definition.
|
21
|
+
# @param subject [WarrantObject | Hash] The subject for which the warrant will apply. Can be a hash with object type and id and an optional relation or an instance of a class that implements the WarrantObject module and its methods (`warrant_object_type` and `warrant_object_id`). The object type must be one of your system's existing object type.
|
22
|
+
# @param policy [String] - A boolean expression that must evaluate to true for this warrant to apply. The expression can reference variables that are provided in the context attribute of access check requests. (optional)
|
23
23
|
#
|
24
24
|
# @return [Warrant] created warrant
|
25
25
|
#
|
@@ -30,7 +30,7 @@ module Warrant
|
|
30
30
|
# @raise [Warrant::NotFoundError]
|
31
31
|
# @raise [Warrant::UnauthorizedError]
|
32
32
|
# @raise [Warrant::WarrantError]
|
33
|
-
def self.create(object, relation, subject,
|
33
|
+
def self.create(object, relation, subject, policy = nil, options = {})
|
34
34
|
params = {
|
35
35
|
object_type: object.respond_to?(:warrant_object_type) ? object.warrant_object_type.to_s : object[:object_type],
|
36
36
|
object_id: object.respond_to?(:warrant_object_id) ? object.warrant_object_id.to_s : object[:object_id],
|
@@ -39,15 +39,58 @@ module Warrant
|
|
39
39
|
object_type: subject.respond_to?(:warrant_object_type) ? subject.warrant_object_type.to_s : subject[:object_type],
|
40
40
|
object_id: subject.respond_to?(:warrant_object_id) ? subject.warrant_object_id.to_s : subject[:object_id]
|
41
41
|
},
|
42
|
-
|
42
|
+
policy: policy
|
43
43
|
}
|
44
|
-
res = APIOperations.post(URI.parse("#{::Warrant.config.api_base}/
|
45
|
-
|
44
|
+
res = APIOperations.post(URI.parse("#{::Warrant.config.api_base}/v2/warrants"), params: Util.normalize_params(params), options: options)
|
45
|
+
|
46
|
+
case res
|
47
|
+
when Net::HTTPSuccess
|
48
|
+
res_json = JSON.parse(res.body, symbolize_names: true)
|
49
|
+
subject = Subject.new(res_json[:subject][:objectType], res_json[:subject][:objectId], res_json[:subject][:relation])
|
50
|
+
Warrant.new(res_json[:objectType], res_json[:objectId], res_json[:relation], subject, res_json[:policy], res['Warrant-Token'])
|
51
|
+
else
|
52
|
+
APIOperations.raise_error(res)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# Batch creates multiple warrants with given parameters
|
57
|
+
#
|
58
|
+
# @param [Array<Hash>] warrants Array of warrants to create.
|
59
|
+
# @option warrants [WarrantObject | Hash] :object The object to which the warrant will apply. Object can be a hash with object type and id or an instance of a class that implements the WarrantObject module and its methods (`warrant_object_type` and `warrant_object_id`). The object type must be one of your system's existing object type.
|
60
|
+
# @option warrants [String] :relation The relation for this object to subject association. The relation must be valid as per the object type definition.
|
61
|
+
# @option warrants [WarrantObject | Hash] :subject The subject for which the warrant will apply. Can be a hash with object type and id and an optional relation or an instance of a class that implements the WarrantObject module and its methods (`warrant_object_type` and `warrant_object_id`). The object type must be one of your system's existing object type.
|
62
|
+
# @option warrants [String] :policy A boolean expression that must evaluate to true for this warrant to apply. The expression can reference variables that are provided in the context attribute of access check requests. (optional)
|
63
|
+
#
|
64
|
+
# @return [Array<Warrant>] all created warrants
|
65
|
+
#
|
66
|
+
# @raise [Warrant::DuplicateRecordError]
|
67
|
+
# @raise [Warrant::InternalError]
|
68
|
+
# @raise [Warrant::InvalidParameterError]
|
69
|
+
# @raise [Warrant::InvalidRequestError]
|
70
|
+
# @raise [Warrant::NotFoundError]
|
71
|
+
# @raise [Warrant::UnauthorizedError]
|
72
|
+
def self.batch_create(warrants, options = {})
|
73
|
+
mapped_warrants = warrants.map{ |warrant|
|
74
|
+
{
|
75
|
+
object_type: warrant[:object].respond_to?(:warrant_object_type) ? warrant[:object].warrant_object_type.to_s : warrant[:object_type],
|
76
|
+
object_id: warrant[:object].respond_to?(:warrant_object_id) ? warrant[:object].warrant_object_id.to_s : warrant[:object_id],
|
77
|
+
relation: warrant[:relation],
|
78
|
+
subject: {
|
79
|
+
object_type: warrant[:subject].respond_to?(:warrant_object_type) ? warrant[:subject].warrant_object_type.to_s : warrant[:subject][:object_type],
|
80
|
+
object_id: warrant[:subject].respond_to?(:warrant_object_id) ? warrant[:subject].warrant_object_id.to_s : warrant[:subject][:object_id]
|
81
|
+
},
|
82
|
+
policy: warrant[:policy]
|
83
|
+
}
|
84
|
+
}
|
85
|
+
res = APIOperations.post(URI.parse("#{::Warrant.config.api_base}/v2/warrants"), params: Util.normalize_params(mapped_warrants), options: options)
|
46
86
|
|
47
87
|
case res
|
48
88
|
when Net::HTTPSuccess
|
49
|
-
|
50
|
-
|
89
|
+
res_json = JSON.parse(res.body, symbolize_names: true)
|
90
|
+
res_json.map{ |warrant|
|
91
|
+
subject = Subject.new(warrant[:subject][:objectType], warrant[:subject][:objectId], warrant[:subject][:relation])
|
92
|
+
Warrant.new(warrant[:objectType], warrant[:objectId], warrant[:relation], subject, warrant[:policy], res['Warrant-Token'])
|
93
|
+
}
|
51
94
|
else
|
52
95
|
APIOperations.raise_error(res)
|
53
96
|
end
|
@@ -55,10 +98,10 @@ module Warrant
|
|
55
98
|
|
56
99
|
# Deletes a warrant specified by the combination of object_type, object_id, relation, and subject.
|
57
100
|
#
|
58
|
-
# @param object [WarrantObject | Hash]
|
59
|
-
# @param relation [String] The relation
|
60
|
-
# @param subject [WarrantObject | Hash]
|
61
|
-
# @param
|
101
|
+
# @param object [WarrantObject | Hash] The object to which the warrant applies. Can be a hash with object type and id or an instance of a class that implements the WarrantObject module and its methods (`warrant_object_type` and `warrant_object_id`). The object type must be one of your system's existing object type.
|
102
|
+
# @param relation [String] The relation for this object to subject association. The relation must be valid as per the object type definition.
|
103
|
+
# @param subject [WarrantObject | Hash] The subject to for which the warrant applies. Can be a hash with object type and id and an optional relation or an instance of a class that implements the WarrantObject module and its methods (`warrant_object_type` and `warrant_object_id`). The object type must be one of your system's existing object type.
|
104
|
+
# @param policy [String] - A boolean expression that must evaluate to true for this warrant to apply. The expression can reference variables that are provided in the context attribute of access check requests. (optional)
|
62
105
|
#
|
63
106
|
# @return [nil] if delete was successful
|
64
107
|
#
|
@@ -67,7 +110,7 @@ module Warrant
|
|
67
110
|
# @raise [Warrant::NotFoundError]
|
68
111
|
# @raise [Warrant::UnauthorizedError]
|
69
112
|
# @raise [Warrant::WarrantError]
|
70
|
-
def self.delete(object, relation, subject,
|
113
|
+
def self.delete(object, relation, subject, policy = nil, options = {})
|
71
114
|
params = {
|
72
115
|
object_type: object.respond_to?(:warrant_object_type) ? object.warrant_object_type.to_s : object[:object_type],
|
73
116
|
object_id: object.respond_to?(:warrant_object_id) ? object.warrant_object_id.to_s : object[:object_id],
|
@@ -76,13 +119,89 @@ module Warrant
|
|
76
119
|
object_type: subject.respond_to?(:warrant_object_type) ? subject.warrant_object_type.to_s : subject[:object_type],
|
77
120
|
object_id: subject.respond_to?(:warrant_object_id) ? subject.warrant_object_id.to_s : subject[:object_id]
|
78
121
|
},
|
79
|
-
|
122
|
+
policy: policy
|
80
123
|
}
|
81
|
-
res = APIOperations.delete(URI.parse("#{::Warrant.config.api_base}/
|
124
|
+
res = APIOperations.delete(URI.parse("#{::Warrant.config.api_base}/v2/warrants"), params: Util.normalize_params(params), options: options)
|
82
125
|
|
83
126
|
case res
|
84
127
|
when Net::HTTPSuccess
|
85
|
-
return
|
128
|
+
return res['Warrant-Token']
|
129
|
+
else
|
130
|
+
APIOperations.raise_error(res)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
# Batch deletes multiple warrants with given parameters
|
135
|
+
#
|
136
|
+
# @param [Array<Hash>] warrants Array of warrants to delete.
|
137
|
+
# @option warrants [WarrantObject | Hash] :object The object to which the warrant applies. Can be a hash with object type and id or an instance of a class that implements the WarrantObject module and its methods (`warrant_object_type` and `warrant_object_id`). The object type must be one of your system's existing object type.
|
138
|
+
# @option warrants [String] :relation The relation for this object to subject association. The relation must be valid as per the object type definition.
|
139
|
+
# @option warrants [WarrantObject | Hash] :subject The subject for which the warrant applies. Can be a hash with object type and id and an optional relation or an instance of a class that implements the WarrantObject module and its methods (`warrant_object_type` and `warrant_object_id`). The object type must be one of your system's existing object type.
|
140
|
+
# @option warrants [String] :policy A boolean expression that must evaluate to true for this warrant to apply. The expression can reference variables that are provided in the context attribute of access check requests. (optional)
|
141
|
+
#
|
142
|
+
# @return [nil] if delete was successful
|
143
|
+
#
|
144
|
+
# @raise [Warrant::InternalError]
|
145
|
+
# @raise [Warrant::InvalidRequestError]
|
146
|
+
# @raise [Warrant::NotFoundError]
|
147
|
+
# @raise [Warrant::UnauthorizedError]
|
148
|
+
# @raise [Warrant::WarrantError]
|
149
|
+
def self.batch_delete(warrants, options = {})
|
150
|
+
mapped_warrants = warrants.map{ |warrant|
|
151
|
+
{
|
152
|
+
object_type: warrant[:object].respond_to?(:warrant_object_type) ? warrant[:object].warrant_object_type.to_s : warrant[:object_type],
|
153
|
+
object_id: warrant[:object].respond_to?(:warrant_object_id) ? warrant[:object].warrant_object_id.to_s : warrant[:object_id],
|
154
|
+
relation: warrant[:relation],
|
155
|
+
subject: {
|
156
|
+
object_type: warrant[:subject].respond_to?(:warrant_object_type) ? warrant[:subject].warrant_object_type.to_s : warrant[:subject][:object_type],
|
157
|
+
object_id: warrant[:subject].respond_to?(:warrant_object_id) ? warrant[:subject].warrant_object_id.to_s : warrant[:subject][:object_id]
|
158
|
+
},
|
159
|
+
policy: warrant[:policy]
|
160
|
+
}
|
161
|
+
}
|
162
|
+
res = APIOperations.delete(URI.parse("#{::Warrant.config.api_base}/v2/warrants"), params: Util.normalize_params(mapped_warrants), options: options)
|
163
|
+
|
164
|
+
case res
|
165
|
+
when Net::HTTPSuccess
|
166
|
+
return res['Warrant-Token']
|
167
|
+
else
|
168
|
+
APIOperations.raise_error(res)
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
# List all warrants for your organization and environment
|
173
|
+
#
|
174
|
+
# @param [Hash] filters Filters to apply to result set
|
175
|
+
# @param [Hash] options Options to apply on a per-request basis
|
176
|
+
# @option filters [String] :object_type _Required if object_id is provided._ Only return warrants whose object_type matches this value. (optional)
|
177
|
+
# @option filters [String] :object_id Only return warrants whose object_id matches this value. (optional)
|
178
|
+
# @option filters [String] :relation Only return warrants whose relation matches this value. (optional)
|
179
|
+
# @option filters [String] :subject_type Required if :subjectId is provided. Only return warrants with a subject whose objectType matches this value. (optional)
|
180
|
+
# @option filters [String] :subject_id Only return warrants with a subject whose object_id matches this value. (optional)
|
181
|
+
# @option filters [String] :subject_relation Only return warrants with a subject whose relation matches this value. (optional)
|
182
|
+
# @option filters [Integer] :limit A positive integer representing the maximum number of items to return in the response. Must be less than or equal to 1000. Defaults to 25. (optional)
|
183
|
+
# @option filters [String] :prev_cursor A cursor representing your place in a list of results. Requests containing prev_cursor will return the results immediately preceding the cursor. (optional)
|
184
|
+
# @option filters [String] :next_cursor A cursor representing your place in a list of results. Requests containing next_cursor will return the results immediately following the cursor. (optional)
|
185
|
+
# @option filters [String] :sort_by The column to sort the result by. Unless otherwise specified, all list endpoints are sorted by their unique identifier by default. Supported values for objects are +object_type+, +object_id+, and +created_at+ (optional)
|
186
|
+
# @option filters [String] :sort_order The order in which to sort the result by. Valid values are +ASC+ and +DESC+. Defaults to +ASC+. (optional)
|
187
|
+
# @option options [String] :warrant_token A valid warrant token from a previous write operation or latest. Used to specify desired consistency for this read operation. (optional)
|
188
|
+
#
|
189
|
+
# @return [Array<Warrant>] all permissions for your organization
|
190
|
+
#
|
191
|
+
# @raise [Warrant::InternalError]
|
192
|
+
# @raise [Warrant::InvalidParameterError]
|
193
|
+
# @raise [Warrant::UnauthorizedError]
|
194
|
+
def self.list(filters = {}, options = {})
|
195
|
+
res = APIOperations.get(URI.parse("#{::Warrant.config.api_base}/v2/warrants"), params: Util.normalize_params(filters), options: options)
|
196
|
+
|
197
|
+
case res
|
198
|
+
when Net::HTTPSuccess
|
199
|
+
list_result = JSON.parse(res.body, symbolize_names: true)
|
200
|
+
warrants = list_result[:results].map{ |warrant|
|
201
|
+
subject = Subject.new(warrant[:subject][:objectType], warrant[:subject][:objectId], warrant[:subject][:relation])
|
202
|
+
Warrant.new(warrant[:objectType], warrant[:objectId], warrant[:relation], subject, warrant[:policy])
|
203
|
+
}
|
204
|
+
return ListResponse.new(warrants, list_result[:prevCursor], list_result[:nextCursor])
|
86
205
|
else
|
87
206
|
APIOperations.raise_error(res)
|
88
207
|
end
|
@@ -90,7 +209,7 @@ module Warrant
|
|
90
209
|
|
91
210
|
# Query to find all warrants for a given object or subject.
|
92
211
|
#
|
93
|
-
# @param warrant_query [
|
212
|
+
# @param warrant_query [String] Query to run for a set of warrants.
|
94
213
|
# @option filters [Integer] :page A positive integer (starting with 1) representing the page of items to return in response. Used in conjunction with the limit param. (optional)
|
95
214
|
# @option filters [Integer] :limit A positive integer representing the max number of items to return in response. (optional)
|
96
215
|
#
|
@@ -101,54 +220,19 @@ module Warrant
|
|
101
220
|
# @raise [Warrant::MissingRequiredParameterError]
|
102
221
|
# @raise [Warrant::UnauthorizedError]
|
103
222
|
# @raise [Warrant::WarrantError]
|
104
|
-
def self.query(
|
105
|
-
|
223
|
+
def self.query(query, filters: {}, options: {})
|
224
|
+
params = filters.merge(q: query)
|
225
|
+
res = APIOperations.get(URI.parse("#{::Warrant.config.api_base}/v2/query"), params: Util.normalize_params(params), options: options)
|
106
226
|
|
107
227
|
case res
|
108
228
|
when Net::HTTPSuccess
|
109
|
-
|
110
|
-
|
111
|
-
subject = Subject.new(warrant[
|
112
|
-
Warrant.new(warrant[
|
229
|
+
query_response = JSON.parse(res.body, symbolize_names: true)
|
230
|
+
query_results = query_response[:results].map{ |result|
|
231
|
+
subject = Subject.new(result[:warrant][:subject][:objectType], result[:warrant][:subject][:objectId], result[:warrant][:subject][:relation])
|
232
|
+
warrant = Warrant.new(result[:warrant][:objectType], result[:warrant][:objectId], result[:warrant][:relation], subject, result[:warrant][:policy])
|
233
|
+
QueryResult.new(result[:objectType], result[:objectId], warrant, result[:isImplicit], result[:meta])
|
113
234
|
}
|
114
|
-
|
115
|
-
if query_result['meta']['feature']
|
116
|
-
query_result['meta']['feature'].each{ |featureId, feature|
|
117
|
-
query_result['meta']['feature'][featureId] = Feature.new(feature['featureId'])
|
118
|
-
}
|
119
|
-
end
|
120
|
-
|
121
|
-
if query_result['meta']['pricing-tier']
|
122
|
-
query_result['meta']['pricing-tier'].each{ |pricingTierId, pricingTier|
|
123
|
-
query_result['meta']['pricing-tier'][pricingTierId] = PricingTier.new(pricingTier['pricingTierId'])
|
124
|
-
}
|
125
|
-
end
|
126
|
-
|
127
|
-
if query_result['meta']['permission']
|
128
|
-
query_result['meta']['permission'].each{ |permissionId, permission|
|
129
|
-
query_result['meta']['permission'][permissionId] = Permission.new(permission['permissionId'], permission['name'], permission['description'])
|
130
|
-
}
|
131
|
-
end
|
132
|
-
|
133
|
-
if query_result['meta']['role']
|
134
|
-
query_result['meta']['role'].each{ |roleId, role|
|
135
|
-
query_result['meta']['role'][roleId] = Role.new(role['roleId'], role['name'], role['description'])
|
136
|
-
}
|
137
|
-
end
|
138
|
-
|
139
|
-
if query_result['meta']['user']
|
140
|
-
query_result['meta']['user'].each{ |userId, user|
|
141
|
-
query_result['meta']['user'][userId] = User.new(user['userId'], user['email'], user['createdAt'])
|
142
|
-
}
|
143
|
-
end
|
144
|
-
|
145
|
-
if query_result['meta']['tenant']
|
146
|
-
query_result['meta']['tenant'].each{ |tenantId, tenant|
|
147
|
-
query_result['meta']['tenant'][tenantId] = Tenant.new(tenant['tenantId'], tenant['name'], tenant['createdAt'])
|
148
|
-
}
|
149
|
-
end
|
150
|
-
|
151
|
-
query_result
|
235
|
+
return ListResponse.new(query_results, query_response[:prevCursor], query_response[:nextCursor])
|
152
236
|
else
|
153
237
|
APIOperations.raise_error(res)
|
154
238
|
end
|
@@ -166,8 +250,7 @@ module Warrant
|
|
166
250
|
# * object_type (String) - The type of object. Must be one of your system's existing object types.
|
167
251
|
# * object_id (String) - The id of the specific object.
|
168
252
|
# * relation (String) - The relation for this object to subject association. The relation must be valid as per the object type definition. (optional)
|
169
|
-
#
|
170
|
-
# @param consistent_read [Boolean] Boolean flag indicating whether or not to enforce strict consistency for this access check. Defaults to false. (optional)
|
253
|
+
# * context [Hash] - Object containing key-value pairs that specifies the context the warrant should be checked in. (optional)
|
171
254
|
# @param debug [Boolean] Boolean flag indicating whether or not to return debug information for this access check. Defaults to false. (optional)
|
172
255
|
#
|
173
256
|
# @return [Boolean] whether or not the given access check is authorized
|
@@ -188,12 +271,12 @@ module Warrant
|
|
188
271
|
# @raise [Warrant::InvalidParameterError]
|
189
272
|
# @raise [Warrant::NotFoundError]
|
190
273
|
# @raise [Warrant::UnauthorizedError]
|
191
|
-
def self.is_authorized?(params = {})
|
274
|
+
def self.is_authorized?(params = {}, options = {})
|
192
275
|
unless ::Warrant.config.authorize_endpoint.nil?
|
193
|
-
return edge_authorize?(params)
|
276
|
+
return edge_authorize?(params, options)
|
194
277
|
end
|
195
278
|
|
196
|
-
return authorize?(params)
|
279
|
+
return authorize?(params, options)
|
197
280
|
end
|
198
281
|
|
199
282
|
# Checks whether a specified access check is authorized or not.
|
@@ -201,8 +284,7 @@ module Warrant
|
|
201
284
|
# @param object [WarrantObject] Object to check in the access check. Object must include WarrantObject module and implements its methods (`warrant_object_type` and `warrant_object_id`). The object type must be one of your system's existing object type.
|
202
285
|
# @param relation [String] The relation to check for this object to subject association. The relation must be valid as per the object type definition.
|
203
286
|
# @param subject [WarrantObject] Subject to check in the access check. Subject must include WarrantObject module and implements its methods (`warrant_object_type` and `warrant_object_id`).
|
204
|
-
# @option options [Hash] :context Object containing key-value pairs that specifies the context the warrant should be checked in. (optional)
|
205
|
-
# @option options [Boolean] :consistent_read Boolean flag indicating whether or not to enforce strict consistency for this access check. Defaults to false. (optional)
|
287
|
+
# @option options [Hash] :context Object containing key-value pairs that specifies the context the warrant should be checked in. (optional)
|
206
288
|
# @option options [Boolean] :debug Boolean flag indicating whether or not to return debug information for this access check. Defaults to false. (optional)
|
207
289
|
#
|
208
290
|
# @return [Boolean] whether or not the given access check is authorized
|
@@ -217,13 +299,21 @@ module Warrant
|
|
217
299
|
# @raise [Warrant::NotFoundError]
|
218
300
|
# @raise [Warrant::UnauthorizedError]
|
219
301
|
def self.check(object, relation, subject, options = {})
|
220
|
-
if
|
302
|
+
if object.is_a?(WarrantObject)
|
303
|
+
object_type = object.warrant_object_type.to_s
|
304
|
+
object_id = object.warrant_object_id.to_s
|
305
|
+
else
|
306
|
+
object_type = object[:object_type]
|
307
|
+
object_id = object[:object_id]
|
308
|
+
end
|
309
|
+
|
310
|
+
if subject.is_a?(Subject)
|
221
311
|
subject = {
|
222
312
|
object_type: subject.object_type,
|
223
313
|
object_id: subject.object_id,
|
224
314
|
relation: subject.relation
|
225
315
|
}.compact!
|
226
|
-
|
316
|
+
elsif subject.is_a?(WarrantObject)
|
227
317
|
subject = {
|
228
318
|
object_type: subject.warrant_object_type.to_s,
|
229
319
|
object_id: subject.warrant_object_id.to_s
|
@@ -233,26 +323,24 @@ module Warrant
|
|
233
323
|
unless ::Warrant.config.authorize_endpoint.nil?
|
234
324
|
return edge_authorize?(
|
235
325
|
warrants: [{
|
236
|
-
object_type:
|
237
|
-
object_id:
|
326
|
+
object_type: object_type,
|
327
|
+
object_id: object_id,
|
238
328
|
relation: relation,
|
239
329
|
subject: subject,
|
240
330
|
context: options[:context]
|
241
331
|
}],
|
242
|
-
consistent_read: options[:consistent_read],
|
243
332
|
debug: options[:debug]
|
244
333
|
)
|
245
334
|
end
|
246
335
|
|
247
336
|
return authorize?(
|
248
337
|
warrants: [{
|
249
|
-
object_type:
|
250
|
-
object_id:
|
338
|
+
object_type: object_type,
|
339
|
+
object_id: object_id,
|
251
340
|
relation: relation,
|
252
341
|
subject: subject,
|
253
342
|
context: options[:context]
|
254
343
|
}],
|
255
|
-
consistent_read: options[:consistent_read],
|
256
344
|
debug: options[:debug]
|
257
345
|
)
|
258
346
|
end
|
@@ -265,7 +353,6 @@ module Warrant
|
|
265
353
|
# * relation (String) - The relation to check for this object to subject association. The relation must be valid as per the object type definition.
|
266
354
|
# * subject (WarrantObject) Subject to check in the access check. Subject must include WarrantObject module and implements its methods (`warrant_object_type` and `warrant_object_id`).
|
267
355
|
# @option options [Hash] :context Object containing key-value pairs that specifies the context the warrant should be checked in. (optional)
|
268
|
-
# @option options [Boolean] :consistent_read Boolean flag indicating whether or not to enforce strict consistency for this access check. Defaults to false. (optional)
|
269
356
|
# @option options [Boolean] :debug Boolean flag indicating whether or not to return debug information for this access check. Defaults to false. (optional)
|
270
357
|
#
|
271
358
|
# @return [Boolean] whether or not the given access check is authorized
|
@@ -288,22 +375,32 @@ module Warrant
|
|
288
375
|
# @raise [Warrant::UnauthorizedError]
|
289
376
|
def self.check_many(op, warrants, options = {})
|
290
377
|
normalized_warrants = warrants.map do |warrant|
|
291
|
-
if warrant[:
|
378
|
+
if warrant[:object].is_a?(WarrantObject)
|
379
|
+
object_type = warrant[:object].warrant_object_type.to_s
|
380
|
+
object_id = warrant[:object].warrant_object_id.to_s
|
381
|
+
else
|
382
|
+
object_type = warrant[:object][:object_type]
|
383
|
+
object_id = warrant[:object][:object_id]
|
384
|
+
end
|
385
|
+
|
386
|
+
if warrant[:subject].is_a?(Subject)
|
292
387
|
subject = {
|
293
388
|
object_type: warrant[:subject].object_type,
|
294
389
|
object_id: warrant[:subject].object_id,
|
295
390
|
relation: warrant[:subject].relation
|
296
391
|
}.compact!
|
297
|
-
|
392
|
+
elsif warrant[:subject].is_a?(WarrantObject)
|
298
393
|
subject = {
|
299
394
|
object_type: warrant[:subject].warrant_object_type.to_s,
|
300
395
|
object_id: warrant[:subject].warrant_object_id.to_s
|
301
396
|
}
|
397
|
+
else
|
398
|
+
subject = warrant[:subject]
|
302
399
|
end
|
303
400
|
|
304
401
|
{
|
305
|
-
object_type:
|
306
|
-
object_id:
|
402
|
+
object_type: object_type,
|
403
|
+
object_id: object_id,
|
307
404
|
relation: warrant[:relation],
|
308
405
|
subject: subject,
|
309
406
|
context: warrant[:context]
|
@@ -311,20 +408,18 @@ module Warrant
|
|
311
408
|
end
|
312
409
|
|
313
410
|
unless ::Warrant.config.authorize_endpoint.nil?
|
314
|
-
return edge_authorize?(
|
411
|
+
return edge_authorize?({
|
315
412
|
op: op,
|
316
413
|
warrants: normalized_warrants,
|
317
|
-
consistent_read: options[:consistent_read],
|
318
414
|
debug: options[:debug]
|
319
|
-
)
|
415
|
+
}, options)
|
320
416
|
end
|
321
417
|
|
322
|
-
return authorize?(
|
418
|
+
return authorize?({
|
323
419
|
op: op,
|
324
420
|
warrants: normalized_warrants,
|
325
|
-
consistent_read: options[:consistent_read],
|
326
421
|
debug: options[:debug]
|
327
|
-
)
|
422
|
+
}, options)
|
328
423
|
end
|
329
424
|
|
330
425
|
# Checks whether a given user has a given permission.
|
@@ -332,7 +427,6 @@ module Warrant
|
|
332
427
|
# @param user_id [String] Id of the user to check
|
333
428
|
# @param permission_id [String] Id of the permission to check on the user
|
334
429
|
# @param context [Hash] - Object containing key-value pairs that specifies the context the warrant should be checked in. (optional)
|
335
|
-
# @param consistentRead [Boolean] Boolean flag indicating whether or not to enforce strict consistency for this access check. Defaults to false. (optional)
|
336
430
|
# @param debug [Boolean] Boolean flag indicating whether or not to return debug information for this access check. Defaults to false. (optional)
|
337
431
|
#
|
338
432
|
# @return [Boolean] whether or not the user has the given permission
|
@@ -341,21 +435,20 @@ module Warrant
|
|
341
435
|
# @raise [Warrant::InvalidParameterError]
|
342
436
|
# @raise [Warrant::NotFoundError]
|
343
437
|
# @raise [Warrant::UnauthorizedError]
|
344
|
-
def self.user_has_permission?(params = {})
|
345
|
-
return is_authorized?(
|
438
|
+
def self.user_has_permission?(params = {}, options = {})
|
439
|
+
return is_authorized?({
|
346
440
|
warrants: [{
|
347
441
|
object_type: Permission::OBJECT_TYPE,
|
348
442
|
object_id: params[:permission_id],
|
349
|
-
relation:
|
443
|
+
relation: params[:relation],
|
350
444
|
subject: {
|
351
445
|
object_type: User::OBJECT_TYPE,
|
352
446
|
object_id: params[:user_id]
|
353
447
|
},
|
354
448
|
context: params[:context]
|
355
449
|
}],
|
356
|
-
consistentRead: params[:consistentRead],
|
357
450
|
debug: params[:debug]
|
358
|
-
)
|
451
|
+
}, options)
|
359
452
|
end
|
360
453
|
|
361
454
|
# Checks whether a given subject has a given feature.
|
@@ -365,7 +458,6 @@ module Warrant
|
|
365
458
|
# * object_id (String) - The id of the specific object.
|
366
459
|
# @param feature_id [String] Id of the feature to check on the subject
|
367
460
|
# @param context [Hash] - Object containing key-value pairs that specifies the context the warrant should be checked in. (optional)
|
368
|
-
# @param consistent_read [Boolean] Boolean flag indicating whether or not to enforce strict consistency for this access check. Defaults to false. (optional)
|
369
461
|
# @param debug [Boolean] Boolean flag indicating whether or not to return debug information for this access check. Defaults to false. (optional)
|
370
462
|
#
|
371
463
|
# @return [Boolean] whether or not the user has the given permission
|
@@ -374,27 +466,26 @@ module Warrant
|
|
374
466
|
# @raise [Warrant::InvalidParameterError]
|
375
467
|
# @raise [Warrant::NotFoundError]
|
376
468
|
# @raise [Warrant::UnauthorizedError]
|
377
|
-
def self.has_feature?(params = {})
|
378
|
-
return is_authorized?(
|
469
|
+
def self.has_feature?(params = {}, options = {})
|
470
|
+
return is_authorized?({
|
379
471
|
warrants: [{
|
380
472
|
object_type: Feature::OBJECT_TYPE,
|
381
473
|
object_id: params[:feature_id],
|
382
|
-
relation:
|
474
|
+
relation: params[:relation],
|
383
475
|
subject: {
|
384
476
|
object_type: params[:subject][:object_type],
|
385
477
|
object_id: params[:subject][:object_id]
|
386
478
|
},
|
387
479
|
context: params[:context]
|
388
480
|
}],
|
389
|
-
consistent_read: params[:consistent_read],
|
390
481
|
debug: params[:debug]
|
391
|
-
)
|
482
|
+
}, options)
|
392
483
|
end
|
393
484
|
|
394
485
|
private
|
395
486
|
|
396
|
-
def self.authorize?(params = {})
|
397
|
-
res = APIOperations.post(URI.parse("#{::Warrant.config.api_base}/v2/
|
487
|
+
def self.authorize?(params = {}, options = {})
|
488
|
+
res = APIOperations.post(URI.parse("#{::Warrant.config.api_base}/v2/check"), params: Util.normalize_params(params), options: options)
|
398
489
|
res_json = JSON.parse(res.body)
|
399
490
|
|
400
491
|
case res
|
@@ -411,9 +502,9 @@ module Warrant
|
|
411
502
|
end
|
412
503
|
end
|
413
504
|
|
414
|
-
def self.edge_authorize?(params = {})
|
415
|
-
request_url = URI.parse("#{::Warrant.config.authorize_endpoint}/v2/
|
416
|
-
res = APIOperations.post(request_url, Util.normalize_params(params))
|
505
|
+
def self.edge_authorize?(params = {}, options = {})
|
506
|
+
request_url = URI.parse("#{::Warrant.config.authorize_endpoint}/v2/check")
|
507
|
+
res = APIOperations.post(request_url, params: Util.normalize_params(params), options: options)
|
417
508
|
res_json = JSON.parse(res.body)
|
418
509
|
|
419
510
|
case res
|
data/lib/warrant/util.rb
CHANGED
@@ -21,6 +21,12 @@ module Warrant
|
|
21
21
|
case params
|
22
22
|
when Hash
|
23
23
|
params.each_with_object({}) do |(k, v), new_opts|
|
24
|
+
# Leave context hash as-is to allow for any naming convention (snake_case vs camelCase)
|
25
|
+
if k.to_s == "context"
|
26
|
+
new_opts[k] = v
|
27
|
+
next
|
28
|
+
end
|
29
|
+
|
24
30
|
new_key = Util.camelcase(k.to_s)
|
25
31
|
|
26
32
|
case v
|
data/lib/warrant/version.rb
CHANGED
data/lib/warrant.rb
CHANGED
@@ -11,6 +11,9 @@ require "warrant/warrant_query"
|
|
11
11
|
|
12
12
|
require "warrant/api_operations"
|
13
13
|
require "warrant/errors"
|
14
|
+
require "warrant/models/list_response"
|
15
|
+
require "warrant/models/query_result"
|
16
|
+
require "warrant/models/object"
|
14
17
|
require "warrant/models/feature"
|
15
18
|
require "warrant/models/permission"
|
16
19
|
require "warrant/models/pricing_tier"
|
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:
|
4
|
+
version: 5.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Warrant
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-11-16 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
|
@@ -29,8 +29,11 @@ files:
|
|
29
29
|
- lib/warrant/api_operations.rb
|
30
30
|
- lib/warrant/errors.rb
|
31
31
|
- lib/warrant/models/feature.rb
|
32
|
+
- lib/warrant/models/list_response.rb
|
33
|
+
- lib/warrant/models/object.rb
|
32
34
|
- lib/warrant/models/permission.rb
|
33
35
|
- lib/warrant/models/pricing_tier.rb
|
36
|
+
- lib/warrant/models/query_result.rb
|
34
37
|
- lib/warrant/models/role.rb
|
35
38
|
- lib/warrant/models/session.rb
|
36
39
|
- lib/warrant/models/subject.rb
|