Skip to content

Commit

Permalink
Expiration time (#62)
Browse files Browse the repository at this point in the history
Expose cookie life time parameter to customize it. Default is 3600.
Minor fixes for code quality
  • Loading branch information
munvier authored and jasny committed Feb 24, 2017
1 parent cc130f2 commit 0cb18c0
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
6 changes: 3 additions & 3 deletions examples/ajax-broker/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
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']}();
Expand All @@ -22,7 +22,7 @@
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;
Expand Down
13 changes: 9 additions & 4 deletions src/Broker.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php
namespace Jasny\SSO;

use Jasny\ValidationResult;

/**
* Single sign-on broker.
*
Expand Down Expand Up @@ -41,14 +39,20 @@ 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");
Expand All @@ -57,6 +61,7 @@ public function __construct($url, $broker, $secret)
$this->url = $url;
$this->broker = $broker;
$this->secret = $secret;
$this->cookie_lifetime = $cookie_lifetime;

if (isset($_COOKIE[$this->getCookieName()])) $this->token = $_COOKIE[$this->getCookieName()];
}
Expand Down Expand Up @@ -95,7 +100,7 @@ public function generateToken()
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, '/');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/NotAttachedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
class NotAttachedException extends Exception
{

}
}
4 changes: 2 additions & 2 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public function startBrokerSession()
{
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);
}

Expand Down

0 comments on commit 0cb18c0

Please sign in to comment.