@@ -318,38 +318,53 @@ private function removeTokenInRequest(RequestInterface $request): void
318318 {
319319 assert ($ request instanceof Request);
320320
321- $ json = json_decode ($ request ->getBody () ?? '' );
322-
323321 if (isset ($ _POST [$ this ->config ->tokenName ])) {
324322 // We kill this since we're done and we don't want to pollute the POST array.
325323 unset($ _POST [$ this ->config ->tokenName ]);
326324 $ request ->setGlobal ('post ' , $ _POST );
327- } elseif (isset ($ json ->{$ this ->config ->tokenName })) {
328- // We kill this since we're done and we don't want to pollute the JSON data.
329- unset($ json ->{$ this ->config ->tokenName });
330- $ request ->setBody (json_encode ($ json ));
325+ } else {
326+ $ body = $ request ->getBody () ?? '' ;
327+ $ json = json_decode ($ body );
328+ if ($ json !== null && json_last_error () === JSON_ERROR_NONE ) {
329+ // We kill this since we're done and we don't want to pollute the JSON data.
330+ unset($ json ->{$ this ->config ->tokenName });
331+ $ request ->setBody (json_encode ($ json ));
332+ } else {
333+ parse_str ($ body , $ parsed );
334+ // We kill this since we're done and we don't want to pollute the BODY data.
335+ unset($ parsed [$ this ->config ->tokenName ]);
336+ $ request ->setBody (http_build_query ($ parsed ));
337+ }
331338 }
332339 }
333340
334341 private function getPostedToken (RequestInterface $ request ): ?string
335342 {
336343 assert ($ request instanceof IncomingRequest);
337344
338- // Does the token exist in POST, HEADER or optionally php:://input - json data.
345+ // Does the token exist in POST, HEADER or optionally php:://input - json data or PUT, DELETE, PATCH - raw data .
339346
340347 if ($ tokenValue = $ request ->getPost ($ this ->config ->tokenName )) {
341348 return $ tokenValue ;
342349 }
343350
344- if ($ request ->hasHeader ($ this ->config ->headerName ) && ! empty ($ request ->header ($ this ->config ->headerName )->getValue ())) {
351+ if ($ request ->hasHeader ($ this ->config ->headerName )
352+ && $ request ->header ($ this ->config ->headerName )->getValue () !== ''
353+ && $ request ->header ($ this ->config ->headerName )->getValue () !== []) {
345354 return $ request ->header ($ this ->config ->headerName )->getValue ();
346355 }
347356
348357 $ body = (string ) $ request ->getBody ();
349- $ json = json_decode ($ body );
350358
351- if ($ body !== '' && ! empty ($ json ) && json_last_error () === JSON_ERROR_NONE ) {
352- return $ json ->{$ this ->config ->tokenName } ?? null ;
359+ if ($ body !== '' ) {
360+ $ json = json_decode ($ body );
361+ if ($ json !== null && json_last_error () === JSON_ERROR_NONE ) {
362+ return $ json ->{$ this ->config ->tokenName } ?? null ;
363+ }
364+
365+ parse_str ($ body , $ parsed );
366+
367+ return $ parsed [$ this ->config ->tokenName ] ?? null ;
353368 }
354369
355370 return null ;
0 commit comments