Skip to content

Commit ec7d7ba

Browse files
committed
Restored support for NIL as well as empty AttributeValues
1 parent 9eab546 commit ec7d7ba

4 files changed

Lines changed: 35 additions & 7 deletions

File tree

lib/onelogin/ruby-saml/attribute_value.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ module RubySaml
44
# Wrapper for AttributeValue with multiple values
55
# It is subclass of String to be backwards compatible
66
# Use AttributeValue#values to get all values as an array
7-
class AttributeValue < String
8-
attr_accessor :values
9-
def initialize(str="", values=[])
7+
module AttributeValue
8+
def values
9+
@values ||= []
10+
@values
11+
end
12+
def values=(values)
1013
@values = values
11-
super(str)
1214
end
1315
end
1416
end

lib/onelogin/ruby-saml/response.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,17 @@ def attributes
6060

6161
stmt_element.elements.each do |attr_element|
6262
name = attr_element.attributes["Name"]
63-
values = attr_element.elements.collect(&:text)
63+
values = attr_element.elements.collect{|e|
64+
# SAMLCore requires that nil AttributeValues MUST contain xsi:nil XML attribute set to "true" or "1"
65+
# otherwise the value is to be regarded as empty.
66+
["true", "1"].include?(e.attributes['xsi:nil']) ? nil : e.text.to_s
67+
}
68+
69+
# Monkey-patch first value to contain all values (so that the type is retained)
70+
attr_value = values.first
71+
attr_value.extend AttributeValue
72+
attr_value.values = values.reverse # retain XML order
6473

65-
# Set up a string-like wrapper for the values array
66-
attr_value = AttributeValue.new(values.first, values.reverse)
6774
# Merge values if the Attribute has already been seen
6875
if result[name]
6976
attr_value.values += result[name].values

test/response_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,16 @@ class RubySamlTest < Test::Unit::TestCase
249249
response = OneLogin::RubySaml::Response.new(fixture(:response_with_multiple_attribute_values))
250250
assert_equal ['role2', 'role1'], response.attributes[:role].values
251251
end
252+
253+
should "return nil value correctly" do
254+
response = OneLogin::RubySaml::Response.new(fixture(:response_with_multiple_attribute_values))
255+
assert_nil response.attributes[:attribute_with_nil_value]
256+
end
257+
258+
should "return multiple values including nil and empty string" do
259+
response = OneLogin::RubySaml::Response.new(fixture(:response_with_multiple_attribute_values))
260+
assert_equal [nil, nil, "valuePresent", ""], response.attributes[:attribute_with_nils_and_empty_strings].values
261+
end
252262
end
253263
end
254264

test/responses/response_with_multiple_attribute_values.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@
5252
<saml:Attribute Name="role">
5353
<saml:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">role2</saml:AttributeValue>
5454
</saml:Attribute>
55+
<saml:Attribute Name="attribute_with_nil_value">
56+
<saml:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
57+
</saml:Attribute>
58+
<saml:Attribute Name="attribute_with_nils_and_empty_strings">
59+
<saml:AttributeValue/>
60+
<saml:AttributeValue>valuePresent</saml:AttributeValue>
61+
<saml:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
62+
<saml:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="1"/>
63+
</saml:Attribute>
5564
</saml:AttributeStatement>
5665
</saml:Assertion>
5766
</samlp:Response>

0 commit comments

Comments
 (0)