Skip to content

Commit d3b5913

Browse files
authored
Merge pull request #536 from JDrizzy/master
Fetch attribute value
2 parents 6f8046e + 1ae0c82 commit d3b5913

3 files changed

Lines changed: 64 additions & 1 deletion

File tree

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,9 @@ Imagine this `saml:AttributeStatement`
467467
<saml:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
468468
<saml:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="1"/>
469469
</saml:Attribute>
470+
<saml:Attribute Name="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname">
471+
<saml:AttributeValue>usersName</saml:AttributeValue>
472+
</saml:Attribute>
470473
</saml:AttributeStatement>
471474
```
472475
@@ -477,7 +480,8 @@ pp(response.attributes) # is an OneLogin::RubySaml::Attributes object
477480
"another_value"=>["value1", "value2"],
478481
"role"=>["role1", "role2", "role3"],
479482
"attribute_with_nil_value"=>[nil],
480-
"attribute_with_nils_and_empty_strings"=>["", "valuePresent", nil, nil]}>
483+
"attribute_with_nils_and_empty_strings"=>["", "valuePresent", nil, nil]
484+
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname"=>["usersName"]}>
481485
482486
# Active single_value_compatibility
483487
OneLogin::RubySaml::Attributes.single_value_compatibility = true
@@ -494,6 +498,9 @@ pp(response.attributes.single(:role))
494498
pp(response.attributes.multi(:role))
495499
# => ["role1", "role2", "role3"]
496500
501+
pp(response.attributes.fetch(:role))
502+
# => "role1"
503+
497504
pp(response.attributes[:attribute_with_nil_value])
498505
# => nil
499506
@@ -509,6 +516,9 @@ pp(response.attributes.single(:not_exists))
509516
pp(response.attributes.multi(:not_exists))
510517
# => nil
511518
519+
pp(response.attributes.fetch(/givenname/))
520+
# => "usersName"
521+
512522
# Deactive single_value_compatibility
513523
OneLogin::RubySaml::Attributes.single_value_compatibility = false
514524
@@ -524,6 +534,9 @@ pp(response.attributes.single(:role))
524534
pp(response.attributes.multi(:role))
525535
# => ["role1", "role2", "role3"]
526536
537+
pp(response.attributes.fetch(:role))
538+
# => ["role1", "role2", "role3"]
539+
527540
pp(response.attributes[:attribute_with_nil_value])
528541
# => [nil]
529542
@@ -538,6 +551,9 @@ pp(response.attributes.single(:not_exists))
538551
539552
pp(response.attributes.multi(:not_exists))
540553
# => nil
554+
555+
pp(response.attributes.fetch(/givenname/))
556+
# => ["usersName"]
541557
```
542558
543559
The `saml:AuthnContextClassRef` of the AuthNRequest can be provided by `settings.authn_context`; possible values are described at [SAMLAuthnCxt]. The comparison method can be set using `settings.authn_context_comparison` parameter. Possible values include: 'exact', 'better', 'maximum' and 'minimum' (default value is 'exact').

lib/onelogin/ruby-saml/attributes.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,25 @@ def ==(other)
113113
end
114114
end
115115

116+
# Fetch attribute value using name or regex
117+
# @param name [String|Regexp] The attribute name
118+
# @return [String|Array] Depending on the single value compatibility status this returns:
119+
# - First value if single_value_compatibility = true
120+
# response.attributes['mail'] # => 'user@example.com'
121+
# - All values if single_value_compatibility = false
122+
# response.attributes['mail'] # => ['user@example.com','user@example.net']
123+
#
124+
def fetch(name)
125+
attributes.each_key do |attribute_key|
126+
if name.is_a?(Regexp)
127+
return self[attribute_key] if name.match?(attribute_key)
128+
elsif canonize_name(name) == canonize_name(attribute_key)
129+
return self[attribute_key]
130+
end
131+
end
132+
nil
133+
end
134+
116135
protected
117136

118137
# stringifies all names so both 'email' and :email return the same result

test/attributes_test.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
2+
3+
require 'onelogin/ruby-saml/attributes'
4+
5+
class AttributesTest < Minitest::Test
6+
describe 'Attributes' do
7+
let(:attributes) do
8+
OneLogin::RubySaml::Attributes.new({
9+
'email' => ['tom@hanks.com'],
10+
'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname' => ['Tom'],
11+
'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname' => ['Hanks']
12+
})
13+
end
14+
15+
it 'fetches string attribute' do
16+
assert_equal('tom@hanks.com', attributes.fetch('email'))
17+
end
18+
19+
it 'fetches symbol attribute' do
20+
assert_equal('tom@hanks.com', attributes.fetch(:email))
21+
end
22+
23+
it 'fetches regexp attribute' do
24+
assert_equal('Tom', attributes.fetch(/givenname/))
25+
assert_equal('Hanks', attributes.fetch(/surname/))
26+
end
27+
end
28+
end

0 commit comments

Comments
 (0)