OneLogin_Saml2_Utils::addSign PHP Метод

addSign() публичный статический Метод

Adds signature key and senders certificate to an element (Message or Assertion).
public static addSign ( string | DomDocument $xml, string $key, string $cert, string $signAlgorithm = XMLSecurityKey::RSA_SHA1 ) : string
$xml string | DomDocument The element we should sign
$key string The private key
$cert string The public
$signAlgorithm string Signature algorithm method
Результат string
    public static function addSign($xml, $key, $cert, $signAlgorithm = XMLSecurityKey::RSA_SHA1)
    {
        if ($xml instanceof DOMDocument) {
            $dom = $xml;
        } else {
            $dom = new DOMDocument();
            $dom = self::loadXML($dom, $xml);
            if (!$dom) {
                throw new Exception('Error parsing xml string');
            }
        }
        /* Load the private key. */
        $objKey = new XMLSecurityKey($signAlgorithm, array('type' => 'private'));
        $objKey->loadKey($key, false);
        /* Get the EntityDescriptor node we should sign. */
        $rootNode = $dom->firstChild;
        /* Sign the metadata with our private key. */
        $objXMLSecDSig = new XMLSecurityDSig();
        $objXMLSecDSig->setCanonicalMethod(XMLSecurityDSig::EXC_C14N);
        $objXMLSecDSig->addReferenceList(array($rootNode), XMLSecurityDSig::SHA1, array('http://www.w3.org/2000/09/xmldsig#enveloped-signature', XMLSecurityDSig::EXC_C14N), array('id_name' => 'ID'));
        $objXMLSecDSig->sign($objKey);
        /* Add the certificate to the signature. */
        $objXMLSecDSig->add509Cert($cert, true);
        $insertBefore = $rootNode->firstChild;
        $messageTypes = array('AuthnRequest', 'Response', 'LogoutRequest', 'LogoutResponse');
        if (in_array($rootNode->localName, $messageTypes)) {
            $issuerNodes = self::query($dom, '/' . $rootNode->tagName . '/saml:Issuer');
            if ($issuerNodes->length == 1) {
                $insertBefore = $issuerNodes->item(0)->nextSibling;
            }
        }
        /* Add the signature. */
        $objXMLSecDSig->insertSignature($rootNode, $insertBefore);
        /* Return the DOM tree as a string. */
        $signedxml = $dom->saveXML();
        return $signedxml;
    }

Usage Example

Пример #1
0
 /**
  * Signs the metadata with the key/cert provided
  *
  * @param string $metadata SAML Metadata XML
  * @param string $key      x509 key
  * @param string $cert     x509 cert
  *
  * @return string Signed Metadata
  */
 public static function signMetadata($metadata, $key, $cert)
 {
     return OneLogin_Saml2_Utils::addSign($metadata, $key, $cert);
 }
All Usage Examples Of OneLogin_Saml2_Utils::addSign