summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLudovic BERLEMONT <ludovic.berlemont@oxand.com>2016-03-24 18:06:32 +0100
committerLudovic BERLEMONT <ludovic.berlemont@oxand.com>2016-03-24 18:06:32 +0100
commitbe9a11b400974f1460d279f4ffa75caf79131a55 (patch)
tree5e0078b93f965932d42a95ed719f25ff52393ef9
parent43be77400104f4a250802259f4641607ac27094f (diff)
downloadsso-be9a11b400974f1460d279f4ffa75caf79131a55.zip
sso-be9a11b400974f1460d279f4ffa75caf79131a55.tar.gz
sso-be9a11b400974f1460d279f4ffa75caf79131a55.tar.bz2
adding method to trash token and current cookies on fail request
-rw-r--r--src/Broker.php35
1 files changed, 29 insertions, 6 deletions
diff --git a/src/Broker.php b/src/Broker.php
index 7554930..1724db2 100644
--- a/src/Broker.php
+++ b/src/Broker.php
@@ -59,8 +59,7 @@ class Broker
$this->secret = $secret;
if (isset($_COOKIE[$this->getCookieName()])) $this->token = $_COOKIE[$this->getCookieName()];
-
-
+
}
/**
@@ -101,6 +100,15 @@ class Broker
}
/**
+ * Trash session token
+ */
+ public function trashToken()
+ {
+ unset($this->token);
+ setcookie($this->getCookieName(), null, time() - 1);
+ }
+
+ /**
* Check if we have an SSO token.
*
* @return boolean
@@ -191,23 +199,38 @@ class Broker
$response = curl_exec($ch);
if (curl_errno($ch) != 0) {
- throw new Exception("Server request failed: " . curl_error($ch), 500);
+ $message = 'Server request failed: ' . curl_error($ch);
+ return $this->fail($message);
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
list($contentType) = explode(';', curl_getinfo($ch, CURLINFO_CONTENT_TYPE));
if ($contentType != 'application/json') {
- $message = "Expected application/json response, got $contentType";
- throw new Exception($message, $httpCode);
+ $message = 'Expected application/json response, got ' . $contentType;
+ return $this->fail($message, $httpCode);
}
$data = json_decode($response, true);
- if ($httpCode >= 400) throw new Exception($data['error'] ?: $response, $httpCode);
+ if ($httpCode >= 400) return $this->fail($data['error'] ?: $response, $httpCode);
return $data;
}
+ /**
+ * An error occured.
+ *
+ * @param $message
+ * @param int $http_status
+ *
+ * @throws Exception
+ */
+ protected function fail($message, $http_status = 500)
+ {
+ $this->trashToken();
+ throw new Exception($message, $http_status);
+ }
+
/**
* Log the client in at the SSO server.