1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\Assert; |
6
|
|
|
|
7
|
|
|
use BadMethodCallException; |
8
|
|
|
use DateTime; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
use Throwable; |
11
|
|
|
use Webmozart\Assert\Assert as Webmozart; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Webmozart\Assert wrapper class |
15
|
|
|
* |
16
|
|
|
* @package simplesamlphp/assert |
17
|
|
|
*/ |
18
|
|
|
final class Assert |
19
|
|
|
{ |
20
|
|
|
private static string $base64_regex = '/^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/'; |
21
|
|
|
|
22
|
|
|
// private static string $datetime_regex = '/^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(.[0-9]+)?$/'; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string $name |
27
|
|
|
* @param array $arguments |
28
|
|
|
*/ |
29
|
|
|
public static function __callStatic($name, $arguments): void |
30
|
|
|
{ |
31
|
|
|
// Handle Exception-parameter |
32
|
|
|
$exception = AssertionFailedException::class; |
33
|
|
|
$last = end($arguments); |
34
|
|
|
if (is_string($last) && class_exists($last) && is_subclass_of($last, Throwable::class)) { |
35
|
|
|
$exception = $last; |
36
|
|
|
|
37
|
|
|
array_pop($arguments); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
try { |
41
|
|
|
call_user_func_array([Webmozart::class, $name], $arguments); |
42
|
|
|
return; |
43
|
|
|
} catch (InvalidArgumentException $e) { |
44
|
|
|
throw new $exception($e->getMessage()); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Note: This test is not bullet-proof but prevents a string containing illegal characters |
51
|
|
|
* from being passed and ensures the string roughly follows the correct format for a Base64 encoded string |
52
|
|
|
* |
53
|
|
|
* @param string $value |
54
|
|
|
* @param string $message |
55
|
|
|
*/ |
56
|
|
|
public static function stringPlausibleBase64(string $value, $message = ''): void |
57
|
|
|
{ |
58
|
|
|
$result = true; |
59
|
|
|
|
60
|
|
|
if (filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$base64_regex]]) === false) { |
61
|
|
|
$result = false; |
62
|
|
|
} else { |
63
|
|
|
$decoded = base64_decode($value, true); |
64
|
|
|
if ($decoded === false) { |
65
|
|
|
$result = false; |
66
|
|
|
} elseif (base64_encode($decoded) !== $value) { |
67
|
|
|
$result = false; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if ($result === false) { |
72
|
|
|
throw new AssertionFailedException( |
73
|
|
|
sprintf( |
74
|
|
|
$message ?: '\'%s\' is not a valid Base64 encoded string', |
75
|
|
|
$value |
76
|
|
|
) |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $value |
84
|
|
|
* @param string $message |
85
|
|
|
*/ |
86
|
|
|
public static function validDateTime(string $value, $message = ''): void |
87
|
|
|
{ |
88
|
|
|
if (DateTime::createFromFormat(DateTime::ISO8601, $value) === false) { |
89
|
|
|
throw new AssertionFailedException( |
90
|
|
|
sprintf( |
91
|
|
|
$message ?: '\'%s\' is not a valid DateTime', |
92
|
|
|
$value |
93
|
|
|
) |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $value |
101
|
|
|
* @param string $message |
102
|
|
|
*/ |
103
|
|
|
public static function validDateTimeZulu(string $value, $message = ''): void |
104
|
|
|
{ |
105
|
|
|
$dateTime = DateTime::createFromFormat(DateTime::ISO8601, $value); |
106
|
|
|
if ($dateTime === false) { |
107
|
|
|
throw new AssertionFailedException( |
108
|
|
|
sprintf( |
109
|
|
|
$message ?: '\'%s\' is not a valid DateTime', |
110
|
|
|
$value |
111
|
|
|
) |
112
|
|
|
); |
113
|
|
|
} elseif ($dateTime->getTimezone() !== 'Z') { |
114
|
|
|
throw new AssertionFailedException( |
115
|
|
|
sprintf( |
116
|
|
|
$message ?: '\'%s\' is not a DateTime expressed in the UTC timezone using the \'Z\' timezone identifier.', |
117
|
|
|
$value |
118
|
|
|
) |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|