Skip to content

Commit

Permalink
Merge pull request #525 from pitbulk/4.x-dev
Browse files Browse the repository at this point in the history
Compatibility with PHP 8.1
  • Loading branch information
bzvestey authored Jun 15, 2022
2 parents 2130bac + 12fc7b8 commit 247a45c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
24 changes: 16 additions & 8 deletions src/Saml2/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,10 @@ public function isValid($requestId = null)

// Check destination
if ($this->document->documentElement->hasAttribute('Destination')) {
$destination = trim($this->document->documentElement->getAttribute('Destination'));
$destination = $this->document->documentElement->getAttribute('Destination');
if (isset($destination)) {
$destination = trim($destination);
}
if (empty($destination)) {
if (!$security['relaxDestinationValidation']) {
throw new ValidationError(
Expand Down Expand Up @@ -308,12 +311,14 @@ public function isValid($requestId = null)
// Check the issuers
$issuers = $this->getIssuers();
foreach ($issuers as $issuer) {
$trimmedIssuer = trim($issuer);
if (empty($trimmedIssuer) || $trimmedIssuer !== $idPEntityId) {
throw new ValidationError(
"Invalid issuer in the Assertion/Response (expected '$idPEntityId', got '$trimmedIssuer')",
ValidationError::WRONG_ISSUER
);
if (isset($issuer)) {
$trimmedIssuer = trim($issuer);
if (empty($trimmedIssuer) || $trimmedIssuer !== $idPEntityId) {
throw new ValidationError(
"Invalid issuer in the Assertion/Response (expected '$idPEntityId', got '$trimmedIssuer')",
ValidationError::WRONG_ISSUER
);
}
}
}

Expand Down Expand Up @@ -554,7 +559,10 @@ public function getAudiences()

$entries = $this->_queryAssertion('/saml:Conditions/saml:AudienceRestriction/saml:Audience');
foreach ($entries as $entry) {
$value = trim($entry->textContent);
$value = $entry->textContent;
if (isset($value)) {
$value = trim($value);
}
if (!empty($value)) {
$audiences[] = $value;
}
Expand Down
27 changes: 25 additions & 2 deletions src/Saml2/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ public static function treeCopyReplace(DomNode $targetNode, DomNode $sourceNode,
*/
public static function formatCert($cert, $heads = true)
{
if (is_null($cert)) {
return;
}

$x509cert = str_replace(array("\x0D", "\r", "\n"), "", $cert);
if (!empty($x509cert)) {
$x509cert = str_replace('-----BEGIN CERTIFICATE-----', "", $x509cert);
Expand All @@ -245,6 +249,10 @@ public static function formatCert($cert, $heads = true)
*/
public static function formatPrivateKey($key, $heads = true)
{
if (is_null($key)) {
return;
}

$key = str_replace(array("\x0D", "\r", "\n"), "", $key);
if (!empty($key)) {
if (strpos($key, '-----BEGIN PRIVATE KEY-----') !== false) {
Expand Down Expand Up @@ -319,7 +327,12 @@ public static function redirect($url, array $parameters = array(), $stay = false
* Verify that the URL matches the regex for the protocol.
* By default this will check for http and https
*/
$wrongProtocol = !preg_match(self::$_protocolRegex, $url);
if (isset(self::$_protocolRegex)) {
$protocol = self::$_protocolRegex;
} else {
$protocol = "";
}
$wrongProtocol = !preg_match($protocol, $url);
$url = filter_var($url, FILTER_VALIDATE_URL);
if ($wrongProtocol || empty($url)) {
throw new Error(
Expand Down Expand Up @@ -773,6 +786,10 @@ public static function parseTime2SAML($time)
*/
public static function parseSAML2Time($time)
{
if (empty($time)) {
return null;
}

$matches = array();

/* We use a very strict regex to parse the timestamp. */
Expand Down Expand Up @@ -1010,7 +1027,10 @@ public static function calculateX509Fingerprint($x509cert, $alg = 'sha1')
if (strncmp($curData, '-----END CERTIFICATE', 20) == 0) {
break;
}
$data .= trim($curData);
if (isset($curData)) {
$curData = trim($curData);
}
$data .= $curData;
}
}

Expand Down Expand Up @@ -1043,6 +1063,9 @@ public static function calculateX509Fingerprint($x509cert, $alg = 'sha1')
*/
public static function formatFingerPrint($fingerprint)
{
if (is_null($fingerprint)) {
return;
}
$formatedFingerprint = str_replace(':', '', $fingerprint);
$formatedFingerprint = strtolower($formatedFingerprint);
return $formatedFingerprint;
Expand Down

0 comments on commit 247a45c

Please sign in to comment.