Skip to content

Commit 027acb8

Browse files
author
Rob Nichols
committed
Refactor get_idp_metadata - reduce scope of conditionals
1 parent a65b68b commit 027acb8

File tree

2 files changed

+18
-26
lines changed

2 files changed

+18
-26
lines changed

lib/onelogin/ruby-saml/idp_metadata_parser.rb

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def parse_remote(url, validate_cert = true, options = {})
3636
end
3737

3838
# Parse the Identity Provider metadata and update the settings with the IdP values
39-
# @param idp_metadata [String]
39+
# @param idp_metadata [String]
4040
# @param options [Hash] :settings to provide the OneLogin::RubySaml::Settings object or an hash for Settings overrides
4141
#
4242
def parse(idp_metadata, options = {})
@@ -67,36 +67,28 @@ def parse(idp_metadata, options = {})
6767
# @raise [HttpError] Failure to fetch remote IdP metadata
6868
def get_idp_metadata(url, validate_cert)
6969
uri = URI.parse(url)
70-
if uri.scheme == "http"
71-
response = Net::HTTP.get_response(uri)
72-
meta_text = response.body
73-
elsif uri.scheme == "https"
74-
http = Net::HTTP.new(uri.host, uri.port)
70+
raise ArgumentError.new("url must begin with http or https") unless /^https?/ =~ uri.scheme
71+
http = Net::HTTP.new(uri.host, uri.port)
72+
73+
if uri.scheme == "https"
7574
http.use_ssl = true
7675
# Most IdPs will probably use self signed certs
77-
if validate_cert
78-
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
79-
80-
# Net::HTTP in Ruby 1.8 did not set the default certificate store
81-
# automatically when VERIFY_PEER was specified.
82-
if RUBY_VERSION < '1.9' && !http.ca_file && !http.ca_path && !http.cert_store
83-
http.cert_store = OpenSSL::SSL::SSLContext::DEFAULT_CERT_STORE
84-
end
85-
else
86-
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
76+
http.verify_mode = validate_cert ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
77+
78+
# Net::HTTP in Ruby 1.8 did not set the default certificate store
79+
# automatically when VERIFY_PEER was specified.
80+
if RUBY_VERSION < '1.9' && !http.ca_file && !http.ca_path && !http.cert_store
81+
http.cert_store = OpenSSL::SSL::SSLContext::DEFAULT_CERT_STORE
8782
end
88-
get = Net::HTTP::Get.new(uri.request_uri)
89-
response = http.request(get)
90-
meta_text = response.body
91-
else
92-
raise ArgumentError.new("url must begin with http or https")
9383
end
9484

95-
unless response.is_a? Net::HTTPSuccess
96-
raise OneLogin::RubySaml::HttpError.new("Failed to fetch idp metadata")
97-
end
85+
get = Net::HTTP::Get.new(uri.request_uri)
86+
response = http.request(get)
87+
return response.body if response.is_a? Net::HTTPSuccess
9888

99-
meta_text
89+
raise OneLogin::RubySaml::HttpError.new(
90+
"Failed to fetch idp metadata: #{response.code}: #{response.message}"
91+
)
10092
end
10193

10294
# @return [String|nil] IdP Entity ID value if exists

test/idp_metadata_parser_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def initialize; end
133133
idp_metadata_parser.parse_remote("https://example.hello.com/access/saml/idp.xml")
134134
end
135135

136-
assert_equal("Failed to fetch idp metadata", exception.message)
136+
assert_match("Failed to fetch idp metadata", exception.message)
137137
end
138138
end
139139
end

0 commit comments

Comments
 (0)