diff --git a/lib/SimpleSAML/XML/SAML20/AuthnResponse.php b/lib/SimpleSAML/XML/SAML20/AuthnResponse.php
index d8c2c74f97cbc4a21a38dc981c8f2ccd0e9bb97b..986fbc5a1552d79ff7b9ed0952c551dbbbaf9eb6 100644
--- a/lib/SimpleSAML/XML/SAML20/AuthnResponse.php
+++ b/lib/SimpleSAML/XML/SAML20/AuthnResponse.php
@@ -337,7 +337,35 @@ class SimpleSAML_XML_SAML20_AuthnResponse extends SimpleSAML_XML_AuthnResponse {
 		//echo '<pre>'; print_r($nameID); echo '</pre>';
 		return $nameID;
 	}
-	
+
+
+	/* This function retrieves the ID of the request this response is a
+	 * response to. This ID is stored in the InResponseTo attribute of the
+	 * top level DOM element.
+	 *
+	 * Returns:
+	 *  The ID of the request this response is a response to, or NULL if
+	 *  we don't know.
+	 */
+	public function getInResponseTo() {
+		$dom = $this->getDOM();
+		if($dom === NULL) {
+			return NULL;
+		}
+
+		assert('$dom instanceof DOMDocument');
+
+		$xPath = new DOMXpath($dom);
+		$xPath->registerNamespace('samlp', 'urn:oasis:names:tc:SAML:2.0:protocol');
+
+		$result = $xPath->query('/samlp:Response/@InResponseTo');
+		if($result->length === 0) {
+			return NULL;
+		}
+
+		return $result->item(0)->value;
+	}		
+			
 
 	// Not updated for response. from request.
 	public function generate($idpentityid, $spentityid, $inresponseto, $nameid, $attributes) {