1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\SAML2\Exception\Protocol; |
6
|
|
|
|
7
|
|
|
use RuntimeException; |
8
|
|
|
use SimpleSAML\Assert\Assert; |
9
|
|
|
use SimpleSAML\SAML2\Constants as C; |
10
|
|
|
use SimpleSAML\SAML2\Exception\ProtocolViolationException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class for representing a SAML 2 error. |
14
|
|
|
* |
15
|
|
|
* @package simplesaml\saml2 |
16
|
|
|
*/ |
17
|
|
|
abstract class AbstractProtocolException extends RuntimeException |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* The top-level status code. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected string $status; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The second-level status code, or NULL if no second-level status code is defined. |
28
|
|
|
* |
29
|
|
|
* @var string|null |
30
|
|
|
*/ |
31
|
|
|
protected ?string $subStatus; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The status message, or NULL if no status message is defined. |
35
|
|
|
* |
36
|
|
|
* @var string|null |
37
|
|
|
*/ |
38
|
|
|
protected ?string $statusMessage; |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Create a SAML 2 error. |
43
|
|
|
* |
44
|
|
|
* @param string $status The top-level status code. |
45
|
|
|
* @param string|null $subStatus The second-level status code. |
46
|
|
|
* Can be NULL, in which case there is no second-level status code. |
47
|
|
|
* @param string|null $statusMessage The status message. |
48
|
|
|
* Can be NULL, in which case there is no status message. |
49
|
|
|
*/ |
50
|
|
|
public function __construct( |
51
|
|
|
string $status, |
52
|
|
|
?string $subStatus = null, |
53
|
|
|
?string $statusMessage = null |
54
|
|
|
) { |
55
|
|
|
Assert::oneOf( |
56
|
|
|
$status, |
57
|
|
|
[ |
58
|
|
|
C::STATUS_SUCCESS, |
59
|
|
|
C::STATUS_REQUESTER, |
60
|
|
|
C::STATUS_RESPONDER, |
61
|
|
|
C::STATUS_VERSION_MISMATCH, |
62
|
|
|
], |
63
|
|
|
'Invalid top-level status code', |
64
|
|
|
ProtocolViolationException::class |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
$st = self::shortStatus($status); |
|
|
|
|
68
|
|
|
if ($subStatus !== null) { |
69
|
|
|
$st .= '/' . $this->shortStatus($subStatus); |
70
|
|
|
} |
71
|
|
|
if ($statusMessage !== null) { |
72
|
|
|
$st .= ': ' . $statusMessage; |
73
|
|
|
} |
74
|
|
|
parent::__construct($st); |
75
|
|
|
|
76
|
|
|
$this->status = $status; |
77
|
|
|
$this->subStatus = $subStatus; |
78
|
|
|
$this->statusMessage = $statusMessage; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the top-level status code. |
84
|
|
|
* |
85
|
|
|
* @return string The top-level status code. |
86
|
|
|
*/ |
87
|
|
|
public function getStatus(): string |
88
|
|
|
{ |
89
|
|
|
return $this->status; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get the second-level status code. |
95
|
|
|
* |
96
|
|
|
* @return string|null The second-level status code or NULL if no second-level status code is present. |
97
|
|
|
*/ |
98
|
|
|
public function getSubStatus(): ?string |
99
|
|
|
{ |
100
|
|
|
return $this->subStatus; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get the status message. |
106
|
|
|
* |
107
|
|
|
* @return string|null The status message or NULL if no status message is present. |
108
|
|
|
*/ |
109
|
|
|
public function getStatusMessage(): ?string |
110
|
|
|
{ |
111
|
|
|
return $this->statusMessage; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Create a short version of the status code. |
117
|
|
|
* |
118
|
|
|
* Remove the 'urn:oasis:names:tc:SAML:2.0:status:'-prefix of status codes |
119
|
|
|
* if it is present. |
120
|
|
|
* |
121
|
|
|
* @param string $status The status code. |
122
|
|
|
* @return string A shorter version of the status code. |
123
|
|
|
*/ |
124
|
|
|
private function shortStatus(string $status): string |
125
|
|
|
{ |
126
|
|
|
return preg_filter(sprintf('/^%s/', Constants::STATUS_PREFIX), '', $status) ?? $status; |
|
|
|
|
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|