Class: InformationCard::SamlToken
- Inherits:
-
IdentityToken
- Object
- IdentityToken
- InformationCard::SamlToken
- Defined in:
- lib/vendor/information_card/saml_token.rb
Instance Method Summary collapse
- #errors ⇒ Object
- #get_X509Certificate(certificate) ⇒ Object
- #process_claims ⇒ Object
- #validate_conditions ⇒ Object
- #verify_digest ⇒ Object
- #verify_signature ⇒ Object
Instance Method Details
#errors ⇒ Object
34 35 36 |
# File 'lib/vendor/information_card/saml_token.rb', line 34 def errors return @errors end |
#get_X509Certificate(certificate) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/vendor/information_card/saml_token.rb', line 129 def get_X509Certificate(certificate) encoding = "-----BEGIN CERTIFICATE-----\n" offset = 0; # strip out the newlines certificate.delete!("=\n") while (segment = certificate[offset, 64]) encoding = encoding + segment + "\n" offset += 64 end encoding = encoding + "-----END CERTIFICATE-----\n" OpenSSL::X509::Certificate.new(encoding) end |
#process_claims ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/vendor/information_card/saml_token.rb', line 111 def process_claims attribute_nodes = XPath.match(@doc, "//saml:AttributeStatement/saml:Attribute", {"saml" => Namespaces::SAML_ASSERTION}) attribute_nodes.each do |node| key = ClaimTypes.lookup(node.attributes['AttributeNamespace'], node.attributes['AttributeName']) value_nodes = XPath.match(node, "saml:AttributeValue", "saml" => Namespaces::SAML_ASSERTION) if (value_nodes.length < 2 or value_nodes.empty?) @claims[key] = value_nodes[0].text else claim_values = [] value_nodes.each{ |value_node| claim_values << value_node.text } @claims[key] = claim_values end end end |
#validate_conditions ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/vendor/information_card/saml_token.rb', line 98 def validate_conditions conditions = XPath.first(@doc, "//saml:Conditions", "saml" => Namespaces::SAML_ASSERTION) condition_errors = {} not_before_time = Time.parse(conditions.attributes['NotBefore']) condition_errors[:not_before] = "Time is before #{not_before_time}" if Time.now.utc < not_before_time not_on_or_after_time = Time.parse(conditions.attributes['NotOnOrAfter']) condition_errors[:not_on_or_after] = "Time is on or after #{not_on_or_after_time}" if Time.now.utc >= not_on_or_after_time @errors[:conditions] = condition_errors unless condition_errors.empty? end |
#verify_digest ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/vendor/information_card/saml_token.rb', line 38 def verify_digest working_doc = REXML::Document.new(@doc.to_s) assertion_node = XPath.first(working_doc, "saml:Assertion", {"saml" => Namespaces::SAML_ASSERTION}) signature_node = XPath.first(assertion_node, "ds:Signature", {"ds" => Namespaces::DS}) signed_info_node = XPath.first(signature_node, "ds:SignedInfo", {"ds" => Namespaces::DS}) digest_value_node = XPath.first(signed_info_node, "ds:Reference/ds:DigestValue", {"ds" => Namespaces::DS}) digest_value = digest_value_node.text signature_node.remove digest_errors = [] canonicalizer = InformationCard::XmlCanonicalizer.new reference_nodes = XPath.match(signed_info_node, "ds:Reference", {"ds" => Namespaces::DS}) # TODO: Check specification to see if digest is required. @errors[:digest] = "No reference nodes to check digest" and return if reference_nodes.nil? or reference_nodes.empty? reference_nodes.each do |node| uri = node.attributes['URI'] nodes_to_verify = XPath.match(working_doc, "saml:Assertion[@AssertionID='#{uri[1..uri.size]}']", {"saml" => Namespaces::SAML_ASSERTION}) nodes_to_verify.each do |node| canonicalized_signed_info = canonicalizer.canonicalize(node) signed_node_hash_sha1 = Base64.encode64(Digest::SHA1.digest(canonicalized_signed_info)).chomp signed_node_hash_sha256 = Base64.encode64(Digest::SHA256.digest(canonicalized_signed_info)).chomp unless signed_node_hash_sha1 == digest_value unless signed_node_hash_sha256 == digest_value digest_errors << "Invalid Digest for #{uri}. Expected #{signed_node_hash} but was #{digest_value}" end end end @errors[:digest] = digest_errors unless digest_errors.empty? end end |
#verify_signature ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/vendor/information_card/saml_token.rb', line 75 def verify_signature working_doc = REXML::Document.new(@doc.to_s) assertion_node = XPath.first(working_doc, "saml:Assertion", {"saml" => Namespaces::SAML_ASSERTION}) signature_node = XPath.first(assertion_node, "ds:Signature", {"ds" => Namespaces::DS}) certificate_value_node = XPath.first(signature_node, "KeyInfo/X509Data/X509Certificate") certificate = get_X509Certificate(certificate_value_node.text) # TODO: here you should validate that the presented certificate is valid public_key_string = certificate.public_key signed_info_node = XPath.first(signature_node, "ds:SignedInfo", {"ds" => Namespaces::DS}) signature_value_node = XPath.first(signature_node, "ds:SignatureValue", {"ds" => Namespaces::DS}) canonicalized_signed_info = InformationCard::XmlCanonicalizer.new.canonicalize(signed_info_node) signature = Base64.decode64(signature_value_node.text) unless public_key_string.verify(OpenSSL::Digest::SHA1.new, signature, canonicalized_signed_info) unless public_key_string.verify(OpenSSL::Digest::SHA256.new, signature, canonicalized_signed_info) @errors[:signature] = "Invalid Signature" end end end |