PageRenderTime 39ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/fedlab/lib/tests/SLOTest.php

http://simplesamlphp-labs.googlecode.com/
PHP | 202 lines | 112 code | 71 blank | 19 comment | 8 complexity | 775b793e55d91fafc255209f72e7fc51 MD5 | raw file
  1. <?php
  2. class sspmod_fedlab_tests_SLOTest extends sspmod_fedlab_BasicSPTest {
  3. protected function register() {
  4. $this->testruns = array('BasicSLOTest' => 'Basic SP-initated Logout Test');
  5. }
  6. public function run($testrun) {
  7. $this->crawler->reset();
  8. // Get authentication request
  9. $result1 = $this->crawler->getURLraw($this->initurl);
  10. // Fail if no authentication request was made
  11. if (!isset($result1['Request'])) throw new Exception('Initiation URL did not return a authentication request');
  12. $request = $result1['Request'];
  13. $requestRaw = $result1['RequestRaw'];
  14. $relaystate = $result1['RelayState'];
  15. // Create Response
  16. $samlResponse = $this->createResponse($testrun, $request, $relaystate);
  17. // Sent response and get web page as result
  18. $result2 = $this->crawler->sendResponse($samlResponse['url'], $samlResponse['Response'], $samlResponse['RelayState']);
  19. // Check output
  20. $this->requireLoginOK($testrun, $result2['body']);
  21. // SP Initiated Logout...
  22. $result3 = $this->crawler->getURLraw($this->initslo);
  23. $logoutRequest = $result3['Request'];
  24. $logoutRequestRaw = $result3['RequestRaw'];
  25. $logoutRelayState = $result3['RelayState'];
  26. // Create logout response
  27. $this->log($testrun, 'Creating LogoutResponse');
  28. $logoutResponse = $this->createLogoutResponse($testrun, $logoutRequest, $logoutRelayState);
  29. $binding = new SAML2_HTTPRedirect();
  30. $binding->setDestination($logoutResponse['url']);
  31. $redirURL = $binding->getRedirectURL($logoutResponse['ResponseObj']);
  32. $this->log($testrun, 'Sending LogoutResponse');
  33. $result4 = $this->crawler->getURLraw($redirURL);
  34. // Get authentication request
  35. $this->log($testrun, 'Sending a new request to the initURL endpoint, to verify if user is logged in or not');
  36. $result5 = $this->crawler->getURLraw($this->initurl);
  37. # getDebugOutput($testrun, $body, $request, $relaystate, $response, $logoutRequest, $logoutRelayState, $LogoutResponse, $result2) {
  38. $debugoutput = $this->getDebugOutputExtended($testrun, $result4['body'], $requestRaw, $samlResponse['RelayState'], $samlResponse['Response'],
  39. $logoutRequestRaw, $logoutResponse['RelayState'], $logoutResponse['Response'], $result5['body']);
  40. $this->expectedResult($testrun, $result5['body'], $debugoutput);
  41. # error_log('url to logout: ' . $this->initslo);
  42. # echo '<pre>'; print_r($this->flushResults()); exit;
  43. return $this->flushResults();
  44. }
  45. protected function expectedResult($testrun, $body, $debugoutput) {
  46. if (strstr($body, 'andreas@uninett.no')) {
  47. $this->setResult(sspmod_fedlab_Tester::STATUS_FATAL, $testrun, $testrun, $this->testruns[$testrun], $debugoutput);
  48. } else {
  49. $this->setResult(sspmod_fedlab_Tester::STATUS_OK, $testrun, $testrun, $this->testruns[$testrun], $debugoutput);
  50. }
  51. }
  52. protected function createLogoutResponse($testrun, $logoutRequest, $logoutRelayState) {
  53. $this->log($testrun, 'Creating response with relaystate [' . $logoutRelayState. ']');
  54. $idpMetadata = SimpleSAML_Configuration::loadFromArray($this->idpmetadata);
  55. $spMetadata = SimpleSAML_Configuration::loadFromArray($this->metadata);
  56. // Get SingleLogoutService URL
  57. $consumerURLf = $spMetadata->getDefaultEndpoint('SingleLogoutService', array('urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect'));
  58. $consumerURL = $consumerURLf['Location'];
  59. /* Create an send response. */
  60. $response = sspmod_saml2_Message::buildLogoutResponse($idpMetadata, $spMetadata);
  61. $response->setRelayState($logoutRequest->getRelayState());
  62. $response->setInResponseTo($logoutRequest->getId());
  63. $keyArray = SimpleSAML_Utilities::loadPrivateKey($idpMetadata, TRUE);
  64. $certArray = SimpleSAML_Utilities::loadPublicKey($idpMetadata, FALSE);
  65. $privateKey = new XMLSecurityKey(XMLSecurityKey::RSA_SHA1, array('type' => 'private'));
  66. $privateKey->loadKey($keyArray['PEM'], FALSE);
  67. $response->setSignatureKey($privateKey);
  68. if ($certArray === NULL) throw new Exception('No certificates found. [1]');
  69. if (!array_key_exists('PEM', $certArray)) throw new Exception('No certificates found. [2]');
  70. $response->setCertificates(array($certArray['PEM']));
  71. #$this->tweakResponse($testrun, $response);
  72. $msgStr = $response->toUnsignedXML();
  73. #$this->tweakResponseDOM($testrun, $msgStr);
  74. $msgStr = $msgStr->ownerDocument->saveXML($msgStr);
  75. # echo '<pre>'; echo(htmlspecialchars($msgStr)); exit;
  76. # $msgStr = base64_encode($msgStr);
  77. # $msgStr = htmlspecialchars($msgStr);
  78. return array('url' => $consumerURL, 'Response' => $msgStr, 'ResponseObj' => $response, 'RelayState' => $logoutRelayState);
  79. }
  80. protected function requireLoginOK($testrun, $body) {
  81. if (!strstr($body, 'andreas@uninett.no')) {
  82. throw new Exception('Login was not OK. Could not find attribute name on page after login.');
  83. }
  84. }
  85. protected function getDebugOutputExtended($testrun, $body, $request, $relaystate, $response, $logoutRequest, $logoutRelayState, $LogoutResponse, $result2) {
  86. $sb = 'NA';
  87. if(preg_match('|<body.*?>(.*?)</body>|is', $body, $matches)) {
  88. $sb = strip_tags($matches[1], '<p><span><div><table><tr><td><ul><li><ol><dd><dt><dl><code><pre>');
  89. }
  90. $sb2 = 'NA';
  91. if(preg_match('|<body.*?>(.*?)</body>|is', $result2, $matches)) {
  92. $sb2 = strip_tags($matches[1], '<p><span><div><table><tr><td><ul><li><ol><dd><dt><dl><code><pre>');
  93. }
  94. $html = '<div class="debugoutput">
  95. <p>AuthnRequest:</p>
  96. <div><pre class="debugbox"><code>' . htmlspecialchars(SimpleSAML_Utilities::formatXMLString($request)) . '</code></pre></div>
  97. <p>RelayState:</p>
  98. <div><pre class="debugbox"><code>' . var_export($relaystate, TRUE) . '</div>
  99. <p>Response:</p>
  100. <div><pre class="debugbox"><code>' . htmlspecialchars(SimpleSAML_Utilities::formatXMLString($response)) . '</code></pre></div>
  101. <p>LogoutRequest:</p>
  102. <div><pre class="debugbox"><code>' . htmlspecialchars(SimpleSAML_Utilities::formatXMLString($logoutRequest)) . '</code></pre></div>
  103. <p>LogoutRequest RelayState:</p>
  104. <div><pre class="debugbox"><code>' . var_export($logoutRelayState, TRUE) . '</div>
  105. <p>LogoutResponse:</p>
  106. <div><pre class="debugbox"><code>' . htmlspecialchars(SimpleSAML_Utilities::formatXMLString($LogoutResponse)) . '</code></pre></div>
  107. <p>Resulting output from web page after logout:</p>
  108. <div class="htmlout">' . $sb . '</div>
  109. <p>Resulting output from web page after trying to access the attribute viewer again after being logged out (should not be logged in then):</p>
  110. <div class="htmlout">' . $sb2 . '</div>
  111. </div>
  112. ';
  113. #echo '<div>' . $html . '</div>'; exit;
  114. return $html;
  115. }
  116. function getConfig($testrun) {
  117. $config = parent::getConfig($testrun);
  118. return $config;
  119. }
  120. }