|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\Module\perun\Auth\Process; |
| 6 | + |
| 7 | +use SimpleSAML\Auth\ProcessingFilter; |
| 8 | +use SimpleSAML\Logger; |
| 9 | +use SimpleSAML\Module\perun\Disco; |
| 10 | + |
| 11 | +/** |
| 12 | + * Auth proc filter, which should be used if Proxy provides MFA, and wants to first pass the request to the upstream |
| 13 | + * IdP. The Perun Disco page, tries to add new ACRs to the request, if MFA has been requested, to prevent the IdP from |
| 14 | + * failing. As a result, we then get modified requested ACRs, which should be restored to the previous (original) state |
| 15 | + * using this authproc filter. It should be run on one of the first places of the IdP authproc chain. |
| 16 | + */ |
| 17 | +class RestoreAcrs extends ProcessingFilter |
| 18 | +{ |
| 19 | + public const CONFIG_FILE_NAME = 'module_perun.php'; |
| 20 | + |
| 21 | + private const DEBUG_PREFIX = 'perun:RestoreAcrs'; |
| 22 | + |
| 23 | + public function __construct($config, $reserved) |
| 24 | + { |
| 25 | + parent::__construct($config, $reserved); |
| 26 | + } |
| 27 | + |
| 28 | + public function process(&$request) |
| 29 | + { |
| 30 | + $this->restoreAcrs($request); |
| 31 | + } |
| 32 | + |
| 33 | + public static function storeAcrs(array &$state, array $acrsToAdd) |
| 34 | + { |
| 35 | + if (!empty($acrsToAdd) |
| 36 | + && !empty($state[Disco::SAML_REQUESTED_AUTHN_CONTEXT][Disco::STATE_AUTHN_CONTEXT_CLASS_REF]) |
| 37 | + && sizeof($state[Disco::SAML_REQUESTED_AUTHN_CONTEXT][Disco::STATE_AUTHN_CONTEXT_CLASS_REF]) <= 1 |
| 38 | + && in_array( |
| 39 | + Disco::MFA_PROFILE, |
| 40 | + $state[Disco::SAML_REQUESTED_AUTHN_CONTEXT][Disco::STATE_AUTHN_CONTEXT_CLASS_REF], |
| 41 | + true |
| 42 | + ) |
| 43 | + ) { |
| 44 | + Logger::debug( |
| 45 | + self::DEBUG_PREFIX . ': Modifying ACRs list to pass regular authentication to the IdP to fallback to proxy MFA' |
| 46 | + ); |
| 47 | + $original = []; |
| 48 | + $new = []; |
| 49 | + foreach ($state[Disco::SAML_REQUESTED_AUTHN_CONTEXT][Disco::STATE_AUTHN_CONTEXT_CLASS_REF] as $acr) { |
| 50 | + $original[] = $acr; |
| 51 | + $new[] = $acr; |
| 52 | + } |
| 53 | + foreach ($acrsToAdd as $acr) { |
| 54 | + $new[] = $acr; |
| 55 | + } |
| 56 | + $new = array_unique($new); |
| 57 | + |
| 58 | + unset($state[Disco::SAML_REQUESTED_AUTHN_CONTEXT][Disco::STATE_AUTHN_CONTEXT_CLASS_REF]); |
| 59 | + if (isset($state[Disco::SAML_REQUESTED_AUTHN_CONTEXT_ORIGINAL])) { |
| 60 | + unset($state[Disco::SAML_REQUESTED_AUTHN_CONTEXT_ORIGINAL]); |
| 61 | + } |
| 62 | + Logger::debug(self::DEBUG_PREFIX . ': original ACRs: ' . join(',', $original)); |
| 63 | + $state[Disco::SAML_REQUESTED_AUTHN_CONTEXT_ORIGINAL] = $original; |
| 64 | + Logger::debug(self::DEBUG_PREFIX . ': ACRs after modification: ' . join(',', $new)); |
| 65 | + $state[Disco::SAML_REQUESTED_AUTHN_CONTEXT][Disco::STATE_AUTHN_CONTEXT_CLASS_REF] = $new; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private function restoreAcrs(&$request) |
| 70 | + { |
| 71 | + if (!empty($request[Disco::SAML_REQUESTED_AUTHN_CONTEXT_ORIGINAL])) { |
| 72 | + unset($request[Disco::SAML_REQUESTED_AUTHN_CONTEXT][Disco::STATE_AUTHN_CONTEXT_CLASS_REF]); |
| 73 | + $handle = &$request[Disco::SAML_REQUESTED_AUTHN_CONTEXT][Disco::STATE_AUTHN_CONTEXT_CLASS_REF]; |
| 74 | + $handle = $request[Disco::SAML_REQUESTED_AUTHN_CONTEXT_ORIGINAL]; |
| 75 | + unset($request[Disco::SAML_REQUESTED_AUTHN_CONTEXT_ORIGINAL]); |
| 76 | + Logger::debug(self::DEBUG_PREFIX . ': ACRS restored: ' . join(',', $handle)); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments