Skip to content

Commit

Permalink
Refactor & move xsd-specific tests to xml-common
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Jan 7, 2025
1 parent 0984792 commit 6b7684d
Show file tree
Hide file tree
Showing 13 changed files with 219 additions and 674 deletions.
34 changes: 6 additions & 28 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,46 +311,24 @@
* @method static void nullOrThrows(Closure|null $expression, string $class, string $message = '', string $exception = '')
* @method static void allThrows(Closure[] $expression, string $class, string $message = '', string $exception = '')
*
* @method static void validHexBinary(mixed $value, string $message = '', string $exception = '')
* @method static void validNMToken(mixed $value, string $message = '', string $exception = '')
* @method static void validNMTokens(mixed $value, string $message = '', string $exception = '')
* @method static void validDuration(mixed $value, string $message = '', string $exception = '')
* @method static void stringPlausibleBase64(mixed $value, string $message = '', string $exception = '')
* @method static void validDateTime(mixed $value, string $message = '', string $exception = '')
* @method static void validBase64(mixed $value, string $message = '', string $exception = '')
* @method static void notInArray(mixed $value, array $values, string $message = '', string $exception = '')
* @method static void validURN(mixed $value, string $message = '', string $exception = '')
* @method static void validURI(mixed $value, string $message = '', string $exception = '')
* @method static void validURL(mixed $value, string $message = '', string $exception = '')
* @method static void validNCName(mixed $value, string $message = '', string $exception = '')
* @method static void validQName(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidHexBinary(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidNMToken(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidNMTokens(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidDuration(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrStringPlausibleBase64(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidDateTime(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidBase64(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrNotInArray(mixed $value, array $values, string $message = '', string $exception = '')
* @method static void nullOrValidURN(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidURI(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidURL(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidNCName(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidQName(mixed $value, string $message = '', string $exception = '')
* @method static void allValidHexBinary(mixed $value, string $message = '', string $exception = '')
* @method static void allValidNMToken(mixed $value, string $message = '', string $exception = '')
* @method static void allValidNMTokens(mixed $value, string $message = '', string $exception = '')
* @method static void allValidDuration(mixed $value, string $message = '', string $exception = '')
* @method static void allStringPlausibleBase64(mixed $value, string $message = '', string $exception = '')
* @method static void allValidDateTime(mixed $value, string $message = '', string $exception = '')
* @method static void allValidBase64(mixed $value, string $message = '', string $exception = '')
* @method static void allNotInArray(mixed $value, array $values, string $message = '', string $exception = '')
* @method static void allValidURN(mixed $value, string $message = '', string $exception = '')
* @method static void allValidURI(mixed $value, string $message = '', string $exception = '')
* @method static void allValidURL(mixed $value, string $message = '', string $exception = '')
* @method static void allValidNCName(mixed $value, string $message = '', string $exception = '')
* @method static void allValidQName(mixed $value, string $message = '', string $exception = '')
*/
final class Assert
{
use CustomAssertionTrait;
use Base64Trait;
use NotInArrayTrait;
use URITrait;


/**
Expand Down
64 changes: 64 additions & 0 deletions src/Base64Trait.php
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,
));
}
}
}
Loading

0 comments on commit 6b7684d

Please sign in to comment.