| Conditions | 20 |
| Paths | 408 |
| Total Lines | 122 |
| Code Lines | 72 |
| Lines | 0 |
| Ratio | 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 |
||
| 23 | public function send(SAML2_Message $msg, SimpleSAML_Configuration $srcMetadata, SimpleSAML_Configuration $dstMetadata = NULL) |
||
| 24 | { |
||
| 25 | $issuer = $msg->getIssuer(); |
||
| 26 | |||
| 27 | $ctxOpts = array( |
||
| 28 | 'ssl' => array( |
||
| 29 | 'capture_peer_cert' => TRUE, |
||
| 30 | ), |
||
| 31 | ); |
||
| 32 | |||
| 33 | // Determine if we are going to do a MutualSSL connection between the IdP and SP - Shoaib |
||
| 34 | if ($srcMetadata->hasValue('saml.SOAPClient.certificate')) { |
||
| 35 | $cert = $srcMetadata->getValue('saml.SOAPClient.certificate'); |
||
| 36 | if ($cert !== FALSE) { |
||
| 37 | $ctxOpts['ssl']['local_cert'] = SimpleSAML_Utilities::resolveCert( |
||
| 38 | $srcMetadata->getString('saml.SOAPClient.certificate') |
||
| 39 | ); |
||
| 40 | if ($srcMetadata->hasValue('saml.SOAPClient.privatekey_pass')) { |
||
| 41 | $ctxOpts['ssl']['passphrase'] = $srcMetadata->getString('saml.SOAPClient.privatekey_pass'); |
||
| 42 | } |
||
| 43 | } |
||
| 44 | } else { |
||
| 45 | /* Use the SP certificate and privatekey if it is configured. */ |
||
| 46 | $privateKey = SimpleSAML_Utilities::loadPrivateKey($srcMetadata); |
||
| 47 | $publicKey = SimpleSAML_Utilities::loadPublicKey($srcMetadata); |
||
| 48 | if ($privateKey !== NULL && $publicKey !== NULL && isset($publicKey['PEM'])) { |
||
| 49 | $keyCertData = $privateKey['PEM'] . $publicKey['PEM']; |
||
| 50 | $file = SimpleSAML_Utilities::getTempDir() . '/' . sha1($keyCertData) . '.pem'; |
||
| 51 | if (!file_exists($file)) { |
||
| 52 | SimpleSAML_Utilities::writeFile($file, $keyCertData); |
||
| 53 | } |
||
| 54 | $ctxOpts['ssl']['local_cert'] = $file; |
||
| 55 | if (isset($privateKey['password'])) { |
||
| 56 | $ctxOpts['ssl']['passphrase'] = $privateKey['password']; |
||
| 57 | } |
||
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | // do peer certificate verification |
||
| 62 | if ($dstMetadata !== NULL) { |
||
| 63 | $peerPublicKeys = $dstMetadata->getPublicKeys('signing', TRUE); |
||
| 64 | $certData = ''; |
||
| 65 | foreach ($peerPublicKeys as $key) { |
||
| 66 | if ($key['type'] !== 'X509Certificate') { |
||
| 67 | continue; |
||
| 68 | } |
||
| 69 | $certData .= "-----BEGIN CERTIFICATE-----\n" . |
||
| 70 | chunk_split($key['X509Certificate'], 64) . |
||
| 71 | "-----END CERTIFICATE-----\n"; |
||
| 72 | } |
||
| 73 | $peerCertFile = SimpleSAML_Utilities::getTempDir() . '/' . sha1($certData) . '.pem'; |
||
| 74 | if (!file_exists($peerCertFile)) { |
||
| 75 | SimpleSAML_Utilities::writeFile($peerCertFile, $certData); |
||
| 76 | } |
||
| 77 | // create ssl context |
||
| 78 | $ctxOpts['ssl']['verify_peer'] = TRUE; |
||
| 79 | $ctxOpts['ssl']['verify_depth'] = 1; |
||
| 80 | $ctxOpts['ssl']['cafile'] = $peerCertFile; |
||
| 81 | } |
||
| 82 | |||
| 83 | $context = stream_context_create($ctxOpts); |
||
| 84 | if ($context === NULL) { |
||
| 85 | throw new Exception('Unable to create SSL stream context'); |
||
| 86 | } |
||
| 87 | |||
| 88 | $options = array( |
||
| 89 | 'uri' => $issuer, |
||
| 90 | 'location' => $msg->getDestination(), |
||
| 91 | 'stream_context' => $context, |
||
| 92 | ); |
||
| 93 | |||
| 94 | if ($srcMetadata->hasValue('saml.SOAPClient.proxyhost')) { |
||
| 95 | $options['proxy_host'] = $srcMetadata->getValue('saml.SOAPClient.proxyhost'); |
||
| 96 | } |
||
| 97 | |||
| 98 | if ($srcMetadata->hasValue('saml.SOAPClient.proxyport')) { |
||
| 99 | $options['proxy_port'] = $srcMetadata->getValue('saml.SOAPClient.proxyport'); |
||
| 100 | } |
||
| 101 | |||
| 102 | $x = new SoapClient(NULL, $options); |
||
| 103 | |||
| 104 | // Add soap-envelopes |
||
| 105 | $request = $msg->toSignedXML(); |
||
| 106 | $request = self::START_SOAP_ENVELOPE . $request->ownerDocument->saveXML($request) . self::END_SOAP_ENVELOPE; |
||
| 107 | |||
| 108 | SAML2_Utils::getContainer()->debugMessage($request, 'out'); |
||
| 109 | |||
| 110 | $action = 'http://www.oasis-open.org/committees/security'; |
||
| 111 | $version = '1.1'; |
||
| 112 | $destination = $msg->getDestination(); |
||
| 113 | |||
| 114 | /* Perform SOAP Request over HTTP */ |
||
| 115 | $soapresponsexml = $x->__doRequest($request, $destination, $action, $version); |
||
| 116 | if ($soapresponsexml === NULL || $soapresponsexml === "") { |
||
| 117 | throw new Exception('Empty SOAP response, check peer certificate.'); |
||
| 118 | } |
||
| 119 | |||
| 120 | SAML2_Utils::getContainer()->debugMessage($soapresponsexml, 'in'); |
||
| 121 | |||
| 122 | // Convert to SAML2_Message (DOMElement) |
||
| 123 | try { |
||
| 124 | $dom = SAML2_DOMDocumentFactory::fromString($soapresponsexml); |
||
| 125 | } catch (SAML2_Exception_RuntimeException $e) { |
||
| 126 | throw new Exception('Not a SOAP response.', 0, $e); |
||
| 127 | } |
||
| 128 | |||
| 129 | $soapfault = $this->getSOAPFault($dom); |
||
| 130 | if (isset($soapfault)) { |
||
| 131 | throw new Exception($soapfault); |
||
| 132 | } |
||
| 133 | //Extract the message from the response |
||
| 134 | $samlresponse = SAML2_Utils::xpQuery($dom->firstChild, '/soap-env:Envelope/soap-env:Body/*[1]'); |
||
| 135 | $samlresponse = SAML2_Message::fromXML($samlresponse[0]); |
||
| 136 | |||
| 137 | /* Add validator to message which uses the SSL context. */ |
||
| 138 | self::addSSLValidator($samlresponse, $context); |
||
| 139 | |||
| 140 | SAML2_Utils::getContainer()->getLogger()->debug("Valid ArtifactResponse received from IdP"); |
||
| 141 | |||
| 142 | return $samlresponse; |
||
| 143 | |||
| 144 | } |
||
| 145 | |||
| 241 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.