| Conditions | 13 |
| Paths | 25 |
| Total Lines | 78 |
| Code Lines | 40 |
| 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 |
||
| 133 | public function receive(ServerRequestInterface $request): AbstractMessage |
||
| 134 | { |
||
| 135 | $query = $request->getQueryParams(); |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Put the SAMLRequest/SAMLResponse from the actual query string into $message, |
||
| 139 | * and assert that the result from parseQuery() in $query and the parsing of the SignedQuery in $res agree |
||
| 140 | */ |
||
| 141 | if (array_key_exists('SAMLRequest', $query)) { |
||
| 142 | $message = $query['SAMLRequest']; |
||
| 143 | $signedQuery = 'SAMLRequest=' . urlencode($query['SAMLRequest']); |
||
| 144 | } elseif (array_key_exists('SAMLResponse', $query)) { |
||
| 145 | $message = $query['SAMLResponse']; |
||
| 146 | $signedQuery = 'SAMLResponse=' . urlencode($query['SAMLResponse']); |
||
| 147 | } else { |
||
| 148 | throw new Exception('Missing SAMLRequest or SAMLResponse parameter.'); |
||
| 149 | } |
||
| 150 | |||
| 151 | if (array_key_exists('SAMLRequest', $query) && array_key_exists('SAMLResponse', $query)) { |
||
| 152 | throw new Exception('Both SAMLRequest and SAMLResponse provided.'); |
||
| 153 | } |
||
| 154 | |||
| 155 | if (isset($query['SAMLEncoding']) && $query['SAMLEncoding'] !== C::BINDING_HTTP_REDIRECT_DEFLATE) { |
||
| 156 | throw new Exception(sprintf('Unknown SAMLEncoding: %s', $query['SAMLEncoding'])); |
||
| 157 | } |
||
| 158 | |||
| 159 | $message = base64_decode($message, true); |
||
| 160 | if ($message === false) { |
||
| 161 | throw new Exception('Error while base64 decoding SAML message.'); |
||
| 162 | } |
||
| 163 | |||
| 164 | $message = gzinflate($message); |
||
| 165 | if ($message === false) { |
||
| 166 | throw new Exception('Error while inflating SAML message.'); |
||
| 167 | } |
||
| 168 | |||
| 169 | $document = DOMDocumentFactory::fromString($message); |
||
| 170 | Utils::getContainer()->debugMessage($document->documentElement, 'in'); |
||
| 171 | $message = MessageFactory::fromXML($document->documentElement); |
||
| 172 | |||
| 173 | if (array_key_exists('RelayState', $query)) { |
||
| 174 | $this->setRelayState($query['RelayState']); |
||
| 175 | $signedQuery .= '&RelayState=' . urlencode($query['RelayState']); |
||
| 176 | } |
||
| 177 | |||
| 178 | if (!array_key_exists('Signature', $query)) { |
||
| 179 | return $message; |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * 3.4.5.2 - SAML Bindings |
||
| 184 | * |
||
| 185 | * If the message is signed, the Destination XML attribute in the root SAML element of the protocol |
||
| 186 | * message MUST contain the URL to which the sender has instructed the user agent to deliver the |
||
| 187 | * message. |
||
| 188 | */ |
||
| 189 | Assert::notNull($message->getDestination(), ProtocolViolationException::class); |
||
| 190 | // Validation of the Destination must be done upstream |
||
| 191 | |||
| 192 | if (!array_key_exists('SigAlg', $query)) { |
||
| 193 | throw new Exception('Missing signature algorithm.'); |
||
| 194 | } else { |
||
| 195 | $signedQuery .= '&SigAlg=' . urlencode($query['SigAlg']); |
||
| 196 | } |
||
| 197 | |||
| 198 | $container = ContainerSingleton::getInstance(); |
||
| 199 | $blacklist = $container->getBlacklistedEncryptionAlgorithms(); |
||
| 200 | $verifier = (new SignatureAlgorithmFactory($blacklist))->getAlgorithm( |
||
|
|
|||
| 201 | $query['SigAlg'], |
||
| 202 | // TODO: Need to use the key from the metadata |
||
| 203 | PEMCertificatesMock::getPublicKey(PEMCertificatesMock::SELFSIGNED_PUBLIC_KEY), |
||
| 204 | ); |
||
| 205 | |||
| 206 | if ($verifier->verify($signedQuery, base64_decode($query['Signature'])) === false) { |
||
| 207 | throw new SignatureVerificationFailedException('Failed to verify signature.'); |
||
| 208 | } |
||
| 209 | |||
| 210 | return $message; |
||
| 211 | } |
||
| 213 |