Skip to content

Commit

Permalink
Refactor assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Jan 8, 2025
1 parent 886e2a5 commit 380601a
Show file tree
Hide file tree
Showing 19 changed files with 34 additions and 196 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

"psr/clock": "^1.0",
"psr/log": "^2.0 | ^3.0",
"simplesamlphp/assert": "^1.6",
"simplesamlphp/xml-common": "~1.23.0",
"simplesamlphp/xml-security": "~1.12.0"
"simplesamlphp/assert": "^1.8",
"simplesamlphp/xml-common": "~1.24.0",
"simplesamlphp/xml-security": "~1.13.0"
},
"require-dev": {
"beste/clock": "^3.0",
Expand Down
155 changes: 2 additions & 153 deletions src/SAML11/Assert/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,7 @@

namespace SimpleSAML\SAML11\Assert;

use BadMethodCallException; // Requires ext-spl
use DateTime; // requires ext-date
use DateTimeImmutable; // requires ext-date
use InvalidArgumentException; // Requires ext-spl
use SimpleSAML\Assert\Assert as BaseAssert;
use SimpleSAML\Assert\AssertionFailedException;
use Throwable;

use function array_pop;
use function array_unshift;
use function call_user_func_array;
use function end;
use function enum_exists;
use function function_exists;
use function get_class;
use function is_object;
use function is_resource;
use function is_string;
use function is_subclass_of;
use function lcfirst;
use function method_exists;
use function preg_match; // Requires ext-pcre
use function strval;
use SimpleSAML\XML\Assert\Assert as BaseAssert;

/**
* SimpleSAML\SAML11\Assert\Assert wrapper class
Expand All @@ -43,136 +21,7 @@
* @method static void allValidURI(mixed $value, string $message = '', string $exception = '')
* @method static void allValidEntityID(mixed $value, string $message = '', string $exception = '')
*/
final class Assert
class Assert extends BaseAssert
{
use CustomAssertionTrait;


/**
* @param string $name
* @param array<mixed> $arguments
*/
public static function __callStatic(string $name, array $arguments): void
{
// Handle Exception-parameter
$exception = AssertionFailedException::class;

$last = end($arguments);
if (is_string($last) && class_exists($last) && is_subclass_of($last, Throwable::class)) {
$exception = $last;
array_pop($arguments);
}

try {
if (method_exists(static::class, $name)) {
call_user_func_array([static::class, $name], $arguments);
return;
} elseif (preg_match('/^nullOr(.*)$/i', $name, $matches)) {
$method = lcfirst($matches[1]);
if (method_exists(static::class, $method)) {
call_user_func_array([static::class, 'nullOr'], [[static::class, $method], $arguments]);
} elseif (method_exists(BaseAssert::class, $method)) {
call_user_func_array([static::class, 'nullOr'], [[BaseAssert::class, $method], $arguments]);
} else {
throw new BadMethodCallException(sprintf("Assertion named `%s` does not exists.", $method));
}
} elseif (preg_match('/^all(.*)$/i', $name, $matches)) {
$method = lcfirst($matches[1]);
if (method_exists(static::class, $method)) {
call_user_func_array([static::class, 'all'], [[static::class, $method], $arguments]);
} elseif (method_exists(BaseAssert::class, $method)) {
call_user_func_array([static::class, 'all'], [[BaseAssert::class, $method], $arguments]);
} else {
throw new BadMethodCallException(sprintf("Assertion named `%s` does not exists.", $method));
}
} else {
throw new BadMethodCallException(sprintf("Assertion named `%s` does not exists.", $name));
}
} catch (InvalidArgumentException $e) {
throw new $exception($e->getMessage());
}
}


/**
* Handle nullOr* for either Webmozart or for our custom assertions
*
* @param callable $method
* @param array<mixed> $arguments
* @return void
*/
private static function nullOr(callable $method, array $arguments): void
{
$value = reset($arguments);
($value === null) || call_user_func_array($method, $arguments);
}


/**
* all* for our custom assertions
*
* @param callable $method
* @param array<mixed> $arguments
* @return void
*/
private static function all(callable $method, array $arguments): void
{
$values = array_pop($arguments);
foreach ($values as $value) {
$tmp = $arguments;
array_unshift($tmp, $value);
call_user_func_array($method, $tmp);
}
}


/**
* @param mixed $value
*
* @return string
*/
protected static function valueToString(mixed $value): string
{
if (is_resource($value)) {
return 'resource';
}

if (null === $value) {
return 'null';
}

if (true === $value) {
return 'true';
}

if (false === $value) {
return 'false';
}

if (is_array($value)) {
return 'array';
}

if (is_object($value)) {
if (method_exists($value, '__toString')) {
return $value::class . ': ' . self::valueToString($value->__toString());
}

if ($value instanceof DateTime || $value instanceof DateTimeImmutable) {
return $value::class . ': ' . self::valueToString($value->format('c'));
}

if (function_exists('enum_exists') && enum_exists(get_class($value))) {
return get_class($value) . '::' . $value->name;
}

return $value::class;
}

if (is_string($value)) {
return '"' . $value . '"';
}

return strval($value);
}
}
25 changes: 8 additions & 17 deletions src/SAML11/Assert/CustomAssertionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace SimpleSAML\SAML11\Assert;

use SimpleSAML\Assert\Assert as BaseAssert;
use SimpleSAML\Assert\AssertionFailedException;
use SimpleSAML\SAML11\Exception\ProtocolViolationException;
use SimpleSAML\XML\Exception\SchemaViolationException;
Expand All @@ -18,7 +17,7 @@ trait CustomAssertionTrait

/***********************************************************************************
* NOTE: Custom assertions may be added below this line. *
* They SHOULD be marked as `private` to ensure the call is forced *
* 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. *
Expand All @@ -29,16 +28,12 @@ trait CustomAssertionTrait
* @param string $value
* @param string $message
*/
private static function validDateTime(string $value, string $message = ''): void
protected static function validDateTime(string $value, string $message = ''): void
{
try {
BaseAssert::validDateTime($value, $message);
} catch (AssertionFailedException $e) {
throw new SchemaViolationException($e->getMessage());
}
parent::validDateTime($value, $message, SchemaViolationException::class);

try {
BaseAssert::endsWith(
static::endsWith(
$value,
'Z',
$message ?: '%s is not a DateTime expressed in the UTC timezone using the \'Z\' timezone identifier.',
Expand All @@ -53,18 +48,14 @@ private static function validDateTime(string $value, string $message = ''): void
* @param string $value
* @param string $message
*/
private static function validURI(string $value, string $message = ''): void
protected static function validURI(string $value, string $message = ''): void
{
try {
BaseAssert::validURI($value, $message);
} catch (AssertionFailedException $e) {
throw new SchemaViolationException($e->getMessage());
}
parent::validURI($value, $message, SchemaViolationException::class);

try {
BaseAssert::notWhitespaceOnly($value, $message ?: '%s is not a SAML1.1-compliant URI');
static::notWhitespaceOnly($value, $message ?: '%s is not a SAML1.1-compliant URI');
// If it doesn't have a scheme, it's not an absolute URI
BaseAssert::regex($value, self::$scheme_regex, $message ?: '%s is not a SAML1.1-compliant URI');
static::regex($value, self::$scheme_regex, $message ?: '%s is not a SAML1.1-compliant URI');
} catch (AssertionFailedException $e) {
throw new ProtocolViolationException($e->getMessage());
}
Expand Down
2 changes: 1 addition & 1 deletion src/SAML11/Compat/AbstractContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Psr\Clock\ClockInterface;
use Psr\Log\LoggerInterface;
use SimpleSAML\Assert\Assert;
use SimpleSAML\SAML11\Assert\Assert;
use SimpleSAML\SAML11\XML\ExtensionPointInterface;
use SimpleSAML\XML\AbstractElement;
use SimpleSAML\XML\Exception\SchemaViolationException;
Expand Down
5 changes: 2 additions & 3 deletions src/SAML11/XML/ExtensionPointTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
namespace SimpleSAML\SAML11\XML;

use RuntimeException;
use SimpleSAML\Assert\Assert;
use SimpleSAML\SAML11\Assert\Assert as SAMLAssert;
use SimpleSAML\SAML11\Assert\Assert;
use SimpleSAML\XML\Exception\SchemaViolationException;

/**
Expand Down Expand Up @@ -58,7 +57,7 @@ public static function getXsiTypeNamespaceURI(): string
RuntimeException::class,
);

SAMLAssert::validURI(static::XSI_TYPE_NAMESPACE, SchemaViolationException::class);
Assert::validURI(static::XSI_TYPE_NAMESPACE, SchemaViolationException::class);
return static::XSI_TYPE_NAMESPACE;
}

Expand Down
5 changes: 2 additions & 3 deletions src/SAML11/XML/saml/AbstractAssertionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

use DateTimeImmutable;
use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\SAML11\Assert\Assert as SAMLAssert;
use SimpleSAML\SAML11\Assert\Assert;
use SimpleSAML\SAML11\Compat\ContainerSingleton;
use SimpleSAML\SAML11\Constants as C;
use SimpleSAML\SAML11\Exception\ProtocolViolationException;
Expand Down Expand Up @@ -249,7 +248,7 @@ public static function fromXML(DOMElement $xml): static
// Strip sub-seconds - See paragraph 1.2.2 of SAML core specifications
$issueInstant = preg_replace('/([.][0-9]+Z)$/', 'Z', $issueInstant, 1);

SAMLAssert::validDateTime($issueInstant, ProtocolViolationException::class);
Assert::validDateTime($issueInstant, ProtocolViolationException::class);
$issueInstant = new DateTimeImmutable($issueInstant);

$conditions = Conditions::getChildrenOfClass($xml);
Expand Down
7 changes: 3 additions & 4 deletions src/SAML11/XML/saml/AbstractAuthorityBindingType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
namespace SimpleSAML\SAML11\XML\saml;

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\SAML11\Assert\Assert as SAMLAssert;
use SimpleSAML\SAML11\Assert\Assert;
use SimpleSAML\SAML11\Constants as C;
use SimpleSAML\XML\Exception\InvalidDOMElementException;

Expand All @@ -30,8 +29,8 @@ final public function __construct(
protected string $Binding,
) {
Assert::validQName($AuthorityKind);
SAMLAssert::validURI($Location);
SAMLAssert::validURI($Binding);
Assert::validURI($Location);
Assert::validURI($Binding);
}


Expand Down
2 changes: 1 addition & 1 deletion src/SAML11/XML/saml/AbstractCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace SimpleSAML\SAML11\XML\saml;

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\SAML11\Assert\Assert;
use SimpleSAML\SAML11\Constants as C;
use SimpleSAML\SAML11\Utils;
use SimpleSAML\SAML11\XML\ExtensionPointInterface;
Expand Down
2 changes: 1 addition & 1 deletion src/SAML11/XML/saml/AbstractStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace SimpleSAML\SAML11\XML\saml;

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\SAML11\Assert\Assert;
use SimpleSAML\SAML11\Constants as C;
use SimpleSAML\SAML11\Utils;
use SimpleSAML\SAML11\XML\ExtensionPointInterface;
Expand Down
2 changes: 1 addition & 1 deletion src/SAML11/XML/saml/AbstractSubjectStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace SimpleSAML\SAML11\XML\saml;

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\SAML11\Assert\Assert;
use SimpleSAML\SAML11\Constants as C;
use SimpleSAML\SAML11\Utils;
use SimpleSAML\SAML11\XML\ExtensionPointInterface;
Expand Down
2 changes: 1 addition & 1 deletion src/SAML11/XML/saml/AssertionIDReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace SimpleSAML\SAML11\XML\saml;

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\SAML11\Assert\Assert;
use SimpleSAML\SAML11\XML\StringElementTrait;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\Exception\SchemaViolationException;
Expand Down
2 changes: 1 addition & 1 deletion src/SAML11/XML/saml/AttributeValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use DateTimeImmutable;
use DateTimeInterface;
use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\SAML11\Assert\Assert;
use SimpleSAML\SAML11\Constants as C;
use SimpleSAML\XML\AbstractElement;
use SimpleSAML\XML\Chunk;
Expand Down
2 changes: 1 addition & 1 deletion src/SAML11/XML/samlp/AbstractQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace SimpleSAML\SAML11\XML\samlp;

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\SAML11\Assert\Assert;
use SimpleSAML\SAML11\Constants as C;
use SimpleSAML\SAML11\Utils;
use SimpleSAML\SAML11\XML\ExtensionPointInterface;
Expand Down
2 changes: 1 addition & 1 deletion src/SAML11/XML/samlp/AbstractRequestAbstractType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use DateTimeImmutable;
use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\SAML11\Assert\Assert;
use SimpleSAML\SAML11\Utils\XPath;
use SimpleSAML\XML\Exception\SchemaViolationException;

Expand Down
2 changes: 1 addition & 1 deletion src/SAML11/XML/samlp/AbstractResponseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use DateTimeImmutable;
use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\SAML11\Assert\Assert;
use SimpleSAML\SAML11\XML\saml\Assertion;
use SimpleSAML\SAML11\XML\samlp\Status;
use SimpleSAML\XML\Exception\SchemaViolationException;
Expand Down
2 changes: 1 addition & 1 deletion src/SAML11/XML/samlp/AbstractStatusCodeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace SimpleSAML\SAML11\XML\samlp;

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\SAML11\Assert\Assert;
use SimpleSAML\SAML11\Constants as C;
use SimpleSAML\XML\Exception\InvalidDOMElementException;

Expand Down
2 changes: 1 addition & 1 deletion src/SAML11/XML/samlp/AbstractSubjectQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace SimpleSAML\SAML11\XML\samlp;

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\SAML11\Assert\Assert;
use SimpleSAML\SAML11\Constants as C;
use SimpleSAML\SAML11\Utils;
use SimpleSAML\SAML11\XML\ExtensionPointInterface;
Expand Down
Loading

0 comments on commit 380601a

Please sign in to comment.