Skip to content

Commit 0cb18c0

Browse files
munvierjasny
authored andcommitted
Expiration time (#62)
Expose cookie life time parameter to customize it. Default is 3600. Minor fixes for code quality
1 parent cc130f2 commit 0cb18c0

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

examples/ajax-broker/api.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
header("Content-Type: application/json");
99
header("HTTP/1.1 400 Bad Request");
1010
echo json_encode(['error' => 'Command not specified']);
11-
exit();
12-
}
11+
return;
12+
}
1313

1414
try {
1515
$result = $broker->{$_REQUEST['command']}();
@@ -22,7 +22,7 @@
2222
if (!empty($_GET['callback'])) {
2323
if (!isset($result)) $result = null;
2424
if (!isset($status)) $status = isset($result) ? 200 : 204;
25-
25+
2626
header('Content-Type: application/javascript');
2727
echo $_GET['callback'] . '(' . json_encode($result) . ', ' . $status . ')';
2828
return;

src/Broker.php

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<?php
22
namespace Jasny\SSO;
33

4-
use Jasny\ValidationResult;
5-
64
/**
75
* Single sign-on broker.
86
*
@@ -41,14 +39,20 @@ class Broker
4139
*/
4240
protected $userinfo;
4341

42+
/**
43+
* Cookie lifetime
44+
* @var int
45+
*/
46+
protected $cookie_lifetime;
47+
4448
/**
4549
* Class constructor
4650
*
4751
* @param string $url Url of SSO server
4852
* @param string $broker My identifier, given by SSO provider.
4953
* @param string $secret My secret word, given by SSO provider.
5054
*/
51-
public function __construct($url, $broker, $secret)
55+
public function __construct($url, $broker, $secret, $cookie_lifetime = 3600)
5256
{
5357
if (!$url) throw new \InvalidArgumentException("SSO server URL not specified");
5458
if (!$broker) throw new \InvalidArgumentException("SSO broker id not specified");
@@ -57,6 +61,7 @@ public function __construct($url, $broker, $secret)
5761
$this->url = $url;
5862
$this->broker = $broker;
5963
$this->secret = $secret;
64+
$this->cookie_lifetime = $cookie_lifetime;
6065

6166
if (isset($_COOKIE[$this->getCookieName()])) $this->token = $_COOKIE[$this->getCookieName()];
6267
}
@@ -95,7 +100,7 @@ public function generateToken()
95100
if (isset($this->token)) return;
96101

97102
$this->token = base_convert(md5(uniqid(rand(), true)), 16, 36);
98-
setcookie($this->getCookieName(), $this->token, time() + 3600, '/');
103+
setcookie($this->getCookieName(), $this->token, time() + $this->cookie_lifetime, '/');
99104
}
100105

101106
/**

src/NotAttachedException.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
class NotAttachedException extends Exception
99
{
1010

11-
}
11+
}

src/Server.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ public function startBrokerSession()
6868
{
6969
if (isset($this->brokerId)) return;
7070

71-
$sid = $this->getBrokerSessionID();
71+
$sid = $this->getBrokerSessionID();
7272

73-
if ($sid == false) {
73+
if ($sid === false) {
7474
return $this->fail("Broker didn't send a session key", 400);
7575
}
7676

0 commit comments

Comments
 (0)