docusign_webforms 1.0.2.rc12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +34 -0
  3. data/Gemfile +7 -0
  4. data/README.md +131 -0
  5. data/Rakefile +8 -0
  6. data/docusign_webforms.gemspec +48 -0
  7. data/lib/docusign_webforms/api/form_instance_management_api.rb +272 -0
  8. data/lib/docusign_webforms/api/form_management_api.rb +192 -0
  9. data/lib/docusign_webforms/client/api_client.rb +600 -0
  10. data/lib/docusign_webforms/client/api_error.rb +37 -0
  11. data/lib/docusign_webforms/client/auth/oauth.rb +1061 -0
  12. data/lib/docusign_webforms/configuration.rb +203 -0
  13. data/lib/docusign_webforms/models/account_id.rb +175 -0
  14. data/lib/docusign_webforms/models/assertion_id.rb +175 -0
  15. data/lib/docusign_webforms/models/authentication_instant.rb +175 -0
  16. data/lib/docusign_webforms/models/authentication_method.rb +175 -0
  17. data/lib/docusign_webforms/models/brand_id.rb +175 -0
  18. data/lib/docusign_webforms/models/client_user_id.rb +175 -0
  19. data/lib/docusign_webforms/models/component_key.rb +174 -0
  20. data/lib/docusign_webforms/models/count.rb +175 -0
  21. data/lib/docusign_webforms/models/create_instance_request_body.rb +264 -0
  22. data/lib/docusign_webforms/models/created_date_time.rb +175 -0
  23. data/lib/docusign_webforms/models/date_time.rb +174 -0
  24. data/lib/docusign_webforms/models/envelope_id.rb +175 -0
  25. data/lib/docusign_webforms/models/expiration_date_time.rb +175 -0
  26. data/lib/docusign_webforms/models/expiration_offset.rb +175 -0
  27. data/lib/docusign_webforms/models/form_sort_by.rb +175 -0
  28. data/lib/docusign_webforms/models/form_url.rb +175 -0
  29. data/lib/docusign_webforms/models/guid.rb +174 -0
  30. data/lib/docusign_webforms/models/http_error.rb +195 -0
  31. data/lib/docusign_webforms/models/http_success.rb +184 -0
  32. data/lib/docusign_webforms/models/instance_id.rb +175 -0
  33. data/lib/docusign_webforms/models/instance_source.rb +31 -0
  34. data/lib/docusign_webforms/models/instance_status.rb +31 -0
  35. data/lib/docusign_webforms/models/instance_token.rb +175 -0
  36. data/lib/docusign_webforms/models/is_private_access.rb +175 -0
  37. data/lib/docusign_webforms/models/is_published.rb +175 -0
  38. data/lib/docusign_webforms/models/is_standalone.rb +175 -0
  39. data/lib/docusign_webforms/models/last_modified_date_time.rb +175 -0
  40. data/lib/docusign_webforms/models/return_url.rb +175 -0
  41. data/lib/docusign_webforms/models/search_text.rb +175 -0
  42. data/lib/docusign_webforms/models/security_domain.rb +175 -0
  43. data/lib/docusign_webforms/models/source.rb +31 -0
  44. data/lib/docusign_webforms/models/start_position.rb +175 -0
  45. data/lib/docusign_webforms/models/tags.rb +175 -0
  46. data/lib/docusign_webforms/models/template_properties.rb +217 -0
  47. data/lib/docusign_webforms/models/token_expiration_date_time.rb +175 -0
  48. data/lib/docusign_webforms/models/update_instance_request_body.rb +184 -0
  49. data/lib/docusign_webforms/models/user_filter.rb +30 -0
  50. data/lib/docusign_webforms/models/user_id.rb +175 -0
  51. data/lib/docusign_webforms/models/web_form.rb +268 -0
  52. data/lib/docusign_webforms/models/web_form_adm_type.rb +39 -0
  53. data/lib/docusign_webforms/models/web_form_component.rb +248 -0
  54. data/lib/docusign_webforms/models/web_form_component_type.rb +34 -0
  55. data/lib/docusign_webforms/models/web_form_components_map.rb +175 -0
  56. data/lib/docusign_webforms/models/web_form_content.rb +217 -0
  57. data/lib/docusign_webforms/models/web_form_id.rb +175 -0
  58. data/lib/docusign_webforms/models/web_form_instance.rb +295 -0
  59. data/lib/docusign_webforms/models/web_form_instance_envelopes.rb +183 -0
  60. data/lib/docusign_webforms/models/web_form_instance_list.rb +187 -0
  61. data/lib/docusign_webforms/models/web_form_instance_metadata.rb +237 -0
  62. data/lib/docusign_webforms/models/web_form_metadata.rb +377 -0
  63. data/lib/docusign_webforms/models/web_form_name.rb +175 -0
  64. data/lib/docusign_webforms/models/web_form_properties.rb +193 -0
  65. data/lib/docusign_webforms/models/web_form_published_names.rb +175 -0
  66. data/lib/docusign_webforms/models/web_form_source.rb +29 -0
  67. data/lib/docusign_webforms/models/web_form_state.rb +29 -0
  68. data/lib/docusign_webforms/models/web_form_summary.rb +250 -0
  69. data/lib/docusign_webforms/models/web_form_summary_list.rb +247 -0
  70. data/lib/docusign_webforms/models/web_form_user_info.rb +209 -0
  71. data/lib/docusign_webforms/models/web_form_values.rb +175 -0
  72. data/lib/docusign_webforms/models/web_form_version_id.rb +175 -0
  73. data/lib/docusign_webforms/version.rb +14 -0
  74. data/lib/docusign_webforms.rb +64 -0
  75. data/runLinter.sh +1 -0
  76. data/tests/Gemfile +5 -0
  77. data/tests/spec/unit_tests_using_jwt_spec.rb +38 -0
  78. metadata +382 -0
