|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace SimpleSAML\Module\perun\Auth\Process; |
| 4 | + |
| 5 | +use SimpleSAML\Auth\ProcessingFilter; |
| 6 | +use SimpleSAML\Auth\State; |
| 7 | +use SimpleSAML\Configuration; |
| 8 | +use SimpleSAML\Error\Exception; |
| 9 | +use SimpleSAML\Logger; |
| 10 | +use SimpleSAML\Module; |
| 11 | +use SimpleSAML\Module\perun\Adapter; |
| 12 | +use SimpleSAML\Utils\HTTP; |
| 13 | + |
| 14 | +class EnsureVoMember extends ProcessingFilter |
| 15 | +{ |
| 16 | + const ENSURE_VO_MEMBER = 'ensureVoMember'; |
| 17 | + const TRIGGER_ATTR = 'triggerAttr'; |
| 18 | + const VO_DEFS_ATTR = 'voDefsAttr'; |
| 19 | + const LOGIN_URL = 'loginURL'; |
| 20 | + const REGISTRAR_URL = 'registrarURL'; |
| 21 | + const INTERFACE_PROPNAME = 'interface'; |
| 22 | + const RPC = 'rpc'; |
| 23 | + |
| 24 | + private $triggerAttr; |
| 25 | + private $voDefsAttr; |
| 26 | + private $adapter; |
| 27 | + private $loginUrlAttr; |
| 28 | + private $registrarUrl; |
| 29 | + |
| 30 | + public function __construct($config, $reserved) |
| 31 | + { |
| 32 | + parent::__construct($config, $reserved); |
| 33 | + $config = Configuration::loadFromArray($config); |
| 34 | + |
| 35 | + if (is_null($config)) { |
| 36 | + throw new Exception( |
| 37 | + 'perun:EnsureVoMember: Property \'' . self::ENSURE_VO_MEMBER . '\' is missing or invalid!' |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + $this->triggerAttr = $config->getString(self::TRIGGER_ATTR, ""); |
| 42 | + |
| 43 | + if (empty($this->triggerAttr)) { |
| 44 | + throw new Exception( |
| 45 | + 'perun:EnsureVoMember: Missing configuration option \'' . self::TRIGGER_ATTR . '\'' |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + $this->voDefsAttr = $config->getString(self::VO_DEFS_ATTR, ""); |
| 50 | + |
| 51 | + if (empty($this->voDefsAttr)) { |
| 52 | + throw new Exception( |
| 53 | + 'perun:EnsureVoMember: Missing configuration option \'' . self::VO_DEFS_ATTR . '\'' |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + $this->loginUrlAttr = $config->getString(self::LOGIN_URL, null); |
| 58 | + $this->registrarUrl = $config->getString(self::REGISTRAR_URL, null); |
| 59 | + |
| 60 | + $interface = $config->getString(self::INTERFACE_PROPNAME, self::RPC); |
| 61 | + $this->adapter = Adapter::getInstance($interface); |
| 62 | + } |
| 63 | + |
| 64 | + public function process(&$request) |
| 65 | + { |
| 66 | + if (isset($request['SPMetadata']['entityid'])) { |
| 67 | + $spEntityId = $request['SPMetadata']['entityid']; |
| 68 | + } else { |
| 69 | + throw new Exception( |
| 70 | + 'perun:EnsureVoMember: Cannot find entityID of remote SP. ' . |
| 71 | + 'hint: Do you have this filter in IdP context?' |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + if (isset($request['perun']['user'])) { |
| 76 | + $user = $request['perun']['user']; |
| 77 | + } else { |
| 78 | + throw new Exception( |
| 79 | + 'perun:EnsureVoMember: ' . |
| 80 | + 'missing mandatory field \'perun.user\' in request.' . |
| 81 | + 'Hint: Did you configured PerunIdentity filter before this filter?' |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + $facility = $this->adapter->getFacilityByEntityId($spEntityId); |
| 86 | + |
| 87 | + if ($facility === null) { |
| 88 | + Logger::debug('perun:EnsureVoMember: skip execution - no facility provided'); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + $attrValues = $this->adapter->getFacilityAttributesValues( |
| 93 | + $facility, |
| 94 | + [$this->voDefsAttr, $this->triggerAttr, $this->loginUrlAttr] |
| 95 | + ); |
| 96 | + |
| 97 | + $triggerAttrValue = $attrValues[$this->triggerAttr]; |
| 98 | + if ($triggerAttrValue === null || $triggerAttrValue === false) { |
| 99 | + Logger::debug( |
| 100 | + 'perun:EnsureVoMember: skip execution - attribute ' . self::TRIGGER_ATTR . ' is null or false' |
| 101 | + ); |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + $voShortName = $attrValues[$this->voDefsAttr]; |
| 106 | + if (empty($voShortName)) { |
| 107 | + Logger::debug( |
| 108 | + 'perun:EnsureVoMember: skip execution - attribute ' . self::VO_DEFS_ATTR . ' has null or no value' |
| 109 | + ); |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + $canAccess = $this->adapter->isUserInVo($user, $voShortName); |
| 114 | + if ($canAccess) { |
| 115 | + Logger::debug('perun:EnsureVoMember: user allowed to continue'); |
| 116 | + } else { |
| 117 | + $this->redirect($request, $attrValues[$this->loginUrlAttr], $voShortName); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private function redirect($request, $loginUrl, $voShortName) |
| 122 | + { |
| 123 | + if (!empty($voShortName) && |
| 124 | + !empty($this->registrarUrl) && |
| 125 | + $this->adapter->hasRegistrationFormByVoShortName($voShortName) |
| 126 | + ) { |
| 127 | + $this->redirectToRegistration($loginUrl, $voShortName); |
| 128 | + } else { |
| 129 | + $this->redirectUnapproved($request); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private function redirectToRegistration($loginUrl, $voShortName) |
| 134 | + { |
| 135 | + HTTP::redirectTrustedURL( |
| 136 | + $this->registrarUrl, |
| 137 | + [ |
| 138 | + 'vo' => $voShortName, |
| 139 | + 'targetnew' => $loginUrl, |
| 140 | + 'targetexisting' => $loginUrl |
| 141 | + ] |
| 142 | + ); |
| 143 | + } |
| 144 | + |
| 145 | + private function redirectUnapproved($request) |
| 146 | + { |
| 147 | + $id = State::saveState($request, 'perunauthorize:Perunauthorize'); |
| 148 | + $url = Module::getModuleURL('perunauthorize/perunauthorize_403.php'); |
| 149 | + |
| 150 | + $params = []; |
| 151 | + $params['StateId'] = $id; |
| 152 | + $params['administrationContact'] = $request['SPMetadata']['administrationContact']; |
| 153 | + $params['serviceName'] = $request['SPMetadata']['name']['en']; |
| 154 | + |
| 155 | + if (isset($request['SPMetadata']['InformationURL']['en'])) { |
| 156 | + $params['informationURL'] = $request['SPMetadata']['InformationURL']['en']; |
| 157 | + } |
| 158 | + |
| 159 | + HTTP::redirectTrustedURL($url, $params); |
| 160 | + } |
| 161 | +} |
0 commit comments