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,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