summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJulien Verhaeghe <echec.will.win@gmail.com>2017-02-24 23:44:27 +0100
committerArnold Daniels <arnold@jasny.net>2017-02-24 23:44:27 +0100
commit0cb18c072e7b14db3d2d2549c051f41ca837e5e9 (patch)
tree20c10178e07ea07e2543154bafd04abcaf4d3b1e
parentcc130f231c209bef5962d6afc395190002bd461f (diff)
downloadsso-0cb18c072e7b14db3d2d2549c051f41ca837e5e9.zip
sso-0cb18c072e7b14db3d2d2549c051f41ca837e5e9.tar.gz
sso-0cb18c072e7b14db3d2d2549c051f41ca837e5e9.tar.bz2
Expiration time (#62)v0.3.0
Expose cookie life time parameter to customize it. Default is 3600. Minor fixes for code quality
-rw-r--r--examples/ajax-broker/api.php6
-rw-r--r--src/Broker.php13
-rw-r--r--src/NotAttachedException.php2
-rw-r--r--src/Server.php4
4 files changed, 15 insertions, 10 deletions
diff --git a/examples/ajax-broker/api.php b/examples/ajax-broker/api.php
index 996b813..304febd 100644
--- a/examples/ajax-broker/api.php
+++ b/examples/ajax-broker/api.php
@@ -8,8 +8,8 @@ if (empty($_REQUEST['command']) || !method_exists($broker, $_REQUEST['command'])
header("Content-Type: application/json");
header("HTTP/1.1 400 Bad Request");
echo json_encode(['error' => 'Command not specified']);
- exit();
-}
+ return;
+}
try {
$result = $broker->{$_REQUEST['command']}();
@@ -22,7 +22,7 @@ try {
if (!empty($_GET['callback'])) {
if (!isset($result)) $result = null;
if (!isset($status)) $status = isset($result) ? 200 : 204;
-
+
header('Content-Type: application/javascript');
echo $_GET['callback'] . '(' . json_encode($result) . ', ' . $status . ')';
return;
diff --git a/src/Broker.php b/src/Broker.php
index fd503a8..98b2ae9 100644
--- a/src/Broker.php
+++ b/src/Broker.php
@@ -1,8 +1,6 @@
<?php
namespace Jasny\SSO;
-use Jasny\ValidationResult;
-
/**
* Single sign-on broker.
*
@@ -42,13 +40,19 @@ class Broker
protected $userinfo;
/**
+ * Cookie lifetime
+ * @var int
+ */
+ protected $cookie_lifetime;
+
+ /**
* Class constructor
*
* @param string $url Url of SSO server
* @param string $broker My identifier, given by SSO provider.
* @param string $secret My secret word, given by SSO provider.
*/
- public function __construct($url, $broker, $secret)
+ public function __construct($url, $broker, $secret, $cookie_lifetime = 3600)
{
if (!$url) throw new \InvalidArgumentException("SSO server URL not specified");
if (!$broker) throw new \InvalidArgumentException("SSO broker id not specified");
@@ -57,6 +61,7 @@ class Broker
$this->url = $url;
$this->broker = $broker;
$this->secret = $secret;
+ $this->cookie_lifetime = $cookie_lifetime;
if (isset($_COOKIE[$this->getCookieName()])) $this->token = $_COOKIE[$this->getCookieName()];
}
@@ -95,7 +100,7 @@ class Broker
if (isset($this->token)) return;
$this->token = base_convert(md5(uniqid(rand(), true)), 16, 36);
- setcookie($this->getCookieName(), $this->token, time() + 3600, '/');
+ setcookie($this->getCookieName(), $this->token, time() + $this->cookie_lifetime, '/');
}
/**
diff --git a/src/NotAttachedException.php b/src/NotAttachedException.php
index aee24e4..b413148 100644
--- a/src/NotAttachedException.php
+++ b/src/NotAttachedException.php
@@ -8,4 +8,4 @@ namespace Jasny\SSO;
class NotAttachedException extends Exception
{
-} \ No newline at end of file
+}
diff --git a/src/Server.php b/src/Server.php
index 2815107..21f28d6 100644
--- a/src/Server.php
+++ b/src/Server.php
@@ -68,9 +68,9 @@ abstract class Server
{
if (isset($this->brokerId)) return;
- $sid = $this->getBrokerSessionID();
+ $sid = $this->getBrokerSessionID();
- if ($sid == false) {
+ if ($sid === false) {
return $this->fail("Broker didn't send a session key", 400);
}