|
5 | 5 | namespace SimpleSAML\Module\debugsp\Controller; |
6 | 6 |
|
7 | 7 | use SimpleSAML\Logger; |
| 8 | +use SimpleSAML\Session; |
| 9 | +use SimpleSAML\Utils\HTTP; |
8 | 10 | use Symfony\Component\HttpFoundation\Request; |
9 | 11 | use Symfony\Component\HttpFoundation\Response; |
10 | 12 | use SimpleSAML\HTTP\RunnableResponse; |
@@ -39,10 +41,100 @@ public function assertionConsumerService(string $sourceId): RunnableResponse |
39 | 41 | $_POST['SAMLResponse'] = $_POST['_SAMLResponse']; |
40 | 42 | } |
41 | 43 |
|
| 44 | + // Store the SAML Response as received (verbatim) in the session, so the Test SP (sp.php) can show it. |
| 45 | + // This saves a trip to the SAML tracer browser plugin. |
| 46 | + $samlResponse = self::parseSamlResponse(); |
| 47 | + if ($samlResponse === null) { |
| 48 | + // Let parent handle the request (and the error) |
| 49 | + return parent::assertionConsumerService($sourceId); |
| 50 | + } |
| 51 | + $session = Session::getSessionFromRequest(); |
| 52 | + $session->setData('array', 'DEBUGSP_SAML_RESPONSE', $samlResponse); |
| 53 | + |
| 54 | + // Intercept SAML error responses (e.g. the Responder / AuthnFailed response that the EntraID mock |
| 55 | + // IdP returns when the user presses Cancel at the login page). Instead of letting SimpleSAMLphp |
| 56 | + // handle the error (which results in an SSP error page), redirect back to the Test SP (sp.php), |
| 57 | + // which shows a "SAML Error Status" section with the details and the XML of the response. |
| 58 | + if ($samlResponse['StatusCode'] !== '' && $samlResponse['StatusCode'] !== \SAML2\Constants::STATUS_SUCCESS) { |
| 59 | + Logger::info('debugsp: received SAML error response, redirecting to the Test SP'); |
| 60 | + |
| 61 | + $httpUtils = new HTTP(); |
| 62 | + $relayState = $_REQUEST['RelayState'] ?? null; |
| 63 | + if (is_string($relayState) && strlen($relayState) > 0) { |
| 64 | + return new RunnableResponse([$httpUtils, 'redirectTrustedURL'], [$httpUtils->checkURLAllowed($relayState)]); |
| 65 | + } |
| 66 | + |
| 67 | + // No (valid) RelayState: redirect to the Test SP for this authsource |
| 68 | + $returnTo = $httpUtils->getBaseURL() . 'sp.php?sp=' . urlencode($sourceId); |
| 69 | + return new RunnableResponse([$httpUtils, 'redirectTrustedURL'], [$returnTo]); |
| 70 | + } |
| 71 | + |
42 | 72 | // Let parent handle the response |
43 | 73 | return parent::assertionConsumerService($sourceId); |
44 | 74 | } |
45 | 75 |
|
| 76 | + /** |
| 77 | + * Parse the raw SAMLResponse of the current request. Returns an array with the main fields of the |
| 78 | + * response and its assertion, and the XML of the response as received ('xml'). |
| 79 | + * Returns null when the request carries no SAML 2.0 Response. |
| 80 | + * |
| 81 | + * @return array|null |
| 82 | + */ |
| 83 | + private static function parseSamlResponse(): ?array |
| 84 | + { |
| 85 | + $encoded = $_POST['SAMLResponse'] ?? $_GET['SAMLResponse'] ?? null; |
| 86 | + if (!is_string($encoded) || $encoded === '') { |
| 87 | + return null; |
| 88 | + } |
| 89 | + |
| 90 | + $xml = base64_decode($encoded, true); |
| 91 | + if ($xml === false || $xml === '') { |
| 92 | + return null; |
| 93 | + } |
| 94 | + |
| 95 | + $doc = new \DOMDocument(); |
| 96 | + if (!@$doc->loadXML($xml)) { |
| 97 | + // HTTP-Redirect binding responses are deflated after base64 encoding |
| 98 | + $inflated = @gzinflate($xml); |
| 99 | + if ($inflated === false || !@$doc->loadXML($inflated)) { |
| 100 | + return null; |
| 101 | + } |
| 102 | + $xml = $inflated; |
| 103 | + } |
| 104 | + |
| 105 | + $xpath = new \DOMXPath($doc); |
| 106 | + $xpath->registerNamespace('samlp', 'urn:oasis:names:tc:SAML:2.0:protocol'); |
| 107 | + $xpath->registerNamespace('saml', 'urn:oasis:names:tc:SAML:2.0:assertion'); |
| 108 | + |
| 109 | + if ($xpath->query('/samlp:Response')->length !== 1) { |
| 110 | + return null; // not a SAML 2.0 Response |
| 111 | + } |
| 112 | + |
| 113 | + $value = static fn (string $expression): string => (string) $xpath->evaluate("string($expression)"); |
| 114 | + $assertion = '/samlp:Response/saml:Assertion'; |
| 115 | + |
| 116 | + return [ |
| 117 | + 'ID' => $value('/samlp:Response/@ID'), |
| 118 | + 'InResponseTo' => $value('/samlp:Response/@InResponseTo'), |
| 119 | + 'IssueInstant' => $value('/samlp:Response/@IssueInstant'), |
| 120 | + 'Destination' => $value('/samlp:Response/@Destination'), |
| 121 | + 'Issuer' => $value('/samlp:Response/saml:Issuer'), |
| 122 | + 'StatusCode' => $value('/samlp:Response/samlp:Status/samlp:StatusCode/@Value'), |
| 123 | + 'SubStatusCode' => $value('/samlp:Response/samlp:Status/samlp:StatusCode/samlp:StatusCode/@Value'), |
| 124 | + 'StatusMessage' => $value('/samlp:Response/samlp:Status/samlp:StatusMessage'), |
| 125 | + 'EncryptedAssertion' => $xpath->query('/samlp:Response/saml:EncryptedAssertion')->length > 0 ? 'yes' : '', |
| 126 | + 'Assertion ID' => $value("$assertion/@ID"), |
| 127 | + 'Assertion IssueInstant' => $value("$assertion/@IssueInstant"), |
| 128 | + 'Conditions NotBefore' => $value("$assertion/saml:Conditions/@NotBefore"), |
| 129 | + 'Conditions NotOnOrAfter' => $value("$assertion/saml:Conditions/@NotOnOrAfter"), |
| 130 | + 'Audience' => $value("$assertion/saml:Conditions/saml:AudienceRestriction/saml:Audience"), |
| 131 | + 'AuthnInstant' => $value("$assertion/saml:AuthnStatement/@AuthnInstant"), |
| 132 | + 'SessionNotOnOrAfter' => $value("$assertion/saml:AuthnStatement/@SessionNotOnOrAfter"), |
| 133 | + 'SessionIndex' => $value("$assertion/saml:AuthnStatement/@SessionIndex"), |
| 134 | + 'xml' => $xml, |
| 135 | + ]; |
| 136 | + } |
| 137 | + |
46 | 138 |
|
47 | 139 | /** |
48 | 140 | * Metadata endpoint for SAML SP |
|
0 commit comments