 
 # 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)
+sign_get_request = (saml_request, private_key, relay_state, response=false) ->
+  action = if response then "SAMLResponse" else "SAMLRequest"
+  data = "#{action}=" + encodeURIComponent(saml_request)
+  if relay_state
+    data += "&RelayState=" + encodeURIComponent(relay_state)
+  data += "&SigAlg=" + encodeURIComponent('http://www.w3.org/2001/04/xmldsig-more#rsa-sha256')
+
+  saml_request_data = "#{action}=" + 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)
 
+  saml_response_out = saml_request if response
+  saml_request_out = saml_request unless response
   {
-    SAMLRequest: saml_request
+    SAMLResponse: saml_response_out
+    SAMLRequest: saml_request_out
     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')
   }

          async.lift(parse_response_header) saml_response, cb_wf
        (response_header, cb_wf) =>
          response = { response_header }
          #cb_wf new Error("SAML Response was not success!") unless check_status_success(saml_response)
          switch
            when saml_response.getElementsByTagNameNS(XMLNS.SAMLP, 'Response').length is 1
              response.type = 'authn_response'
              parse_authn_response saml_response, @private_key, identity_provider.certificates, cb_wf
            when saml_response.getElementsByTagNameNS(XMLNS.SAMLP, 'LogoutResponse').length is 1
              response.type = 'logout_response'
              setImmediate cb_wf, null, {}
            when saml_response.getElementsByTagNameNS(XMLNS.SAMLP, 'LogoutRequest').length is 1

      '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'
