Skip to content

Commit db42747

Browse files
authored
Merge pull request #10 from Staffbase/add-module-exceptions
Add module exceptions
2 parents 242fa4a + b9fb057 commit db42747

13 files changed

Lines changed: 114 additions & 32 deletions

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class RemoteCallHandler extends AbstractRemoteCallHandler implements DeleteInsta
9696

9797
// pass it to the PluginSession on construction as the last parameter
9898
$remoteCallHandler = new RemoteCallHandler($db);
99-
$session = new PluginSessiona(PLUGIN_ID, $secret, $sessionHandler, null, $remoteCallHandler);
99+
$session = new PluginSession(PLUGIN_ID, $secret, $sessionHandler, null, $remoteCallHandler);
100100

101101
/* Unreachable code in a delete call follows */
102102
...
@@ -121,6 +121,6 @@ To run the tests a simple `# composer test` command in the root directory will s
121121

122122
## License
123123

124-
Copyright 2017-2018 Staffbase GmbH.
124+
Copyright 2017-2019 Staffbase GmbH.
125125

126126
Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0

doc/api.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
* [getSessionData](#getsessiondata)
3232
* [setSessionVar](#setsessionvar)
3333
* [isUserView](#isuserview)
34+
* [SSOAuthenticationException](#ssoauthenticationexception)
35+
* [SSOException](#ssoexception)
3436
* [SSOToken](#ssotoken)
3537
* [getAudience](#getaudience-1)
3638
* [getExpireAtTime](#getexpireattime-1)
@@ -552,6 +554,32 @@ PluginSession::isUserView( ): boolean
552554

553555
---
554556

557+
## SSOAuthenticationException
558+
559+
Class SSOAuthenticationException
560+
561+
An SSO Exception type which indicates
562+
a failure during the authentication process
563+
caused by invalid input.
564+
565+
Can be used to identify cases which can
566+
be handled with a soft http error eg.: 401.
567+
568+
* Full name: \Staffbase\plugins\sdk\Exceptions\SSOAuthenticationException
569+
* Parent class: \Staffbase\plugins\sdk\Exceptions\SSOException
570+
571+
572+
## SSOException
573+
574+
Class SSOException
575+
576+
A general SSO Exception type to group
577+
exceptions from this library.
578+
579+
* Full name: \Staffbase\plugins\sdk\Exceptions\SSOException
580+
* Parent class:
581+
582+
555583
## SSOToken
556584

557585
A container which is able to decrypt and store the data transmitted
@@ -957,4 +985,4 @@ PEM encoded key
957985

958986

959987
--------
960-
> This document was automatically generated from source code comments on 2019-02-07 using [phpDocumentor](http://www.phpdoc.org/) and [cvuorinen/phpdoc-markdown-public](https://github.com/cvuorinen/phpdoc-markdown-public)
988+
> This document was automatically generated from source code comments on 2019-08-29 using [phpDocumentor](http://www.phpdoc.org/) and [cvuorinen/phpdoc-markdown-public](https://github.com/cvuorinen/phpdoc-markdown-public)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* SSO Session implementation, based on this doc:
4+
* https://developers.staffbase.com/api/plugin-sso/
5+
*
6+
* PHP version 5.5.9
7+
*
8+
* @category Authentication
9+
* @copyright 2017-2019 Staffbase, GmbH.
10+
* @author Vitaliy Ivanov
11+
* @license http://www.apache.org/licenses/LICENSE-2.0
12+
* @link https://github.com/staffbase/plugins-sdk-php
13+
*/
14+
15+
namespace Staffbase\plugins\sdk\Exceptions;
16+
17+
/**
18+
* Class SSOAuthenticationException
19+
*
20+
* An SSO Exception type which indicates
21+
* a failure during the authentication process
22+
* caused by invalid input.
23+
*
24+
* Can be used to identify cases which can
25+
* be handled with a soft http error eg.: 401.
26+
*/
27+
class SSOAuthenticationException extends SSOException {}

src/Exceptions/SSOException.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* SSO Session implementation, based on this doc:
4+
* https://developers.staffbase.com/api/plugin-sso/
5+
*
6+
* PHP version 5.5.9
7+
*
8+
* @category Authentication
9+
* @copyright 2017-2019 Staffbase, GmbH.
10+
* @author Vitaliy Ivanov
11+
* @license http://www.apache.org/licenses/LICENSE-2.0
12+
* @link https://github.com/staffbase/plugins-sdk-php
13+
*/
14+
15+
namespace Staffbase\plugins\sdk\Exceptions;
16+
17+
use Exception;
18+
19+
/**
20+
* Class SSOException
21+
*
22+
* A general SSO Exception type to group
23+
* exceptions from this library.
24+
*/
25+
class SSOException extends Exception {}

src/PluginSession.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@
66
* PHP version 5.5.9
77
*
88
* @category Authentication
9-
* @copyright 2017 Staffbase, GmbH.
9+
* @copyright 2017-2019 Staffbase, GmbH.
1010
* @author Vitaliy Ivanov
1111
* @license http://www.apache.org/licenses/LICENSE-2.0
1212
* @link https://github.com/staffbase/plugins-sdk-php
1313
*/
1414

1515
namespace Staffbase\plugins\sdk;
1616

17-
use Exception;
1817
use SessionHandlerInterface;
1918
use Staffbase\plugins\sdk\SSOData;
2019
use Staffbase\plugins\sdk\SSOToken;
20+
use Staffbase\plugins\sdk\Exceptions\SSOException;
21+
use Staffbase\plugins\sdk\Exceptions\SSOAuthenticationException;
2122
use Staffbase\plugins\sdk\RemoteCall\RemoteCallInterface;
2223
use Staffbase\plugins\sdk\RemoteCall\DeleteInstanceCallHandlerInterface;
2324

@@ -52,15 +53,15 @@ class PluginSession extends SSOData
5253
* @param $leeway in seconds to compensate clock skew
5354
* @param $remoteCallHandler a class handling remote calls
5455
*
55-
* @throws Exception
56+
* @throws SSOAuthenticationException | SSOException
5657
*/
5758
public function __construct($pluginId, $appSecret, SessionHandlerInterface $sessionHandler = null, $leeway = 0, RemoteCallInterface $remoteCallHandler = null) {
5859

5960
if (!$pluginId)
60-
throw new Exception('Empty plugin ID.');
61+
throw new SSOException('Empty plugin ID.');
6162

6263
if (!$appSecret)
63-
throw new Exception('Empty app secret.');
64+
throw new SSOException('Empty app secret.');
6465

6566
if ($sessionHandler)
6667
session_set_save_handler($sessionHandler, true);
@@ -73,11 +74,11 @@ public function __construct($pluginId, $appSecret, SessionHandlerInterface $sess
7374
// lets hint to bad class usage, as these cases should never happen.
7475

7576
if($pid && $jwt) {
76-
throw new Exception('Tried to initialize the session with both PID and JWT provided.');
77+
throw new SSOAuthenticationException('Tried to initialize the session with both PID and JWT provided.');
7778
}
7879

7980
if (!$pid && !$jwt) {
80-
throw new Exception('Missing PID or JWT query parameter in Request.');
81+
throw new SSOAuthenticationException('Missing PID or JWT query parameter in Request.');
8182
}
8283

8384
$this->pluginInstanceId = $pid;
@@ -120,7 +121,7 @@ public function __construct($pluginId, $appSecret, SessionHandlerInterface $sess
120121
// requests with spoofed PID are not allowed
121122
if (!isset($_SESSION[$this->pluginInstanceId][self::KEY_SSO])
122123
|| empty($_SESSION[$this->pluginInstanceId][self::KEY_SSO]))
123-
throw new Exception('Tried to access an instance without previous authentication.');
124+
throw new SSOAuthenticationException('Tried to access an instance without previous authentication.');
124125

125126
// decide if we are in user view or not
126127
if($this->isEditor() && (!isset($_GET[self::QUERY_PARAM_USERVIEW]) || $_GET[self::QUERY_PARAM_USERVIEW] !== 'true'))

src/RemoteCall/AbstractRemoteCallHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* PHP version 5.5.9
77
*
88
* @category Authentication
9-
* @copyright 2018 Staffbase, GmbH.
9+
* @copyright 2017-2019 Staffbase, GmbH.
1010
* @author Vitaliy Ivanov
1111
* @license http://www.apache.org/licenses/LICENSE-2.0
1212
* @link https://github.com/staffbase/plugins-sdk-php

src/RemoteCall/DeleteInstanceCallHandlerInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* PHP version 5.5.9
77
*
88
* @category Authentication
9-
* @copyright 2018 Staffbase, GmbH.
9+
* @copyright 2017-2019 Staffbase, GmbH.
1010
* @author Vitaliy Ivanov
1111
* @license http://www.apache.org/licenses/LICENSE-2.0
1212
* @link https://github.com/staffbase/plugins-sdk-php

src/RemoteCall/RemoteCallInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* PHP version 5.5.9
77
*
88
* @category Authentication
9-
* @copyright 2018 Staffbase, GmbH.
9+
* @copyright 2017-2019 Staffbase, GmbH.
1010
* @author Vitaliy Ivanov
1111
* @license http://www.apache.org/licenses/LICENSE-2.0
1212
* @link https://github.com/staffbase/plugins-sdk-php
@@ -17,7 +17,7 @@
1717
* Interface RemoteCallInterface
1818
*
1919
* A generic interface describing the protocol with the
20-
* Staffbase Backend after a Remote SSO cal was issued.
20+
* Staffbase Backend after a Remote SSO call was issued.
2121
*
2222
* @package Staffbase\plugins\sdk\RemoteCall
2323
*/
@@ -31,7 +31,7 @@ interface RemoteCallInterface
3131
public function exitSuccess();
3232

3333
/**
34-
* Stop the execution by providing a 5XX HTTP response
34+
* Stop the execution by providing a non 2XX HTTP response
3535
*
3636
* This will tell Staffbase that it should try again later.
3737
*/

src/SSOData.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* PHP version 5.5.9
77
*
88
* @category Authentication
9-
* @copyright 2017 Staffbase, GmbH.
9+
* @copyright 2017-2019 Staffbase, GmbH.
1010
* @author Vitaliy Ivanov
1111
* @license http://www.apache.org/licenses/LICENSE-2.0
1212
* @link https://github.com/staffbase/plugins-sdk-php

src/SSOToken.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,22 @@
66
* PHP version 5.5.9
77
*
88
* @category Authentication
9-
* @copyright 2017 Staffbase, GmbH.
9+
* @copyright 2017-2019 Staffbase, GmbH.
1010
* @author Vitaliy Ivanov
1111
* @license http://www.apache.org/licenses/LICENSE-2.0
1212
* @link https://github.com/staffbase/plugins-sdk-php
1313
*/
1414

1515
namespace Staffbase\plugins\sdk;
1616

17-
use Exception;
1817
use Lcobucci\JWT\Parser;
1918
use Lcobucci\JWT\Builder;
2019
use Lcobucci\JWT\ValidationData;
2120
use Lcobucci\JWT\Claim\Validatable;
2221
use Lcobucci\JWT\Signer\Keychain;
2322
use Lcobucci\JWT\Signer\Rsa\Sha256;
23+
use Staffbase\plugins\sdk\Exceptions\SSOException;
24+
use Staffbase\plugins\sdk\Exceptions\SSOAuthenticationException;
2425

2526
/**
2627
* A container which is able to decrypt and store the data transmitted
@@ -40,18 +41,18 @@ class SSOToken extends SSOData
4041
* @param string $tokenData The token text.
4142
* @param int $leeway count of seconds added to current timestamp
4243
*
43-
* @throws Exception on invalid parameters.
44+
* @throws SSOException on invalid parameters.
4445
*/
4546
public function __construct($appSecret, $tokenData, $leeway = 0) {
4647

4748
if (!trim($appSecret))
48-
throw new Exception('Parameter appSecret for SSOToken is empty.');
49+
throw new SSOException('Parameter appSecret for SSOToken is empty.');
4950

5051
if (!trim($tokenData))
51-
throw new Exception('Parameter tokenData for SSOToken is empty.');
52+
throw new SSOException('Parameter tokenData for SSOToken is empty.');
5253

5354
if (!is_numeric($leeway))
54-
throw new Exception('Parameter leeway has to be numeric.');
55+
throw new SSOException('Parameter leeway has to be numeric.');
5556

5657
// convert secret to PEM if its a plain base64 string and does not yield an url
5758
if(strpos(trim($appSecret),'-----') !== 0 && strpos(trim($appSecret), 'file://') !==0 )
@@ -69,7 +70,7 @@ public function __construct($appSecret, $tokenData, $leeway = 0) {
6970
*
7071
* @return Lcobucci\JWT\Token;
7172
*
72-
* @throws Exception if the parsing/verification/validation of the token fails.
73+
* @throws SSOAuthenticationException if the parsing/verification/validation of the token fails.
7374
*/
7475
protected function parseToken($appSecret, $tokenData, $leeway) {
7576

@@ -81,7 +82,7 @@ protected function parseToken($appSecret, $tokenData, $leeway) {
8182
$keychain = new Keychain();
8283

8384
if (!$this->token->verify($signer, $keychain->getPublicKey($appSecret)))
84-
throw new Exception('Token verification failed.');
85+
throw new SSOAuthenticationException('Token verification failed.');
8586

8687
// validate claims
8788
$data = new ValidationData(time() +$leeway); // iat, nbf and exp are validated by default
@@ -92,7 +93,7 @@ protected function parseToken($appSecret, $tokenData, $leeway) {
9293

9394
// its a security risk to work with tokens lacking instance id
9495
if (!trim($this->getInstanceId()))
95-
throw new Exception('Token lacks instance id.');
96+
throw new SSOAuthenticationException('Token lacks instance id.');
9697
}
9798

9899
/**
@@ -128,7 +129,7 @@ public static function base64ToPEMPublicKey($data) {
128129
*
129130
* @param Lcobucci\JWT\ValidationData $data to validate against
130131
*
131-
* @throws Exception always.
132+
* @throws SSOAuthenticationException always.
132133
*/
133134
protected function throwVerboseException(ValidationData $data) {
134135

@@ -144,13 +145,13 @@ protected function throwVerboseException(ValidationData $data) {
144145
$operator = array_pop($segments);
145146
$operand = $data->get($claimName);
146147

147-
throw new Exception("Token Validation failed on claim '$claimName' $claimValue $operator $operand.");
148+
throw new SSOAuthenticationException("Token Validation failed on claim '$claimName' $claimValue $operator $operand.");
148149
}
149150
}
150151
}
151152

152153
// unknown reason, probably an addition to used library
153-
throw new Exception('Token Validation failed.');
154+
throw new SSOAuthenticationException('Token Validation failed.');
154155
}
155156

156157
/**

0 commit comments

Comments
 (0)