OneLogin_Saml2_Response::_decryptAssertion PHP Method

_decryptAssertion() protected method

Decrypts the Assertion (DOMDocument)
protected _decryptAssertion ( DomNode $dom ) : DOMDocument
$dom DomNode DomDocument
return DOMDocument Decrypted Assertion
    protected function _decryptAssertion($dom)
    {
        $pem = $this->_settings->getSPkey();
        if (empty($pem)) {
            throw new Exception("No private key available, check settings");
        }
        $objenc = new XMLSecEnc();
        $encData = $objenc->locateEncryptedData($dom);
        if (!$encData) {
            throw new Exception("Cannot locate encrypted assertion");
        }
        $objenc->setNode($encData);
        $objenc->type = $encData->getAttribute("Type");
        if (!($objKey = $objenc->locateKey())) {
            throw new Exception("Unknown algorithm");
        }
        $key = null;
        if ($objKeyInfo = $objenc->locateKeyInfo($objKey)) {
            if ($objKeyInfo->isEncrypted) {
                $objencKey = $objKeyInfo->encryptedCtx;
                $objKeyInfo->loadKey($pem, false, false);
                $key = $objencKey->decryptKey($objKeyInfo);
            } else {
                // symmetric encryption key support
                $objKeyInfo->loadKey($pem, false, false);
            }
        }
        if (empty($objKey->key)) {
            $objKey->loadKey($key);
        }
        $decrypted = $objenc->decryptNode($objKey, true);
        if ($decrypted instanceof DOMDocument) {
            return $decrypted;
        } else {
            $encryptedAssertion = $decrypted->parentNode;
            $container = $encryptedAssertion->parentNode;
            # Fix possible issue with saml namespace
            if (!$decrypted->hasAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:saml') && !$decrypted->hasAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:saml2') && !$decrypted->hasAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns') && !$container->hasAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:saml') && !$container->hasAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:saml2')) {
                if (strpos($encryptedAssertion->tagName, 'saml2:') !== false) {
                    $ns = 'xmlns:saml2';
                } else {
                    if (strpos($encryptedAssertion->tagName, 'saml:') != false) {
                        $ns = 'xmlns:saml';
                    } else {
                        $ns = 'xmlns';
                    }
                }
                $decrypted->setAttributeNS('http://www.w3.org/2000/xmlns/', $ns, OneLogin_Saml2_Constants::NS_SAML);
            }
            $container->replaceChild($decrypted, $encryptedAssertion);
            return $decrypted->ownerDocument;
        }
    }