|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\Module\perun\Auth\Process; |
| 6 | + |
| 7 | +use DateTime; |
| 8 | +use SimpleSAML\Auth\ProcessingFilter; |
| 9 | +use SimpleSAML\Auth\State; |
| 10 | +use SimpleSAML\Configuration; |
| 11 | +use SimpleSAML\Error\Exception; |
| 12 | +use SimpleSAML\Locale\Translate; |
| 13 | +use SimpleSAML\Logger; |
| 14 | +use SimpleSAML\Module; |
| 15 | +use SimpleSAML\Module\perun\PerunConstants; |
| 16 | +use SimpleSAML\Session; |
| 17 | +use SimpleSAML\Utils\HTTP; |
| 18 | + |
| 19 | +class IsEligible extends ProcessingFilter |
| 20 | +{ |
| 21 | + public const STAGE = 'perun:IsEligible'; |
| 22 | + |
| 23 | + public const DEBUG_PREFIX = self::STAGE . ' - '; |
| 24 | + |
| 25 | + public const REDIRECT = 'perun/403_is_eligible.php'; |
| 26 | + |
| 27 | + public const TEMPLATE = 'perun:403-is-eligible-tpl.php'; |
| 28 | + |
| 29 | + public const DEFAULT_VALIDITY_PERIOD_MONTHS = 12; |
| 30 | + |
| 31 | + public const PARAM_STATE_ID = PerunConstants::STATE_ID; |
| 32 | + |
| 33 | + public const PARAM_RESTART_URL = 'restart_url'; |
| 34 | + |
| 35 | + public const TRIGGER_ATTRIBUTE = 'trigger_attribute'; |
| 36 | + |
| 37 | + public const ELIGIBLE_LAST_SEEN_TIMESTAMP_ATTRIBUTE = 'eligible_last_seen_timestamp_attribute'; |
| 38 | + |
| 39 | + public const VALIDITY_PERIOD_MONTHS = 'validity_period_months'; |
| 40 | + |
| 41 | + public const TRANSLATIONS = 'translations'; |
| 42 | + |
| 43 | + public const OLD_VALUE_HEADER_TRANSLATION = 'old_value_header'; |
| 44 | + |
| 45 | + public const OLD_VALUE_TEXT_TRANSLATION = 'old_value_text'; |
| 46 | + |
| 47 | + public const OLD_VALUE_BUTTON_TRANSLATION = 'old_value_button'; |
| 48 | + |
| 49 | + public const OLD_VALUE_CONTACT_TRANSLATION = 'old_value_contact'; |
| 50 | + |
| 51 | + public const NO_VALUE_HEADER_TRANSLATION = 'no_value_header'; |
| 52 | + |
| 53 | + public const NO_VALUE_TEXT_TRANSLATION = 'no_value_text'; |
| 54 | + |
| 55 | + public const NO_VALUE_BUTTON_TRANSLATION = 'no_value_button'; |
| 56 | + |
| 57 | + public const NO_VALUE_CONTACT_TRANSLATION = 'no_value_contact'; |
| 58 | + |
| 59 | + public const HEADER_TRANSLATION = 'header_translation'; |
| 60 | + |
| 61 | + public const TEXT_TRANSLATION = 'text_translation'; |
| 62 | + |
| 63 | + public const BUTTON_TRANSLATION = 'button_translation'; |
| 64 | + |
| 65 | + public const CONTACT_TRANSLATION = 'contact_translation'; |
| 66 | + |
| 67 | + private $triggerAttribute; |
| 68 | + |
| 69 | + private $timestampAttribute; |
| 70 | + |
| 71 | + private $validityPeriodMonths; |
| 72 | + |
| 73 | + private $filterConfig; |
| 74 | + |
| 75 | + private $translations; |
| 76 | + |
| 77 | + public function __construct($config, $reserved) |
| 78 | + { |
| 79 | + parent::__construct($config, $reserved); |
| 80 | + $this->filterConfig = Configuration::loadFromArray($config); |
| 81 | + |
| 82 | + $this->triggerAttribute = $this->filterConfig->getString(self::TRIGGER_ATTRIBUTE); |
| 83 | + $this->timestampAttribute = $this->filterConfig->getString(self::ELIGIBLE_LAST_SEEN_TIMESTAMP_ATTRIBUTE); |
| 84 | + |
| 85 | + $this->validityPeriodMonths = $this->filterConfig->getInteger( |
| 86 | + self::VALIDITY_PERIOD_MONTHS, |
| 87 | + self::DEFAULT_VALIDITY_PERIOD_MONTHS |
| 88 | + ); |
| 89 | + |
| 90 | + $this->translations = $this->filterConfig->getArray(self::TRANSLATIONS, []); |
| 91 | + } |
| 92 | + |
| 93 | + public function process(&$request) |
| 94 | + { |
| 95 | + assert(is_array($request)); |
| 96 | + assert(!empty($request[PerunConstants::DESTINATION])); |
| 97 | + assert(!empty($request[PerunConstants::ATTRIBUTES])); |
| 98 | + |
| 99 | + $attributesReleasedToSp = []; |
| 100 | + if (!empty($request[PerunConstants::DESTINATION][PerunConstants::DESTINATION_ATTRIBUTES])) { |
| 101 | + $attributesReleasedToSp = $request[PerunConstants::DESTINATION][PerunConstants::DESTINATION_ATTRIBUTES]; |
| 102 | + } |
| 103 | + |
| 104 | + if (!in_array($this->triggerAttribute, $attributesReleasedToSp, true)) { |
| 105 | + Logger::info( |
| 106 | + self::DEBUG_PREFIX . 'SP does not consume the trigger attribute \'' . $this->triggerAttribute . '\'. Terminating execution of this filter.' |
| 107 | + ); |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + $lastSeenEligibleTimestampString = null; |
| 112 | + if (!empty($request[PerunConstants::ATTRIBUTES][$this->timestampAttribute])) { |
| 113 | + $lastSeenEligibleTimestampString = $request[PerunConstants::ATTRIBUTES][$this->timestampAttribute][0]; |
| 114 | + } else { |
| 115 | + Logger::info( |
| 116 | + self::DEBUG_PREFIX . 'Timestamp of the last seen eligibility is empty, cannot let user go through. Redirecting to unauthorized explanation page.' |
| 117 | + ); |
| 118 | + $this->unauthorized($request, false); |
| 119 | + } |
| 120 | + |
| 121 | + $lastSeenEligibleTimestamp = DateTime::createFromFormat('Y-m-d H:i:s', $lastSeenEligibleTimestampString); |
| 122 | + $lastSeenEligibleTimestamp = $lastSeenEligibleTimestamp->modify('+' . $this->validityPeriodMonths . 'months'); |
| 123 | + $now = new DateTime(); |
| 124 | + |
| 125 | + if ($lastSeenEligibleTimestamp < $now) { |
| 126 | + Logger::info( |
| 127 | + self::DEBUG_PREFIX . 'Last seen eligibility timestamp value \'' . $lastSeenEligibleTimestampString . '\' is out of the defined period of ' . $this->validityPeriodMonths . ' months until now. Redirecting to unauthorized explanation page.' |
| 128 | + ); |
| 129 | + $this->unauthorized($request, true); |
| 130 | + } |
| 131 | + Logger::info( |
| 132 | + self::DEBUG_PREFIX . 'Last seen eligibility timestamp value \'' . $lastSeenEligibleTimestampString . '\' is inside the defined period of ' . $this->validityPeriodMonths . ' months until now. Continue to next filter.' |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + public function unauthorized(&$state, $hasValue) |
| 137 | + { |
| 138 | + $translations = $this->loadLocalTranslations($hasValue); |
| 139 | + $state[self::TRANSLATIONS] = $translations; |
| 140 | + |
| 141 | + $state = State::saveState($state, self::STAGE); |
| 142 | + |
| 143 | + try { |
| 144 | + $session = Session::getSessionFromRequest(); |
| 145 | + $session->doLogout('default-sp'); |
| 146 | + } catch (Exception|\Exception $exception) { |
| 147 | + Logger::warning(self::DEBUG_PREFIX . 'Error when logging user out. Logout has failed!'); |
| 148 | + Logger::debug( |
| 149 | + self::DEBUG_PREFIX . 'Details about the logout failure \'' . $exception->getMessage() . '\'.' |
| 150 | + ); |
| 151 | + } |
| 152 | + $url = Module::getModuleURL(self::REDIRECT); |
| 153 | + $params = [ |
| 154 | + self::PARAM_STATE_ID => $state, |
| 155 | + ]; |
| 156 | + |
| 157 | + HTTP::redirectTrustedURL($url, $params); |
| 158 | + } |
| 159 | + |
| 160 | + public static function loadLocalTranslation( |
| 161 | + Translate $translator, |
| 162 | + string $key, |
| 163 | + array $translations, |
| 164 | + string $defaultTranslationKey |
| 165 | + ): string { |
| 166 | + if (!empty($translations[$key])) { |
| 167 | + $translation = $translations[$key]; |
| 168 | + $translationKey = '{' . self::STAGE . '_' . $key . '}'; |
| 169 | + $translator->includeInlineTranslation($translationKey, $translation); |
| 170 | + return $translationKey; |
| 171 | + } |
| 172 | + return $defaultTranslationKey; |
| 173 | + } |
| 174 | + |
| 175 | + private function loadLocalTranslations($hasValue): array |
| 176 | + { |
| 177 | + if ($hasValue) { |
| 178 | + $header = $this->loadTranslation(self::OLD_VALUE_HEADER_TRANSLATION, $this->translations); |
| 179 | + $text = $this->loadTranslation(self::OLD_VALUE_TEXT_TRANSLATION, $this->translations); |
| 180 | + $button = $this->loadTranslation(self::OLD_VALUE_BUTTON_TRANSLATION, $this->translations); |
| 181 | + $contact = $this->loadTranslation(self::OLD_VALUE_CONTACT_TRANSLATION, $this->translations); |
| 182 | + } else { |
| 183 | + $header = $this->loadTranslation(self::NO_VALUE_HEADER_TRANSLATION, $this->translations); |
| 184 | + $text = $this->loadTranslation(self::NO_VALUE_TEXT_TRANSLATION, $this->translations); |
| 185 | + $button = $this->loadTranslation(self::NO_VALUE_BUTTON_TRANSLATION, $this->translations); |
| 186 | + $contact = $this->loadTranslation(self::NO_VALUE_CONTACT_TRANSLATION, $this->translations); |
| 187 | + } |
| 188 | + return [ |
| 189 | + self::HEADER_TRANSLATION => $header, |
| 190 | + self::TEXT_TRANSLATION => $text, |
| 191 | + self::BUTTON_TRANSLATION => $button, |
| 192 | + self::CONTACT_TRANSLATION => $contact, |
| 193 | + ]; |
| 194 | + } |
| 195 | + |
| 196 | + private function loadTranslation(string $key, array $translations): array |
| 197 | + { |
| 198 | + if (array_key_exists($key, $translations)) { |
| 199 | + return $translations[$key]; |
| 200 | + } |
| 201 | + return []; |
| 202 | + } |
| 203 | +} |
0 commit comments