|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2010 SURFnet B.V. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +use OpenConext\EngineBlock\Service\AuthenticationStateHelperInterface; |
| 20 | +use OpenConext\EngineBlock\Service\ProcessingStateHelperInterface; |
| 21 | +use OpenConext\EngineBlock\Stepup\StepupGatewayCallOutHelper; |
| 22 | +use SAML2\Constants; |
| 23 | +use SAML2\Response; |
| 24 | +use Symfony\Component\HttpFoundation\Request; |
| 25 | + |
| 26 | +class EngineBlock_Corto_Module_Service_SRAMInterrupt |
| 27 | + implements EngineBlock_Corto_Module_Service_ServiceInterface |
| 28 | +{ |
| 29 | + /** |
| 30 | + * @var EngineBlock_Corto_ProxyServer |
| 31 | + */ |
| 32 | + protected $_server; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var AuthenticationStateHelperInterface |
| 36 | + */ |
| 37 | + private $_authenticationStateHelper; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var ProcessingStateHelperInterface |
| 41 | + */ |
| 42 | + private $_processingStateHelper; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var StepupGatewayCallOutHelper |
| 46 | + */ |
| 47 | + private $_stepupGatewayCallOutHelper; |
| 48 | + |
| 49 | + |
| 50 | + public function __construct( |
| 51 | + EngineBlock_Corto_ProxyServer $server, |
| 52 | + AuthenticationStateHelperInterface $stateHelper, |
| 53 | + ProcessingStateHelperInterface $processingStateHelper, |
| 54 | + StepupGatewayCallOutHelper $stepupGatewayCallOutHelper |
| 55 | + ) |
| 56 | + { |
| 57 | + $this->_server = $server; |
| 58 | + $this->_authenticationStateHelper = $stateHelper; |
| 59 | + $this->_processingStateHelper = $processingStateHelper; |
| 60 | + $this->_stepupGatewayCallOutHelper = $stepupGatewayCallOutHelper; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @param $serviceName |
| 65 | + * @param Request $httpRequest |
| 66 | + */ |
| 67 | + public function serve($serviceName, Request $httpRequest) |
| 68 | + { |
| 69 | + |
| 70 | + $application = EngineBlock_ApplicationSingleton::getInstance(); |
| 71 | + |
| 72 | + $sramEndpoint = $application->getDiContainer()->getSRAMEndpoint(); |
| 73 | + $sramApiToken = $sramEndpoint->getApiToken(); |
| 74 | + $sramEntitlementsLocation = $sramEndpoint->getEntitlementsLocation(); |
| 75 | + // $sramEntitlementsLocation = 'http://192.168.0.1:12345/entitlements'; |
| 76 | + |
| 77 | + $log = $application->getLogInstance(); |
| 78 | + |
| 79 | + error_log("EngineBlock_Corto_Module_Service_SRAMInterrupt"); |
| 80 | + |
| 81 | + // Get active request |
| 82 | + $id = $httpRequest->get('ID'); |
| 83 | + |
| 84 | + $nextProcessStep = $this->_processingStateHelper->getStepByRequestId( |
| 85 | + $id, |
| 86 | + ProcessingStateHelperInterface::STEP_SRAM |
| 87 | + ); |
| 88 | + |
| 89 | + $receivedResponse = $nextProcessStep->getResponse(); |
| 90 | + $receivedRequest = $this->_server->getReceivedRequestFromResponse($receivedResponse); |
| 91 | + |
| 92 | + /* |
| 93 | + * TODO Add SRAM stuff |
| 94 | + * Manipulate attributes |
| 95 | + */ |
| 96 | + $attributes = $receivedResponse->getAssertion()->getAttributes(); |
| 97 | + $nonce = $receivedResponse->getSRAMInterruptNonce(); |
| 98 | + |
| 99 | + $headers = array( |
| 100 | + "Authorization: $sramApiToken" |
| 101 | + ); |
| 102 | + |
| 103 | + $post = array( |
| 104 | + 'nonce' => $nonce |
| 105 | + ); |
| 106 | + |
| 107 | + $options = [ |
| 108 | + CURLOPT_HEADER => false, |
| 109 | + CURLOPT_RETURNTRANSFER => true, |
| 110 | + CURLOPT_HTTPHEADER => $headers, |
| 111 | + CURLOPT_POST => true, |
| 112 | + CURLOPT_POSTFIELDS => $post, |
| 113 | + ]; |
| 114 | + |
| 115 | + |
| 116 | + $ch = curl_init($sramEntitlementsLocation); |
| 117 | + curl_setopt_array($ch, $options); |
| 118 | + |
| 119 | + $data = curl_exec($ch); |
| 120 | + curl_close($ch); |
| 121 | + |
| 122 | + $body = json_decode($data); |
| 123 | + $entitlements = $body->entitlements; |
| 124 | + |
| 125 | + |
| 126 | + if ($entitlements) { |
| 127 | + $attributes['eduPersonEntitlement'] = $entitlements; |
| 128 | + $receivedResponse->getAssertion()->setAttributes($attributes); |
| 129 | + } |
| 130 | + |
| 131 | + /* |
| 132 | + * Continue to Consent/StepUp |
| 133 | + */ |
| 134 | + |
| 135 | + // Flush log if SP or IdP has additional logging enabled |
| 136 | + $issuer = $receivedResponse->getIssuer() ? $receivedResponse->getIssuer()->getValue() : ''; |
| 137 | + $idp = $this->_server->getRepository()->fetchIdentityProviderByEntityId($issuer); |
| 138 | + |
| 139 | + if ($receivedRequest->isDebugRequest()) { |
| 140 | + $sp = $this->_server->getEngineSpRole($this->_server); |
| 141 | + } else { |
| 142 | + $issuer = $receivedRequest->getIssuer() ? $receivedRequest->getIssuer()->getValue() : ''; |
| 143 | + $sp = $this->_server->getRepository()->fetchServiceProviderByEntityId($issuer); |
| 144 | + } |
| 145 | + |
| 146 | + // When dealing with an SP that acts as a trusted proxy, we should use the proxying SP and not the proxy itself. |
| 147 | + if ($sp->getCoins()->isTrustedProxy()) { |
| 148 | + // Overwrite the trusted proxy SP instance with that of the SP that uses the trusted proxy. |
| 149 | + $sp = $this->_server->findOriginalServiceProvider($receivedRequest, $log); |
| 150 | + } |
| 151 | + |
| 152 | + $pdpLoas = $receivedResponse->getPdpRequestedLoas(); |
| 153 | + $loaRepository = $application->getDiContainer()->getLoaRepository(); |
| 154 | + $authnRequestLoas = $receivedRequest->getStepupObligations($loaRepository->getStepUpLoas()); |
| 155 | + |
| 156 | + $shouldUseStepup = $this->_stepupGatewayCallOutHelper->shouldUseStepup($idp, $sp, $authnRequestLoas, $pdpLoas); |
| 157 | + |
| 158 | + // Goto consent if no Stepup authentication is needed |
| 159 | + if (!$shouldUseStepup) { |
| 160 | + $this->_server->sendConsentAuthenticationRequest($receivedResponse, $receivedRequest, $nextProcessStep->getRole(), $this->_authenticationStateHelper->getAuthenticationState()); |
| 161 | + return; |
| 162 | + } |
| 163 | + |
| 164 | + $log->info('Handle Stepup authentication callout'); |
| 165 | + |
| 166 | + // Add Stepup authentication step |
| 167 | + $currentProcessStep = $this->_processingStateHelper->addStep( |
| 168 | + $receivedRequest->getId(), |
| 169 | + ProcessingStateHelperInterface::STEP_STEPUP, |
| 170 | + $application->getDiContainer()->getStepupIdentityProvider($this->_server), |
| 171 | + $receivedResponse |
| 172 | + ); |
| 173 | + |
| 174 | + // Get mapped AuthnClassRef and get NameId |
| 175 | + $nameId = clone $receivedResponse->getNameId(); |
| 176 | + $authnClassRef = $this->_stepupGatewayCallOutHelper->getStepupLoa($idp, $sp, $authnRequestLoas, $pdpLoas); |
| 177 | + |
| 178 | + |
| 179 | + |
| 180 | + $this->_server->sendStepupAuthenticationRequest( |
| 181 | + $receivedRequest, |
| 182 | + $currentProcessStep->getRole(), |
| 183 | + $authnClassRef, |
| 184 | + $nameId, |
| 185 | + $sp->getCoins()->isStepupForceAuthn() |
| 186 | + ); |
| 187 | + |
| 188 | + |
| 189 | + } |
| 190 | +} |
0 commit comments