-
Notifications
You must be signed in to change notification settings - Fork 701
Expand file tree
/
Copy pathError.php
More file actions
162 lines (140 loc) · 4.24 KB
/
Error.php
File metadata and controls
162 lines (140 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
declare(strict_types=1);
namespace SimpleSAML\Module\saml;
use SimpleSAML\Error as SSPError;
use SimpleSAML\Module\saml\Error as SAMLError;
use SimpleSAML\Module\saml\Error\NoPassive;
use SimpleSAML\SAML2\Constants;
use Throwable;
/**
* Class for representing a SAML 2 error.
*
* @package SimpleSAMLphp
*/
class Error extends SSPError\Exception
{
/**
* Create a SAML 2 error.
*
* @param string $status The top-level status code.
* @param string|null $subStatus The second-level status code.
* Can be NULL, in which case there is no second-level status code.
* @param string|null $statusMessage The status message.
* Can be NULL, in which case there is no status message.
* @param \Throwable|null $cause The cause of this exception. Can be NULL.
*/
public function __construct(
private string $status,
private ?string $subStatus = null,
private ?string $statusMessage = null,
?Throwable $cause = null,
) {
$st = self::shortStatus($status);
if ($subStatus !== null) {
$st .= '/' . self::shortStatus($subStatus);
}
if ($statusMessage !== null) {
$st .= ': ' . $statusMessage;
}
parent::__construct($st, 0, $cause);
}
/**
* Get the top-level status code.
*
* @return string The top-level status code.
*/
public function getStatus(): string
{
return $this->status;
}
/**
* Get the second-level status code.
*
* @return string|null The second-level status code or NULL if no second-level status code is present.
*/
public function getSubStatus(): ?string
{
return $this->subStatus;
}
/**
* Get the status message.
*
* @return string|null The status message or NULL if no status message is present.
*/
public function getStatusMessage(): ?string
{
return $this->statusMessage;
}
/**
* Create a SAML2 error from an exception.
*
* This function attempts to create a SAML2 error with the appropriate
* status codes from an arbitrary exception.
*
* @param \Throwable $e The original exception.
* @return \SimpleSAML\Error\Exception The new exception.
*/
public static function fromException(Throwable $e): SSPError\Exception
{
if ($e instanceof SAMLError) {
// Return the original exception unchanged
return $e;
} else {
$e = new self(
Constants::STATUS_RESPONDER,
null,
$e::class . ': ' . $e->getMessage(),
$e,
);
}
return $e;
}
/**
* Create a normal exception from a SAML2 error.
*
* This function attempts to reverse the operation of the fromException() function.
* If it is unable to create a more specific exception, it will return the current
* object.
*
* @see \SimpleSAML\Module\saml\Error::fromException()
*
* @return \SimpleSAML\Error\Exception An exception representing this error.
*/
public function toException(): SSPError\Exception
{
$e = null;
switch ($this->status) {
case Constants::STATUS_RESPONDER:
switch ($this->subStatus) {
case Constants::STATUS_NO_PASSIVE:
$e = new NoPassive(
Constants::STATUS_RESPONDER,
$this->statusMessage,
);
break;
}
break;
}
if ($e === null) {
return $this;
}
return $e;
}
/**
* Create a short version of the status code.
*
* Remove the 'urn:oasis:names:tc:SAML:2.0:status:'-prefix of status codes
* if it is present.
*
* @param string $status The status code.
* @return string A shorter version of the status code.
*/
private static function shortStatus(string $status): string
{
$t = 'urn:oasis:names:tc:SAML:2.0:status:';
if (substr($status, 0, strlen($t)) === $t) {
return substr($status, strlen($t));
}
return $status;
}
}