|
| 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\Auth\State; |
| 9 | +use SimpleSAML\Configuration; |
| 10 | +use SimpleSAML\Error\Exception; |
| 11 | +use SimpleSAML\Logger; |
| 12 | +use SimpleSAML\Module; |
| 13 | +use SimpleSAML\Module\perun\Adapter; |
| 14 | +use SimpleSAML\Module\perun\PerunConstants; |
| 15 | +use SimpleSAML\Utils\HTTP; |
| 16 | + |
| 17 | +/** |
| 18 | + * Class checks if the user has approved given aup, and forwards to approval page if not. |
| 19 | + */ |
| 20 | +class PerunAup extends ProcessingFilter |
| 21 | +{ |
| 22 | + public const STAGE = 'perun:PerunAup'; |
| 23 | + public const DEBUG_PREFIX = self::STAGE . ' - '; |
| 24 | + |
| 25 | + public const CALLBACK = 'perun/perun_aup_callback.php'; |
| 26 | + public const REDIRECT = 'perun/perun_aup.php'; |
| 27 | + public const TEMPLATE = 'perun:perun-aup-tpl.php'; |
| 28 | + |
| 29 | + public const PARAM_STATE_ID = PerunConstants::STATE_ID; |
| 30 | + public const PARAM_APPROVAL_URL = 'approvalUrl'; |
| 31 | + |
| 32 | + public const INTERFACE = 'interface'; |
| 33 | + public const AUP_ATTR = 'attribute'; |
| 34 | + public const AUP_VALUE = 'value'; |
| 35 | + public const APPROVAL_URL = 'approval_url'; |
| 36 | + public const CALLBACK_PARAMETER_NAME = 'callback_parameter_name'; |
| 37 | + public const PERUN_APPROVAL_URL = 'perun_approval_url'; |
| 38 | + |
| 39 | + private $adapter; |
| 40 | + private $aupAttr; |
| 41 | + private $aupValue; |
| 42 | + private $approvalUrl; |
| 43 | + private $callbackParameterName; |
| 44 | + private $perunApprovalUrl; |
| 45 | + private $config; |
| 46 | + private $filterConfig; |
| 47 | + |
| 48 | + public function __construct($config, $reserved) |
| 49 | + { |
| 50 | + parent::__construct($config, $reserved); |
| 51 | + $this->config = $config; |
| 52 | + $this->filterConfig = Configuration::loadFromArray($config); |
| 53 | + |
| 54 | + $interface = $this->filterConfig->getString(self::INTERFACE, Adapter::RPC); |
| 55 | + $this->adapter = Adapter::getInstance($interface); |
| 56 | + |
| 57 | + $this->aupAttr = $this->filterConfig->getString(self::AUP_ATTR, null); |
| 58 | + if (empty($this->aupAttr)) { |
| 59 | + throw new Exception( |
| 60 | + self::DEBUG_PREFIX . 'Invalid configuration: no attribute containing approved AUP ' . 'has been configured. Use option \'' . self::AUP_ATTR . '\' to configure the name of the Perun' . 'attribute, which should contain the approved AUP version.' |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + $this->aupValue = $this->filterConfig->getString(self::AUP_VALUE, null); |
| 65 | + if (empty($this->aupValue)) { |
| 66 | + throw new Exception( |
| 67 | + self::DEBUG_PREFIX . 'Invalid configuration: no value signaling AUP which needs to be approved ' . 'has been configured. Use option \'' . self::AUP_VALUE . '\' to configure the value, which needs to ' . 'be present in the attribute containing the approved AUP version.' |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + $this->approvalUrl = $this->filterConfig->getString(self::APPROVAL_URL, null); |
| 72 | + $this->callbackParameterName = $this->filterConfig->getString(self::CALLBACK_PARAMETER_NAME, null); |
| 73 | + $this->perunApprovalUrl = $this->filterConfig->getString(self::PERUN_APPROVAL_URL, null); |
| 74 | + if (empty($this->approvalUrl) && empty($this->callbackParameterName) && empty($this->perunApprovalUrl)) { |
| 75 | + throw new Exception( |
| 76 | + self::DEBUG_PREFIX . 'Invalid configuration: no URL where user should approve the AUP ' . 'has been configured. Use option \'' . self::APPROVAL_URL . '\' to configure the URL and ' . 'option \'' . self::CALLBACK_PARAMETER_NAME . '\' to configure name of the callback parameter. |
| 77 | + . If you wish to use the Perun registrar, use the option \'' . self::PERUN_APPROVAL_URL . '\'.' |
| 78 | + ); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public function process(&$request) |
| 83 | + { |
| 84 | + assert(is_array($request)); |
| 85 | + assert(!empty($request[PerunConstants::PERUN][PerunConstants::USER])); |
| 86 | + |
| 87 | + if (empty($request[PerunConstants::PERUN][PerunConstants::USER])) { |
| 88 | + throw new Exception( |
| 89 | + self::DEBUG_PREFIX . 'Request does not contain Perun user. Did you configure ' . PerunUser::STAGE . ' filter before this filter in the processing chain?' |
| 90 | + ); |
| 91 | + } |
| 92 | + $user = $request[PerunConstants::PERUN][PerunConstants::USER]; |
| 93 | + |
| 94 | + $aupAttr = null; |
| 95 | + $userAttributesValues = $this->adapter->getUserAttributesValues($user, [$this->aupAttr]); |
| 96 | + if (empty($userAttributesValues) || empty($userAttributesValues[$this->aupAttr])) { |
| 97 | + Logger::warning( |
| 98 | + self::DEBUG_PREFIX . 'Attribute \'' . $this->aupAttr . '\' is empty. Probably could not be ' |
| 99 | + . 'fetched. Redirecting user to approve AUP.' |
| 100 | + ); |
| 101 | + } else { |
| 102 | + $aupAttr = $userAttributesValues[$this->aupAttr]; |
| 103 | + } |
| 104 | + |
| 105 | + if ($aupAttr === $this->aupValue) { |
| 106 | + Logger::info( |
| 107 | + self::DEBUG_PREFIX . 'User approved AUP did match the expected value, continue processing.' |
| 108 | + ); |
| 109 | + |
| 110 | + return; |
| 111 | + } |
| 112 | + Logger::info( |
| 113 | + self::DEBUG_PREFIX . 'User did not approve the expected AUP. Expected value \'' |
| 114 | + . $this->aupValue . '\', actual value \'' . $aupAttr . '\'. Redirecting user to AUP approval page.' |
| 115 | + ); |
| 116 | + $this->redirect($request); |
| 117 | + } |
| 118 | + |
| 119 | + private function redirect(&$request): void |
| 120 | + { |
| 121 | + $request[PerunConstants::CONTINUE_FILTER_CONFIG] = $this->config; |
| 122 | + $stateId = State::saveState($request, self::STAGE); |
| 123 | + $callback = Module::getModuleURL(self::CALLBACK, [ |
| 124 | + self::PARAM_STATE_ID => $stateId, |
| 125 | + ]); |
| 126 | + if (!empty($this->approvalUrl) && !empty($this->callbackParameterName)) { |
| 127 | + Logger::debug( |
| 128 | + self::DEBUG_PREFIX . 'Redirecting to \'' . $this->approvalUrl . ', callback parameter \'' |
| 129 | + . $this->callbackParameterName . '\' with value \'' . $callback . '\'' |
| 130 | + ); |
| 131 | + HTTP::redirectTrustedURL($this->approvalUrl, [ |
| 132 | + $this->callbackParameterName => $callback, |
| 133 | + ]); |
| 134 | + } elseif (!empty($this->perunApprovalUrl)) { |
| 135 | + $params[PerunConstants::TARGET_NEW] = $callback; |
| 136 | + $params[PerunConstants::TARGET_EXISTING] = $callback; |
| 137 | + $params[PerunConstants::TARGET_EXTENDED] = $callback; |
| 138 | + |
| 139 | + $url = Module::getModuleURL(self::REDIRECT); |
| 140 | + $approvalUrl = HTTP::addURLParameters($this->approvalUrl, $params); |
| 141 | + Logger::debug( |
| 142 | + self::DEBUG_PREFIX . 'Redirecting to \'' . self::REDIRECT . ', approval URL \'' |
| 143 | + . $approvalUrl . '\'' |
| 144 | + ); |
| 145 | + HTTP::redirectTrustedURL($url, [ |
| 146 | + self::PARAM_APPROVAL_URL => $approvalUrl, |
| 147 | + ]); |
| 148 | + } else { |
| 149 | + throw new Exception(self::DEBUG_PREFIX . 'No configuration for AUP approval enabled. Cannot proceed'); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments