Skip to content

Commit b373252

Browse files
committed
More Rubocop fixes
1 parent c0ab792 commit b373252

15 files changed

+114
-424
lines changed

.rubocop_todo.yml

Lines changed: 11 additions & 326 deletions
Large diffs are not rendered by default.

lib/onelogin/ruby-saml/attributes.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ def initialize(attrs = {})
4040

4141
# Iterate over all attributes
4242
#
43-
def each
44-
attributes.each{|name, values| yield name, values}
43+
def each(&block)
44+
attributes.each(&block)
4545
end
4646

4747
# Test attribute presence by name

lib/onelogin/ruby-saml/authrequest.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Authrequest < SamlMessage
2424
#
2525
def initialize
2626
@uuid = OneLogin::RubySaml::Utils.uuid
27+
super()
2728
end
2829

2930
def request_id

lib/onelogin/ruby-saml/idp_metadata_parser.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ module Vocabulary
4242

4343
# fetch IdP descriptors from a metadata document
4444
def self.get_idps(metadata_document, only_entity_id=nil)
45-
path = "//md:EntityDescriptor#{only_entity_id && '[@entityID="' + only_entity_id + '"]'}/md:IDPSSODescriptor"
45+
path = "//md:EntityDescriptor#{('[@entityID="' + only_entity_id + '"]') if only_entity_id}/md:IDPSSODescriptor"
4646
REXML::XPath.match(
4747
metadata_document,
4848
path,
@@ -197,7 +197,7 @@ def parse_to_idp_metadata_array(idp_metadata, options = {})
197197
# @raise [HttpError] Failure to fetch remote IdP metadata
198198
def get_idp_metadata(url, validate_cert)
199199
uri = URI.parse(url)
200-
raise ArgumentError.new("url must begin with http or https") unless /^https?/ =~ uri.scheme
200+
raise ArgumentError.new("url must begin with http or https") unless /^https?/.match?(uri.scheme)
201201
http = Net::HTTP.new(uri.host, uri.port)
202202

203203
if uri.scheme == "https"
@@ -250,14 +250,14 @@ def to_hash(options = {})
250250
#
251251
def valid_until
252252
root = @idpsso_descriptor.root
253-
root.attributes['validUntil'] if root && root.attributes
253+
root.attributes['validUntil'] if root&.attributes
254254
end
255255

256256
# @return [String|nil] 'cacheDuration' attribute of metadata
257257
#
258258
def cache_duration
259259
root = @idpsso_descriptor.root
260-
root.attributes['cacheDuration'] if root && root.attributes
260+
root.attributes['cacheDuration'] if root&.attributes
261261
end
262262

263263
# @param name_id_priority [String|Array<String>] The prioritized list of NameIDFormat values to select. Will select first value if nil.
@@ -308,7 +308,7 @@ def single_signon_service_url(binding_priority = nil)
308308
"md:SingleSignOnService[@Binding=\"#{binding}\"]/@Location",
309309
SamlMetadata::NAMESPACE
310310
)
311-
node.value if node
311+
node&.value
312312
end
313313

314314
# @param binding_priority [String|Array<String>] The prioritized list of Binding values to select. Will select first value if nil.
@@ -323,7 +323,7 @@ def single_logout_service_url(binding_priority = nil)
323323
"md:SingleLogoutService[@Binding=\"#{binding}\"]/@Location",
324324
SamlMetadata::NAMESPACE
325325
)
326-
node.value if node
326+
node&.value
327327
end
328328

329329
# @param binding_priority [String|Array<String>] The prioritized list of Binding values to select. Will select first value if nil.
@@ -338,7 +338,7 @@ def single_logout_response_service_url(binding_priority = nil)
338338
"md:SingleLogoutService[@Binding=\"#{binding}\"]/@ResponseLocation",
339339
SamlMetadata::NAMESPACE
340340
)
341-
node.value if node
341+
node&.value
342342
end
343343

