fusionauth_client 1.0.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.ruby-version +1 -0
- data/Gemfile +9 -0
- data/LICENSE.txt +201 -0
- data/README.md +64 -0
- data/Rakefile +10 -0
- data/build.savant +84 -0
- data/fusionauth-ruby-client.iml +15 -0
- data/fusionauth_client.gemspec +34 -0
- data/lib/fusionauth/fusionauth_client.rb +1816 -0
- data/lib/fusionauth/rest_client.rb +330 -0
- metadata +59 -0
@@ -0,0 +1,330 @@
|
|
1
|
+
require 'base64'
|
2
|
+
require 'json'
|
3
|
+
require 'net/http'
|
4
|
+
|
5
|
+
module FusionAuth
|
6
|
+
class RESTClient
|
7
|
+
def initialize
|
8
|
+
@url = ''.dup
|
9
|
+
@parameters = {}
|
10
|
+
@proxy = {}
|
11
|
+
@headers = {}
|
12
|
+
@body_handler = nil
|
13
|
+
@certificate = nil
|
14
|
+
@connect_timeout = 1000
|
15
|
+
@read_timeout = 2000
|
16
|
+
@error_response_handler = nil
|
17
|
+
@error_type = nil
|
18
|
+
@method = nil
|
19
|
+
@success_response_handler = nil
|
20
|
+
end
|
21
|
+
|
22
|
+
def authorization(authorization)
|
23
|
+
@headers['Authorization'] = authorization
|
24
|
+
self
|
25
|
+
end
|
26
|
+
|
27
|
+
def basic_authorization(username, password)
|
28
|
+
if username != nil && password != nil
|
29
|
+
credentials = username + ':' + password
|
30
|
+
|
31
|
+
encoded = Base64.encode64(credentials)
|
32
|
+
@headers['Authorization'] = "Basic #{encoded}"
|
33
|
+
end
|
34
|
+
|
35
|
+
self
|
36
|
+
end
|
37
|
+
|
38
|
+
#
|
39
|
+
# Sets the BodyHandler for this RESTClient
|
40
|
+
#
|
41
|
+
# @param body_handler [BodyHandler] The body handler.
|
42
|
+
# @return [RESTClient] this
|
43
|
+
#
|
44
|
+
def body_handler(body_handler)
|
45
|
+
@body_handler = body_handler
|
46
|
+
self
|
47
|
+
end
|
48
|
+
|
49
|
+
def certificate(certificate)
|
50
|
+
@certificate = certificate
|
51
|
+
self
|
52
|
+
end
|
53
|
+
|
54
|
+
def connect_timeout(connect_timeout)
|
55
|
+
@connect_timeout = connect_timeout
|
56
|
+
self
|
57
|
+
end
|
58
|
+
|
59
|
+
def read_timeout(read_timeout)
|
60
|
+
@read_timeout = read_timeout
|
61
|
+
self
|
62
|
+
end
|
63
|
+
|
64
|
+
def delete
|
65
|
+
@method = 'DELETE'
|
66
|
+
self
|
67
|
+
end
|
68
|
+
|
69
|
+
def error_response_handler(error_response_handler)
|
70
|
+
@error_response_handler = error_response_handler
|
71
|
+
self
|
72
|
+
end
|
73
|
+
|
74
|
+
def get
|
75
|
+
@method = 'GET'
|
76
|
+
self
|
77
|
+
end
|
78
|
+
|
79
|
+
def go
|
80
|
+
if @url.size == 0
|
81
|
+
raise ArgumentError, 'You must specify a URL'
|
82
|
+
end
|
83
|
+
|
84
|
+
if @method == nil
|
85
|
+
raise ArgumentError, 'You must specify a HTTP method'
|
86
|
+
end
|
87
|
+
|
88
|
+
response = ClientResponse.new
|
89
|
+
response.request = (@body_handler != nil) ? @body_handler.body_object : nil
|
90
|
+
response.method = @method
|
91
|
+
|
92
|
+
begin
|
93
|
+
if @parameters.length > 0
|
94
|
+
if @url.index('?') == nil
|
95
|
+
@url<<'?'
|
96
|
+
end
|
97
|
+
|
98
|
+
list = []
|
99
|
+
@parameters.each { |key, values|
|
100
|
+
values.each { |value|
|
101
|
+
list<<"#{URI.encode_www_form_component(key)}=#{URI.encode_www_form_component(value)}"
|
102
|
+
}
|
103
|
+
}
|
104
|
+
|
105
|
+
@url<<list.join('&')
|
106
|
+
end
|
107
|
+
|
108
|
+
response.url = URI(@url)
|
109
|
+
opts = {:p_addr => @proxy[:p_addr], :p_port => @proxy[:p_port], :p_user => @proxy[:p_user], :p_pass => @proxy[:p_pass], :open_timeout => @connect_timeout, :read_timeout => @read_timeout}
|
110
|
+
if @certificate != nil
|
111
|
+
opts[:cert] = @certificate
|
112
|
+
end
|
113
|
+
if response.url.scheme == 'https'
|
114
|
+
opts[:use_ssl] = true
|
115
|
+
end
|
116
|
+
if @body_handler != nil
|
117
|
+
@body_handler.set_headers(@headers)
|
118
|
+
end
|
119
|
+
|
120
|
+
http_response = nil
|
121
|
+
Net::HTTP.start(response.url.hostname, response.url.port, opts) { |http|
|
122
|
+
request = nil
|
123
|
+
if @method == 'COPY'
|
124
|
+
request = Net::HTTP::Copy.new(response.url, @headers)
|
125
|
+
elsif @method == 'DELETE'
|
126
|
+
request = Net::HTTP::Delete.new(response.url, @headers)
|
127
|
+
elsif @method == 'GET'
|
128
|
+
request = Net::HTTP::Get.new(response.url, @headers)
|
129
|
+
elsif @method == 'HEAD'
|
130
|
+
request = Net::HTTP::Head.new(response.url, @headers)
|
131
|
+
elsif @method == 'LOCK'
|
132
|
+
request = Net::HTTP::Lock.new(response.url, @headers)
|
133
|
+
elsif @method == 'MKCOL'
|
134
|
+
request = Net::HTTP::Mkcol.new(response.url, @headers)
|
135
|
+
elsif @method == 'MOVE'
|
136
|
+
request = Net::HTTP::Move.new(response.url, @headers)
|
137
|
+
elsif @method == 'OPTIONS'
|
138
|
+
request = Net::HTTP::Options.new(response.url, @headers)
|
139
|
+
elsif @method == 'PATCH'
|
140
|
+
request = Net::HTTP::Patch.new(response.url, @headers)
|
141
|
+
elsif @method == 'POST'
|
142
|
+
request = Net::HTTP::Post.new(response.url, @headers)
|
143
|
+
elsif @method == 'PROPFIND'
|
144
|
+
request = Net::HTTP::Propfind.new(response.url, @headers)
|
145
|
+
elsif @method == 'PROPPATCH'
|
146
|
+
request = Net::HTTP::Proppatch.new(response.url, @headers)
|
147
|
+
elsif @method == 'PUT'
|
148
|
+
request = Net::HTTP::Put.new(response.url, @headers)
|
149
|
+
elsif @method == 'TRACE'
|
150
|
+
request = Net::HTTP::Trace.new(response.url, @headers)
|
151
|
+
elsif @method == 'UNLOCK'
|
152
|
+
request = Net::HTTP::Unlock.new(response.url, @headers)
|
153
|
+
else
|
154
|
+
raise ArgumentError, "Invalid HTTP method #{@method}"
|
155
|
+
end
|
156
|
+
|
157
|
+
request.body = response.request
|
158
|
+
http_response = http.request(request)
|
159
|
+
}
|
160
|
+
|
161
|
+
response.status = http_response.code.to_i
|
162
|
+
if response.status < 200 || response.status > 299
|
163
|
+
if http_response.class.body_permitted? && !http_response.body.nil? && http_response.body.size > 0 && @error_response_handler != nil
|
164
|
+
response.error_response = @error_response_handler.call(http_response.body)
|
165
|
+
end
|
166
|
+
elsif http_response.class.body_permitted? && !http_response.body.nil? && http_response.body.size > 0 && @success_response_handler != nil
|
167
|
+
response.success_response = @success_response_handler.call(http_response.body)
|
168
|
+
end
|
169
|
+
rescue Exception => e
|
170
|
+
response.status = -1
|
171
|
+
response.exception = e
|
172
|
+
# e.backtrace.each {|l| p l}
|
173
|
+
end
|
174
|
+
|
175
|
+
response
|
176
|
+
end
|
177
|
+
|
178
|
+
def header(name, value)
|
179
|
+
@headers[name] = value
|
180
|
+
self
|
181
|
+
end
|
182
|
+
|
183
|
+
def headers(headers)
|
184
|
+
@headers.merge!(headers)
|
185
|
+
self
|
186
|
+
end
|
187
|
+
|
188
|
+
def post
|
189
|
+
@method = 'POST'
|
190
|
+
self
|
191
|
+
end
|
192
|
+
|
193
|
+
def put
|
194
|
+
@method = 'PUT'
|
195
|
+
self
|
196
|
+
end
|
197
|
+
|
198
|
+
def success_response_handler(success_response_handler)
|
199
|
+
@success_response_handler = success_response_handler
|
200
|
+
self
|
201
|
+
end
|
202
|
+
|
203
|
+
def uri(uri)
|
204
|
+
if @url.size == 0
|
205
|
+
self
|
206
|
+
end
|
207
|
+
|
208
|
+
if @url[@url.size - 1] == '/' && uri[0] == '/'
|
209
|
+
@url<<uri[1..uri.size]
|
210
|
+
elsif @url[@url.size - 1] != '/' && uri[0] != '/'
|
211
|
+
@url<<"/#{uri}"
|
212
|
+
else
|
213
|
+
@url<<uri
|
214
|
+
end
|
215
|
+
|
216
|
+
self
|
217
|
+
end
|
218
|
+
|
219
|
+
def url(url)
|
220
|
+
@url = url.dup
|
221
|
+
self
|
222
|
+
end
|
223
|
+
|
224
|
+
#
|
225
|
+
# Add a URL parameter as a key value pair.
|
226
|
+
#
|
227
|
+
# @param name [String] The URL parameter name.
|
228
|
+
# @param value [String} ]The url parameter value. The <code>.toString()</ code> method will be used to
|
229
|
+
# get the <code>String</code> used in the URL parameter. If the object type is a
|
230
|
+
# @link Collection} a key value pair will be added for each value in the collection.
|
231
|
+
# @link ZonedDateTime} will also be handled uniquely in that the <code>long</ code> will
|
232
|
+
# be used to set in the request using <code>ZonedDateTime.toInstant().toEpochMilli()</code>
|
233
|
+
# @return This.
|
234
|
+
#
|
235
|
+
def url_parameter(name, value)
|
236
|
+
if value == nil
|
237
|
+
return self
|
238
|
+
end
|
239
|
+
|
240
|
+
if value.is_a? Array
|
241
|
+
@parameters[name] = value
|
242
|
+
else
|
243
|
+
values = @parameters[name]
|
244
|
+
if values == nil
|
245
|
+
values = []
|
246
|
+
@parameters[name] = values
|
247
|
+
end
|
248
|
+
values<<value
|
249
|
+
end
|
250
|
+
|
251
|
+
self
|
252
|
+
end
|
253
|
+
|
254
|
+
#
|
255
|
+
# Append a url path segment. <p>
|
256
|
+
# For Example: <pre>
|
257
|
+
# .url("http://www.foo.com ")
|
258
|
+
# .urlSegment(" bar ")
|
259
|
+
# </pre>
|
260
|
+
# This will result in a url of <code>http://www.foo.com/bar</code>
|
261
|
+
#
|
262
|
+
# @param value The url path segment. A nil value will be ignored.
|
263
|
+
# @return This.
|
264
|
+
#/
|
265
|
+
def url_segment(value)
|
266
|
+
if value == nil
|
267
|
+
return self
|
268
|
+
end
|
269
|
+
|
270
|
+
if @url[@url.size - 1] != '/'
|
271
|
+
@url<<'/'
|
272
|
+
end
|
273
|
+
|
274
|
+
@url<<value
|
275
|
+
self
|
276
|
+
end
|
277
|
+
|
278
|
+
private
|
279
|
+
def to_http_uri(uri)
|
280
|
+
uri.path + (uri.query == nil ? '' : "?#{uri.query}")
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
284
|
+
class ClientResponse
|
285
|
+
attr_accessor :url, :request, :method, :status, :success_response, :error_response, :exception
|
286
|
+
|
287
|
+
def was_successful
|
288
|
+
@status >= 200 && @status <= 299
|
289
|
+
end
|
290
|
+
end
|
291
|
+
|
292
|
+
class JSONBodyHandler
|
293
|
+
attr_accessor :length, :body
|
294
|
+
|
295
|
+
def initialize(body_object)
|
296
|
+
@body = JSON.generate(body_object)
|
297
|
+
end
|
298
|
+
|
299
|
+
#
|
300
|
+
# Returns the body String for the request
|
301
|
+
#
|
302
|
+
# @return [String] The body as a String
|
303
|
+
def body_object
|
304
|
+
@body
|
305
|
+
end
|
306
|
+
|
307
|
+
#
|
308
|
+
# Sets any headers necessary for the body to be processed.
|
309
|
+
#
|
310
|
+
# @param headers [Hash] The headers hash to add any headers needed by this BodyHandler
|
311
|
+
# @return [Object] The object
|
312
|
+
def set_headers(headers)
|
313
|
+
headers['Length'] = body.bytesize.to_s
|
314
|
+
headers['Content-Type'] = 'application/json'
|
315
|
+
nil
|
316
|
+
end
|
317
|
+
end
|
318
|
+
|
319
|
+
class JSONResponseHandler
|
320
|
+
attr_accessor :type
|
321
|
+
|
322
|
+
def initialize(type)
|
323
|
+
@type = type
|
324
|
+
end
|
325
|
+
|
326
|
+
def call(body)
|
327
|
+
JSON.parse(body, :object_class => @type)
|
328
|
+
end
|
329
|
+
end
|
330
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fusionauth_client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.11
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brian Pontarelli
|
8
|
+
- Daniel DeGroff
|
9
|
+
autorequire:
|
10
|
+
bindir: exe
|
11
|
+
cert_chain: []
|
12
|
+
date: 2018-09-10 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: This library contains the Ruby client library that helps you connect
|
15
|
+
your application to FusionAuth.
|
16
|
+
email:
|
17
|
+
- brian@fusionauth.io
|
18
|
+
- daniel@fusionauth.io
|
19
|
+
executables: []
|
20
|
+
extensions: []
|
21
|
+
extra_rdoc_files: []
|
22
|
+
files:
|
23
|
+
- ".gitignore"
|
24
|
+
- ".ruby-version"
|
25
|
+
- Gemfile
|
26
|
+
- LICENSE.txt
|
27
|
+
- README.md
|
28
|
+
- Rakefile
|
29
|
+
- build.savant
|
30
|
+
- fusionauth-ruby-client.iml
|
31
|
+
- fusionauth_client.gemspec
|
32
|
+
- lib/fusionauth/fusionauth_client.rb
|
33
|
+
- lib/fusionauth/rest_client.rb
|
34
|
+
homepage: https://github.com/FusionAuth/fusionauth-ruby-client
|
35
|
+
licenses:
|
36
|
+
- Apache-2.0
|
37
|
+
metadata:
|
38
|
+
allowed_push_host: https://rubygems.org
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 2.5.1
|
56
|
+
signing_key:
|
57
|
+
specification_version: 4
|
58
|
+
summary: The Ruby client library for FusionAuth
|
59
|
+
test_files: []
|