|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\Module\stepupsfo\Controller; |
| 6 | + |
| 7 | +use SAML2\Binding; |
| 8 | +use SAML2\HTTPPost; |
| 9 | +use SAML2\Response; |
| 10 | +use SimpleSAML\Auth; |
| 11 | +use SimpleSAML\Configuration; |
| 12 | +use SimpleSAML\Error; |
| 13 | +use SimpleSAML\Logger; |
| 14 | +use SimpleSAML\Metadata\MetaDataStorageHandler; |
| 15 | +use SimpleSAML\Module; |
| 16 | +use SimpleSAML\Module\saml\Message; |
| 17 | +use SimpleSAML\XHTML\Template; |
| 18 | + |
| 19 | +use function sprintf; |
| 20 | +use function var_export; |
| 21 | + |
| 22 | +/** |
| 23 | + * Controller class for the stepupsfo module. |
| 24 | + * |
| 25 | + * This class serves the different views available in the module. |
| 26 | + * |
| 27 | + * @package SimpleSAML\Module\stepupsfo |
| 28 | + */ |
| 29 | +class SFO |
| 30 | +{ |
| 31 | + /** @var \SimpleSAML\Configuration */ |
| 32 | + protected Configuration $config; |
| 33 | + |
| 34 | + |
| 35 | + /** |
| 36 | + * Controller constructor. |
| 37 | + * |
| 38 | + * It initializes the global configuration and auth source configuration for the controllers implemented here. |
| 39 | + * |
| 40 | + * @param \SimpleSAML\Configuration $config The configuration to use by the controllers. |
| 41 | + * |
| 42 | + * @throws \Exception |
| 43 | + */ |
| 44 | + public function __construct( |
| 45 | + Configuration $config, |
| 46 | + ) { |
| 47 | + $this->config = $config; |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + /** |
| 52 | + * Perform second factor only |
| 53 | + * |
| 54 | + * @return \SimpleSAML\XHTML\Template |
| 55 | + */ |
| 56 | + public function acs(): Template |
| 57 | + { |
| 58 | + Logger::debug('SFO - receiving response'); |
| 59 | + |
| 60 | + $b = Binding::getCurrentBinding(); |
| 61 | + |
| 62 | + if (!($b instanceof HTTPPost)) { |
| 63 | + throw new Error\BadRequest('Only HTTP-POST binding supported for SFO.'); |
| 64 | + } |
| 65 | + |
| 66 | + $response = $b->receive(); |
| 67 | + if (!($response instanceof Response)) { |
| 68 | + throw new Error\BadRequest('Invalid message received to SFO AssertionConsumerService endpoint.'); |
| 69 | + } |
| 70 | + |
| 71 | + $issuer = $response->getIssuer(); |
| 72 | + $relaystate = $response->getRelayState(); |
| 73 | + $inResponseTo = $response->getInResponseTo(); |
| 74 | + |
| 75 | + Logger::info(sprintf( |
| 76 | + 'SFO - received response; Issuer = %s, InResponseTo = %s', |
| 77 | + var_export($issuer, true), |
| 78 | + var_export($inResponseTo, true) |
| 79 | + ); |
| 80 | + Logger::debug('SFO - received response; RelayState = ' . $relaystate); |
| 81 | + |
| 82 | + $prestate = Auth\State::loadState($relaystate, 'stepupsfo:pre'); |
| 83 | + $spMetadata = $prestate['sfo:sp:metadata']; |
| 84 | + $idpEntityId = $prestate['sfo:idp:entityid']; |
| 85 | + |
| 86 | + // check that the issuer is the one we are expecting |
| 87 | + if ($idpEntityId !== $issuer) { |
| 88 | + throw new Error\Exception( |
| 89 | + 'The issuer of the response does not match to the SFO identity provider we sent the request to.' |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + // Look up metadata for the IdP |
| 94 | + $metadataHandler = MetaDataStorageHandler::getMetadataHandler(); |
| 95 | + try { |
| 96 | + $idpMetadata = $metadataHandler->getMetaDataConfig($idpEntityId, 'saml20-idp-remote'); |
| 97 | + } catch (Exception $e) { |
| 98 | + /* Not found. */ |
| 99 | + throw new Error\Exception(sprintf( |
| 100 | + 'Could not find the metadata of SFO IdP with entity ID %s', |
| 101 | + var_export($entityId, true) |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + // Validate the received response |
| 106 | + try { |
| 107 | + $assertions = Message::processResponse($spMetadata, $idpMetadata, $response); |
| 108 | + } catch (Module\saml\Error $e) { |
| 109 | + // the status of the response wasn't "success" |
| 110 | + Logger::debug('SFO - status response received, showing error page.'); |
| 111 | + |
| 112 | + $t = new Template($this->config, 'stepupsfo:handlestatus.php'); |
| 113 | + $t->data['status'] = $e->getStatus(); |
| 114 | + $t->data['subStatus'] = $e->getSubStatus(); |
| 115 | + $t->data['statusMessage'] = $e->getStatusMessage(); |
| 116 | + $t->data['selfserviceUrl'] = $idpMetadata->getString('sfo:selfserviceUrl', ''); |
| 117 | + |
| 118 | + return $t; |
| 119 | + } |
| 120 | + |
| 121 | + Logger::debug('SFO - successful response received, resume processing'); |
| 122 | + Auth\ProcessingChain::resumeProcessing($prestate); |
| 123 | + } |
| 124 | +} |
0 commit comments