Skip to content

Commit cb78e6c

Browse files
committed
Adds Response#name_id_format
1 parent c47dc02 commit cb78e6c

2 files changed

Lines changed: 38 additions & 8 deletions

File tree

lib/onelogin/ruby-saml/response.rb

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,25 @@ def is_valid?
8282
# @return [String] the NameID provided by the SAML response from the IdP.
8383
#
8484
def name_id
85-
@name_id ||= begin
86-
encrypted_node = xpath_first_from_signed_assertion('/a:Subject/a:EncryptedID')
87-
if encrypted_node
88-
node = decrypt_nameid(encrypted_node)
89-
else
90-
node = xpath_first_from_signed_assertion('/a:Subject/a:NameID')
85+
@name_id ||=
86+
if name_id_node
87+
name_id_node.text
9188
end
92-
node.nil? ? nil : node.text
93-
end
9489
end
9590

9691
alias_method :nameid, :name_id
9792

93+
# @return [String] the NameID Format provided by the SAML response from the IdP.
94+
#
95+
def name_id_format
96+
@name_id_format ||=
97+
if name_id_node && name_id_node.attribute("Format")
98+
name_id_node.attribute("Format").value
99+
end
100+
end
101+
102+
alias_method :nameid_format, :name_id_format
103+
98104

99105
# Gets the SessionIndex from the AuthnStatement.
100106
# Could be used to be stored in the local session in order
@@ -638,6 +644,18 @@ def validate_signature
638644
true
639645
end
640646

647+
def name_id_node
648+
@name_id_node ||=
649+
begin
650+
encrypted_node = xpath_first_from_signed_assertion('/a:Subject/a:EncryptedID')
651+
if encrypted_node
652+
node = decrypt_nameid(encrypted_node)
653+
else
654+
node = xpath_first_from_signed_assertion('/a:Subject/a:NameID')
655+
end
656+
end
657+
end
658+
641659
# Extracts the first appearance that matchs the subelt (pattern)
642660
# Search on any Assertion that is signed, or has a Response parent signed
643661
# @param subelt [String] The XPath pattern

test/response_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,13 @@ class RubySamlTest < Minitest::Test
774774
end
775775
end
776776

777+
describe "#name_id_format" do
778+
it "extract the value of the name id element" do
779+
assert_equal "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress", response.name_id_format
780+
assert_equal "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress", response_with_signed_assertion.name_id_format
781+
end
782+
end
783+
777784
describe "#sessionindex" do
778785
it "extract the value of the sessionindex element" do
779786
response = OneLogin::RubySaml::Response.new(fixture(:simple_saml_php))
@@ -1001,12 +1008,17 @@ class RubySamlTest < Minitest::Test
10011008
assert_raises(OneLogin::RubySaml::ValidationError, "An EncryptedID found and no SP private key found on the settings to decrypt it") do
10021009
assert_equal "test@onelogin.com", response_encrypted_nameid.nameid
10031010
end
1011+
1012+
assert_raises(OneLogin::RubySaml::ValidationError, "An EncryptedID found and no SP private key found on the settings to decrypt it") do
1013+
assert_equal "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress", response_encrypted_nameid.name_id_format
1014+
end
10041015
end
10051016

10061017
it 'is possible when encryptID inside the assertion and settings has the private key' do
10071018
settings.private_key = ruby_saml_key_text
10081019
response_encrypted_nameid.settings = settings
10091020
assert_equal "test@onelogin.com", response_encrypted_nameid.nameid
1021+
assert_equal "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress", response_encrypted_nameid.name_id_format
10101022
end
10111023

10121024
end

0 commit comments

Comments
 (0)