         '@xmlns:saml': XMLNS.SAML
         'saml:Issuer': issuer
         'samlp:Status':
-          'samlp:StatusCode': '@Value': 'urn:oasis:names:tc:SAML:2.0:status:Success'
+          'samlp:StatusCode': '@Value': status
     }, { headless: true }
     ).end()
 


# Takes a compressed/base64 enoded @saml_request and @private_key and signs the request using RSA-SHA256. It returns
# the result as an object containing the query parameters.
sign_get_request = (saml_request, private_key, relay_state) ->
  saml_request_data = "SAMLRequest=" + encodeURIComponent(saml_request)
  relay_state_data = if relay_state? then "&RelayState=" + encodeURIComponent(relay_state) else ""
  sigalg_data = "&SigAlg=" + encodeURIComponent('http://www.w3.org/2001/04/xmldsig-more#rsa-sha256')
  sign = crypto.createSign 'RSA-SHA256'
  sign.update(saml_request_data + relay_state_data + sigalg_data)

  {
    SAMLRequest: saml_request
    RelayState: relay_state
    SigAlg: 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha256'
    Signature: sign.sign(format_pem(private_key, 'PRIVATE KEY'), 'base64')
  }

sign_get_response = (saml_request, private_key) ->
  data = "SAMLResponse=" + encodeURIComponent(saml_request) + "&SigAlg=" + encodeURIComponent('http://www.w3.org/2001/04/xmldsig-more#rsa-sha256')
  sign = crypto.createSign 'RSA-SHA256'
  sign.update(data)

  {
    SAMLResponse: saml_request
    SigAlg: 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha256'
    Signature: sign.sign(format_pem(private_key, 'PRIVATE KEY'), 'base64')
  }

      'samlp:SessionIndex': session_index
  .end()

# Creates a LogoutResponse and returns it as a string of xml.
create_logout_response = (issuer, in_response_to, destination, status='urn:oasis:names:tc:SAML:2.0:status:Success') ->
  xmlbuilder.create(
    {'samlp:LogoutResponse':
        '@Destination': destination
        '@ID': '_' + crypto.randomBytes(21).toString('hex')
        '@InResponseTo': in_response_to
        '@IssueInstant': (new Date()).toISOString()
        '@Version': '2.0'
