1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\Assert; |
6
|
|
|
|
7
|
|
|
use GuzzleHttp\Psr7\Exception\MalformedUriException; |
8
|
|
|
use GuzzleHttp\Psr7\Uri; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
|
11
|
|
|
use function sprintf; |
12
|
|
|
use function strlen; |
13
|
|
|
use function substr; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @package simplesamlphp/assert |
17
|
|
|
*/ |
18
|
|
|
trait URITrait |
19
|
|
|
{ |
20
|
|
|
/*********************************************************************************** |
21
|
|
|
* NOTE: Custom assertions may be added below this line. * |
22
|
|
|
* They SHOULD be marked as `protected` to ensure the call is forced * |
23
|
|
|
* through __callStatic(). * |
24
|
|
|
* Assertions marked `public` are called directly and will * |
25
|
|
|
* not handle any custom exception passed to it. * |
26
|
|
|
***********************************************************************************/ |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $value |
31
|
|
|
* @param string $message |
32
|
|
|
*/ |
33
|
|
|
protected static function validURN(string $value, string $message = ''): void |
34
|
|
|
{ |
35
|
|
|
try { |
36
|
|
|
$uri = new Uri($value); |
37
|
|
|
} catch (MalformedUriException $e) { |
38
|
|
|
throw new InvalidArgumentException(sprintf( |
39
|
|
|
$message ?: '\'%s\' is not a valid RFC3986 compliant URI', |
40
|
|
|
$value, |
41
|
|
|
)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if ( |
45
|
|
|
$uri->getScheme() !== 'urn' |
46
|
|
|
|| (($uri->getScheme() !== null) && $uri->getPath() !== substr($value, strlen($uri->getScheme()) + 1)) |
47
|
|
|
) { |
48
|
|
|
throw new InvalidArgumentException(sprintf( |
49
|
|
|
$message ?: '\'%s\' is not a valid RFC8141 compliant URN', |
50
|
|
|
$value, |
51
|
|
|
)); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $value |
58
|
|
|
* @param string $message |
59
|
|
|
*/ |
60
|
|
|
protected static function validURL(string $value, string $message = ''): void |
61
|
|
|
{ |
62
|
|
|
try { |
63
|
|
|
$uri = new Uri($value); |
64
|
|
|
} catch (MalformedUriException $e) { |
65
|
|
|
throw new InvalidArgumentException(sprintf( |
66
|
|
|
$message ?: '\'%s\' is not a valid RFC3986 compliant URI', |
67
|
|
|
$value, |
68
|
|
|
)); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if ($uri->getScheme() !== 'http' && $uri->getScheme() !== 'https') { |
72
|
|
|
throw new InvalidArgumentException(sprintf( |
73
|
|
|
$message ?: '\'%s\' is not a valid RFC2396 compliant URL', |
74
|
|
|
$value, |
75
|
|
|
)); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $value |
82
|
|
|
* @param string $message |
83
|
|
|
*/ |
84
|
|
|
protected static function validURI(string $value, string $message = ''): void |
85
|
|
|
{ |
86
|
|
|
try { |
87
|
|
|
new Uri($value); |
88
|
|
|
} catch (MalformedUriException $e) { |
89
|
|
|
throw new InvalidArgumentException(sprintf( |
90
|
|
|
$message ?: '\'%s\' is not a valid RFC3986 compliant URI', |
91
|
|
|
$value, |
92
|
|
|
)); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|