Module: Samlr::Tools::RequestBuilder

Defined in:
lib/samlr/tools/request_builder.rb

Overview

Use this for building the SAML auth request XML

Class Method Summary collapse

Class Method Details

.build(options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/samlr/tools/request_builder.rb', line 8

def self.build(options = {})
  consumer_service_url = options[:consumer_service_url]
  issuer               = options[:issuer]
  name_identity_format = options[:name_identity_format]
  allow_create         = options[:allow_create] || "true"
  authn_context        = options[:authn_context]

  builder = Nokogiri::XML::Builder.new do |xml|
    xml.AuthnRequest("xmlns:samlp" => NS_MAP["samlp"], "xmlns:saml" => NS_MAP["saml"], "ID" => Samlr::Tools.uuid, "IssueInstant" => Samlr::Tools::Timestamp.stamp, "Version" => "2.0") do
      xml.doc.root.namespace = xml.doc.root.namespace_definitions.find { |ns| ns.prefix == "samlp" }

      unless consumer_service_url.nil?
        xml.doc.root["AssertionConsumerServiceURL"] = consumer_service_url
      end

      unless issuer.nil?
        xml["saml"].Issuer(issuer)
      end

      unless name_identity_format.nil?
        xml["samlp"].NameIDPolicy("AllowCreate" => allow_create, "Format" => name_identity_format)
      end

      unless authn_context.nil?
        xml["samlp"].RequestedAuthnContext("Comparison" => "exact") do
          xml["saml"].AuthnContextClassRef(authn_context)
        end
      end
    end
  end

  builder.to_xml(COMPACT)
end