docusign_rooms 1.0.0.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (177) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +66 -0
  3. data/Gemfile +7 -0
  4. data/Gemfile.lock +71 -0
  5. data/LICENSE +21 -0
  6. data/README.md +80 -0
  7. data/Rakefile +8 -0
  8. data/docusign_rooms-1.0.0.pre.alpha.gem +0 -0
  9. data/docusign_rooms.gemspec +45 -0
  10. data/git_push.sh +55 -0
  11. data/lib/.DS_Store +0 -0
  12. data/lib/docusign_rooms.rb +193 -0
  13. data/lib/docusign_rooms/.DS_Store +0 -0
  14. data/lib/docusign_rooms/api/accounts_api.rb +73 -0
  15. data/lib/docusign_rooms/api/activity_types_api.rb +69 -0
  16. data/lib/docusign_rooms/api/closing_statuses_api.rb +69 -0
  17. data/lib/docusign_rooms/api/contact_sides_api.rb +69 -0
  18. data/lib/docusign_rooms/api/countries_api.rb +69 -0
  19. data/lib/docusign_rooms/api/currencies_api.rb +69 -0
  20. data/lib/docusign_rooms/api/documents_api.rb +200 -0
  21. data/lib/docusign_rooms/api/e_sign_permission_profiles_api.rb +73 -0
  22. data/lib/docusign_rooms/api/external_form_fill_sessions_api.rb +79 -0
  23. data/lib/docusign_rooms/api/fields_api.rb +92 -0
  24. data/lib/docusign_rooms/api/financing_types_api.rb +69 -0
  25. data/lib/docusign_rooms/api/form_details_api.rb +77 -0
  26. data/lib/docusign_rooms/api/form_groups_api.rb +89 -0
  27. data/lib/docusign_rooms/api/form_libraries_api.rb +158 -0
  28. data/lib/docusign_rooms/api/offices_api.rb +310 -0
  29. data/lib/docusign_rooms/api/origins_of_leads_api.rb +69 -0
  30. data/lib/docusign_rooms/api/property_types_api.rb +69 -0
  31. data/lib/docusign_rooms/api/regions_api.rb +306 -0
  32. data/lib/docusign_rooms/api/roles_api.rb +328 -0
  33. data/lib/docusign_rooms/api/room_contact_types_api.rb +69 -0
  34. data/lib/docusign_rooms/api/room_folders_api.rb +93 -0
  35. data/lib/docusign_rooms/api/room_templates_api.rb +97 -0
  36. data/lib/docusign_rooms/api/rooms_api.rb +1146 -0
  37. data/lib/docusign_rooms/api/seller_decision_types_api.rb +69 -0
  38. data/lib/docusign_rooms/api/special_circumstance_types_api.rb +69 -0
  39. data/lib/docusign_rooms/api/states_api.rb +69 -0
  40. data/lib/docusign_rooms/api/task_date_types_api.rb +69 -0
  41. data/lib/docusign_rooms/api/task_list_templates_api.rb +89 -0
  42. data/lib/docusign_rooms/api/task_lists_api.rb +188 -0
  43. data/lib/docusign_rooms/api/task_responsibility_types_api.rb +69 -0
  44. data/lib/docusign_rooms/api/task_statuses_api.rb +69 -0
  45. data/lib/docusign_rooms/api/time_zones_api.rb +69 -0
  46. data/lib/docusign_rooms/api/transaction_sides_api.rb +69 -0
  47. data/lib/docusign_rooms/api/users_api.rb +908 -0
  48. data/lib/docusign_rooms/client/.DS_Store +0 -0
  49. data/lib/docusign_rooms/client/api_client.rb +590 -0
  50. data/lib/docusign_rooms/client/api_error.rb +37 -0
  51. data/lib/docusign_rooms/client/auth/oauth.rb +1061 -0
  52. data/lib/docusign_rooms/configuration.rb +202 -0
  53. data/lib/docusign_rooms/models/account_summary.rb +262 -0
  54. data/lib/docusign_rooms/models/activity_type.rb +192 -0
  55. data/lib/docusign_rooms/models/api_error.rb +192 -0
  56. data/lib/docusign_rooms/models/assignable_roles.rb +239 -0
  57. data/lib/docusign_rooms/models/classic_admin_to_invite.rb +216 -0
  58. data/lib/docusign_rooms/models/classic_agent_to_invite.rb +240 -0
  59. data/lib/docusign_rooms/models/classic_manager_permissions.rb +291 -0
  60. data/lib/docusign_rooms/models/classic_manager_to_invite.rb +338 -0
  61. data/lib/docusign_rooms/models/closing_status.rb +192 -0
  62. data/lib/docusign_rooms/models/contact_side.rb +192 -0
  63. data/lib/docusign_rooms/models/country.rb +192 -0
  64. data/lib/docusign_rooms/models/currency.rb +192 -0
  65. data/lib/docusign_rooms/models/custom_data.rb +192 -0
  66. data/lib/docusign_rooms/models/depends_on.rb +192 -0
  67. data/lib/docusign_rooms/models/designated_office.rb +188 -0
  68. data/lib/docusign_rooms/models/designated_region.rb +188 -0
  69. data/lib/docusign_rooms/models/document.rb +265 -0
  70. data/lib/docusign_rooms/models/document_user.rb +291 -0
  71. data/lib/docusign_rooms/models/document_user_for_create.rb +188 -0
  72. data/lib/docusign_rooms/models/e_sign_account_role_settings.rb +183 -0
  73. data/lib/docusign_rooms/models/e_sign_permission_profile.rb +201 -0
  74. data/lib/docusign_rooms/models/e_sign_permission_profile_list.rb +185 -0
  75. data/lib/docusign_rooms/models/external_form_fill_session.rb +183 -0
  76. data/lib/docusign_rooms/models/external_form_fill_session_for_create.rb +211 -0
  77. data/lib/docusign_rooms/models/field.rb +248 -0
  78. data/lib/docusign_rooms/models/field_configuration.rb +259 -0
  79. data/lib/docusign_rooms/models/field_data.rb +185 -0
  80. data/lib/docusign_rooms/models/field_data_for_create.rb +185 -0
  81. data/lib/docusign_rooms/models/field_data_for_update.rb +185 -0
  82. data/lib/docusign_rooms/models/field_set.rb +203 -0
  83. data/lib/docusign_rooms/models/financing_type.rb +192 -0
  84. data/lib/docusign_rooms/models/form_details.rb +246 -0
  85. data/lib/docusign_rooms/models/form_for_add.rb +188 -0
  86. data/lib/docusign_rooms/models/form_group_summary.rb +201 -0
  87. data/lib/docusign_rooms/models/form_group_summary_list.rb +230 -0
  88. data/lib/docusign_rooms/models/form_library_summary.rb +201 -0
  89. data/lib/docusign_rooms/models/form_library_summary_list.rb +230 -0
  90. data/lib/docusign_rooms/models/form_summary.rb +201 -0
  91. data/lib/docusign_rooms/models/form_summary_list.rb +230 -0
  92. data/lib/docusign_rooms/models/global_activity_types.rb +185 -0
  93. data/lib/docusign_rooms/models/global_closing_statuses.rb +185 -0
  94. data/lib/docusign_rooms/models/global_contact_sides.rb +185 -0
  95. data/lib/docusign_rooms/models/global_countries.rb +185 -0
  96. data/lib/docusign_rooms/models/global_currencies.rb +185 -0
  97. data/lib/docusign_rooms/models/global_financing_types.rb +185 -0
  98. data/lib/docusign_rooms/models/global_origins_of_leads.rb +185 -0
  99. data/lib/docusign_rooms/models/global_property_types.rb +185 -0
  100. data/lib/docusign_rooms/models/global_room_contact_types.rb +185 -0
  101. data/lib/docusign_rooms/models/global_seller_decision_types.rb +185 -0
  102. data/lib/docusign_rooms/models/global_special_circumstance_types.rb +185 -0
  103. data/lib/docusign_rooms/models/global_states.rb +185 -0
  104. data/lib/docusign_rooms/models/global_task_date_types.rb +185 -0
  105. data/lib/docusign_rooms/models/global_task_responsibility_types.rb +185 -0
  106. data/lib/docusign_rooms/models/global_task_statuses.rb +185 -0
  107. data/lib/docusign_rooms/models/global_time_zones.rb +185 -0
  108. data/lib/docusign_rooms/models/global_transaction_sides.rb +185 -0
  109. data/lib/docusign_rooms/models/locked_out_details.rb +188 -0
  110. data/lib/docusign_rooms/models/nullable_field_data.rb +185 -0
  111. data/lib/docusign_rooms/models/nullable_permissions.rb +660 -0
  112. data/lib/docusign_rooms/models/office.rb +287 -0
  113. data/lib/docusign_rooms/models/office_for_create.rb +269 -0
  114. data/lib/docusign_rooms/models/office_reference_count.rb +192 -0
  115. data/lib/docusign_rooms/models/office_reference_count_list.rb +185 -0
  116. data/lib/docusign_rooms/models/office_summary.rb +282 -0
  117. data/lib/docusign_rooms/models/office_summary_list.rb +230 -0
  118. data/lib/docusign_rooms/models/origin_of_lead.rb +192 -0
  119. data/lib/docusign_rooms/models/permissions.rb +660 -0
  120. data/lib/docusign_rooms/models/property_type.rb +192 -0
  121. data/lib/docusign_rooms/models/region.rb +206 -0
  122. data/lib/docusign_rooms/models/region_reference_count.rb +192 -0
  123. data/lib/docusign_rooms/models/region_reference_count_list.rb +185 -0
  124. data/lib/docusign_rooms/models/region_summary.rb +201 -0
  125. data/lib/docusign_rooms/models/region_summary_list.rb +230 -0
  126. data/lib/docusign_rooms/models/role.rb +246 -0
  127. data/lib/docusign_rooms/models/role_for_create.rb +201 -0
  128. data/lib/docusign_rooms/models/role_for_update.rb +201 -0
  129. data/lib/docusign_rooms/models/role_summary.rb +228 -0
  130. data/lib/docusign_rooms/models/role_summary_list.rb +230 -0
  131. data/lib/docusign_rooms/models/room.rb +282 -0
  132. data/lib/docusign_rooms/models/room_contact_type.rb +192 -0
  133. data/lib/docusign_rooms/models/room_document.rb +246 -0
  134. data/lib/docusign_rooms/models/room_document_list.rb +230 -0
  135. data/lib/docusign_rooms/models/room_folder.rb +201 -0
  136. data/lib/docusign_rooms/models/room_folder_list.rb +230 -0
  137. data/lib/docusign_rooms/models/room_for_create.rb +248 -0
  138. data/lib/docusign_rooms/models/room_invite.rb +240 -0
  139. data/lib/docusign_rooms/models/room_invite_response.rb +237 -0
  140. data/lib/docusign_rooms/models/room_picture.rb +183 -0
  141. data/lib/docusign_rooms/models/room_summary.rb +273 -0
  142. data/lib/docusign_rooms/models/room_summary_list.rb +230 -0
  143. data/lib/docusign_rooms/models/room_template.rb +201 -0
  144. data/lib/docusign_rooms/models/room_templates_summary_list.rb +230 -0
  145. data/lib/docusign_rooms/models/room_user.rb +246 -0
  146. data/lib/docusign_rooms/models/room_user_for_update.rb +192 -0
  147. data/lib/docusign_rooms/models/room_user_removal_detail.rb +183 -0
  148. data/lib/docusign_rooms/models/room_user_summary.rb +237 -0
  149. data/lib/docusign_rooms/models/room_users_result.rb +230 -0
  150. data/lib/docusign_rooms/models/select_list_field_option.rb +201 -0
  151. data/lib/docusign_rooms/models/seller_decision_type.rb +192 -0
  152. data/lib/docusign_rooms/models/special_circumstance_type.rb +192 -0
  153. data/lib/docusign_rooms/models/state.rb +192 -0
  154. data/lib/docusign_rooms/models/task_date_type.rb +192 -0
  155. data/lib/docusign_rooms/models/task_list.rb +275 -0
  156. data/lib/docusign_rooms/models/task_list_for_create.rb +183 -0
  157. data/lib/docusign_rooms/models/task_list_summary.rb +264 -0
  158. data/lib/docusign_rooms/models/task_list_summary_list.rb +185 -0
  159. data/lib/docusign_rooms/models/task_list_template.rb +210 -0
  160. data/lib/docusign_rooms/models/task_list_template_list.rb +230 -0
  161. data/lib/docusign_rooms/models/task_responsibility_type.rb +192 -0
  162. data/lib/docusign_rooms/models/task_status.rb +192 -0
  163. data/lib/docusign_rooms/models/task_summary.rb +291 -0
  164. data/lib/docusign_rooms/models/time_zone.rb +192 -0
  165. data/lib/docusign_rooms/models/transaction_side.rb +192 -0
  166. data/lib/docusign_rooms/models/user.rb +338 -0
  167. data/lib/docusign_rooms/models/user_for_update.rb +188 -0
  168. data/lib/docusign_rooms/models/user_summary.rb +307 -0
  169. data/lib/docusign_rooms/models/user_summary_list.rb +230 -0
  170. data/lib/docusign_rooms/models/user_to_invite.rb +337 -0
  171. data/lib/docusign_rooms/version.rb +14 -0
  172. data/tests/Gemfile +5 -0
  173. data/tests/Gemfile.lock +42 -0
  174. data/tests/docs/Test.pdf +0 -0
  175. data/tests/docs/private.pem +27 -0
  176. data/tests/spec/unit_tests_using_jwt_spec.rb +125 -0
  177. metadata +420 -0
@@ -0,0 +1,590 @@
1
+ =begin
2
+ #DocuSign Rooms API
3
+
4
+ #An API for an integrator to access the features of DocuSign Rooms
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_Rooms
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_Rooms.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