Skip to content

Commit 9581371

Browse files
committed
Add support of HTTP basic and API key auth to Ruby codegen
1 parent 1657f2e commit 9581371

File tree

4 files changed

+41
-66
lines changed

4 files changed

+41
-66
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ module {{moduleName}}
1616
#
1717
# @example
1818
# Swagger.configure do |config|
19-
# config.api_key = '1234567890abcdef' # required
20-
# config.username = 'wordlover' # optional, but needed for user-related functions
21-
# config.password = 'i<3words' # optional, but needed for user-related functions
19+
# config.api_key['api_key'] = '1234567890abcdef' # api key authentication
20+
# config.username = 'wordlover' # http basic authentication
21+
# config.password = 'i<3words' # http basic authentication
2222
# config.format = 'json' # optional, defaults to 'json'
2323
# end
2424
#

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module {{moduleName}}
22
module Swagger
33
class Configuration
4-
attr_accessor :format, :api_key, :username, :password, :auth_token, :scheme, :host, :base_path, :user_agent, :logger, :inject_format, :force_ending_format, :camelize_params, :user_agent
5-
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
5+
66
# Defaults go in here..
77
def initialize
88
@format = 'json'
@@ -13,6 +13,8 @@ module {{moduleName}}
1313
@inject_format = false
1414
@force_ending_format = false
1515
@camelize_params = true
16+
@api_key = {}
17+
@api_key_prefix = {}
1618
end
1719
end
1820
end

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

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,14 @@ module {{moduleName}}
1313
attributes[:format] ||= Swagger.configuration.format
1414
attributes[:params] ||= {}
1515

16+
update_params_for_auth(attributes)
17+
1618
# Set default headers
1719
default_headers = {
1820
'Content-Type' => "application/#{attributes[:format].downcase}",
19-
:api_key => Swagger.configuration.api_key,
2021
'User-Agent' => Swagger.configuration.user_agent
2122
}
2223

23-
# api_key from headers hash trumps the default, even if its value is blank
24-
if attributes[:headers].present? && attributes[:headers].has_key?(:api_key)
25-
default_headers.delete(:api_key)
26-
end
27-
28-
# api_key from params hash trumps all others (headers and default_headers)
29-
if attributes[:params].present? && attributes[:params].has_key?(:api_key)
30-
default_headers.delete(:api_key)
31-
attributes[:headers].delete(:api_key) if attributes[:headers].present?
32-
end
33-
3424
# Merge argument headers into defaults
3525
attributes[:headers] = default_headers.merge(attributes[:headers] || {})
3626

@@ -46,6 +36,31 @@ module {{moduleName}}
4636
end
4737
end
4838

