Conditions | 12 |
Paths | 23 |
Total Lines | 72 |
Code Lines | 40 |
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( |
||
152 | xml: $message, |
||
153 | schemaFile: $this->getSchemaValidation() ? self::$schemaFile : null, |
||
154 | ); |
||
155 | Utils::getContainer()->debugMessage($document->documentElement, 'in'); |
||
156 | $message = MessageFactory::fromXML($document->documentElement); |
||
157 | |||
158 | if (array_key_exists('RelayState', $query)) { |
||
159 | $signedQuery .= '&RelayState=' . urlencode($query['RelayState']); |
||
160 | $this->setRelayState($query['RelayState']); |
||
161 | } |
||
162 | |||
163 | if (!array_key_exists('Signature', $query)) { |
||
164 | return $message; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * 3.4.5.2 - SAML Bindings |
||
169 | * |
||
170 | * If the message is signed, the Destination XML attribute in the root SAML element of the protocol |
||
171 | * message MUST contain the URL to which the sender has instructed the user agent to deliver the |
||
172 | * message. |
||
173 | */ |
||
174 | Assert::notNull($message->getDestination()); // Validation of the value must be done upstream |
||
175 | |||
176 | if (!array_key_exists('SigAlg', $query)) { |
||
177 | throw new Exception('Missing signature algorithm.'); |
||
178 | } else { |
||
179 | $signedQuery .= '&SigAlg=' . urlencode($query['SigAlg']); |
||
180 | } |
||
181 | |||
182 | $container = ContainerSingleton::getInstance(); |
||
183 | $blacklist = $container->getBlacklistedEncryptionAlgorithms(); |
||
184 | $verifier = (new SignatureAlgorithmFactory($blacklist))->getAlgorithm( |
||
|
|||
185 | $query['SigAlg'], |
||
186 | // TODO: Need to use the key from the metadata |
||
187 | PEMCertificatesMock::getPublicKey(PEMCertificatesMock::SELFSIGNED_PUBLIC_KEY), |
||
188 | ); |
||
189 | |||
190 | if ($verifier->verify($signedQuery, base64_decode($query['Signature'])) === false) { |
||
191 | throw new SignatureVerificationFailedException('Failed to verify signature.'); |
||
192 | } |
||
193 | |||
194 | return $message; |
||
195 | } |
||
197 |