Skip to content

Commit 2e285ed

Browse files
committed
Support skipping SSL certification verification
in Ruby clients by e.g.: SwaggerClient::Swagger.configure do |config| config.verify_ssl = false end
1 parent 9618960 commit 2e285ed

File tree

4 files changed

+38
-20
lines changed

4 files changed

+38
-20
lines changed

modules/swagger-codegen/src/main/resources/ruby/swagger/configuration.mustache

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module {{moduleName}}
22
module Swagger
33
class Configuration
4-
attr_accessor :format, :api_key, :api_key_prefix, :username, :password, :auth_token, :scheme, :host, :base_path, :user_agent, :logger, :inject_format, :force_ending_format, :camelize_params, :user_agent
4+
attr_accessor :format, :api_key, :api_key_prefix, :username, :password, :auth_token, :scheme, :host, :base_path, :user_agent, :logger, :inject_format, :force_ending_format, :camelize_params, :user_agent, :verify_ssl
55

66
# Defaults go in here..
77
def initialize
@@ -13,8 +13,16 @@ module {{moduleName}}
1313
@inject_format = false
1414
@force_ending_format = false
1515
@camelize_params = true
16+
17+
# keys for API key authentication (param-name => api-key)
1618
@api_key = {}
19+
# api-key prefix for API key authentication, e.g. "Bearer" (param-name => api-key-prefix)
1720
@api_key_prefix = {}
21+
22+
# whether to verify SSL certificate, default to true
23+
# Note: do NOT set it to false in production code, otherwise you would
24+
# face multiple types of cryptographic attacks
25+
@verify_ssl = true
1826
end
1927
end
2028
end

modules/swagger-codegen/src/main/resources/ruby/swagger/request.mustache

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,39 +160,40 @@ module {{moduleName}}
160160
#TODO use configuration setting to determine if debugging
161161
#logger = Logger.new STDOUT
162162
#logger.debug self.url
163+
164+
request_options = {
165+
:ssl_verifypeer => Swagger.configuration.verify_ssl,
166+
:headers => self.headers.stringify_keys
167+
}
163168
response = case self.http_method.to_sym
164169
when :get,:GET
165170
Typhoeus::Request.get(
166171
self.url,
167-
:headers => self.headers.stringify_keys,
172+
request_options
168173
)
169174

170175
when :post,:POST
171176
Typhoeus::Request.post(
172177
self.url,
173-
:body => self.outgoing_body,
174-
:headers => self.headers.stringify_keys,
178+
request_options.merge(:body => self.outgoing_body)
175179
)
176180

177181
when :patch,:PATCH
178182
Typhoeus::Request.patch(
179183
self.url,
180-
:body => self.outgoing_body,
181-
:headers => self.headers.stringify_keys,
184+
request_options.merge(:body => self.outgoing_body)
182185
)
183186

184187
when :put,:PUT
185188
Typhoeus::Request.put(
186189
self.url,
187-
:body => self.outgoing_body,
188-
:headers => self.headers.stringify_keys,
190+
request_options.merge(:body => self.outgoing_body)
189191
)
190192

191193
when :delete,:DELETE
192194
Typhoeus::Request.delete(
193195
self.url,
194-
:body => self.outgoing_body,
195-
:headers => self.headers.stringify_keys,
196+
request_options.merge(:body => self.outgoing_body)
196197
)
197198
end
198199
Response.new(response)

samples/client/petstore/ruby/lib/swagger_client/swagger/configuration.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module SwaggerClient
22
module Swagger
33
class Configuration
4-
attr_accessor :format, :api_key, :api_key_prefix, :username, :password, :auth_token, :scheme, :host, :base_path, :user_agent, :logger, :inject_format, :force_ending_format, :camelize_params, :user_agent
4+
attr_accessor :format, :api_key, :api_key_prefix, :username, :password, :auth_token, :scheme, :host, :base_path, :user_agent, :logger, :inject_format, :force_ending_format, :camelize_params, :user_agent, :verify_ssl
55

66
# Defaults go in here..
77
def initialize
@@ -13,8 +13,16 @@ def initialize
1313
@inject_format = false
1414
@force_ending_format = false
1515
@camelize_params = true
16+
17+
# keys for API key authentication (param-name => api-key)
1618
@api_key = {}
19+
# api-key prefix for API key authentication, e.g. "Bearer" (param-name => api-key-prefix)
1720
@api_key_prefix = {}
21+
22+
# whether to verify SSL certificate, default to true
23+
# Note: do NOT set it to false in production code, otherwise you would
24+
# face multiple types of cryptographic attacks
25+
@verify_ssl = true
1826
end
1927
end
2028
end

samples/client/petstore/ruby/lib/swagger_client/swagger/request.rb

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,39 +159,40 @@ def make
159159
#TODO use configuration setting to determine if debugging
160160
#logger = Logger.new STDOUT
161161
#logger.debug self.url
162+
163+
request_options = {
164+
:ssl_verifypeer => Swagger.configuration.verify_ssl,
165+
:headers => self.headers.stringify_keys
166+
}
162167
response = case self.http_method.to_sym
163168
when :get,:GET
164169
Typhoeus::Request.get(
165170
self.url,
166-
:headers => self.headers.stringify_keys,
171+
request_options
167172
)
168173

169174
when :post,:POST
170175
Typhoeus::Request.post(
171176
self.url,
172-
:body => self.outgoing_body,
173-
:headers => self.headers.stringify_keys,
177+
request_options.merge(:body => self.outgoing_body)
174178
)
175179

176180
when :patch,:PATCH
177181
Typhoeus::Request.patch(
178182
self.url,
179-
:body => self.outgoing_body,
180-
:headers => self.headers.stringify_keys,
183+
request_options.merge(:body => self.outgoing_body)
181184
)
182185

183186
when :put,:PUT
184187
Typhoeus::Request.put(
185188
self.url,
186-
:body => self.outgoing_body,
187-
:headers => self.headers.stringify_keys,
189+
request_options.merge(:body => self.outgoing_body)
188190
)
189191

190192
when :delete,:DELETE
191193
Typhoeus::Request.delete(
192194
self.url,
193-
:body => self.outgoing_body,
194-
:headers => self.headers.stringify_keys,
195+
request_options.merge(:body => self.outgoing_body)
195196
)
196197
end
197198
Response.new(response)

0 commit comments

Comments
 (0)