344344
# @return [String|nil] Unformatted Certificate if exists
@@ -394,7 +394,7 @@ def fingerprint(certificate, fingerprint_algorithm = XMLSecurity::Document::SHA1
394394
#
395395
def attribute_names
396396
nodes = REXML::XPath.match(
397-
@idpsso_descriptor ,
397+
@idpsso_descriptor,
398398
"saml:Attribute/@Name",
399399
SamlMetadata::NAMESPACE
400400
)
@@ -404,8 +404,8 @@ def attribute_names
404404
def merge_certificates_into(parsed_metadata)
405405
if (certificates.size == 1 &&
406406
(certificates_has_one('signing') || certificates_has_one('encryption'))) ||
407-
(certificates_has_one('signing') && certificates_has_one('encryption') &&
408-
certificates["signing"][0] == certificates["encryption"][0])
407+
(certificates_has_one('signing') && certificates_has_one('encryption') &&
408+
certificates["signing"][0] == certificates["encryption"][0])
409409

410410
parsed_metadata[:idp_cert] = if certificates.key?("signing")
411411
certificates["signing"][0]

lib/onelogin/ruby-saml/logging.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,29 @@
66
module OneLogin
77
module RubySaml
88
class Logging
9-
DEFAULT_LOGGER = ::Logger.new(STDOUT)
9+
DEFAULT_LOGGER = ::Logger.new($stdout)
1010

1111
def self.logger
1212
@logger ||= begin
13-
(defined?(::Rails) && Rails.respond_to?(:logger) && Rails.logger) ||
14-
DEFAULT_LOGGER
13+
logger = Rails.logger if defined?(::Rails) && Rails.respond_to?(:logger)
14+
logger ||= DEFAULT_LOGGER
1515
end
1616
end
1717

18-
def self.logger=(logger)
19-
@logger = logger
18+
class << self
19+
attr_writer :logger
2020
end
2121

2222
def self.debug(message)
23-
return if !!ENV["ruby-saml/testing"]
23+
return if ENV["ruby-saml/testing"]
2424

25-
logger.debug message
25+
logger.debug(message)
2626
end
2727

2828
def self.info(message)
29-
return if !!ENV["ruby-saml/testing"]
29+
return if ENV["ruby-saml/testing"]
3030

31-
logger.info message
31+
logger.info(message)
3232
end
3333
end
3434
end

lib/onelogin/ruby-saml/logoutrequest.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Logoutrequest < SamlMessage
2121
#
2222
def initialize
2323
@uuid = OneLogin::RubySaml::Utils.uuid
24+
super()
2425
end
2526

2627
def request_id

lib/onelogin/ruby-saml/logoutresponse.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def initialize(response, settings = nil, options = {})
4747
@options = options
4848
@response = decode_raw_saml(response, settings)
4949
@document = XMLSecurity::SignedDocument.new(@response)
50+
super()
5051
end
5152

5253
def response_id
@@ -159,11 +160,11 @@ def validate_structure
159160
true
160161
end
161162

162-
# Validates that the Logout Response provided in the initialization is not empty,
163-
# also check that the setting and the IdP cert were also provided
164-
# @return [Boolean] True if the required info is found, otherwise False if soft=True
165-
# @raise [ValidationError] if soft == false and validation fails
166-
#
163+
# Validates that the Logout Response provided in the initialization is not empty,
164+
# also check that the setting and the IdP cert were also provided
165+
# @return [Boolean] True if the required info is found, otherwise False if soft=True
166+
# @raise [ValidationError] if soft == false and validation fails
167+
#
167168
def valid_state?
168169
return append_error("Blank logout response") if response.empty?
169170

lib/onelogin/ruby-saml/metadata.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def add_sp_certificates(sp_sso, settings)
7070
cert = settings.get_sp_cert
7171
cert_new = settings.get_sp_cert_new
7272

73-
for sp_cert in [cert, cert_new]
73+
[cert, cert_new].each do |sp_cert|
7474
if sp_cert
7575
cert_text = Base64.encode64(sp_cert.to_der).gsub("\n", '')
7676
kd = sp_sso.add_element "md:KeyDescriptor", { "use" => "signing" }

lib/onelogin/ruby-saml/response.rb

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Response < SamlMessage
3838
AVAILABLE_OPTIONS = [
3939
:allowed_clock_drift, :check_duplicated_attributes, :matches_request_id, :settings, :skip_audience, :skip_authnstatement, :skip_conditions,
4040
:skip_destination, :skip_recipient_check, :skip_subject_confirmation
41-
]
41+
].freeze
4242
# TODO: Update the comment on initialize to describe every option
4343

