OneLogin_Saml2_Utils::formatCert PHP Method

formatCert() public static method

Returns a x509 cert (adding header & footer if required).
public static formatCert ( string $cert, boolean $heads = true ) : string
$cert string A x509 unformated cert
$heads boolean True if we want to include head and footer
return string $x509 Formatted cert
    public static function formatCert($cert, $heads = true)
    {
        $x509cert = str_replace(array("\r", "\r", "\n"), "", $cert);
        if (!empty($x509cert)) {
            $x509cert = str_replace('-----BEGIN CERTIFICATE-----', "", $x509cert);
            $x509cert = str_replace('-----END CERTIFICATE-----', "", $x509cert);
            $x509cert = str_replace(' ', '', $x509cert);
            if ($heads) {
                $x509cert = "-----BEGIN CERTIFICATE-----\n" . chunk_split($x509cert, 64, "\n") . "-----END CERTIFICATE-----\n";
            }
        }
        return $x509cert;
    }

Usage Example

Example #1
0
 /**
  * Tests the generateNameId method of the OneLogin_Saml2_Utils
  *
  * @covers OneLogin_Saml2_Utils::generateNameId
  */
 public function testGenerateNameId()
 {
     //$xml = '<root xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'.$decrypted.'</root>';
     //$newDoc = new DOMDocument();
     $nameIdValue = 'ONELOGIN_ce998811003f4e60f8b07a311dc641621379cfde';
     $entityId = 'http://stuff.com/endpoints/metadata.php';
     $nameIDFormat = 'urn:oasis:names:tc:SAML:2.0:nameid-format:unspecified';
     $nameId = OneLogin_Saml2_Utils::generateNameId($nameIdValue, $entityId, $nameIDFormat);
     $expectedNameId = '<saml:NameID SPNameQualifier="http://stuff.com/endpoints/metadata.php" Format="urn:oasis:names:tc:SAML:2.0:nameid-format:unspecified">ONELOGIN_ce998811003f4e60f8b07a311dc641621379cfde</saml:NameID>';
     $this->assertEquals($nameId, $expectedNameId);
     $settingsDir = TEST_ROOT . '/settings/';
     include $settingsDir . 'settings1.php';
     $x509cert = $settingsInfo['idp']['x509cert'];
     $key = OneLogin_Saml2_Utils::formatCert($x509cert);
     $nameIdEnc = OneLogin_Saml2_Utils::generateNameId($nameIdValue, $entityId, $nameIDFormat, $key);
     $nameidExpectedEnc = '<saml:EncryptedID><xenc:EncryptedData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" Type="http://www.w3.org/2001/04/xmlenc#Element"><xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes128-cbc"/><dsig:KeyInfo xmlns:dsig="http://www.w3.org/2000/09/xmldsig#"><xenc:EncryptedKey><xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5"/><xenc:CipherData><xenc:CipherValue>';
     $this->assertContains($nameidExpectedEnc, $nameIdEnc);
 }
All Usage Examples Of OneLogin_Saml2_Utils::formatCert