docusign_admin 1.0.0.beta

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