Skip to content

Commit 016e457

Browse files
committed
Rubocop fixes
1 parent d175b9f commit 016e457

File tree

6 files changed

+39
-47
lines changed

6 files changed

+39
-47
lines changed

lib/ruby_saml/logoutresponse.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def in_response_to
6969
# @return [String] Gets the Issuer from the Logout Response.
7070
#
7171
def issuer
72-
@issuer ||=document.at_xpath(
72+
@issuer ||= document.at_xpath(
7373
"/p:LogoutResponse/a:Issuer",
7474
{ "p" => RubySaml::XML::NS_PROTOCOL, "a" => RubySaml::XML::NS_ASSERTION }
7575
)&.text

lib/ruby_saml/memoizable.rb

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
# frozen_string_literal: true
22

33
module RubySaml
4+
# Mixin for memoizing methods
45
module Memoizable
6+
# Creates a memoized method
7+
#
8+
# @param method_name [Symbol] the name of the method to memoize
9+
# @param original_method [Symbol, nil] the original method to memoize (defaults to method_name)
10+
def self.included(base)
11+
base.extend(ClassMethods)
12+
end
13+
514
private
615

716
# Memoizes the result of a block using the given name as the cache key
@@ -16,14 +25,7 @@ def memoize(cache_key)
1625
instance_variable_set(cache_key, yield)
1726
end
1827

19-
# Creates a memoized method
20-
#
21-
# @param method_name [Symbol] the name of the method to memoize
22-
# @param original_method [Symbol, nil] the original method to memoize (defaults to method_name)
23-
def self.included(base)
24-
base.extend(ClassMethods)
25-
end
26-
28+
# Class methods for memoization
2729
module ClassMethods
2830
# Defines multiple memoized methods
2931
#

lib/ruby_saml/response.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def session_expires_at
187187
@expires_at ||= begin
188188
node = xpath_first_from_signed_assertion('/a:AuthnStatement')
189189
parse_time(node, "SessionNotOnOrAfter") if node
190-
end
190+
end
191191
end
192192

193193
# Gets the AuthnInstant from the AuthnStatement.
@@ -241,7 +241,7 @@ def status_code
241241

242242
code
243243
end
244-
end
244+
end
245245
end
246246

247247
# @return [String] the StatusMessage value from a SAML Response.
@@ -254,7 +254,7 @@ def status_message
254254
)
255255

256256
nodes.first&.text if nodes.size == 1
257-
end
257+
end
258258
end
259259

260260
# Gets the Condition Element of the SAML Response if exists.
@@ -303,7 +303,7 @@ def issuers
303303

304304
nodes = issuer_response_nodes + issuer_assertion_nodes
305305
nodes.map(&:text).reject(&:empty?).uniq
306-
end
306+
end
307307
end
308308

309309
# @return [String|nil] The InResponseTo attribute from the SAML Response.
@@ -355,7 +355,7 @@ def assertion_id
355355
@assertion_id ||= begin
356356
node = xpath_first_from_signed_assertion('')
357357
node.nil? ? nil : node['ID']
358-
end
358+
end
359359
end
360360

361361
private
@@ -761,9 +761,9 @@ def validate_subject_confirmation
761761
next unless confirmation_data_node
762762

763763
next if (confirmation_data_node['InResponseTo'] && confirmation_data_node['InResponseTo'] != in_response_to) ||
764-
(confirmation_data_node['NotBefore'] && now < (parse_time(confirmation_data_node, "NotBefore") - allowed_clock_drift)) ||
765-
(confirmation_data_node['NotOnOrAfter'] && now >= (parse_time(confirmation_data_node, "NotOnOrAfter") + allowed_clock_drift)) ||
766-
(confirmation_data_node['Recipient'] && !options[:skip_recipient_check] && settings && confirmation_data_node['Recipient'] != settings.assertion_consumer_service_url)
764+
(confirmation_data_node['NotBefore'] && now < (parse_time(confirmation_data_node, "NotBefore") - allowed_clock_drift)) ||
765+
(confirmation_data_node['NotOnOrAfter'] && now >= (parse_time(confirmation_data_node, "NotOnOrAfter") + allowed_clock_drift)) ||
766+
(confirmation_data_node['Recipient'] && !options[:skip_recipient_check] && settings && confirmation_data_node['Recipient'] != settings.assertion_consumer_service_url)
767767

768768
valid_subject_confirmation = true
769769
break

lib/ruby_saml/slo_logoutrequest.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def session_indexes
115115
document.xpath(
116116
"/p:LogoutRequest/p:SessionIndex",
117117
{ "p" => RubySaml::XML::NS_PROTOCOL }
118-
).map { |node| node.text }
118+
).map(&:text)
119119
end
120120

121121
private

lib/ruby_saml/xml/signed_document_info.rb

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

33
module RubySaml
44
module XML
5+
# Represents the information extracted from a signed document.
56
class SignedDocumentInfo
67
attr_reader :noko,
78
:check_malformed_doc
@@ -22,16 +23,10 @@ def validate_document(idp_cert_fingerprint, options = {})
2223
# Get certificate from document
2324
if certificate_object
2425
# Calculate fingerprint using specified algorithm
25-
if options[:fingerprint_alg]
26-
fingerprint = certificate_fingerprint(options[:fingerprint_alg])
27-
else
28-
fingerprint = certificate_fingerprint('SHA256')
29-
end
26+
fingerprint = certificate_fingerprint(options[:fingerprint_alg] || 'SHA256')
3027

