Skip to content

Commit 391ec35

Browse files
Merge pull request #19 from Staffbase/NFS-1239-Update-Link
NFS-1239: Link updated
2 parents 7b92caf + 8e901cb commit 391ec35

3 files changed

Lines changed: 37 additions & 24 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![Build Status](https://travis-ci.org/Staffbase/plugins-sdk-php.svg?branch=master)](https://travis-ci.org/Staffbase/plugins-sdk-php)
44

5-
If you are developing your own plugin for your Staffbase app we describe the authentication flow of a plugin at https://developers.staffbase.com/api/plugin-sso/. While this documentation just covers the conceptual ideas of the interface of plugins though – the so called Plugin SSO – we want to provide a library to help you develop your first plugin for Staffbase even faster. This SDK provides the basic functionality to parse and verify a provided token for PHP.
5+
If you are developing your own plugin for your Staffbase app we describe the authentication flow of a plugin at https://developers.staffbase.com/guide/customplugin-overview/. While this documentation just covers the conceptual ideas of the interface of plugins though – the so called Plugin SSO – we want to provide a library to help you develop your first plugin for Staffbase even faster. This SDK provides the basic functionality to parse and verify a provided token for PHP.
66

77
## Installation
88

@@ -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-2019 Staffbase GmbH.
124+
Copyright 2017-2021 Staffbase GmbH.
125125

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

test/SSODataTest.php

Lines changed: 7 additions & 4 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 2017-2019 Staffbase, GmbH.
9+
* @copyright 2017-2021 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
@@ -16,6 +16,7 @@
1616

1717
use PHPUnit\Framework\TestCase;
1818
use Staffbase\plugins\sdk\SSOData;
19+
use DateTimeImmutable;
1920

2021
class SSODataTest extends TestCase
2122
{
@@ -30,12 +31,14 @@ class SSODataTest extends TestCase
3031
public static function getTokenData()
3132
{
3233

34+
$date = new DateTimeImmutable();
35+
3336
$tokenData = [];
3437

3538
$tokenData[SSOData::CLAIM_AUDIENCE] = 'testPlugin';
36-
$tokenData[SSOData::CLAIM_EXPIRE_AT] = strtotime('10 minutes');
37-
$tokenData[SSOData::CLAIM_NOT_BEFORE] = strtotime('-1 minute');
38-
$tokenData[SSOData::CLAIM_ISSUED_AT] = time();
39+
$tokenData[SSOData::CLAIM_EXPIRE_AT] = $date->modify('10 minutes');
40+
$tokenData[SSOData::CLAIM_NOT_BEFORE] = $date->modify('-1 minute');
41+
$tokenData[SSOData::CLAIM_ISSUED_AT] = $date;
3942
$tokenData[SSOData::CLAIM_ISSUER] = 'api.staffbase.com';
4043
$tokenData[SSOData::CLAIM_INSTANCE_ID] = '55c79b6ee4b06c6fb19bd1e2';
4144
$tokenData[SSOData::CLAIM_INSTANCE_NAME] = 'Our locations';

test/SSOTokenTest.php

Lines changed: 28 additions & 18 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 2017-2019 Staffbase, GmbH.
9+
* @copyright 2017-2021 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
@@ -19,10 +19,13 @@
1919
use phpseclib\Crypt\RSA;
2020
use PHPUnit\Framework\TestCase;
2121
use Lcobucci\JWT\Builder;
22+
use Lcobucci\JWT\Configuration;
23+
use Lcobucci\JWT\Signer\Key\InMemory;
2224
use Lcobucci\JWT\Signer\Rsa\Sha256;
2325
use Staffbase\plugins\sdk\Exceptions\SSOAuthenticationException;
2426
use Staffbase\plugins\sdk\Exceptions\SSOException;
2527
use Staffbase\plugins\sdk\SSOToken;
28+
use DateTimeImmutable;
2629

2730
class SSOTokenTest extends TestCase
2831
{
@@ -58,18 +61,17 @@ public function setUp(): void {
5861
*/
5962
public static function createSignedTokenFromData($privateKey, $tokenData) {
6063

61-
$signer = new Sha256();
62-
$key = new Key($privateKey);
64+
$config = Configuration::forSymmetricSigner(new Sha256(), InMemory::plainText($privateKey));
6365

64-
return (new Builder())
66+
return ($config->builder())
6567
->issuedBy($tokenData[SSOToken::CLAIM_ISSUER])
6668
->permittedFor($tokenData[SSOToken::CLAIM_AUDIENCE])
6769
->issuedAt($tokenData[SSOToken::CLAIM_ISSUED_AT])
6870
->canOnlyBeUsedAfter($tokenData[SSOToken::CLAIM_NOT_BEFORE])
6971
->expiresAt($tokenData[SSOToken::CLAIM_EXPIRE_AT])
72+
->relatedTo($tokenData[SSOToken::CLAIM_USER_ID])
7073
->withClaim(SSOToken::CLAIM_INSTANCE_ID, $tokenData[SSOToken::CLAIM_INSTANCE_ID])
7174
->withClaim(SSOToken::CLAIM_INSTANCE_NAME, $tokenData[SSOToken::CLAIM_INSTANCE_NAME])
72-
->withClaim(SSOToken::CLAIM_USER_ID, $tokenData[SSOToken::CLAIM_USER_ID])
7375
->withClaim(SSOToken::CLAIM_USER_EXTERNAL_ID, $tokenData[SSOToken::CLAIM_USER_EXTERNAL_ID])
7476
->withClaim(SSOToken::CLAIM_USER_FULL_NAME, $tokenData[SSOToken::CLAIM_USER_FULL_NAME])
7577
->withClaim(SSOToken::CLAIM_USER_FIRST_NAME, $tokenData[SSOToken::CLAIM_USER_FIRST_NAME])
@@ -83,8 +85,7 @@ public static function createSignedTokenFromData($privateKey, $tokenData) {
8385
->withClaim(SSOToken::CLAIM_BRANCH_ID, $tokenData[SSOToken::CLAIM_BRANCH_ID])
8486
->withClaim(SSOToken::CLAIM_BRANCH_SLUG, $tokenData[SSOToken::CLAIM_BRANCH_SLUG])
8587
->withClaim(SSOToken::CLAIM_SESSION_ID, $tokenData[SSOToken::CLAIM_SESSION_ID])
86-
->sign($signer, $key)
87-
->getToken();
88+
->getToken($config->signer(), $config->signingKey());
8889
}
8990

9091
/**
@@ -96,15 +97,17 @@ public static function createSignedTokenFromData($privateKey, $tokenData) {
9697
*/
9798
private static function createUnsignedTokenFromData($tokenData) {
9899

99-
return (new Builder())
100+
$config = Configuration::forUnsecuredSigner();
101+
102+
return ($config->builder())
100103
->issuedBy($tokenData[SSOToken::CLAIM_ISSUER])
101104
->permittedFor($tokenData[SSOToken::CLAIM_AUDIENCE])
102105
->issuedAt($tokenData[SSOToken::CLAIM_ISSUED_AT])
103106
->canOnlyBeUsedAfter($tokenData[SSOToken::CLAIM_NOT_BEFORE])
104107
->expiresAt($tokenData[SSOToken::CLAIM_EXPIRE_AT])
108+
->relatedTo($tokenData[SSOToken::CLAIM_USER_ID])
105109
->withClaim(SSOToken::CLAIM_INSTANCE_ID, $tokenData[SSOToken::CLAIM_INSTANCE_ID])
106110
->withClaim(SSOToken::CLAIM_INSTANCE_NAME, $tokenData[SSOToken::CLAIM_INSTANCE_NAME])
107-
->withClaim(SSOToken::CLAIM_USER_ID, $tokenData[SSOToken::CLAIM_USER_ID])
108111
->withClaim(SSOToken::CLAIM_USER_EXTERNAL_ID, $tokenData[SSOToken::CLAIM_USER_EXTERNAL_ID])
109112
->withClaim(SSOToken::CLAIM_USER_FULL_NAME, $tokenData[SSOToken::CLAIM_USER_FULL_NAME])
110113
->withClaim(SSOToken::CLAIM_USER_FIRST_NAME, $tokenData[SSOToken::CLAIM_USER_FIRST_NAME])
@@ -118,7 +121,7 @@ private static function createUnsignedTokenFromData($tokenData) {
118121
->withClaim(SSOToken::CLAIM_BRANCH_ID, $tokenData[SSOToken::CLAIM_BRANCH_ID])
119122
->withClaim(SSOToken::CLAIM_BRANCH_SLUG, $tokenData[SSOToken::CLAIM_BRANCH_SLUG])
120123
->withClaim(SSOToken::CLAIM_SESSION_ID, $tokenData[SSOToken::CLAIM_SESSION_ID])
121-
->getToken();
124+
->getToken($config->signer(), $config->signingKey());
122125
}
123126

124127
/**
@@ -197,7 +200,7 @@ public function testConstructorRefuseNonNumericLeeway() {
197200
public function testConstructorToFailOnExpiredToken() {
198201

199202
$tokenData = SSODataTest::getTokenData();
200-
$tokenData[SSOToken::CLAIM_EXPIRE_AT] = strtotime("-1 minute");
203+
$tokenData[SSOToken::CLAIM_EXPIRE_AT] = (new DateTimeImmutable())->modify("-1 minute");
201204

202205
$token = self::createSignedTokenFromData($this->privateKey, $tokenData);
203206

@@ -216,7 +219,7 @@ public function testConstructorToFailOnExpiredToken() {
216219
public function testConstructorToFailOnFutureToken() {
217220

218221
$tokenData = SSODataTest::getTokenData();
219-
$tokenData[SSOToken::CLAIM_NOT_BEFORE] = strtotime("+1 minute");
222+
$tokenData[SSOToken::CLAIM_NOT_BEFORE] = (new DateTimeImmutable())->modify("+1 minute");
220223

221224
$token = self::createSignedTokenFromData($this->privateKey, $tokenData);
222225

@@ -235,7 +238,7 @@ public function testConstructorToFailOnFutureToken() {
235238
public function testConstructorToFailOnTokenIssuedInTheFuture() {
236239

237240
$tokenData = SSODataTest::getTokenData();
238-
$tokenData[SSOToken::CLAIM_ISSUED_AT] = strtotime("+10 second");
241+
$tokenData[SSOToken::CLAIM_ISSUED_AT] = (new DateTimeImmutable())->modify("+10 second");
239242

240243
$token = self::createSignedTokenFromData($this->privateKey, $tokenData);
241244

@@ -255,7 +258,7 @@ public function testConstructorAcceptsLeewayForTokenIssuedInTheFuture() {
255258

256259
$leeway = 11;
257260
$tokenData = SSODataTest::getTokenData();
258-
$tokenData[SSOToken::CLAIM_ISSUED_AT] = strtotime("+10 second");
261+
$tokenData[SSOToken::CLAIM_ISSUED_AT] = (new DateTimeImmutable())->modify("+10 second");
259262

260263
$token = self::createSignedTokenFromData($this->privateKey, $tokenData);
261264

@@ -297,8 +300,8 @@ public function testConstructorToFailOnUnsignedToken() {
297300

298301
$token = self::createUnsignedTokenFromData($tokenData);
299302

300-
$this->expectException(BadMethodCallException::class);
301-
$this->expectExceptionMessage('This token is not signed');
303+
$this->expectException(SSOAuthenticationException::class);
304+
$this->expectExceptionMessage('Token verification failed.');
302305

303306
new SSOToken($this->publicKey, $token);
304307
}
@@ -342,11 +345,18 @@ public function testAccessorsGiveCorrectValues() {
342345
$ssoToken = new SSOToken($this->publicKey, $token);
343346

344347
foreach ($accessors as $key => $fn) {
348+
349+
$data = $tokenData[$key];
350+
351+
if ($data instanceof DateTimeImmutable) {
352+
$data = $data->getTimestamp();
353+
}
354+
345355
$this->assertEquals(
346356
call_user_func([$ssoToken,$fn]),
347-
$tokenData[$key],
357+
$data,
348358
"called $fn expected ".
349-
is_array($tokenData[$key]) ? print_r($tokenData[$key], true) : $tokenData[$key]);
359+
is_array($data) ? print_r($data, true) : $data);
350360

351361
}
352362
}

0 commit comments

Comments
 (0)