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
1919use phpseclib \Crypt \RSA ;
2020use PHPUnit \Framework \TestCase ;
2121use Lcobucci \JWT \Builder ;
22+ use Lcobucci \JWT \Configuration ;
23+ use Lcobucci \JWT \Signer \Key \InMemory ;
2224use Lcobucci \JWT \Signer \Rsa \Sha256 ;
2325use Staffbase \plugins \sdk \Exceptions \SSOAuthenticationException ;
2426use Staffbase \plugins \sdk \Exceptions \SSOException ;
2527use Staffbase \plugins \sdk \SSOToken ;
28+ use DateTimeImmutable ;
2629
2730class 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,42 +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- }
89-
90- /**
91- * Create an unsigned token by omitting sign().
92- *
93- * @param array $tokenData associative array of claims
94- *
95- * @return string Encoded token.
96- */
97- private static function createUnsignedTokenFromData ($ tokenData ) {
98-
99- return (new Builder ())
100- ->issuedBy ($ tokenData [SSOToken::CLAIM_ISSUER ])
101- ->permittedFor ($ tokenData [SSOToken::CLAIM_AUDIENCE ])
102- ->issuedAt ($ tokenData [SSOToken::CLAIM_ISSUED_AT ])
103- ->canOnlyBeUsedAfter ($ tokenData [SSOToken::CLAIM_NOT_BEFORE ])
104- ->expiresAt ($ tokenData [SSOToken::CLAIM_EXPIRE_AT ])
105- ->withClaim (SSOToken::CLAIM_INSTANCE_ID , $ tokenData [SSOToken::CLAIM_INSTANCE_ID ])
106- ->withClaim (SSOToken::CLAIM_INSTANCE_NAME , $ tokenData [SSOToken::CLAIM_INSTANCE_NAME ])
107- ->withClaim (SSOToken::CLAIM_USER_ID , $ tokenData [SSOToken::CLAIM_USER_ID ])
108- ->withClaim (SSOToken::CLAIM_USER_EXTERNAL_ID , $ tokenData [SSOToken::CLAIM_USER_EXTERNAL_ID ])
109- ->withClaim (SSOToken::CLAIM_USER_FULL_NAME , $ tokenData [SSOToken::CLAIM_USER_FULL_NAME ])
110- ->withClaim (SSOToken::CLAIM_USER_FIRST_NAME , $ tokenData [SSOToken::CLAIM_USER_FIRST_NAME ])
111- ->withClaim (SSOToken::CLAIM_USER_LAST_NAME , $ tokenData [SSOToken::CLAIM_USER_LAST_NAME ])
112- ->withClaim (SSOToken::CLAIM_USER_ROLE , $ tokenData [SSOToken::CLAIM_USER_ROLE ])
113- ->withClaim (SSOToken::CLAIM_ENTITY_TYPE , $ tokenData [SSOToken::CLAIM_ENTITY_TYPE ])
114- ->withClaim (SSOToken::CLAIM_THEME_TEXT_COLOR , $ tokenData [SSOToken::CLAIM_THEME_TEXT_COLOR ])
115- ->withClaim (SSOToken::CLAIM_THEME_BACKGROUND_COLOR , $ tokenData [SSOToken::CLAIM_THEME_BACKGROUND_COLOR ])
116- ->withClaim (SSOToken::CLAIM_USER_LOCALE , $ tokenData [SSOToken::CLAIM_USER_LOCALE ])
117- ->withClaim (SSOToken::CLAIM_USER_TAGS , $ tokenData [SSOToken::CLAIM_USER_TAGS ])
118- ->withClaim (SSOToken::CLAIM_BRANCH_ID , $ tokenData [SSOToken::CLAIM_BRANCH_ID ])
119- ->withClaim (SSOToken::CLAIM_BRANCH_SLUG , $ tokenData [SSOToken::CLAIM_BRANCH_SLUG ])
120- ->withClaim (SSOToken::CLAIM_SESSION_ID , $ tokenData [SSOToken::CLAIM_SESSION_ID ])
121- ->getToken ();
88+ ->getToken ($ config ->signer (), $ config ->signingKey ());
12289 }
12390
12491 /**
@@ -197,7 +164,7 @@ public function testConstructorRefuseNonNumericLeeway() {
197164 public function testConstructorToFailOnExpiredToken () {
198165
199166 $ tokenData = SSODataTest::getTokenData ();
200- $ tokenData [SSOToken::CLAIM_EXPIRE_AT ] = strtotime ("-1 minute " );
167+ $ tokenData [SSOToken::CLAIM_EXPIRE_AT ] = ( new DateTimeImmutable ())-> modify ("-1 minute " );
201168
202169 $ token = self ::createSignedTokenFromData ($ this ->privateKey , $ tokenData );
203170
@@ -216,7 +183,7 @@ public function testConstructorToFailOnExpiredToken() {
216183 public function testConstructorToFailOnFutureToken () {
217184
218185 $ tokenData = SSODataTest::getTokenData ();
219- $ tokenData [SSOToken::CLAIM_NOT_BEFORE ] = strtotime ("+1 minute " );
186+ $ tokenData [SSOToken::CLAIM_NOT_BEFORE ] = ( new DateTimeImmutable ())-> modify ("+1 minute " );
220187
221188 $ token = self ::createSignedTokenFromData ($ this ->privateKey , $ tokenData );
222189
@@ -235,7 +202,7 @@ public function testConstructorToFailOnFutureToken() {
235202 public function testConstructorToFailOnTokenIssuedInTheFuture () {
236203
237204 $ tokenData = SSODataTest::getTokenData ();
238- $ tokenData [SSOToken::CLAIM_ISSUED_AT ] = strtotime ("+10 second " );
205+ $ tokenData [SSOToken::CLAIM_ISSUED_AT ] = ( new DateTimeImmutable ())-> modify ("+10 second " );
239206
240207 $ token = self ::createSignedTokenFromData ($ this ->privateKey , $ tokenData );
241208
@@ -255,7 +222,7 @@ public function testConstructorAcceptsLeewayForTokenIssuedInTheFuture() {
255222
256223 $ leeway = 11 ;
257224 $ tokenData = SSODataTest::getTokenData ();
258- $ tokenData [SSOToken::CLAIM_ISSUED_AT ] = strtotime ("+10 second " );
225+ $ tokenData [SSOToken::CLAIM_ISSUED_AT ] = ( new DateTimeImmutable ())-> modify ("+10 second " );
259226
260227 $ token = self ::createSignedTokenFromData ($ this ->privateKey , $ tokenData );
261228
@@ -284,25 +251,6 @@ public function testConstructorToFailOnMissingInstanceId() {
284251 new SSOToken ($ this ->publicKey , $ token );
285252 }
286253
287- /**
288- * @test
289- *
290- * Test constructor throws exception on a unsigned token.
291- *
292- * @covers \Staffbase\plugins\sdk\SSOToken::__construct
293- */
294- public function testConstructorToFailOnUnsignedToken () {
295-
296- $ tokenData = SSODataTest::getTokenData ();
297-
298- $ token = self ::createUnsignedTokenFromData ($ tokenData );
299-
300- $ this ->expectException (BadMethodCallException::class);
301- $ this ->expectExceptionMessage ('This token is not signed ' );
302-
303- new SSOToken ($ this ->publicKey , $ token );
304- }
305-
306254 /**
307255 * @test
308256 *
@@ -342,11 +290,18 @@ public function testAccessorsGiveCorrectValues() {
342290 $ ssoToken = new SSOToken ($ this ->publicKey , $ token );
343291
344292 foreach ($ accessors as $ key => $ fn ) {
293+
294+ $ data = $ tokenData [$ key ];
295+
296+ if ($ data instanceof DateTimeImmutable) {
297+ $ data = $ data ->getTimestamp ();
298+ }
299+
345300 $ this ->assertEquals (
346301 call_user_func ([$ ssoToken ,$ fn ]),
347- $ tokenData [ $ key ] ,
302+ $ data ,
348303 "called $ fn expected " .
349- is_array ($ tokenData [ $ key ] ) ? print_r ($ tokenData [ $ key ] , true ) : $ tokenData [ $ key ] );
304+ is_array ($ data ) ? print_r ($ data , true ) : $ data );
350305
351306 }
352307 }
0 commit comments