-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor & move xsd-specific tests to xml-common
- Loading branch information
Showing
13 changed files
with
219 additions
and
674 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\Assert; | ||
|
||
use InvalidArgumentException; | ||
|
||
use function base64_decode; | ||
use function base64_encode; | ||
use function filter_var; | ||
use function sprintf; | ||
use function strlen; | ||
|
||
/** | ||
* @package simplesamlphp/assert | ||
*/ | ||
trait Base64Trait | ||
{ | ||
/** @var string */ | ||
private static string $base64_regex = '/^(?:[a-z0-9+\/]{4})*(?:[a-z0-9+\/]{2}==|[a-z0-9+\/]{3}=)?$/i'; | ||
|
||
|
||
/*********************************************************************************** | ||
* NOTE: Custom assertions may be added below this line. * | ||
* They SHOULD be marked as `protected` to ensure the call is forced * | ||
* through __callStatic(). * | ||
* Assertions marked `public` are called directly and will * | ||
* not handle any custom exception passed to it. * | ||
***********************************************************************************/ | ||
|
||
|
||
/** | ||
* Note: This test is not bullet-proof but prevents a string containing illegal characters | ||
* from being passed and ensures the string roughly follows the correct format for a Base64 encoded string | ||
* | ||
* @param string $value | ||
* @param string $message | ||
*/ | ||
protected static function validBase64(string $value, string $message = ''): void | ||
{ | ||
$result = true; | ||
|
||
if (filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$base64_regex]]) === false) { | ||
$result = false; | ||
} elseif (strlen($value) % 4 !== 0) { | ||
$result = false; | ||
} else { | ||
$decoded = base64_decode($value, true); | ||
if (empty($decoded)) { // Invalid _or_ empty string | ||
$result = false; | ||
} elseif (base64_encode($decoded) !== $value) { | ||
$result = false; | ||
} | ||
} | ||
|
||
if ($result === false) { | ||
throw new InvalidArgumentException(sprintf( | ||
$message ?: '\'%s\' is not a valid Base64 encoded string', | ||
$value, | ||
)); | ||
} | ||
} | ||
} |
Oops, something went wrong.