|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SimpleSAML\Module\casserver\Cas; |
|
4
|
|
|
|
|
5
|
|
|
use SimpleSAML\Configuration; |
|
6
|
|
|
use SimpleSAML\Logger; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Validates if a CAS service can use server |
|
10
|
|
|
* @package SimpleSAML\Module\casserver\Cas |
|
11
|
|
|
*/ |
|
12
|
|
|
class ServiceValidator |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var Configuration |
|
16
|
|
|
*/ |
|
17
|
|
|
private $mainConfig; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* ServiceValidator constructor. |
|
21
|
|
|
* @param Configuration $mainConfig |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct(Configuration $mainConfig) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->mainConfig = $mainConfig; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Check that the $service is allowed, and if so return the configuration to use. |
|
30
|
|
|
* @param string $service The service url. Assume to already be url decoded |
|
31
|
|
|
* @return Configuration|null Return the configuration to use for this service, or null if service is not allowed |
|
32
|
|
|
*/ |
|
33
|
|
|
public function checkServiceURL($service) |
|
34
|
|
|
{ |
|
35
|
|
|
$isValidService = false; |
|
36
|
|
|
$legalUrl = 'undefined'; |
|
37
|
|
|
$configOverride = null; |
|
38
|
|
|
foreach ($this->mainConfig->getArray('legal_service_urls', []) as $index => $value) { |
|
39
|
|
|
// Support two styles: 0 => 'https://example' and 'https://example' => [ extra config ] |
|
40
|
|
|
if (is_int($index)) { |
|
41
|
|
|
$legalUrl = $value; |
|
42
|
|
|
$configOverride = null; |
|
43
|
|
|
} else { |
|
44
|
|
|
$legalUrl = $index; |
|
45
|
|
|
$configOverride = $value; |
|
46
|
|
|
} |
|
47
|
|
|
if (empty($legalUrl)) { |
|
48
|
|
|
Logger::warning("Ignoring empty CAS legal service url '$legalUrl'."); |
|
49
|
|
|
continue; |
|
50
|
|
|
} |
|
51
|
|
|
if (!ctype_alnum($legalUrl[0])) { |
|
52
|
|
|
// Probably a regex. Suppress errors incase the format is invalid |
|
53
|
|
|
$result = @preg_match($legalUrl, $service); |
|
54
|
|
|
if ($result === 1) { |
|
55
|
|
|
$isValidService = true; |
|
56
|
|
|
break; |
|
57
|
|
|
} elseif ($result === false) { |
|
58
|
|
|
Logger::warning("Invalid CAS legal service url '$legalUrl'. Error ".preg_last_error()); |
|
59
|
|
|
} |
|
60
|
|
|
} elseif (strpos($service, $legalUrl) === 0) { |
|
61
|
|
|
$isValidService = true; |
|
62
|
|
|
break; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
if ($isValidService) { |
|
66
|
|
|
$serviceConfig = $this->mainConfig->toArray(); |
|
67
|
|
|
// Return contextual information about which url rule triggered the validation |
|
68
|
|
|
$serviceConfig['casService'] = [ |
|
69
|
|
|
'matchingUrl' => $legalUrl, |
|
70
|
|
|
'serviceUrl' => $service, |
|
71
|
|
|
]; |
|
72
|
|
|
if ($configOverride) { |
|
73
|
|
|
$serviceConfig = array_merge($serviceConfig, $configOverride); |
|
74
|
|
|
} |
|
75
|
|
|
return Configuration::loadFromArray($serviceConfig); |
|
76
|
|
|
} |
|
77
|
|
|
return null; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|