docusign_click 1.0.0.beta

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