39+
def update_params_for_auth(attributes)
40+
(attributes[:auth_names] || []).each do |auth_name|
41+
case auth_name
42+
{{#authMethods}}
43+
when '{{name}}'
44+
{{#isApiKey}}{{#isKeyInHeader}}attributes[:headers] ||= {}
45+
attributes[:headers]['{{keyParamName}}'] = get_api_key_with_prefix('{{keyParamName}}'){{/isKeyInHeader}}{{#isKeyInQuery}}attributes[:params] ||= {}
46+
attributes[:params]['{{keyParamName}}'] = get_api_key_with_prefix('{{keyParamName}}'){{/isKeyInQuery}}{{/isApiKey}}
47+
{{#isBasic}}attributes[:headers] ||= {}
48+
http_auth_header = 'Basic ' + ["#{Swagger.configuration.username}:#{Swagger.configuration.password}"].pack('m').delete("\r\n")
49+
attributes[:headers]['Authorization'] = http_auth_header{{/isBasic}}
50+
{{#isOAuth}}# TODO: support oauth{{/isOAuth}}
51+
{{/authMethods}}
52+
end
53+
end
54+
end
55+
56+
def get_api_key_with_prefix(param_name)
57+
if Swagger.configuration.api_key_prefix[param_name].present?
58+
"#{Swagger.configuration.api_key_prefix[param_name]} #{Swagger.configuration.api_key[param_name]}"
59+
else
60+
Swagger.configuration.api_key[param_name]
61+
end
62+
end
63+
4964
# Construct a base URL
5065
def url(options = {})
5166
u = Addressable::URI.new(
@@ -58,9 +73,6 @@ module {{moduleName}}
5873
# Drop trailing question mark, if present
5974
u.sub! /\?$/, ''
6075

61-
# Obfuscate API key?
62-
u.sub! /api\_key=\w+/, 'api_key=YOUR_API_KEY' if options[:obfuscated]
63-
6476
u
6577
end
6678

@@ -129,7 +141,7 @@ module {{moduleName}}
129141
next if self.path.include? "{#{key}}" # skip path params
130142
next if value.blank? && value.class != FalseClass # skip empties
131143
if Swagger.configuration.camelize_params
132-
key = key.to_s.camelize(:lower).to_sym unless key.to_sym == :api_key # api_key is not a camelCased param
144+
key = key.to_s.camelize(:lower).to_sym
133145
end
134146
query_values[key] = value.to_s
135147
end

samples/client/petstore/ruby/spec/request_spec.rb

Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
describe SwaggerClient::Swagger::Request do
44

5-
before(:each) do
5+
before(:each) do
66
SwaggerClient::Swagger.configure do |config|
77
inject_format = true
88
config.api_key = 'special-key'
@@ -22,7 +22,7 @@
2222
it "sets default response format to json" do
2323
@request.format.should == 'json'
2424
end
25-
25+
2626
it "allows params to be nil" do
2727
@request = SwaggerClient::Swagger::Request.new(@default_http_method, @default_path, :params => nil)
2828
@request.query_string.should == ""
@@ -55,7 +55,7 @@
5555
end
5656

5757
end
58-
58+
5959
describe "body" do
6060

6161
it "camelCases parameters" do
@@ -67,7 +67,7 @@
6767
}))
6868
@request.body.keys.should == [:badDog, :goodDog]
6969
end
70-
70+
7171
end
7272

7373
describe "path" do
@@ -140,7 +140,7 @@
140140
@request.query_string.should =~ /\?limit=100/
141141
@request.url.should =~ /\?limit=100/
142142
end
143-
143+
144144
it "camelCases parameters" do
145145
@request = SwaggerClient::Swagger::Request.new(@default_http_method, @default_path, @default_params.merge({
146146
:params => {
@@ -150,51 +150,12 @@
150150
}))
151151
@request.query_string.should == "?badDog=bud&goodDog=dud"
152152
end
153-
153+
154154
it "converts boolean values to their string representation" do
155155
params = {:stringy => "fish", :truthy => true, :falsey => false}
156156
@request = SwaggerClient::Swagger::Request.new(:get, 'fakeMethod', :params => params)
157157
@request.query_string.should == "?falsey=false&stringy=fish&truthy=true"
158158
end
159-
160-
end
161-
162-
describe "API key" do
163-
164-
it "is inferred from the Swagger base configuration by default" do
165-
SwaggerClient::Swagger.configure {|c| c.api_key = "xyz" }
166-
SwaggerClient::Swagger::Request.new(:get, "word/json").headers[:api_key].should == "xyz"
167-
end
168-
169-
it "can be obfuscated for public display" do
170-
@request = SwaggerClient::Swagger::Request.new(:get, "words/fancy", @default_params.merge({
171-
:params => {
172-
:word => "dog",
173-
:api_key => "123456"
174-
}
175-
}))
176-
177-
@request.url.should =~ /api\_key=123456/
178-
@request.url(:obfuscated => true).should =~ /api\_key=YOUR\_API\_KEY/
179-
end
180-
181-
it "allows a key in the params to override the configuration-level key, even if it's blank" do
182-
SwaggerClient::Swagger.configure {|c| c.api_key = "abc" }
183-
@request_with_key = SwaggerClient::Swagger::Request.new(:get, "word/json", :params => {:api_key => "jkl"})
184-
@request_with_key.headers[:api_key].should be_nil
185-
@request_with_key.params[:api_key].should == "jkl"
186-
187-
@request_without_key = SwaggerClient::Swagger::Request.new(:get, "word/json", :params => {:api_key => nil})
188-
@request_without_key.headers[:api_key].should be_nil
189-
@request_without_key.params[:api_key].should be_nil
190-
end
191-
192-
it "allows a key in the headers to override the configuration-level key, even if it's blank" do
193-
SwaggerClient::Swagger.configure {|c| c.api_key = "hij" }
194-
SwaggerClient::Swagger::Request.new(:get, "word/json").headers[:api_key].should == "hij"
195-
SwaggerClient::Swagger::Request.new(:get, "word/json", :headers => {:api_key => "jkl"}).headers[:api_key].should == "jkl"
196-
SwaggerClient::Swagger::Request.new(:get, "word/json", :headers => {:api_key => nil}).headers[:api_key].should be_nil
197-
end
198159

199160
end
200161

0 commit comments

Comments
 (0)