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