1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace flipbox\saml\core\validators; |
5
|
|
|
|
6
|
|
|
use flipbox\saml\core\records\AbstractProvider; |
7
|
|
|
use SAML2\Configuration\Destination; |
8
|
|
|
use SAML2\Response\Validation\ConstraintValidator\DestinationMatches; |
9
|
|
|
use SAML2\Response\Validation\ConstraintValidator\IsSuccessful; |
10
|
|
|
use SAML2\Response\Validation\Result as ResponseResult; |
11
|
|
|
use SAML2\Assertion\Validation\Result as AssertionResult; |
12
|
|
|
use SAML2\Response as SamlResponse; |
13
|
|
|
|
14
|
|
|
class Response |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var AbstractProvider |
18
|
|
|
*/ |
19
|
|
|
private $identityProvider; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var AbstractProvider |
23
|
|
|
*/ |
24
|
|
|
private $serviceProvider; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private $validators = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Response constructor. |
33
|
|
|
* @param AbstractProvider $identityProvider |
34
|
|
|
* @param AbstractProvider $serviceProvider |
35
|
|
|
*/ |
36
|
|
|
public function __construct( |
37
|
|
|
AbstractProvider $identityProvider, |
38
|
|
|
AbstractProvider $serviceProvider |
39
|
|
|
) { |
40
|
|
|
|
41
|
|
|
$this->identityProvider = $identityProvider; |
42
|
|
|
$this->serviceProvider = $serviceProvider; |
43
|
|
|
|
44
|
|
|
$this->addValidators(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
private function addValidators() |
48
|
|
|
{ |
49
|
|
|
$this->validators = [ |
50
|
|
|
new IsSuccessful(), |
51
|
|
|
new DestinationMatches( |
52
|
|
|
new Destination( |
53
|
|
|
$this->serviceProvider->firstSpAcsService()->getLocation() |
54
|
|
|
) |
55
|
|
|
), |
56
|
|
|
|
57
|
|
|
]; |
58
|
|
|
if ($keyStore = $this->identityProvider->signingXMLSecurityKeyStore()) { |
59
|
|
|
$this->validators[] = new SignedElement($keyStore); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param $response |
65
|
|
|
* @return ResponseResult |
66
|
|
|
*/ |
67
|
|
|
public function validate($response): ResponseResult |
68
|
|
|
{ |
69
|
|
|
$responseResult = new ResponseResult(); |
70
|
|
|
foreach ($this->validators as $validator) { |
71
|
|
|
$validator->validate($response, $responseResult); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->validateAssertions($response, $responseResult); |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
return $responseResult; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param SamlResponse $response |
82
|
|
|
* @param ResponseResult $responseResult |
83
|
|
|
*/ |
84
|
|
|
protected function validateAssertions(SamlResponse $response, ResponseResult $responseResult) |
85
|
|
|
{ |
86
|
|
|
$assertionResult = null; |
|
|
|
|
87
|
|
|
foreach ($response->getAssertions() as $assertion) { |
88
|
|
|
$validator = new Assertion( |
89
|
|
|
$response, |
90
|
|
|
$this->identityProvider, |
91
|
|
|
$this->serviceProvider |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
$assertionResult = $validator->validate($assertion); |
95
|
|
|
|
96
|
|
|
$this->addErrorsToResult($responseResult, $assertionResult); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param ResponseResult $responseResult |
102
|
|
|
* @param AssertionResult $assertionResult |
103
|
|
|
*/ |
104
|
|
|
private function addErrorsToResult(ResponseResult $responseResult, AssertionResult $assertionResult) |
105
|
|
|
{ |
106
|
|
|
foreach ($assertionResult->getErrors() as $error) { |
107
|
|
|
$responseResult->addError($error); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.