Conditions | 19 |
Paths | 768 |
Total Lines | 136 |
Code Lines | 80 |
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 |
||
50 | public function send( |
||
51 | AbstractMessage $msg, |
||
52 | Configuration $srcMetadata, |
||
53 | ?Configuration $dstMetadata = null, |
||
54 | ): AbstractMessage { |
||
55 | $issuer = $msg->getIssuer(); |
||
56 | |||
57 | $ctxOpts = [ |
||
58 | 'ssl' => [ |
||
59 | 'capture_peer_cert' => true, |
||
60 | 'allow_self_signed' => true, |
||
61 | ], |
||
62 | ]; |
||
63 | |||
64 | $container = ContainerSingleton::getInstance(); |
||
65 | |||
66 | // Determine if we are going to do a MutualSSL connection between the IdP and SP - Shoaib |
||
67 | if ($srcMetadata->hasValue('saml.SOAPClient.certificate')) { |
||
68 | $cert = $srcMetadata->getValue('saml.SOAPClient.certificate'); |
||
69 | if ($cert !== false) { |
||
70 | $configUtils = new Config(); |
||
71 | $ctxOpts['ssl']['local_cert'] = $configUtils->getCertPath( |
||
72 | $srcMetadata->getString('saml.SOAPClient.certificate'), |
||
73 | ); |
||
74 | if ($srcMetadata->hasValue('saml.SOAPClient.privatekey_pass')) { |
||
75 | $ctxOpts['ssl']['passphrase'] = $srcMetadata->getString('saml.SOAPClient.privatekey_pass'); |
||
76 | } |
||
77 | } |
||
78 | } else { |
||
79 | /* Use the SP certificate and privatekey if it is configured. */ |
||
80 | $cryptoUtils = new Crypto(); |
||
81 | $privateKey = $cryptoUtils->loadPrivateKey($srcMetadata); |
||
82 | $publicKey = $cryptoUtils->loadPublicKey($srcMetadata); |
||
83 | if ($privateKey !== null && $publicKey !== null && isset($publicKey['PEM'])) { |
||
84 | $keyCertData = $privateKey['PEM'] . $publicKey['PEM']; |
||
85 | $file = $container->getTempDir() . '/' . sha1($keyCertData) . '.pem'; |
||
86 | if (!file_exists($file)) { |
||
87 | $container->writeFile($file, $keyCertData); |
||
88 | } |
||
89 | $ctxOpts['ssl']['local_cert'] = $file; |
||
90 | if (isset($privateKey['password'])) { |
||
91 | $ctxOpts['ssl']['passphrase'] = $privateKey['password']; |
||
92 | } |
||
93 | } |
||
94 | } |
||
95 | |||
96 | // do peer certificate verification |
||
97 | if ($dstMetadata !== null) { |
||
98 | $peerPublicKeys = $dstMetadata->getPublicKeys('signing', true); |
||
99 | $certData = ''; |
||
100 | foreach ($peerPublicKeys as $key) { |
||
101 | if ($key['type'] !== 'X509Certificate') { |
||
102 | continue; |
||
103 | } |
||
104 | $certData .= "-----BEGIN CERTIFICATE-----\n" . |
||
105 | chunk_split($key['X509Certificate'], 64) . |
||
106 | "-----END CERTIFICATE-----\n"; |
||
107 | } |
||
108 | $peerCertFile = $container->getTempDir() . '/' . sha1($certData) . '.pem'; |
||
109 | if (!file_exists($peerCertFile)) { |
||
110 | $container->writeFile($peerCertFile, $certData); |
||
111 | } |
||
112 | // create ssl context |
||
113 | $ctxOpts['ssl']['verify_peer'] = true; |
||
114 | $ctxOpts['ssl']['verify_depth'] = 1; |
||
115 | $ctxOpts['ssl']['cafile'] = $peerCertFile; |
||
116 | } |
||
117 | |||
118 | if ($srcMetadata->hasValue('saml.SOAPClient.stream_context.ssl.peer_name')) { |
||
119 | $ctxOpts['ssl']['peer_name'] = $srcMetadata->getString('saml.SOAPClient.stream_context.ssl.peer_name'); |
||
120 | } |
||
121 | |||
122 | $context = stream_context_create($ctxOpts); |
||
123 | |||
124 | $options = [ |
||
125 | 'uri' => $issuer?->getContent(), |
||
126 | 'location' => $msg->getDestination(), |
||
127 | 'stream_context' => $context, |
||
128 | ]; |
||
129 | |||
130 | if ($srcMetadata->hasValue('saml.SOAPClient.proxyhost')) { |
||
131 | $options['proxy_host'] = $srcMetadata->getValue('saml.SOAPClient.proxyhost'); |
||
132 | } |
||
133 | |||
134 | if ($srcMetadata->hasValue('saml.SOAPClient.proxyport')) { |
||
135 | $options['proxy_port'] = $srcMetadata->getValue('saml.SOAPClient.proxyport'); |
||
136 | } |
||
137 | |||
138 | $destination = $msg->getDestination(); |
||
139 | if ($destination === null) { |
||
140 | throw new Exception('Cannot send SOAP message, no destination set.'); |
||
141 | } |
||
142 | |||
143 | // Add soap-envelopes |
||
144 | $env = (new Envelope(new Body([new Chunk($msg->toXML())])))->toXML(); |
||
145 | $request = $env->ownerDocument?->saveXML(); |
||
146 | |||
147 | $container->debugMessage($request, 'out'); |
||
148 | |||
149 | $action = 'http://www.oasis-open.org/committees/security'; |
||
150 | /* Perform SOAP Request over HTTP */ |
||
151 | $x = new BuiltinSoapClient(null, $options); |
||
152 | $soapresponsexml = $x->__doRequest($request, $destination, $action, SOAP_1_1, false); |
||
153 | if (empty($soapresponsexml)) { |
||
154 | throw new Exception('Empty SOAP response, check peer certificate.'); |
||
155 | } |
||
156 | |||
157 | Utils::getContainer()->debugMessage($soapresponsexml, 'in'); |
||
158 | |||
159 | $dom = DOMDocumentFactory::fromString($soapresponsexml); |
||
160 | $env = Envelope::fromXML($dom->documentElement); |
||
161 | $container->debugMessage($env->toXML()->ownerDocument?->saveXML(), 'in'); |
||
162 | |||
163 | $soapfault = $this->getSOAPFault($dom); |
||
164 | if ($soapfault !== null) { |
||
165 | throw new Exception( |
||
166 | sprintf( |
||
167 | "Actor: '%s'; Message: '%s'; Code: '%s'", |
||
168 | $soapfault->getFaultActor()?->getContent(), |
||
169 | $soapfault->getFaultString()->getContent(), |
||
170 | $soapfault->getFaultCode()->getContent(), |
||
171 | ), |
||
172 | ); |
||
173 | } |
||
174 | |||
175 | // Extract the message from the response |
||
176 | /** @var \SimpleSAML\XML\SerializableElementInterface[] $messages */ |
||
177 | $messages = $env->getBody()->getElements(); |
||
178 | $samlresponse = MessageFactory::fromXML($messages[0]->toXML()); |
||
179 | |||
180 | /* Add validator to message which uses the SSL context. */ |
||
181 | self::addSSLValidator($samlresponse, $context); |
||
182 | |||
183 | $container->getLogger()->debug("Valid ArtifactResponse received from IdP"); |
||
184 | |||
185 | return $samlresponse; |
||
186 | } |
||
277 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths