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