 check_status_success = (dom) ->
   status = dom.getElementsByTagNameNS(XMLNS.SAMLP, 'Status')
   return false unless status.length is 1
-  for status_code in status[0].childNodes
+  for status_code in status[0].childNodes or []
     if status_code.attributes?
-      for attr in status_code.attributes
-        return true if attr.name is 'Value' and attr.value is 'urn:oasis:names:tc:SAML:2.0:status:Success'
+      for attr in status_code.attributes or []
+        return true if attr?.name is 'Value' and attr?.value is 'urn:oasis:names:tc:SAML:2.0:status:Success'
   false
 
 get_status = (dom) ->

  throw new Error("Expected 1 Response; found #{response.length}") unless response.length is 1

  response_header = {}
  for attr in response[0].attributes
    switch attr.name
      when "Version"
        throw new Error "Invalid SAML Version #{attr.value}" unless attr.value is "2.0"
      when "Destination"


# Creates metadata and returns it as a string of XML. The metadata has one POST assertion endpoint.
create_metadata = (entity_id, assert_endpoint, signing_certificates, encryption_certificates) ->
  signing_cert_descriptors = for signing_certificate in signing_certificates or []
    {'md:KeyDescriptor': certificate_to_keyinfo('signing', signing_certificate)}

  encryption_cert_descriptors = for encryption_certificate in encryption_certificates or []
    {'md:KeyDescriptor': certificate_to_keyinfo('encryption', encryption_certificate)}

  xmlbuilder.create
