-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate saml-specific assertions to this library
- Loading branch information
Showing
10 changed files
with
338 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
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 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; | ||
|
||
/** | ||
* SimpleSAML\SAML11\Assert\Assert wrapper class | ||
* | ||
* @package simplesamlphp/saml11 | ||
* | ||
* @method static void validDateTime(mixed $value, string $message = '', string $exception = '') | ||
* @method static void validURI(mixed $value, string $message = '', string $exception = '') | ||
* @method static void validEntityID(mixed $value, string $message = '', string $exception = '') | ||
* @method static void nullOrValidDateTime(mixed $value, string $message = '', string $exception = '') | ||
* @method static void nullOrValidURI(mixed $value, string $message = '', string $exception = '') | ||
* @method static void nullOrValidEntityID(mixed $value, string $message = '', string $exception = '') | ||
* @method static void allValidDateTime(mixed $value, string $message = '', string $exception = '') | ||
* @method static void allValidURI(mixed $value, string $message = '', string $exception = '') | ||
* @method static void allValidEntityID(mixed $value, string $message = '', string $exception = '') | ||
*/ | ||
final class Assert | ||
{ | ||
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')); | ||
} | ||
|
||
return $value::class; | ||
} | ||
|
||
if (is_string($value)) { | ||
return '"' . $value . '"'; | ||
} | ||
|
||
return strval($value); | ||
} | ||
} |
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,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\SAML11\Assert; | ||
|
||
use SimpleSAML\Assert\Assert as BaseAssert; | ||
use SimpleSAML\Assert\AssertionFailedException; | ||
use SimpleSAML\SAML11\Constants as C; | ||
use SimpleSAML\SAML11\Exception\ProtocolViolationException; | ||
use SimpleSAML\XML\Exception\SchemaViolationException; | ||
|
||
/** | ||
* @package simplesamlphp/assert | ||
*/ | ||
trait CustomAssertionTrait | ||
{ | ||
private static string $scheme_regex = '/^([a-z][a-z0-9\+\-\.]+[:])/i'; | ||
|
||
/*********************************************************************************** | ||
* NOTE: Custom assertions may be added below this line. * | ||
* They SHOULD be marked as `private` to ensure the call is forced * | ||
* through __callStatic(). * | ||
* Assertions marked `public` are called directly and will * | ||
* not handle any custom exception passed to it. * | ||
***********************************************************************************/ | ||
|
||
|
||
/** | ||
* @param string $value | ||
* @param string $message | ||
*/ | ||
private static function validDateTime(string $value, string $message = ''): void | ||
{ | ||
try { | ||
BaseAssert::validDateTime($value, $message); | ||
} catch (AssertionFailedException $e) { | ||
throw new SchemaViolationException($e->getMessage()); | ||
} | ||
|
||
try { | ||
BaseAssert::endsWith( | ||
$value, | ||
'Z', | ||
$message ?: '%s is not a DateTime expressed in the UTC timezone using the \'Z\' timezone identifier.', | ||
); | ||
} catch (AssertionFailedException $e) { | ||
throw new ProtocolViolationException($e->getMessage()); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* @param string $value | ||
* @param string $message | ||
*/ | ||
private static function validURI(string $value, string $message = ''): void | ||
{ | ||
try { | ||
BaseAssert::validURI($value, $message); | ||
} catch (AssertionFailedException $e) { | ||
throw new SchemaViolationException($e->getMessage()); | ||
} | ||
|
||
try { | ||
BaseAssert::notWhitespaceOnly($value, $message ?: '%s is not a SAML1.1-compliant URI'); | ||
} catch (AssertionFailedException $e) { | ||
throw new ProtocolViolationException($e->getMessage()); | ||
} | ||
} | ||
} |
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
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
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
Oops, something went wrong.