4444
# Constructs the SAML Response. A Response Object that is an extension of the SamlMessage class.
@@ -71,6 +71,8 @@ def initialize(response, options = {})
7171
if assertion_encrypted?
7272
@decrypted_document = generate_decrypted_document
7373
end
74+
75+
super()
7476
end
7577

7678
# Validates the SAML Response with the default values (soft = true)
@@ -93,7 +95,7 @@ def name_id
9395
#
9496
def name_id_format
9597
@name_id_format ||=
96-
if name_id_node && name_id_node.attribute("Format")
98+
if name_id_node&.attribute("Format")
9799
name_id_node.attribute("Format").value
98100
end
99101
end
@@ -104,7 +106,7 @@ def name_id_format
104106
#
105107
def name_id_spnamequalifier
106108
@name_id_spnamequalifier ||=
107-
if name_id_node && name_id_node.attribute("SPNameQualifier")
109+
if name_id_node&.attribute("SPNameQualifier")
108110
name_id_node.attribute("SPNameQualifier").value
109111
end
110112
end
@@ -113,7 +115,7 @@ def name_id_spnamequalifier
113115
#
114116
def name_id_namequalifier
115117
@name_id_namequalifier ||=
116-
if name_id_node && name_id_node.attribute("NameQualifier")
118+
if name_id_node&.attribute("NameQualifier")
117119
name_id_node.attribute("NameQualifier").value
118120
end
119121
end
@@ -165,11 +167,11 @@ def attributes
165167
raise ValidationError.new("Found an Attribute element with duplicated Name")
166168
end
167169

168-
values = node.elements.collect{|e|
169-
if (e.elements.nil? || e.elements.size == 0)
170+
values = node.elements.collect do |e|
171+
if e.elements.nil? || e.elements.size == 0
170172
# SAMLCore requires that nil AttributeValues MUST contain xsi:nil XML attribute set to "true" or "1"
171173
# otherwise the value is to be regarded as empty.
172-
["true", "1"].include?(e.attributes['xsi:nil']) ? nil : Utils.element_text(e)
174+
%w[true 1].include?(e.attributes['xsi:nil']) ? nil : Utils.element_text(e)
173175
# explicitly support saml2:NameID with saml2:NameQualifier if supplied in attributes
174176
# this is useful for allowing eduPersonTargetedId to be passed as an opaque identifier to use to
175177
# identify the subject in an SP rather than email or other less opaque attributes
@@ -180,7 +182,7 @@ def attributes
180182
"#{base_path}#{Utils.element_text(n)}"
181183
end
182184
end
183-
}
185+
end
184186

185187
attributes.add(name, values.flatten)
186188
end
@@ -218,7 +220,7 @@ def status_code
218220
)
219221
if nodes.size == 1
220222
node = nodes[0]
221-
code = node.attributes["Value"] if node && node.attributes
223+
code = node.attributes["Value"] if node&.attributes
222224

