diff --git a/modules/saml2/www/debug.php b/modules/saml2/www/debug.php new file mode 100644 index 0000000000000000000000000000000000000000..7c6b44cedaa18f79349abebd4756924bd875ced4 --- /dev/null +++ b/modules/saml2/www/debug.php @@ -0,0 +1,83 @@ +<?php + +/** + * Endpoint for debugging sent SAML-messages. + * + * This endpoint will display the message to the user before passing it + * to its destination. + * + * @package simpleSAMLphp + * @version $Id$ + */ + +$globalConfig = SimpleSAML_Configuration::getInstance(); + +if (array_key_exists('SAMLRequest', $_REQUEST)) { + $type = 'SAMLRequest'; +} elseif (array_key_exists('SAMLResponse', $_REQUEST)) { + $type = 'SAMLResponse'; +} else { + throw new SimpleSAML_Error_BadRequest('Unknown SAML2 message type.'); +} + +$message = $_REQUEST[$type]; + +$message = @base64_decode($message); +if ($message === FALSE) { + throw new SimpleSAML_Error_BadRequest('Unable to base64-decode message.'); +} + +if ($_SERVER['REQUEST_METHOD'] === 'GET') { + $message = @gzinflate($message); + if ($message === FALSE) { + throw new SimpleSAML_Error_BadRequest('Unable to gzinflate message.'); + } +} + +$document = new DOMDocument(); +if (!$document->loadXML($message)) { + throw new SimpleSAML_Error_BadRequest('Unable to parse XML.'); +} +$root = $document->firstChild; + +if (!$root->hasAttribute('Destination')) { + throw new SimpleSAML_Error_BadRequest('Missing Destination-attribute on root element.'); +} +$realDestination = $root->getAttribute('Destination'); + +SimpleSAML_Utilities::formatDOMElement($root); +$message = $document->saveXML($root); + + +switch($_SERVER['REQUEST_METHOD']) { +case 'GET': + $queryString = $_SERVER['QUERY_STRING']; + + if (strpos($realDestination, '?') === FALSE) { + $url = $realDestination . '?' . $queryString; + } else { + $url = $realDestination . '&' . $queryString; + } + + $t = new SimpleSAML_XHTML_Template($globalConfig, 'httpredirect-debug.php'); + $t->data['url'] = $url; + $t->data['message'] = htmlspecialchars($message); + $t->show(); + exit(); + +case 'POST': + $post = $_POST; + + $t = new SimpleSAML_XHTML_Template($globalConfig, 'post-debug.php'); + + $t->data['post'] = $post; + $t->data['destination'] = $realDestination; + $t->data['responseHTML'] = htmlspecialchars($message); + $t->show(); + exit(); + +default: + throw new SimpleSAML_Error_BadRequest('Unexpected request method: ' . var_export($_SERVER['REQUEST_METHOD'], TRUE)); +} + +?> \ No newline at end of file diff --git a/templates/post-debug.php b/templates/post-debug.php index 2402f1afb93bfd4b7a488d1eab191696fda4c689..a1d4a9c218e7d63fbac42f84314e0346c8676578 100644 --- a/templates/post-debug.php +++ b/templates/post-debug.php @@ -2,6 +2,50 @@ $this->data['icon'] = 'debug.png'; $this->data['autofocus'] = 'sendbutton'; $this->includeAtTemplateBase('includes/header.php'); + +if (array_key_exists('post', $this->data)) { + $post = $this->data['post']; +} else { + /* For backwards compatibility. */ + assert('array_key_exists("response", $this->data)'); + assert('array_key_exists("RelayStateName", $this->data)'); + assert('array_key_exists("RelayState", $this->data)'); + + $post = array( + 'SAMLResponse' => $this->data['response'], + $this->data['RelayStateName'] => $this->data['RelayState'], + ); +} + +/** + * Write out one or more INPUT elements for the given name-value pair. + * + * If the value is a string, this function will write a single INPUT element. + * If the value is an array, it will write multiple INPUT elements to + * recreate the array. + * + * @param string $name The name of the element. + * @param string|array $value The value of the element. + */ +function printItem($name, $value) { + assert('is_string($name)'); + assert('is_string($value) || is_array($value)'); + + if (is_string($value)) { + echo '<input type="hidden" name="' . htmlspecialchars($name) . '" value="' . htmlspecialchars($value) . '" />'; + return; + } + + /* This is an array... */ + foreach ($value as $index => $item) { + printItem($name . '[' . var_export($index, TRUE) . ']', $item); + } +} + +foreach ($post as $name => $value) { + printItem($name, $value); +} + ?> @@ -11,8 +55,11 @@ <p><?php echo($this->t('{admin:debug_sending_message_text_button}')); ?></p> <form method="post" action="<?php echo htmlspecialchars($this->data['destination']); ?>"> - <input type="hidden" name="SAMLResponse" value="<?php echo htmlspecialchars($this->data['response']); ?>" /> - <input type="hidden" name="<?php echo htmlspecialchars($this->data['RelayStateName']); ?>" value="<?php echo htmlspecialchars($this->data['RelayState']); ?>" /> +<?php +foreach ($post as $name => $value) { + printItem($name, $value); +} +?> <input type="submit" value="<?php echo($this->t('{admin:debug_sending_message_send}')); ?>" id="sendbutton" /> </form>