Skip to content

Commit 02c6903

Browse files
committed
Add class AttributeValue that fixes onelogin#117
AttributeValue is a String, but `AttributeValue#values` will return all values for that attribute.
1 parent 9dee0ea commit 02c6903

4 files changed

Lines changed: 88 additions & 2 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module OneLogin
2+
module RubySaml
3+
4+
# Wrapper for AttributeValue with multiple values
5+
# It is subclass of String to be backwards compatible
6+
# Use AttributeValue#values to get all values as an array
7+
class AttributeValue < String
8+
attr_accessor :values
9+
def initialize(str="", values=[])
10+
@values = values
11+
super(str)
12+
end
13+
end
14+
end
15+
end

lib/onelogin/ruby-saml/response.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require "xml_security"
22
require "time"
33
require "nokogiri"
4+
require File.expand_path(File.join(File.dirname(__FILE__), 'attribute_value.rb'))
45

56
# Only supports SAML 2.0
67
module OneLogin
@@ -58,9 +59,11 @@ def attributes
5859

5960
stmt_element.elements.each do |attr_element|
6061
name = attr_element.attributes["Name"]
61-
value = attr_element.elements.first.text
62+
values = attr_element.elements.collect(&:text)
6263

63-
result[name] = value
64+
# Set up a string-like wrapper for the values array
65+
attr_value = AttributeValue.new(values.first, values)
66+
result[name] = attr_value
6467
end
6568

6669
result.keys.each do |key|

test/response_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,23 @@ class RubySamlTest < Test::Unit::TestCase
223223
response = OneLogin::RubySaml::Response.new(response_document_4)
224224
assert_equal Hash.new, response.attributes
225225
end
226+
227+
context "#multiple values" do
228+
should "extract single value as string" do
229+
response = OneLogin::RubySaml::Response.new(fixture(:response_with_multiple_attribute_values))
230+
assert_equal "demo", response.attributes[:uid]
231+
end
232+
233+
should "extract first of multiple values as string for b/w compatibility" do
234+
response = OneLogin::RubySaml::Response.new(fixture(:response_with_multiple_attribute_values))
235+
assert_equal 'value1', response.attributes[:another_value]
236+
end
237+
238+
should "return array with all attributes when asked" do
239+
response = OneLogin::RubySaml::Response.new(fixture(:response_with_multiple_attribute_values))
240+
assert_equal ['value1', 'value2'], response.attributes[:another_value].values
241+
end
242+
end
226243
end
227244

228245
context "#session_expires_at" do
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<samlp:Response xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" ID="GOSAMLR12901174571794" Version="2.0" IssueInstant="2010-11-18T21:57:37Z" Destination="{recipient}">
2+
<samlp:Status>
3+
<samlp:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/></samlp:Status>
4+
<saml:Assertion xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Version="2.0" ID="pfxa46574df-b3b0-a06a-23c8-636413198772" IssueInstant="2010-11-18T21:57:37Z">
5+
<saml:Issuer>https://app.onelogin.com/saml/metadata/13590</saml:Issuer>
6+
<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
7+
<ds:SignedInfo>
8+
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
9+
<ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
10+
<ds:Reference URI="#pfxa46574df-b3b0-a06a-23c8-636413198772">
11+
<ds:Transforms>
12+
<ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
13+
<ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
14+
</ds:Transforms>
15+
<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
16+
<ds:DigestValue>pJQ7MS/ek4KRRWGmv/H43ReHYMs=</ds:DigestValue>
17+
</ds:Reference>
18+
</ds:SignedInfo>
19+
<ds:SignatureValue>yiveKcPdDpuDNj6shrQ3ABwr/cA3CryD2phG/xLZszKWxU5/mlaKt8ewbZOdKKvtOs2pHBy5Dua3k94AF+zxGyel5gOowmoyXJr+AOr+kPO0vli1V8o3hPPUZwRgSX6Q9pS1CqQghKiEasRyylqqJUaPYzmOzOE8/XlMkwiWmO0=</ds:SignatureValue>
20+
<ds:KeyInfo>
21+
<ds:X509Data>
22+
<ds:X509Certificate>MIIBrTCCAaGgAwIBAgIBATADBgEAMGcxCzAJBgNVBAYTAlVTMRMwEQYDVQQIDApDYWxpZm9ybmlhMRUwEwYDVQQHDAxTYW50YSBNb25pY2ExETAPBgNVBAoMCE9uZUxvZ2luMRkwFwYDVQQDDBBhcHAub25lbG9naW4uY29tMB4XDTEwMDMwOTA5NTg0NVoXDTE1MDMwOTA5NTg0NVowZzELMAkGA1UEBhMCVVMxEzARBgNVBAgMCkNhbGlmb3JuaWExFTATBgNVBAcMDFNhbnRhIE1vbmljYTERMA8GA1UECgwIT25lTG9naW4xGTAXBgNVBAMMEGFwcC5vbmVsb2dpbi5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAOjSu1fjPy8d5w4QyL1+zd4hIw1Mkkff4WY/TLG8OZkU5YTSWmmHPD5kvYH5uoXS/6qQ81qXpR2wV8CTowZJULg09ddRdRn8Qsqj1FyOC5slE3y2bZ2oFua72of/49fpujnFT6KnQ61CBMqlDoTQqOT62vGJ8nP6MZWvA6sxqud5AgMBAAEwAwYBAAMBAA==</ds:X509Certificate>
23+
</ds:X509Data>
24+
</ds:KeyInfo>
25+
</ds:Signature>
26+
<saml:Subject>
27+
<saml:NameID Format="urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress">support@onelogin.com</saml:NameID>
28+
<saml:SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer">
29+
<saml:SubjectConfirmationData NotOnOrAfter="2010-11-18T22:02:37Z" Recipient="{recipient}"/></saml:SubjectConfirmation>
30+
</saml:Subject>
31+
<saml:Conditions NotBefore="2010-11-18T21:52:37Z" NotOnOrAfter="2010-11-18T22:02:37Z">
32+
<saml:AudienceRestriction>
33+
<saml:Audience>{audience}</saml:Audience>
34+
</saml:AudienceRestriction>
35+
</saml:Conditions>
36+
<saml:AuthnStatement AuthnInstant="2010-11-18T21:57:37Z" SessionNotOnOrAfter="2010-11-19T21:57:37Z" SessionIndex="_531c32d283bdff7e04e487bcdbc4dd8d">
37+
<saml:AuthnContext>
38+
<saml:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:Password</saml:AuthnContextClassRef>
39+
</saml:AuthnContext>
40+
</saml:AuthnStatement>
41+
<saml:AttributeStatement>
42+
<saml:Attribute Name="uid">
43+
<saml:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">demo</saml:AttributeValue>
44+
</saml:Attribute>
45+
<saml:Attribute Name="another_value">
46+
<saml:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">value1</saml:AttributeValue>
47+
<saml:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">value2</saml:AttributeValue>
48+
</saml:Attribute>
49+
</saml:AttributeStatement>
50+
</saml:Assertion>
51+
</samlp:Response>

0 commit comments

Comments
 (0)