3128
# Check cert matches registered idp cert fingerprint
32-
if fingerprint != idp_cert_fingerprint.gsub(/[^a-zA-Z0-9]/, '').downcase
33-
raise RubySaml::ValidationError.new('Fingerprint mismatch')
34-
end
29+
raise RubySaml::ValidationError.new('Fingerprint mismatch') if fingerprint != idp_cert_fingerprint.gsub(/[^a-zA-Z0-9]/, '').downcase
3530

3631
cert = certificate_object
3732
elsif options[:cert]
@@ -43,31 +38,25 @@ def validate_document(idp_cert_fingerprint, options = {})
4338
validate_signature(cert)
4439
end
4540

46-
def validate_document_with_cert(idp_cert = true)
41+
def validate_document_with_cert(idp_cert)
4742
# Check saml response cert matches provided idp cert
48-
if certificate_object&.to_pem&.!=(idp_cert.to_pem)
49-
raise RubySaml::ValidationError.new('Certificate of the Signature element does not match provided certificate')
50-
end
43+
raise RubySaml::ValidationError.new('Certificate of the Signature element does not match provided certificate') if certificate_object&.to_pem&.!=(idp_cert.to_pem)
5144

5245
validate_signature(idp_cert)
5346
end
5447

5548
def validate_signature(cert)
5649
# TODO: Remove this
5750
# Get certificate object
58-
if cert.is_a?(String)
59-
cert = OpenSSL::X509::Certificate.new(Base64.decode64(cert))
60-
end
51+
cert = OpenSSL::X509::Certificate.new(Base64.decode64(cert)) if cert.is_a?(String)
6152

6253
# Compare digest
6354
calculated_digest = digest_algorithm.digest(canonicalized_subject)
6455
# puts "calculated_digest: #{calculated_digest.bytes}"
6556
# puts "digest_value: #{digest_value.bytes}"
6657
# puts "subject" + canonicalized_subject.inspect
6758
# puts "\n\n\n\n\n\n"
68-
unless calculated_digest == digest_value
69-
raise RubySaml::ValidationError.new('Digest mismatch')
70-
end
59+
raise RubySaml::ValidationError.new('Digest mismatch') unless calculated_digest == digest_value
7160

7261
# puts "signature_hash_algorithm: #{signature_hash_algorithm}"
7362
# puts "signature_value: #{signature_value.bytes}"
@@ -126,6 +115,7 @@ def reference_node
126115
def subject_id
127116
id = uri_from_reference_node || signature_node.parent&.[]('ID')
128117
return id unless !id || id.empty?
118+
129119
raise RubySaml::ValidationError.new('No signed subject ID found')
130120
end
131121

@@ -186,6 +176,7 @@ def certificate_text
186176
# @return [OpenSSL::X509::Certificate] The certificate
187177
def certificate_object
188178
return unless certificate_text
179+
189180
OpenSSL::X509::Certificate.new(certificate_text)
190181
rescue OpenSSL::X509::CertificateError => _e
191182
# TODO: include underlying error
@@ -206,23 +197,19 @@ def certificate_fingerprint(algorithm = 'SHA256')
206197
# Extract inclusive namespaces from the document
207198
# @return [Array<String>, nil] The inclusive namespaces
208199
def inclusive_namespaces
209-
@inclusive_namespaces ||= begin
210-
noko.at_xpath(
211-
'//ec:InclusiveNamespaces',
212-
{ 'ec' => RubySaml::XML::C14N }
213-
)&.[]('PrefixList')&.split
214-
end
200+
@inclusive_namespaces ||= noko.at_xpath(
201+
'//ec:InclusiveNamespaces',
202+
{ 'ec' => RubySaml::XML::C14N }
203+
)&.[]('PrefixList')&.split
215204
end
216205

217206
private
218207

219208
# Get the ds:Signature element from the document
220209
# @return [Nokogiri::XML::Element] The Signature element
221210
def signature_node
222-
@signature_node ||= begin
223-
noko.at_xpath('//ds:Signature', { 'ds' => RubySaml::XML::DSIG }) ||
224-
(raise RubySaml::ValidationError.new('No Signature node found'))
225-
end
211+
@signature_node ||= noko.at_xpath('//ds:Signature', { 'ds' => RubySaml::XML::DSIG }) ||
212+
(raise RubySaml::ValidationError.new('No Signature node found'))
226213
end
227214

228215
# Get the ds:SignedInfo element from the document
@@ -246,7 +233,7 @@ def canon_algorithm_from_signed_info
246233

247234
def canon_algorithm_from_transforms
248235
transforms = reference_node.xpath('./ds:Transforms/ds:Transform', { 'ds' => RubySaml::XML::DSIG })
249-
transform_element = transforms.reverse.detect { |transform_element| transform_element['Algorithm'] }
236+
transform_element = transforms.reverse.detect { |el| el['Algorithm'] }
250237
RubySaml::XML.canon_algorithm(transform_element, default: false)
251238
end
252239

lib/ruby_saml/xml/signed_document_validator.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
module RubySaml
77
module XML
8+
# Wrapper for the SignedDocumentInfo class.
9+
# TODO: This should be refactored and removed
810
module SignedDocumentValidator
911
extend self
1012

@@ -13,6 +15,7 @@ def with_error_handling(errors, soft)
1315
rescue RubySaml::ValidationError => e
1416
errors << e.message
1517
raise e unless soft
18+
1619
errors # TODO: Return false??
1720
end
1821

0 commit comments

Comments
 (0)