223225
unless code == "urn:oasis:names:tc:SAML:2.0:status:Success"
224226
nodes = REXML::XPath.match(
@@ -348,7 +350,7 @@ def allowed_clock_drift
348350
# @return [Boolean] True if the SAML Response contains an EncryptedAssertion element
349351
#
350352
def assertion_encrypted?
351-
! REXML::XPath.first(
353+
!REXML::XPath.first(
352354
document,
353355
"(/p:Response/EncryptedAssertion/)|(/p:Response/a:EncryptedAssertion/)",
354356
{ "p" => PROTOCOL, "a" => ASSERTION }
@@ -622,7 +624,7 @@ def validate_audience
622624
end
623625

624626
unless audiences.include? settings.sp_entity_id
625-
s = audiences.count > 1 ? 's' : '';
627+
s = audiences.count > 1 ? 's' : ''
626628
error_msg = "Invalid Audience#{s}. The audience#{s} #{audiences.join(',')}, did not match the expected audience #{settings.sp_entity_id}"
627629
return append_error(error_msg)
628630
end
@@ -792,7 +794,7 @@ def validate_subject_confirmation
792794
break
793795
end
794796

795-
if !valid_subject_confirmation
797+
unless valid_subject_confirmation
796798
error_msg = "A valid SubjectConfirmation was not found on this Response"
797799
return append_error(error_msg)
798800
end
@@ -895,7 +897,6 @@ def validate_signature
895897
@errors = old_errors
896898
break
897899
end
898-
899900
end
900901
if expired
901902
error_msg = "IdP x509 certificate expired"
@@ -935,13 +936,13 @@ def xpath_first_from_signed_assertion(subelt=nil)
935936
"/p:Response/a:Assertion[@ID=$id]#{subelt}",
936937
{ "p" => PROTOCOL, "a" => ASSERTION },
937938
{ 'id' => doc.signed_element_id }
938-
)
939+
)
939940
node ||= REXML::XPath.first(
940941
doc,
941942
"/p:Response[@ID=$id]/a:Assertion#{subelt}",
942943
{ "p" => PROTOCOL, "a" => ASSERTION },
943944
{ 'id' => doc.signed_element_id }
944-
)
945+
)
945946
node
946947
end
947948

@@ -957,13 +958,13 @@ def xpath_from_signed_assertion(subelt=nil)
957958
"/p:Response/a:Assertion[@ID=$id]#{subelt}",
958959
{ "p" => PROTOCOL, "a" => ASSERTION },
959960
{ 'id' => doc.signed_element_id }
960-
)
961-
node.concat( REXML::XPath.match(
961+
)
962+
node.concat(REXML::XPath.match(
962963
doc,
963964
"/p:Response[@ID=$id]/a:Assertion#{subelt}",
964965
{ "p" => PROTOCOL, "a" => ASSERTION },
965966
{ 'id' => doc.signed_element_id }
966-
))
967+
))
967968
end
968969

969970
# Generates the decrypted_document

lib/onelogin/ruby-saml/saml_message.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ class SamlMessage
2020
ASSERTION = "urn:oasis:names:tc:SAML:2.0:assertion"
2121
PROTOCOL = "urn:oasis:names:tc:SAML:2.0:protocol"
2222

23-
BASE64_FORMAT = %r(\A([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?\Z)
23+
BASE64_FORMAT = %r(\A([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?\Z).freeze
2424
@@mutex = Mutex.new
2525

2626
# @return [Nokogiri::XML::Schema] Gets the schema object of the SAML 2.0 Protocol schema
2727
#
2828
def self.schema
2929
@@mutex.synchronize do
30-
Dir.chdir(File.expand_path("../../../schemas", __FILE__)) do
30+
Dir.chdir(File.expand_path('../../schemas', __dir__)) do
3131
::Nokogiri::XML::Schema(File.read("saml-schema-protocol-2.0.xsd"))
3232
end
3333
end
@@ -98,7 +98,7 @@ def decode_raw_saml(saml, settings = nil)
9898
decoded = decode(saml)
9999
begin
100100
inflate(decoded)
101-
rescue
101+
rescue StandardError
102102
decoded
103103
end
104104
end
@@ -127,7 +127,7 @@ def decode(string)
127127
# @return [String] The encoded string
128128
#
129129
def encode(string)
130-
if Base64.respond_to?('strict_encode64')
130+
if Base64.respond_to?(:strict_encode64)
131131
Base64.strict_encode64(string)
132132
else
133133
Base64.encode64(string).gsub(/\n/, "")

0 commit comments

Comments
 (0)