SimpleSAML_Metadata_Signer::sign PHP Méthode

sign() public static méthode

Signs the given metadata if metadata signing is enabled.
public static sign ( string $metadataString, array $entityMetadata, string $type ) : string
$metadataString string A string with the metadata.
$entityMetadata array The metadata of the entity.
$type string A string which describes the type entity this is, e.g. 'SAML 2 IdP' or 'Shib 1.3 SP'.
Résultat string The $metadataString with the signature embedded.
    public static function sign($metadataString, $entityMetadata, $type)
    {
        $config = SimpleSAML_Configuration::getInstance();
        // check if metadata signing is enabled
        if (!self::isMetadataSigningEnabled($config, $entityMetadata, $type)) {
            return $metadataString;
        }
        // find the key & certificate which should be used to sign the metadata
        $keyCertFiles = self::findKeyCert($config, $entityMetadata, $type);
        $keyFile = \SimpleSAML\Utils\Config::getCertPath($keyCertFiles['privatekey']);
        if (!file_exists($keyFile)) {
            throw new Exception('Could not find private key file [' . $keyFile . '], which is needed to sign the metadata');
        }
        $keyData = file_get_contents($keyFile);
        $certFile = \SimpleSAML\Utils\Config::getCertPath($keyCertFiles['certificate']);
        if (!file_exists($certFile)) {
            throw new Exception('Could not find certificate file [' . $certFile . '], which is needed to sign the metadata');
        }
        $certData = file_get_contents($certFile);
        // convert the metadata to a DOM tree
        try {
            $xml = \SAML2\DOMDocumentFactory::fromString($metadataString);
        } catch (Exception $e) {
            throw new Exception('Error parsing self-generated metadata.');
        }
        $signature_cf = self::getMetadataSigningAlgorithm($config, $entityMetadata, $type);
        // load the private key
        $objKey = new XMLSecurityKey($signature_cf['algorithm'], array('type' => 'private'));
        if (array_key_exists('privatekey_pass', $keyCertFiles)) {
            $objKey->passphrase = $keyCertFiles['privatekey_pass'];
        }
        $objKey->loadKey($keyData, false);
        // get the EntityDescriptor node we should sign
        $rootNode = $xml->firstChild;
        // sign the metadata with our private key
        if ($type == 'ADFS IdP') {
            $objXMLSecDSig = new sspmod_adfs_XMLSecurityDSig($metadataString);
        } else {
            $objXMLSecDSig = new XMLSecurityDSig();
        }
        $objXMLSecDSig->setCanonicalMethod(XMLSecurityDSig::EXC_C14N);
        $objXMLSecDSig->addReferenceList(array($rootNode), $signature_cf['digest'], 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($certData, true);
        // add the signature to the metadata
        $objXMLSecDSig->insertSignature($rootNode, $rootNode->firstChild);
        // return the DOM tree as a string
        return $xml->saveXML();
    }

Usage Example

Exemple #1
0
}
$supported_protocols = array('urn:oasis:names:tc:SAML:1.1:protocol', SAML2_Const::NS_SAMLP);
$metaArray20['metadata-set'] = 'saml20-sp-remote';
$metaArray20['entityid'] = $entityId;
$metaBuilder = new SimpleSAML_Metadata_SAMLBuilder($entityId);
$metaBuilder->addMetadataSP20($metaArray20, $supported_protocols);
$metaBuilder->addOrganizationInfo($metaArray20);
if (!empty($contact)) {
    $metaBuilder->addContact('technical', $contact);
}
foreach ($contacts as $c) {
    $metaBuilder->addContact($c['contactType'], $c);
}
$xml = $metaBuilder->getEntityDescriptorText();
unset($metaArray20['attributes.required']);
unset($metaArray20['UIInfo']);
unset($metaArray20['metadata-set']);
unset($metaArray20['entityid']);
/* Sign the metadata if enabled. */
$xml = SimpleSAML_Metadata_Signer::sign($xml, $spconfig->toArray(), 'SAML 2 SP');
if (array_key_exists('output', $_REQUEST) && $_REQUEST['output'] == 'xhtml') {
    $t = new SimpleSAML_XHTML_Template($config, 'metadata.php', 'admin');
    $t->data['header'] = 'saml20-sp';
    $t->data['metadata'] = htmlspecialchars($xml);
    $t->data['metadataflat'] = '$metadata[' . var_export($entityId, TRUE) . '] = ' . var_export($metaArray20, TRUE) . ';';
    $t->data['metaurl'] = $source->getMetadataURL();
    $t->show();
} else {
    header('Content-Type: application/samlmetadata+xml');
    echo $xml;
}
All Usage Examples Of SimpleSAML_Metadata_Signer::sign