Skip to content

Commit bf2acc6

Browse files
committed
Be able to retrieve not only the message of the last error but the whole exception
2 parents 297065c + 2181668 commit bf2acc6

8 files changed

Lines changed: 161 additions & 24 deletions

File tree

lib/Saml2/Auth.php

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,18 @@ class OneLogin_Saml2_Auth
109109
private $_errors = array();
110110

111111
/**
112-
* Reason of the last error.
112+
* Last error exception object.
113113
*
114-
* @var string|null
114+
* @var Exception|null
115115
*/
116-
private $_errorReason;
116+
private $_lastErrorException;
117+
118+
/**
119+
* Last error.
120+
*
121+
* @var String|null
122+
*/
123+
private $_lastError;
117124

118125
/**
119126
* Last AuthNRequest ID or LogoutRequest ID generated by this Service Provider
@@ -188,7 +195,7 @@ public function setStrict($value)
188195
public function processResponse($requestId = null)
189196
{
190197
$this->_errors = array();
191-
$this->_errorReason = null;
198+
$this->_lastError = $this->_lastErrorException = null;
192199
if (isset($_POST) && isset($_POST['SAMLResponse'])) {
193200
// AuthnResponse -- HTTP_POST Binding
194201
$response = new OneLogin_Saml2_Response($this->_settings, $_POST['SAMLResponse']);
@@ -207,7 +214,8 @@ public function processResponse($requestId = null)
207214
$this->_lastAssertionNotOnOrAfter = $response->getAssertionNotOnOrAfter();
208215
} else {
209216
$this->_errors[] = 'invalid_response';
210-
$this->_errorReason = $response->getError();
217+
$this->_lastErrorException = $response->getErrorException();
218+
$this->_lastError = $response->getError();
211219
}
212220
} else {
213221
$this->_errors[] = 'invalid_binding';
@@ -234,13 +242,15 @@ public function processResponse($requestId = null)
234242
public function processSLO($keepLocalSession = false, $requestId = null, $retrieveParametersFromServer = false, $cbDeleteSession = null, $stay = false)
235243
{
236244
$this->_errors = array();
237-
$this->_errorReason = null;
245+
$this->_lastError = $this->_lastErrorException = null;
238246
if (isset($_GET) && isset($_GET['SAMLResponse'])) {
239247
$logoutResponse = new OneLogin_Saml2_LogoutResponse($this->_settings, $_GET['SAMLResponse']);
240248
$this->_lastResponse = $logoutResponse->getXML();
241249
if (!$logoutResponse->isValid($requestId, $retrieveParametersFromServer)) {
242250
$this->_errors[] = 'invalid_logout_response';
243-
$this->_errorReason = $logoutResponse->getError();
251+
$this->_lastErrorException = $logoutResponse->getErrorException();
252+
$this->_lastError = $logoutResponse->getError();
253+
244254
} else if ($logoutResponse->getStatus() !== OneLogin_Saml2_Constants::STATUS_SUCCESS) {
245255
$this->_errors[] = 'logout_not_success';
246256
} else {
@@ -258,7 +268,8 @@ public function processSLO($keepLocalSession = false, $requestId = null, $retrie
258268
$this->_lastRequest = $logoutRequest->getXML();
259269
if (!$logoutRequest->isValid($retrieveParametersFromServer)) {
260270
$this->_errors[] = 'invalid_logout_request';
261-
$this->_errorReason = $logoutRequest->getError();
271+
$this->_lastErrorException = $logoutRequest->getErrorException();
272+
$this->_lastError = $logoutRequest->getError();
262273
} else {
263274
if (!$keepLocalSession) {
264275
if ($cbDeleteSession === null) {
@@ -407,7 +418,18 @@ public function getErrors()
407418
*/
408419
public function getLastErrorReason()
409420
{
410-
return $this->_errorReason;
421+
return $this->_lastError;
422+
}
423+
424+
425+
/**
426+
* Returns the last error
427+
*
428+
* @return Exception Error
429+
*/
430+
public function getLastErrorException()
431+
{
432+
return $this->_lastErrorException;
411433
}
412434

413435
/**

lib/Saml2/LogoutRequest.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class OneLogin_Saml2_LogoutRequest
4444
/**
4545
* After execute a validation process, this var contains the cause
4646
*
47-
* @var string
47+
* @var Exception
4848
*/
4949
private $_error;
5050

@@ -397,23 +397,37 @@ public function isValid($retrieveParametersFromServer = false)
397397

398398
return true;
399399
} catch (Exception $e) {
400-
$this->_error = $e->getMessage();
400+
$this->_error = $e;
401401
$debug = $this->_settings->isDebugActive();
402402
if ($debug) {
403-
echo htmlentities($this->_error);
403+
echo htmlentities($this->_error->getMessage());
404404
}
405405
return false;
406406
}
407407
}
408408

