File tree Expand file tree Collapse file tree 2 files changed +14
-1
lines changed Expand file tree Collapse file tree 2 files changed +14
-1
lines changed Original file line number Diff line number Diff line change @@ -111,6 +111,10 @@ public static function decode(
111111 if (null === ($ payload = static ::jsonDecode ($ payloadRaw ))) {
112112 throw new UnexpectedValueException ('Invalid claims encoding ' );
113113 }
114+ if (is_array ($ payload )) {
115+ // prevent PHP Fatal Error in edge-cases when payload is empty array
116+ $ payload = (object ) $ payload ;
117+ }
114118 if (!$ payload instanceof stdClass) {
115119 throw new UnexpectedValueException ('Payload must be a JSON object ' );
116120 }
@@ -355,7 +359,7 @@ public static function jsonDecode(string $input)
355359 public static function jsonEncode (array $ input ): string
356360 {
357361 if (PHP_VERSION_ID >= 50400 ) {
358- $ json = \json_encode ($ input , \JSON_UNESCAPED_SLASHES |\ JSON_FORCE_OBJECT );
362+ $ json = \json_encode ($ input , \JSON_UNESCAPED_SLASHES );
359363 } else {
360364 // PHP 5.3 only
361365 $ json = \json_encode ($ input );
Original file line number Diff line number Diff line change @@ -330,6 +330,15 @@ public function testDecodesEmptyArrayAsObject()
330330 $ this ->assertEquals ((object ) $ payload , $ decoded );
331331 }
332332
333+ public function testDecodesArraysInJWTAsArray ()
334+ {
335+ $ key = 'yma6Hq4XQegCVND8ef23OYgxSrC3IKqk ' ;
336+ $ payload = ['foo ' => [1 ,2 ,3 ]];
337+ $ jwt = JWT ::encode ($ payload , $ key , 'HS256 ' );
338+ $ decoded = JWT ::decode ($ jwt , new Key ($ key , 'HS256 ' ));
339+ $ this ->assertEquals ($ payload ['foo ' ], $ decoded ->foo );
340+ }
341+
333342 /**
334343 * @runInSeparateProcess
335344 * @dataProvider provideEncodeDecode
You can’t perform that action at this time.
0 commit comments