@@ -0,0 +1,600 @@
1
+ =begin
2
+ #Web Forms API version 1.1
3
+
4
+ #The Web Forms API facilitates generating semantic HTML forms around everyday contracts.
5
+
6
+ OpenAPI spec version: 1.1.0
7
+ Contact: devcenter@docusign.com
8
+ Generated by: https://github.com/swagger-api/swagger-codegen.git
9
+
10
+ =end
11
+
12
+ require 'date'
13
+ require 'json'
14
+ require 'logger'
15
+ require 'tempfile'
16
+ require 'typhoeus'
17
+ require 'uri'
18
+ require 'jwt'
19
+ require 'addressable/uri'
20
+
21
+ module DocuSign_WebForms
22
+ class ApiClient
23
+ # The Configuration object holding settings to be used in the API client.
24
+ attr_accessor :config
25
+
26
+ # Defines the headers to be used in HTTP requests of all API calls by default.
27
+ #
28
+ # @return [Hash]
29
+ attr_accessor :default_headers
30
+
31
+ attr_accessor :base_path
32
+ attr_accessor :oauth_base_path
33
+
34
+ # Initializes the ApiClient
35
+ # @option config [Configuration] Configuration for initializing the object, default to Configuration.default
36
+ def initialize(config = Configuration.default)
37
+ @config = config
38
+ @user_agent = "Swagger-Codegen/1.1.0/1.0.2.rc12/ruby-#{RUBY_VERSION}p#{RUBY_PATCHLEVEL}"
39
+ @default_headers = {
40
+ 'Content-Type' => "application/json",
41
+ 'User-Agent' => @user_agent
42
+ }
43
+ end
44
+
45
+ def self.default
46
+ @@default ||= ApiClient.new
47
+ end
48
+
49
+ # Call an API with given options.
50
+ #
51
+ # @return [Array<(Object, Fixnum, Hash)>] an array of 3 elements:
52
+ # the data deserialized from response body (could be nil), response status code and response headers.
53
+ def call_api(http_method, path, opts = {})
54
+ request = build_request(http_method, path, opts)
55
+ response = request.run
56
+
57
+ if @config.debugging
58
+ @config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
59
+ end
60
+
61
+ unless response.success?
62
+ if response.timed_out?
63
+ fail ApiError.new('Connection timed out')
64
+ elsif response.code == 0
65
+ # Errors from libcurl will be made visible here
66
+ fail ApiError.new(:code => 0,
67
+ :message => response.return_message)
68
+ else
69
+ fail ApiError.new(:code => response.code,
70
+ :response_headers => response.headers,
71
+ :response_body => response.body),
72
+ response.status_message
73
+ end
74
+ end
75
+
76
+ if opts[:return_type]
77
+ data = deserialize(response, opts[:return_type])
78
+ else
79
+ data = nil
80
+ end
81
+ return data, response.code, response.headers
82
+ end
83
+
84
+ # Builds the HTTP request
85
+ #
86
+ # @param [String] http_method HTTP method/verb (e.g. POST)
87
+ # @param [String] path URL path (e.g. /account/new)
88
+ # @option opts [Hash] :header_params Header parameters
89
+ # @option opts [Hash] :query_params Query parameters
90
+ # @option opts [Hash] :form_params Query parameters
91
+ # @option opts [Object] :body HTTP body (JSON/XML)
92
+ # @return [Typhoeus::Request] A Typhoeus Request
93
+ def build_request(http_method, path, opts = {})
94
+ url = build_request_url(path, opts)
95
+ http_method = http_method.to_sym.downcase
96
+
97
+ header_params = @default_headers.merge(opts[:header_params] || {})
98
+
99
+ # Add SDK default header
100
+ header_params.store("X-DocuSign-SDK", "Ruby")
101
+
102
+ query_params = opts[:query_params] || {}
103
+ form_params = opts[:form_params] || {}
104
+
105
+ update_params_for_auth! header_params, query_params, opts[:auth_names]
106
+
107
+ # set ssl_verifyhosts option based on @config.verify_ssl_host (true/false)
108
+ _verify_ssl_host = @config.verify_ssl_host ? 2 : 0
109
+
110
+ req_opts = {
111
+ :method => http_method,
112
+ :headers => header_params,
113
+ :params => query_params,
114
+ :params_encoding => @config.params_encoding,
115
+ :timeout => @config.timeout,
116
+ :ssl_verifypeer => @config.verify_ssl,
117
+ :ssl_verifyhost => _verify_ssl_host,
118
+ :sslcert => @config.cert_file,
119
+ :sslkey => @config.key_file,
120
+ :verbose => @config.debugging
121
+ }
122
+
123
+ # set custom cert, if provided
124
+ req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert
125
+
126
+ if [:post, :patch, :put, :delete].include?(http_method)
127
+ req_body = build_request_body(header_params, form_params, opts[:body])
128
+ req_opts.update :body => req_body
129
+ if @config.debugging
130
+ @config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
131
+ end
132
+ end
133
+
134
+ Typhoeus::Request.new(url, req_opts)
135
+ end
136
+
137
+ # Check if the given MIME is a JSON MIME.
138
+ # JSON MIME examples:
139
+ # application/json
140
+ # application/json; charset=UTF8
141
+ # APPLICATION/JSON
142
+ # */*
143
+ # @param [String] mime MIME
144
+ # @return [Boolean] True if the MIME is application/json
145
+ def json_mime?(mime)
146
+ (mime == "*/*") || !(mime =~ /\Aapplication\/json(;.*)?\z/i).nil?
147
+ end
148
+
149
+ # Deserialize the response to the given return type.
150
+ #
151
+ # @param [Response] response HTTP response
152
+ # @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]"
153
+ def deserialize(response, return_type)
154
+ body = response.body
155
+ return nil if body.nil? || body.empty?
156
+
157
+ # return response body directly for String return type
158
+ return body if return_type == 'String'
159
+
160
+ # handle file downloading - save response body into a tmp file and return the File instance
161
+ return download_file(response) if return_type == 'File'
162
+
163
+ # ensuring a default content type
164
+ content_type = response.headers['Content-Type'] || 'application/json'
165
+
166
+ fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
167
+
168
+ begin
169
+ data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
170
+ rescue JSON::ParserError => e
171
+ if %w(String Date DateTime).include?(return_type)
172
+ data = body
173
+ else
174
+ raise e
175
+ end
176
+ end
177
+
178
+ convert_to_type data, return_type
179
+ end
180
+
181
+ # Convert data to the given return type.
182
+ # @param [Object] data Data to be converted
183
+ # @param [String] return_type Return type
184
+ # @return [Mixed] Data in a particular type
185
+ def convert_to_type(data, return_type)
186
+ return nil if data.nil?
187
+ case return_type
188
+ when 'String'
189
+ data.to_s
190
+ when 'Integer'
191
+ data.to_i
192
+ when 'Float'
193
+ data.to_f
194
+ when 'BOOLEAN'
195
+ data == true
196
+ when 'DateTime'
197
+ # parse date time (expecting ISO 8601 format)
198
+ DateTime.parse data
199
+ when 'Date'
200
+ # parse date time (expecting ISO 8601 format)
201
+ Date.parse data
202
+ when 'Object'
203
+ # generic object (usually a Hash), return directly
204
+ data
205
+ when /\AArray<(.+)>\z/
206
+ # e.g. Array<Pet>
207
+ sub_type = $1
208
+ data.map {|item| convert_to_type(item, sub_type) }
209
+ when /\AHash\<String, (.+)\>\z/
210
+ # e.g. Hash<String, Integer>
211
+ sub_type = $1
212
+ {}.tap do |hash|
213
+ data.each {|k, v| hash[k] = convert_to_type(v, sub_type) }
214
+ end
215
+ else
216
+ # models, e.g. Pet
217
+ DocuSign_WebForms.const_get(return_type).new.tap do |model|
218
+ model.build_from_hash data
219
+ end
220
+ end
221
+ end
222
+
223
+ # Save response body into a file in (the defined) temporary folder, using the filename
224
+ # from the "Content-Disposition" header if provided, otherwise a random filename.
225
+ #
226
+ # @see Configuration#temp_folder_path
227
+ # @return [Tempfile] the file downloaded
228
+ def download_file(response)
229
+ content_disposition = response.headers['Content-Disposition']
230
+ if content_disposition and content_disposition =~ /filename=/i
231
+ filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
232
+ prefix = sanitize_filename(filename)
233
+ else
234
+ prefix = 'download-'
235
+ end
236
+ prefix = prefix + '-' unless prefix.end_with?('-')
237
+
238
+ tempfile = nil
239
+ encoding = response.body.encoding
240
+ Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding) do |file|
241
+ file.write(response.body)
242
+ tempfile = file
243
+ end
244
+ @config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
245
+ "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
246
+ "will be deleted automatically with GC. It's also recommended to delete the temp file "\
247
+ "explicitly with `tempfile.delete`"
248
+ tempfile
249
+ end
250
+
251
+ # Sanitize filename by removing path.
252
+ # e.g. ../../sun.gif becomes sun.gif
253
+ #
254
+ # @param [String] filename the filename to be sanitized
255
+ # @return [String] the sanitized filename
256
+ def sanitize_filename(filename)
257
+ filename.gsub(/.*[\/\\]/, '')
258
+ end
259
+
260
+ def build_request_url(path, opts)
261
+ # Add leading and trailing slashes to path
262
+ path = "/#{path}".gsub(/\/+/, '/')
263
+ return Addressable::URI.encode("https://" + self.get_oauth_base_path + path) if opts[:oauth]
264
+ Addressable::URI.encode(@config.base_url + path)
265
+ end
266
+
267
+ # Builds the HTTP request body
268
+ #
269
+ # @param [Hash] header_params Header parameters
270
+ # @param [Hash] form_params Query parameters
271
+ # @param [Object] body HTTP body (JSON/XML)
272
+ # @return [String] HTTP body data in the form of string
273
+ def build_request_body(header_params, form_params, body)
274
+ # http form
275
+ if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
276
+ header_params['Content-Type'] == 'multipart/form-data'
277
+ data = {}
278
+ form_params.each do |key, value|
279
+ case value
280
+ when File, Array, nil
281
+ # let typhoeus handle File, Array and nil parameters
282
+ data[key] = value
283
+ else
284
+ if header_params['Content-Type'] == 'multipart/form-data'
285
+ header_params['Content-Disposition'] = 'form-data; name=file; filename=' + key
286
+ data = value
287
+ else
288
+ data[key] = value.to_s
289
+ end
290
+ end
291
+ end
292
+ elsif body
293
+ data = body.is_a?(String) ? body : body.to_json
294
+ else
295
+ data = nil
296
+ end
297
+ data
298
+ end
299
+
300
+ # Update hearder and query params based on authentication settings.
301
+ #
302
+ # @param [Hash] header_params Header parameters
303
+ # @param [Hash] query_params Query parameters
304
+ # @param [String] auth_names Authentication scheme name
305
+ def update_params_for_auth!(header_params, query_params, auth_names)
306
+ Array(auth_names).each do |auth_name|
307
+ auth_setting = @config.auth_settings[auth_name]
308
+ next unless auth_setting
309
+ case auth_setting[:in]
310
+ when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
311
+ when 'query' then query_params[auth_setting[:key]] = auth_setting[:value]
312
+ else fail ArgumentError, 'Authentication token must be in `query` of `header`'
313
+ end
314
+ end
315
+ end
316
+
317
+ # Sets user agent in HTTP header
318
+ #
319
+ # @param [String] user_agent User agent (e.g. swagger-codegen/ruby/1.0.0)
320
+ def user_agent=(user_agent)
321
+ @user_agent = user_agent
322
+ @default_headers['User-Agent'] = @user_agent
323
+ end
324
+
325
+ # Return Accept header based on an array of accepts provided.
326
+ # @param [Array] accepts array for Accept
327
+ # @return [String] the Accept header (e.g. application/json)
328
+ def select_header_accept(accepts)
329
+ return nil if accepts.nil? || accepts.empty?
330
+ # use JSON when present, otherwise use all of the provided
331
+ json_accept = accepts.find { |s| json_mime?(s) }
332
+ return json_accept || accepts.join(',')
333
+ end
334
+
335
+ # Return Content-Type header based on an array of content types provided.
336
+ # @param [Array] content_types array for Content-Type
337
+ # @return [String] the Content-Type header (e.g. application/json)
338
+ def select_header_content_type(content_types)
339
+ # use application/json by default
340
+ return 'application/json' if content_types.nil? || content_types.empty?
341
+ # use JSON when present, otherwise use the first one
342
+ json_content_type = content_types.find { |s| json_mime?(s) }
343
+ return json_content_type || content_types.first
344
+ end
345
+
346
+ # Convert object (array, hash, object, etc) to JSON string.
347
+ # @param [Object] model object to be converted into JSON string
348
+ # @return [String] JSON string representation of the object
349
+ def object_to_http_body(model)
350
+ return model if model.nil? || model.is_a?(String)
351
+ local_body = nil
352
+ if model.is_a?(Array)
353
+ local_body = model.map{|m| object_to_hash(m) }
354
+ else
355
+ local_body = object_to_hash(model)
356
+ end
357
+ local_body.to_json
358
+ end
359
+
360
+ # Convert object(non-array) to hash.
361
+ # @param [Object] obj object to be converted into JSON string
362
+ # @return [String] JSON string representation of the object
363
+ def object_to_hash(obj)
364
+ if obj.respond_to?(:to_hash)
365
+ obj.to_hash
366
+ else
367
+ obj
368
+ end
369
+ end
370
+
371
+ # Build parameter value according to the given collection format.
372
+ # @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
373
+ def build_collection_param(param, collection_format)
374
+ case collection_format
375
+ when :csv
376
+ param.join(',')
377
+ when :ssv
378
+ param.join(' ')
379
+ when :tsv
380
+ param.join("\t")
381
+ when :pipes
382
+ param.join('|')
383
+ when :multi
384
+ # return the array directly as typhoeus will handle it as expected
385
+ param
386
+ else
387
+ fail "unknown collection format: #{collection_format.inspect}"
388
+ end
389
+ end
390
+
391
+ # Helper method to set base_path
392
+ # @param [String] base_path
393
+ def set_base_path(base_path)
394
+ self.base_path = base_path
395
+ end
396
+
397
+ # Helper method to set oauth base path
398
+ # @param [String] oauth_base_path if passed nil it will determined from base_path
399
+ def set_oauth_base_path(oauth_base_path=nil)
400
+ if oauth_base_path
401
+ raise ArgumentError.new('OAuth base path should only include domain name (without https://)') if oauth_base_path.include? "//"
402
+ self.oauth_base_path = oauth_base_path
403
+ return
404
+ end
405
+
406
+ # did we need this check as we can determine it from base path
407
+ #raise ArgumentError.new('oAuthBasePath cannot be empty') unless oauth_base_path
408
+
409
+ # Derive OAuth Base Path if not given
410
+ if self.base_path.nil? or self.base_path.start_with?("https://demo") or self.base_path.start_with?("http://demo") or self.base_path.start_with?("https://apps-d") or self.base_path.start_with?("http://apps-d")
411
+ self.oauth_base_path = OAuth::DEMO_OAUTH_BASE_PATH
412
+ elsif self.base_path.start_with?("https://stage") or self.base_path.start_with?("http://stage") or self.base_path.start_with?("https://apps-s") or self.base_path.start_with?("http://apps-s")
413
+ self.oauth_base_path = OAuth::STAGE_OAUTH_BASE_PATH
414
+ else
415
+ self.oauth_base_path = OAuth::PRODUCTION_OAUTH_BASE_PATH
416
+ end
417
+ end
418
+
419
+ # Helper method to get oauth base path
420
+ def get_oauth_base_path
421
+ if !self.oauth_base_path
422
+ self.set_oauth_base_path()
423
+ end
424
+
425
+ self.oauth_base_path
426
+ end
427
+
428
+ # Helper method to configure the OAuth accessCode/implicit flow parameters
429
+ # @param [String] client_id DocuSign OAuth Client Id(AKA Integrator Key)
430
+ # @param scopes The list of requested scopes. Client applications may be scoped to a limited set of system access.
431
+ # @param [String] redirect_uri This determines where to deliver the response containing the authorization code
432
+ # @param [String] response_type Determines the response type of the authorization request, NOTE: these response types are mutually exclusive for a client application. A public/native client application may only request a response type
433
+ # of "token". A private/trusted client application may only request a response type of "code".
434
+ # @param [String] state Allows for arbitrary state that may be useful to your application. The value in this parameter
435
+ # will be round-tripped along with the response so you can make sure it didn't change.
436
+ # @return [String]
437
+ def get_authorization_uri(client_id, scopes, redirect_uri, response_type, state=nil)
438
+ self.oauth_base_path ||= self.get_oauth_base_path
439
+
440
+ scopes = scopes.join(' ') if scopes.kind_of?(Array)
441
+ scopes = OAuth::SCOPE_SIGNATURE if scopes.empty?
442
+
443
+ uri = "https://%{base_path}/oauth/auth?response_type=%{response_type}&scope=%{scopes}&client_id=%{client_id}&redirect_uri=%{redirect_uri}"
444
+ uri += "&state=%{state}" if state
445
+ uri % {base_path: self.oauth_base_path, response_type:response_type, scopes: scopes, client_id: client_id, redirect_uri: redirect_uri, state: state}
446
+ end
447
+
448
+ # Request JWT User Token
449
+ # @param [String] client_id DocuSign OAuth Client Id(AKA Integrator Key)
450
+ # @param [String] user_id DocuSign user Id to be impersonated
451
+ # @param [String] private_key_or_filename the RSA private key
452
+ # @param [Number] expires_in number of seconds remaining before the JWT assertion is considered as invalid
453
+ # @param scopes The list of requested scopes. Client applications may be scoped to a limited set of system access.
454
+ # @return [OAuth::OAuthToken]
455
+ def request_jwt_user_token(client_id, user_id, private_key_or_filename, expires_in = 3600,scopes=OAuth::SCOPE_SIGNATURE)
456
+ raise ArgumentError.new('client_id cannot be empty') if client_id.empty?
457
+ raise ArgumentError.new('user_id cannot be empty') if user_id.empty?
458
+ raise ArgumentError.new('private_key_or_filename cannot be empty') if private_key_or_filename.empty?
459
+
460
+ scopes = scopes.join(' ') if scopes.kind_of?(Array)
461
+ scopes = OAuth::SCOPE_SIGNATURE if scopes.empty?
462
+ expires_in = 3600 if expires_in > 3600
463
+ now = Time.now.to_i
464
+ claim = {
465
+ "iss" => client_id,
466
+ "sub" => user_id,
467
+ "aud" => self.get_oauth_base_path,
468
+ "iat" => now,
469
+ "exp" => now + expires_in,
470
+ "scope"=> scopes
471
+ }
472
+
473
+ private_key = if private_key_or_filename.include?("-----BEGIN RSA PRIVATE KEY-----")
474
+ private_key_or_filename
475
+ else
476
+ File.read(private_key_or_filename)
477
+ end
478
+
479
+ private_key_bytes = OpenSSL::PKey::RSA.new private_key
480
+ token = JWT.encode claim, private_key_bytes, 'RS256'
481
+ params = {
482
+ :header_params => {"Content-Type" => "application/x-www-form-urlencoded"},
483
+ :form_params => {
484
+ "assertion" => token,
485
+ "grant_type" => OAuth::GRANT_TYPE_JWT
486
+ },
487
+ :return_type => 'OAuth::OAuthToken',
488
+ :oauth => true
489
+ }
490
+ data, status_code, headers = self.call_api("POST", "/oauth/token", params)
491
+
492
+
493
+ raise ApiError.new('Some error accrued during process') if data.nil?
494
+
495
+ self.set_default_header('Authorization', data.token_type + ' ' + data.access_token)
496
+ data
497
+ end
498
+
499
+ # Request JWT User Token
500
+ # @param [String] client_id DocuSign OAuth Client Id(AKA Integrator Key)
501
+ # @param [String] private_key_or_filename the RSA private key
502
+ # @param [Number] expires_in number of seconds remaining before the JWT assertion is considered as invalid
503
+ # @param scopes The list of requested scopes. Client applications may be scoped to a limited set of system access.
504
+ # @return [OAuth::OAuthToken]
505
+ def request_jwt_application_token(client_id, private_key_or_filename, expires_in = 3600,scopes=OAuth::SCOPE_SIGNATURE)
506
+ raise ArgumentError.new('client_id cannot be empty') if client_id.empty?
507
+ raise ArgumentError.new('private_key_or_filename cannot be empty') if private_key_or_filename.empty?
508
+
509
+ scopes = scopes.join(' ') if scopes.kind_of?(Array)
510
+ scopes = OAuth::SCOPE_SIGNATURE if scopes.empty?
511
+ expires_in = 3600 if expires_in > 3600
512
+ now = Time.now.to_i
513
+ claim = {
514
+ "iss" => client_id,
515
+ "aud" => self.get_oauth_base_path,
516
+ "iat" => now,
517
+ "exp" => now + expires_in,
518
+ "scope"=> scopes
519
+ }
520
+
521
+ private_key = if private_key_or_filename.include?("-----BEGIN RSA PRIVATE KEY-----")
522
+ private_key_or_filename
523
+ else
524
+ File.read(private_key_or_filename)
525
+ end
526
+
527
+ private_key_bytes = OpenSSL::PKey::RSA.new private_key
528
+ token = JWT.encode claim, private_key_bytes, 'RS256'
529
+ params = {
530
+ :header_params => {"Content-Type" => "application/x-www-form-urlencoded"},
531
+ :form_params => {
532
+ "assertion" => token,
533
+ "grant_type" => OAuth::GRANT_TYPE_JWT
534
+ },
535
+ :return_type => 'OAuth::OAuthToken',
536
+ :oauth => true
537
+ }
538
+ data, status_code, headers = self.call_api("POST", "/oauth/token", params)
539
+
540
+ raise ApiError.new('Some error accrued during process') if data.nil?
541
+
542
+ self.set_default_header('Authorization', data.token_type + ' ' + data.access_token)
543
+ data
544
+ end
545
+
546
+ # Get User Info method takes the accessToken to retrieve User Account Data.
547
+ # @param [String] access_token
548
+ # @return [OAuth::UserInfo]
549
+ def get_user_info(access_token)
550
+ raise ArgumentError.new('Cannot find a valid access token. Cannot find a valid access token.') if access_token.empty?
551
+
552
+ params = {
553
+ :header_params => {"Authorization" => 'Bearer ' + access_token},
554
+ :return_type => 'OAuth::UserInfo',
555
+ :oauth => true
556
+ }
557
+ data, status_code, headers = self.call_api("GET", '/oauth/userinfo', params)
558
+ data
559
+ end
560
+
561
+ # GenerateAccessToken will exchange the authorization code for an access token and refresh tokens.
562
+ # @param [String] client_id DocuSign OAuth Client Id(AKA Integrator Key)
563
+ # @param [String] client_secret The secret key you generated when you set up the integration in DocuSign Admin console.
564
+ # @param [String] code The authorization code
565
+ def generate_access_token(client_id, client_secret, code)
566
+ raise ArgumentError.new('client_id cannot be empty') if client_id.empty?
567
+ raise ArgumentError.new('client_secret cannot be empty') if client_secret.empty?
568
+ raise ArgumentError.new('code cannot be empty') if code.empty?
569
+
570
+ authcode = "Basic " + Base64.strict_encode64("#{client_id}:#{client_secret}")
571
+ params = {
572
+ :header_params => {
573
+ "Authorization" => authcode,
574
+ "Content-Type" => "application/x-www-form-urlencoded"
575
+ },
576
+ :form_params => {
577
+ "grant_type" => 'authorization_code',
578
+ "code" => code,
579
+ },
580
+ :return_type => 'OAuth::OAuthToken',
581
+ :oauth => true
582
+ }
583
+
584
+ token, status_code, headers = self.call_api("POST", '/oauth/token', params)
585
+
586
+ if status_code == 200
587
+ self.default_headers.store('Authorization', "#{token.token_type} #{token.access_token}")
588
+
589
+ token
590
+ end
591
+ end
592
+
593
+ # Helper method to add default header params
594
+ # @param [String] header_name
595
+ # @param [String] header_value
596
+ def set_default_header(header_name, header_value)
597
+ @default_headers[header_name] = header_value
598
+ end
599
+ end
600
+ end
@@ -0,0 +1,37 @@
1
+ =begin
2
+ #Web Forms API version 1.1
3
+
4
+ #The Web Forms API facilitates generating semantic HTML forms around everyday contracts.
5
+
6
+ OpenAPI spec version: 1.1.0
7
+ Contact: devcenter@docusign.com
8
+ Generated by: https://github.com/swagger-api/swagger-codegen.git
9
+
10
+ =end
11
+
12
+ module DocuSign_WebForms
13
+ class ApiError < StandardError
14
+ attr_reader :code, :response_headers, :response_body
15
+
16
+ # Usage examples:
17
+ # ApiError.new
18
+ # ApiError.new("message")
19
+ # ApiError.new(:code => 500, :response_headers => {}, :response_body => "")
20
+ # ApiError.new(:code => 404, :message => "Not Found")
21
+ def initialize(arg = nil)
22
+ if arg.is_a? Hash
23
+ if arg.key?(:message) || arg.key?('message')
24
+ super(arg[:message] || arg['message'])
25
+ else
26
+ super arg
27
+ end
28
+
29
+ arg.each do |k, v|
30
+ instance_variable_set "@#{k}", v
31
+ end
32
+ else
33
+ super arg
34
+ end
35
+ end
36
+ end
37
+ end