409+
/**
410+
* After execute a validation process, if fails this method returns the Exception of the cause
411+
*
412+
* @return Exception Cause
413+
*/
414+
public function getErrorException()
415+
{
416+
return $this->_error;
417+
}
418+
409419
/**
410420
* After execute a validation process, if fails this method returns the cause
411421
*
412-
* @return string Cause
422+
* @return null|string Error reason
413423
*/
414424
public function getError()
415425
{
416-
return $this->_error;
426+
$errorMsg = null;
427+
if (isset($this->_error)) {
428+
$errorMsg = htmlentities($this->_error->getMessage());
429+
}
430+
return $errorMsg;
417431
}
418432

419433
/**

lib/Saml2/LogoutResponse.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,10 @@ public function isValid($requestId = null, $retrieveParametersFromServer = false
202202
}
203203
return true;
204204
} catch (Exception $e) {
205-
$this->_error = $e->getMessage();
205+
$this->_error = $e;
206206
$debug = $this->_settings->isDebugActive();
207207
if ($debug) {
208-
echo htmlentities($this->_error);
208+
echo htmlentities($this->_error->getMessage());
209209
}
210210
return false;
211211
}
@@ -281,13 +281,27 @@ public function getResponse($deflate = null)
281281
/**
282282
* After execute a validation process, if fails this method returns the cause.
283283
*
284-
* @return string Cause
284+
* @return Exception Cause
285285
*/
286-
public function getError()
286+
public function getErrorException()
287287
{
288288
return $this->_error;
289289
}
290290

291+
/**
292+
* After execute a validation process, if fails this method returns the cause
293+
*
294+
* @return null|string Error reason
295+
*/
296+
public function getError()
297+
{
298+
$errorMsg = null;
299+
if (isset($this->_error)) {
300+
$errorMsg = htmlentities($this->_error->getMessage());
301+
}
302+
return $errorMsg;
303+
}
304+
291305
/**
292306
* @return string the ID of the Response
293307
*/

lib/Saml2/Response.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class OneLogin_Saml2_Response
6060
/**
6161
* After validation, if it fail this var has the cause of the problem
6262
*
63-
* @var string
63+
* @var Exception
6464
*/
6565
private $_error;
6666

@@ -419,10 +419,10 @@ public function isValid($requestId = null)
419419
}
420420
return true;
421421
} catch (Exception $e) {
422-
$this->_error = $e->getMessage();
422+
$this->_error = $e;
423423
$debug = $this->_settings->isDebugActive();
424424
if ($debug) {
425-
echo htmlentities($this->_error);
425+
echo htmlentities($e->getMessage());
426426
}
427427
return false;
428428
}
@@ -1119,13 +1119,27 @@ protected function decryptAssertion($dom)
11191119
/**
11201120
* After execute a validation process, if fails this method returns the cause
11211121
*
1122-
* @return string Cause
1122+
* @return Exception Cause
11231123
*/
1124-
public function getError()
1124+
public function getErrorException()
11251125
{
11261126
return $this->_error;
11271127
}
11281128

1129+
/**
1130+
* After execute a validation process, if fails this method returns the cause
1131+
*
1132+
* @return null|string Error reason
1133+
*/
1134+
public function getError()
1135+
{
1136+
$errorMsg = null;
1137+
if (isset($this->_error)) {
1138+
$errorMsg = htmlentities($this->_error->getMessage());
1139+
}
1140+
return $errorMsg;
1141+
}
1142+
11291143
/**
11301144
* Returns the SAML Response document (If contains an encrypted assertion, decrypts it)
11311145
*

tests/src/OneLogin/Saml2/AuthTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ public function testProcessNoResponse()
115115
* @covers OneLogin_Saml2_Auth::getSessionIndex
116116
* @covers OneLogin_Saml2_Auth::getSessionExpiration
117117
* @covers OneLogin_Saml2_Auth::getLastErrorReason
118+
* * @covers OneLogin_Saml2_Auth::getLastErrorException
118119
*/
119120
public function testProcessResponseInvalid()
120121
{
@@ -132,6 +133,8 @@ public function testProcessResponseInvalid()
132133
$this->assertNull($this->_auth->getAttribute('uid'));
133134
$this->assertEquals($this->_auth->getErrors(), array('invalid_response'));
134135
$this->assertEquals($this->_auth->getLastErrorReason(), "Reference validation failed");
136+
$errorException = $this->_auth->getLastErrorException();
137+
$this->assertEquals("Reference validation failed", $errorException->getMessage());
135138
}
136139

