Module: Samlr::Tools::ResponseBuilder

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

Overview

Use this for building test data, not ready to use for production data

Class Method Summary collapse

Class Method Details

.build(options = {}) ⇒ Object



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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/samlr/tools/response_builder.rb', line 11

def self.build(options = {})
  issue_instant   = options[:issue_instant]  || Samlr::Tools::Timestamp.stamp
  response_id     = options[:response_id]    || Samlr::Tools.uuid
  assertion_id    = options[:assertion_id]   || Samlr::Tools.uuid
  status_code     = options[:status_code]    || "urn:oasis:names:tc:SAML:2.0:status:Success"
  name_id_format  = options[:name_id_format] || EMAIL_FORMAT
  subject_conf_m  = options[:subject_conf_m] || "urn:oasis:names:tc:SAML:2.0:cm:bearer"
  version         = options[:version]        || "2.0"
  auth_context    = options[:auth_context]   || "urn:oasis:names:tc:SAML:2.0:ac:classes:Password"
  issuer          = options[:issuer]         || "ResponseBuilder IdP"
  attributes      = options[:attributes]     || {}

  # Mandatory for responses
  destination     = options.fetch(:destination)
  in_response_to  = options.fetch(:in_response_to)
  name_id         = options.fetch(:name_id)
  not_on_or_after = options.fetch(:not_on_or_after)
  not_before      = options.fetch(:not_before)
  audience        = options.fetch(:audience)

  # Signature settings
  sign_assertion  = [ true, false ].member?(options[:sign_assertion]) ? options[:sign_assertion] : true
  sign_response   = [ true, false ].member?(options[:sign_response]) ? options[:sign_response] : true

  # Fixture controls
  skip_assertion  = options[:skip_assertion]
  skip_conditions = options[:skip_conditions]

  builder = Nokogiri::XML::Builder.new(:encoding => "UTF-8") do |xml|
    xml.Response("xmlns:samlp" => NS_MAP["samlp"], "ID" => response_id, "InResponseTo" => in_response_to, "Version" => version, "IssueInstant" => issue_instant, "Destination" => destination) do
      xml.doc.root.add_namespace_definition("saml", NS_MAP["saml"])
      xml.doc.root.namespace = xml.doc.root.namespace_definitions.find { |ns| ns.prefix == "samlp" }

      xml["saml"].Issuer(issuer)
      xml["samlp"].Status { |xml| xml["samlp"].StatusCode("Value" => status_code) }

      unless skip_assertion
        xml["saml"].Assertion("xmlns:saml" => NS_MAP["saml"], "ID" => assertion_id, "IssueInstant" => issue_instant, "Version" => "2.0") do
          xml["saml"].Issuer(issuer)

          xml["saml"].Subject do
            xml["saml"].NameID(name_id, "Format" => name_id_format)

            xml["saml"].SubjectConfirmation("Method" => subject_conf_m) do
              xml["saml"].SubjectConfirmationData("InResponseTo" => in_response_to, "NotOnOrAfter" => not_on_or_after, "Recipient" => destination)
            end
          end

          unless skip_conditions
            xml["saml"].Conditions("NotBefore" => not_before, "NotOnOrAfter" => not_on_or_after) do
              xml["saml"].AudienceRestriction do
                xml["saml"].Audience(audience)
              end
            end
          end

          xml["saml"].AuthnStatement("AuthnInstant" => issue_instant, "SessionIndex" => assertion_id) do
            xml["saml"].AuthnContext do
              xml["saml"].AuthnContextClassRef(auth_context)
            end
          end

          unless attributes.empty?
            xml["saml"].AttributeStatement do
              attributes.keys.sort.each do |name|
                xml["saml"].Attribute("Name" => name) do
                  values = Array(attributes[name])
                  values.each do |value|
                    xml["saml"].AttributeValue(value, "xmlns:xsi" => NS_MAP["xsi"], "xmlns:xs" => NS_MAP["xs"], "xsi:type" => "xs:string")
                  end
                end
              end
            end
          end
        end
      end
    end
  end

  # The core response is ready, not on to signing
  response = builder.doc

  response = sign(response, assertion_id, options) if sign_assertion
  response = sign(response, response_id, options)  if sign_response

  response.to_xml(COMPACT)
end

.digest(document, element, options) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/samlr/tools/response_builder.rb', line 121

def self.digest(document, element, options)
  c14n_method   = options[:c14n_method]   || "http://www.w3.org/2001/10/xml-exc-c14n#"
  sign_method   = options[:sign_method]   || "http://www.w3.org/2000/09/xmldsig#rsa-sha1"
  digest_method = options[:digest_method] || "http://www.w3.org/2000/09/xmldsig#sha1"
  env_signature = options[:env_signature] || "http://www.w3.org/2000/09/xmldsig#enveloped-signature"
  namespaces    = options[:namespaces]    || [ "#default", "samlp", "saml", "ds", "xs", "xsi" ]

  canoned       = element.canonicalize(C14N, namespaces)
  digest_value  = Base64.encode64(OpenSSL::Digest::SHA1.new.digest(canoned)).delete("\n")

  builder = Nokogiri::XML::Builder.new(:encoding => "UTF-8") do |xml|
    xml.Signature("xmlns" => NS_MAP["ds"]) do

      xml.SignedInfo do
        xml.CanonicalizationMethod("Algorithm" => c14n_method)
        xml.SignatureMethod("Algorithm" => sign_method)

        xml.Reference("URI" => "##{element['ID']}") do
          xml.Transforms do
            xml.Transform("Algorithm" => env_signature)
            xml.Transform("Algorithm" => c14n_method) do
              xml.InclusiveNamespaces("xmlns" => c14n_method, "PrefixList" => namespaces.join(" "))
            end
          end
          xml.DigestMethod("Algorithm" => digest_method)
          xml.DigestValue(digest_value)
        end
      end
    end
  end

  builder.doc.root
end

.sign(document, element_id, options) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/samlr/tools/response_builder.rb', line 99

def self.sign(document, element_id, options)
  certificate  = options[:certificate] || Samlr::Tools::CertificateBuilder.new
  element      = document.at("//*[@ID='#{element_id}']")
  digest       = digest(document, element, options)
  canoned      = digest.at("./ds:SignedInfo", NS_MAP).canonicalize(C14N)
  signature    = certificate.sign(canoned)
  skip_keyinfo = options[:skip_keyinfo]

  Nokogiri::XML::Builder.with(digest) do |xml|
    xml.SignatureValue(signature)
    xml.KeyInfo do
      xml.X509Data do
        xml.X509Certificate(certificate.x509_as_pem)
      end
    end unless skip_keyinfo
  end
  # digest.root.last_element_child.after "<SignatureValue>#{signature}</SignatureValue>"
  element.at("./saml:Issuer", NS_MAP).add_next_sibling(digest)

  document
end