| Conditions | 11 |
| Paths | 23 |
| Total Lines | 69 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 123 | public function receive(ServerRequestInterface $request): AbstractMessage |
||
| 124 | { |
||
| 125 | $query = $request->getQueryParams(); |
||
| 126 | |||
| 127 | if (array_key_exists('SAMLRequest', $query)) { |
||
| 128 | $message = $query['SAMLRequest']; |
||
| 129 | $signedQuery = 'SAMLRequest=' . urlencode($query['SAMLRequest']); |
||
| 130 | } elseif (array_key_exists('SAMLResponse', $query)) { |
||
| 131 | $message = $query['SAMLResponse']; |
||
| 132 | $signedQuery = 'SAMLResponse=' . urlencode($query['SAMLResponse']); |
||
| 133 | } else { |
||
| 134 | throw new Exception('Missing SAMLRequest or SAMLResponse parameter.'); |
||
| 135 | } |
||
| 136 | |||
| 137 | if (isset($query['SAMLEncoding']) && $query['SAMLEncoding'] !== C::BINDING_HTTP_REDIRECT_DEFLATE) { |
||
| 138 | throw new Exception(sprintf('Unknown SAMLEncoding: %s', $query['SAMLEncoding'])); |
||
| 139 | } |
||
| 140 | |||
| 141 | $message = base64_decode($message, true); |
||
| 142 | if ($message === false) { |
||
| 143 | throw new Exception('Error while base64 decoding SAML message.'); |
||
| 144 | } |
||
| 145 | |||
| 146 | $message = gzinflate($message); |
||
| 147 | if ($message === false) { |
||
| 148 | throw new Exception('Error while inflating SAML message.'); |
||
| 149 | } |
||
| 150 | |||
| 151 | $document = DOMDocumentFactory::fromString($message); |
||
| 152 | Utils::getContainer()->debugMessage($document->documentElement, 'in'); |
||
| 153 | $message = MessageFactory::fromXML($document->documentElement); |
||
| 154 | |||
| 155 | if (array_key_exists('RelayState', $query)) { |
||
| 156 | $signedQuery .= '&RelayState=' . urlencode($query['RelayState']); |
||
| 157 | $this->setRelayState($query['RelayState']); |
||
| 158 | } |
||
| 159 | |||
| 160 | if (!array_key_exists('Signature', $query)) { |
||
| 161 | return $message; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * 3.4.5.2 - SAML Bindings |
||
| 166 | * |
||
| 167 | * If the message is signed, the Destination XML attribute in the root SAML element of the protocol |
||
| 168 | * message MUST contain the URL to which the sender has instructed the user agent to deliver the |
||
| 169 | * message. |
||
| 170 | */ |
||
| 171 | Assert::notNull($message->getDestination()); // Validation of the value must be done upstream |
||
| 172 | |||
| 173 | if (!array_key_exists('SigAlg', $query)) { |
||
| 174 | throw new Exception('Missing signature algorithm.'); |
||
| 175 | } else { |
||
| 176 | $signedQuery .= '&SigAlg=' . urlencode($query['SigAlg']); |
||
| 177 | } |
||
| 178 | |||
| 179 | $container = ContainerSingleton::getInstance(); |
||
| 180 | $blacklist = $container->getBlacklistedEncryptionAlgorithms(); |
||
| 181 | $verifier = (new SignatureAlgorithmFactory($blacklist))->getAlgorithm( |
||
| 182 | $query['SigAlg'], |
||
| 183 | // TODO: Need to use the key from the metadata |
||
| 184 | PEMCertificatesMock::getPublicKey(PEMCertificatesMock::SELFSIGNED_PUBLIC_KEY), |
||
| 185 | ); |
||
| 186 | |||
| 187 | if ($verifier->verify($signedQuery, base64_decode($query['Signature'])) === false) { |
||
| 188 | throw new SignatureVerificationFailedException('Failed to verify signature.'); |
||
| 189 | } |
||
| 190 | |||
| 191 | return $message; |
||
| 192 | } |
||
| 194 |