137140
/**
@@ -154,6 +157,8 @@ public function testProcessResponseInvalidRequestId()
154157
$this->_auth->processResponse($requestId);
155158

156159
$this->assertEquals("No Signature found. SAML Response rejected", $this->_auth->getLastErrorReason());
160+
$errorException = $this->_auth->getLastErrorException();
161+
$this->assertEquals("No Signature found. SAML Response rejected", $errorException->getMessage());
157162

158163
$this->_auth->setStrict(true);
159164
$this->_auth->processResponse($requestId);

tests/src/OneLogin/Saml2/LogoutRequestTest.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ public function testCreateDeflatedSAMLLogoutRequestURLParameter()
248248
/**
249249
* Tests the OneLogin_Saml2_LogoutRequest Constructor.
250250
* Case: Able to generate encryptedID with MultiCert
251-
*
251+
*
252252
* @covers OneLogin_Saml2_LogoutRequest
253253
*/
254254
public function testConstructorEncryptIdUsingX509certMulti()
@@ -449,6 +449,34 @@ public function testGetError()
449449
$this->assertContains('The LogoutRequest was received at', $logoutRequest2->getError());
450450
}
451451

452+
/**
453+
* Tests the getErrorException method of the OneLogin_Saml2_LogoutRequest
454+
*
455+
* @covers OneLogin_Saml2_LogoutRequest::getErrorException
456+
*/
457+
public function testGetErrorException()
458+
{
459+
$request = file_get_contents(TEST_ROOT . '/data/logout_requests/logout_request.xml');
460+
461+
$deflatedRequest = gzdeflate($request);
462+
$encodedRequest = base64_encode($deflatedRequest);
463+
464+
$logoutRequest = new OneLogin_Saml2_LogoutRequest($this->_settings, $encodedRequest);
465+
466+
$this->assertNull($logoutRequest->getError());
467+
468+
$this->assertTrue($logoutRequest->isValid());
469+
$this->assertNull($logoutRequest->getError());
470+
471+
$this->_settings->setStrict(true);
472+
$logoutRequest2 = new OneLogin_Saml2_LogoutRequest($this->_settings, $encodedRequest);
473+
474+
$this->assertFalse($logoutRequest2->isValid());
475+
$errorException = $logoutRequest2->getErrorException();
476+
$this->assertContains('The LogoutRequest was received at', $errorException->getMessage());
477+
$this->assertEquals($errorException->getMessage(), $logoutRequest2->getError());
478+
}
479+
452480
/**
453481
* Tests the isValid method of the OneLogin_Saml2_LogoutRequest
454482
* Case Invalid Issuer

tests/src/OneLogin/Saml2/LogoutResponseTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,24 @@ public function testGetError()
113113
$this->_settings->setStrict(true);
114114
$this->assertFalse($response->isValid($requestId));
115115
$this->assertEquals($response->getError(), 'The InResponseTo of the Logout Response: ONELOGIN_21584ccdfaca36a145ae990442dcd96bfe60151e, does not match the ID of the Logout request sent by the SP: invalid_request_id');
116+
}
117+
116118

119+
/**
120+
* Tests the getError method of the OneLogin_Saml2_LogoutRequest
121+
*
122+
* @covers OneLogin_Saml2_LogoutRequest::getErrorException
123+
*/
124+
public function testGetErrorException()
125+
{
126+
$message = file_get_contents(TEST_ROOT . '/data/logout_responses/logout_response_deflated.xml.base64');
127+
$requestId = 'invalid_request_id';
128+
$response = new OneLogin_Saml2_LogoutResponse($this->_settings, $message);
129+
$this->_settings->setStrict(true);
130+
$this->assertFalse($response->isValid($requestId));
131+
$errorException = $response->getErrorException();
132+
$this->assertEquals('The InResponseTo of the Logout Response: ONELOGIN_21584ccdfaca36a145ae990442dcd96bfe60151e, does not match the ID of the Logout request sent by the SP: invalid_request_id', $errorException->getMessage());
133+
$this->assertEquals($errorException->getMessage(), $response->getError());
117134
}
118135

119136
/**

tests/src/OneLogin/Saml2/ResponseTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,29 @@ public function testGetError()
622622
$xml = file_get_contents(TEST_ROOT . '/data/responses/response4.xml.base64');
623623
$response = new OneLogin_Saml2_Response($this->_settings, $xml);
624624

625+
$this->assertNull($response->getErrorException());
626+
627+
$this->assertFalse($response->isValid());
628+
$errorException = $response->getErrorException();
629+
$this->assertEquals('SAML Response must contain 1 assertion', $errorException->getMessage());
630+
631+
$xml2 = file_get_contents(TEST_ROOT . '/data/responses/valid_response.xml.base64');
632+
$response2 = new OneLogin_Saml2_Response($this->_settings, $xml2);
633+
634+
$this->assertTrue($response2->isValid());
635+
$this->assertNull($response2->getErrorException());
636+
}
637+
638+
/**
639+
* Tests the getErrorException method of the OneLogin_Saml2_Response
640+
*
641+
* @covers OneLogin_Saml2_Response::getErrorException
642+
*/
643+
public function testGetErrorException()
644+
{
645+
$xml = file_get_contents(TEST_ROOT . '/data/responses/response4.xml.base64');
646+
$response = new OneLogin_Saml2_Response($this->_settings, $xml);
647+
625648
$this->assertNull($response->getError());
626649

627650
$this->assertFalse($response->isValid());

0 commit comments

Comments
 (0)