 
 # 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) ->
-  data = "SAMLRequest=" + encodeURIComponent(saml_request) + "&SigAlg=" + encodeURIComponent('http://www.w3.org/2001/04/xmldsig-more#rsa-sha256')
+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)
 
   {
-    SAMLRequest: saml_request
+    SAMLResponse: saml_request
     SigAlg: 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha256'
     Signature: sign.sign(format_pem(private_key, 'PRIVATE KEY'), 'base64')
   }

        response_header.destination = attr.value
      when "InResponseTo"
        response_header.in_response_to = attr.value
  response_header

# Takes in an xml @dom of an object containing a SAML Assertion and returns the NameID. If there is no NameID found,


      async.waterfall [
        (cb_wf) ->
          raw = new Buffer(request_body.SAMLResponse || request_body.SAMLRequest, 'base64')
          # For GET requests, it's necessary to inflate the response before parsing it.
          if (get_request)
            return zlib.inflateRaw raw, cb_wf
