| Conditions | 13 |
| Paths | 69 |
| Total Lines | 90 |
| Code Lines | 46 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 124 | public function receive(ServerRequestInterface $request): AbstractMessage |
||
| 125 | { |
||
| 126 | $query = $this->parseQuery(); |
||
| 127 | $signedQuery = $query['SignedQuery']; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Get the SAMLRequest/SAMLResponse from the exact same signed data that will be verified later in |
||
| 131 | * validateSignature into $res using the actual SignedQuery |
||
| 132 | */ |
||
| 133 | $res = []; |
||
| 134 | foreach (explode('&', $signedQuery) as $e) { |
||
| 135 | $tmp = explode('=', $e, 2); |
||
| 136 | $name = $tmp[0]; |
||
| 137 | if (count($tmp) === 2) { |
||
| 138 | $value = $tmp[1]; |
||
| 139 | } else { |
||
| 140 | /* No value for this parameter. */ |
||
| 141 | $value = ''; |
||
| 142 | } |
||
| 143 | $name = urldecode($name); |
||
| 144 | $res[$name] = urldecode($value); |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Put the SAMLRequest/SAMLResponse from the actual query string into $message, |
||
| 149 | * and assert that the result from parseQuery() in $query and the parsing of the SignedQuery in $res agree |
||
| 150 | */ |
||
| 151 | if (array_key_exists('SAMLRequest', $res)) { |
||
| 152 | Assert::same($res['SAMLRequest'], $query['SAMLRequest'], 'Parse failure.'); |
||
| 153 | $message = $res['SAMLRequest']; |
||
| 154 | } elseif (array_key_exists('SAMLResponse', $res)) { |
||
| 155 | Assert::same($res['SAMLResponse'], $query['SAMLResponse'], 'Parse failure.'); |
||
| 156 | $message = $res['SAMLResponse']; |
||
| 157 | } else { |
||
| 158 | throw new Exception('Missing SAMLRequest or SAMLResponse parameter.'); |
||
| 159 | } |
||
| 160 | |||
| 161 | if (isset($query['SAMLEncoding']) && $query['SAMLEncoding'] !== C::BINDING_HTTP_REDIRECT_DEFLATE) { |
||
| 162 | throw new Exception(sprintf('Unknown SAMLEncoding: %s', $query['SAMLEncoding'])); |
||
| 163 | } |
||
| 164 | |||
| 165 | $message = base64_decode($message, true); |
||
| 166 | if ($message === false) { |
||
| 167 | throw new Exception('Error while base64 decoding SAML message.'); |
||
| 168 | } |
||
| 169 | |||
| 170 | $message = gzinflate($message); |
||
| 171 | if ($message === false) { |
||
| 172 | throw new Exception('Error while inflating SAML message.'); |
||
| 173 | } |
||
| 174 | |||
| 175 | $document = DOMDocumentFactory::fromString($message); |
||
| 176 | Utils::getContainer()->debugMessage($document->documentElement, 'in'); |
||
| 177 | $message = MessageFactory::fromXML($document->documentElement); |
||
| 178 | |||
| 179 | if (array_key_exists('RelayState', $query)) { |
||
| 180 | $this->setRelayState($query['RelayState']); |
||
| 181 | } |
||
| 182 | |||
| 183 | if (!array_key_exists('Signature', $query)) { |
||
| 184 | return $message; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * 3.4.5.2 - SAML Bindings |
||
| 189 | * |
||
| 190 | * If the message is signed, the Destination XML attribute in the root SAML element of the protocol |
||
| 191 | * message MUST contain the URL to which the sender has instructed the user agent to deliver the |
||
| 192 | * message. |
||
| 193 | */ |
||
| 194 | Assert::notNull($message->getDestination(), ProtocolViolationException::class); |
||
| 195 | // Validation of the Destination must be done upstream |
||
| 196 | |||
| 197 | if (!array_key_exists('SigAlg', $query)) { |
||
| 198 | throw new Exception('Missing signature algorithm.'); |
||
| 199 | } |
||
| 200 | |||
| 201 | $container = ContainerSingleton::getInstance(); |
||
| 202 | $blacklist = $container->getBlacklistedEncryptionAlgorithms(); |
||
| 203 | $verifier = (new SignatureAlgorithmFactory($blacklist))->getAlgorithm( |
||
|
|
|||
| 204 | $query['SigAlg'], |
||
| 205 | // TODO: Need to use the key from the metadata |
||
| 206 | PEMCertificatesMock::getPublicKey(PEMCertificatesMock::SELFSIGNED_PUBLIC_KEY), |
||
| 207 | ); |
||
| 208 | |||
| 209 | if ($verifier->verify($signedQuery, base64_decode($query['Signature'])) === false) { |
||
| 210 | throw new SignatureVerificationFailedException('Failed to verify signature.'); |
||
| 211 | } |
||
| 212 | |||
| 213 | return $message; |
||
| 214 | } |
||
| 279 |