diff options
author | Arnold Daniels <arnold@jasny.net> | 2015-12-05 19:29:09 -0400 |
---|---|---|
committer | Arnold Daniels <arnold@jasny.net> | 2015-12-05 19:29:09 -0400 |
commit | b11f461f0f06105653748689940a12bc282438c4 (patch) | |
tree | 33f85c3e1ebd096bd928e1d48b2901983558bbd1 | |
parent | e713ee6e31bf5941832dfb8226cb39909a97daa5 (diff) | |
download | sso-b11f461f0f06105653748689940a12bc282438c4.zip sso-b11f461f0f06105653748689940a12bc282438c4.tar.gz sso-b11f461f0f06105653748689940a12bc282438c4.tar.bz2 |
Make it possible to use Server as library rather than controllerv0.1.7
Added option to fail with exception
Make startBrokerSession public
-rw-r--r-- | README.md | 7 | ||||
-rw-r--r-- | src/Server.php | 34 |
2 files changed, 31 insertions, 10 deletions
@@ -53,7 +53,7 @@ These methods are called fetch data from a data souce (like a DB). ```php class MySSOServer extends Jasny\SSO\Server -{ +{ /** * Authenticate using user credentials * @@ -92,6 +92,9 @@ class MySSOServer extends Jasny\SSO\Server The MySSOServer class can be used as controller in an MVC framework. +Alternatively you can use MySSOServer as library class. In that case pass option `fail_exception` to the constructor. +This will make the object throw a Jasny\SSO\Exception, rather than set the HTTP response and exit. + For more information, checkout the `server` example. #### Broker @@ -112,6 +115,8 @@ $user = $boker->getUserInfo(); echo json_encode($user); ``` +For more information, checkout the `broker` and `ajax-broker` example. + ## Examples There is an example server and two example brokers. One with normal redirects and one using diff --git a/src/Server.php b/src/Server.php index 2b03883..3428d4b 100644 --- a/src/Server.php +++ b/src/Server.php @@ -15,6 +15,11 @@ use Desarrolla2\Cache\Adapter; abstract class Server { /** + * @var array + */ + protected $options; + + /** * Cache that stores the special session data for the brokers. * * @var Cache @@ -26,12 +31,20 @@ abstract class Server */ protected $returnType; + /** + * @var mixed + */ + protected $brokerId; + /** * Class constructor + * + * @param array $options */ - public function __construct() + public function __construct(array $options = []) { + $this->options = $options; $this->cache = $this->createCacheAdapter(); } @@ -51,10 +64,12 @@ abstract class Server /** * Start the session for broker requests to the SSO server */ - protected function startBrokerSession() + public function startBrokerSession() { + if (isset($this->brokerId)) return; + if (!isset($_GET['sso_session'])) { - return $this->fail("No session"); + return $this->fail("No session", 400); } $sid = $_GET['sso_session']; @@ -66,23 +81,20 @@ abstract class Server } if (session_status() === PHP_SESSION_ACTIVE) { - if ($linkedId !== session_id()) throw new \Exception("Session has already started."); + if ($linkedId !== session_id()) throw new \Exception("Session has already started", 400); return; } session_id($linkedId); session_start(); - $brokerId = $this->validateBrokerSessionId($sid); - - $this->broker = $brokerId; - return; + $this->brokerId = $this->validateBrokerSessionId($sid); } /** * Validate the broker session id * - * @return string + * @return string the broker id */ protected function validateBrokerSessionId($sid) { @@ -327,6 +339,10 @@ abstract class Server */ protected function fail($message, $http_status = 500) { + if (!empty($this->options['fail_exception'])) { + throw new Exception($message, $http_status); + } + if ($http_status === 500) trigger_error($message, E_USER_WARNING); if ($this->returnType === 'jsonp') { |