@@ -26,6 +26,12 @@ public function testMalformedUtf8StringsFail()
2626 JWT ::encode (['message ' => pack ('c ' , 128 )], 'a ' , 'HS256 ' );
2727 }
2828
29+ public function testInvalidKeyOpensslSignFail ()
30+ {
31+ $ this ->expectException (DomainException::class);
32+ JWT ::sign ('message ' , 'invalid key ' , 'openssl ' );
33+ }
34+
2935 public function testMalformedJsonThrowsException ()
3036 {
3137 $ this ->expectException (DomainException::class);
@@ -107,6 +113,40 @@ public function testExpiredTokenWithLeeway()
107113 $ this ->assertSame ($ decoded ->message , 'abc ' );
108114 }
109115
116+ public function testExpiredExceptionPayload ()
117+ {
118+ $ this ->expectException (ExpiredException::class);
119+ $ payload = [
120+ 'message ' => 'abc ' ,
121+ 'exp ' => time () - 100 , // time in the past
122+ ];
123+ $ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
124+ try {
125+ JWT ::decode ($ encoded , new Key ('my_key ' , 'HS256 ' ));
126+ } catch (ExpiredException $ e ) {
127+ $ exceptionPayload = (array ) $ e ->getPayload ();
128+ $ this ->assertEquals ($ exceptionPayload , $ payload );
129+ throw $ e ;
130+ }
131+ }
132+
133+ public function testBeforeValidExceptionPayload ()
134+ {
135+ $ this ->expectException (BeforeValidException::class);
136+ $ payload = [
137+ 'message ' => 'abc ' ,
138+ 'iat ' => time () + 100 , // time in the future
139+ ];
140+ $ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
141+ try {
142+ JWT ::decode ($ encoded , new Key ('my_key ' , 'HS256 ' ));
143+ } catch (BeforeValidException $ e ) {
144+ $ exceptionPayload = (array ) $ e ->getPayload ();
145+ $ this ->assertEquals ($ exceptionPayload , $ payload );
146+ throw $ e ;
147+ }
148+ }
149+
110150 public function testValidTokenWithNbf ()
111151 {
112152 $ payload = [
@@ -484,4 +524,26 @@ public function testGetHeaders()
484524 $ this ->assertEquals ($ headers ->typ , 'JWT ' );
485525 $ this ->assertEquals ($ headers ->alg , 'HS256 ' );
486526 }
527+
528+ public function testAdditionalHeaderOverrides ()
529+ {
530+ $ msg = JWT ::encode (
531+ ['message ' => 'abc ' ],
532+ 'my_key ' ,
533+ 'HS256 ' ,
534+ 'my_key_id ' ,
535+ [
536+ 'cty ' => 'test-eit;v=1 ' ,
537+ 'typ ' => 'JOSE ' , // override type header
538+ 'kid ' => 'not_my_key_id ' , // should not override $key param
539+ 'alg ' => 'BAD ' , // should not override $alg param
540+ ]
541+ );
542+ $ headers = new stdClass ();
543+ JWT ::decode ($ msg , new Key ('my_key ' , 'HS256 ' ), $ headers );
544+ $ this ->assertEquals ('test-eit;v=1 ' , $ headers ->cty , 'additional field works ' );
545+ $ this ->assertEquals ('JOSE ' , $ headers ->typ , 'typ override works ' );
546+ $ this ->assertEquals ('my_key_id ' , $ headers ->kid , 'key param not overridden ' );
547+ $ this ->assertEquals ('HS256 ' , $ headers ->alg , 'alg param not overridden ' );
548+ }
487549}
0 commit comments