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