Skip to content

Commit 93a2b84

Browse files
author
Ana Martinez
committed
Support for logout options
1 parent 0424df0 commit 93a2b84

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
lines changed

lib/samlr/tools/logout_request_builder.rb

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,39 @@ module Tools
55
# Use this for building the SAML logout request XML
66
module LogoutRequestBuilder
77
def self.build(options = {})
8-
name_id_format = options[:name_id_format] || EMAIL_FORMAT
9-
108
# Mandatory
119
name_id = options.fetch(:name_id)
1210
issuer = options.fetch(:issuer)
1311

1412
builder = Nokogiri::XML::Builder.new do |xml|
1513
xml.LogoutRequest("xmlns:samlp" => NS_MAP["samlp"], "xmlns:saml" => NS_MAP["saml"], "ID" => Samlr::Tools.uuid, "IssueInstant" => Samlr::Tools::Timestamp.stamp, "Version" => "2.0") do
1614
xml.doc.root.namespace = xml.doc.root.namespace_definitions.find { |ns| ns.prefix == "samlp" }
17-
1815
xml["saml"].Issuer(issuer)
19-
xml["saml"].NameID(name_id, "Format" => name_id_format)
16+
xml["saml"].NameID(name_id, logout_options(options))
2017
end
2118
end
2219

2320
builder.to_xml(COMPACT)
2421
end
22+
23+
def self.logout_options(options)
24+
name_id_options = options[:name_id_options] || {}
25+
options = { "Format" => format_option(options)}
26+
options.merge!("NameQualifier" => name_id_options[:name_qualifier]) if name_id_options[:name_qualifier]
27+
options.merge!("SPNameQualifier" => name_id_options[:spname_qualifier]) if name_id_options[:spname_qualifier]
28+
options
29+
end
30+
31+
def self.format_option(options)
32+
if options[:name_id_format]
33+
warn "[DEPRECATION] options[:name_id_format] is deprecated. Please use options[:name_id_options][:format] instead"
34+
options[:name_id_format]
35+
elsif options[:name_id_options] && options[:name_id_options][:format]
36+
options[:name_id_options][:format]
37+
else
38+
EMAIL_FORMAT
39+
end
40+
end
2541
end
2642
end
2743
end

test/unit/test_logout_request.rb

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
require File.expand_path("test/test_helper")
22

33
describe Samlr::LogoutRequest do
4-
before do
5-
@request = Samlr::LogoutRequest.new(
4+
let(:options) {
5+
{
66
:issuer => "https://sp.example.com/saml2",
77
:name_id => "[email protected]"
8-
)
8+
}
9+
}
10+
11+
before do
12+
@request = Samlr::LogoutRequest.new(options)
913
end
1014

1115
describe "#body" do
@@ -36,4 +40,34 @@
3640
end
3741
end
3842
end
43+
44+
describe "with optional params" do
45+
it "understands name_id_format" do
46+
options.merge!(:name_id_format => "some format")
47+
request = Samlr::LogoutRequest.new(options)
48+
49+
assert_match /<saml:NameID Format="some format">/, request.body
50+
end
51+
52+
it "understands [:name_id_options][:format]" do
53+
options.merge!(:name_id_options => {:format => "some format"})
54+
request = Samlr::LogoutRequest.new(options)
55+
56+
assert_match /<saml:NameID Format="some format">/, request.body
57+
end
58+
59+
it "understands NameQualifier" do
60+
options.merge!(:name_id_options => {:name_qualifier => "Some name qualifier"})
61+
request = Samlr::LogoutRequest.new(options)
62+
63+
assert_match /NameQualifier="Some name qualifier"/, request.body
64+
end
65+
66+
it "understands SPNameQualifier" do
67+
options.merge!(:name_id_options => {:spname_qualifier => "Some SPName qualifier"})
68+
request = Samlr::LogoutRequest.new(options)
69+
70+
assert_match /SPNameQualifier="Some SPName qualifier"/, request.body
71+
end
72+
end
3973
end

0 commit comments

Comments
 (0)