php-jwt icon indicating copy to clipboard operation
php-jwt copied to clipboard

JWT signature failed errors are not coming as JSON errors in SLIM

Open bhullar89 opened this issue 8 years ago • 2 comments

I am using Firebase JWT in one of my SLIM framework apps for RESTFul api. But all the JWT errors occur as slim app error rather than throwing back json errors ,which i can return to user.

Error is as below

Slim Application Error

The application could not run because of the following error: Details Type: Firebase\JWT\SignatureInvalidException Message: Signature verification failed File: D:\Xampp\htdocs\slim\test_app\test\vendor\firebase\php-jwt\src\JWT.php Line: 112

try{$decoded = JWT::decode("eyJseXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9leGFtcGxlLm9yZyIsImF1ZCI6Imh0dHA6XC9cL2V4YW1wbGUuY29tIiwiaWF0IjoiMjAxOC0wOS0wNCAwNjoyOTowNCIsIm5iZiI6NzgsInVpZCI6IjEyMzUifQ.nk3IAMGhSVgtluQgn-0xTZvRprzJJHLLj3ZWT_iiXPc", $key, array('HS256'));
					print_r($decoded);
				}catch(\Firebase\JWT\ExpiredException $e){
					 echo 'Caught exception: ',  $e->getMessage(), "\n";
				}

Can you help how can i display JWT errros in json format ,so that my restful api return that json error to user.

bhullar89 avatar Sep 04 '18 04:09 bhullar89

@bhullar89 store the error message in an array , and then just use the json_encode function to get the errors in json format

$result = array( 'status'=>false, 'message'=>null, 'data'=>null ); $fbPublicKey=$this->getFbPublicKey(); try { $decodedToken = JWT::decode($oauthToken, $fbPublicKey , ["RS256"]); $result['status']=true; $result['data']=(array)$decodedToken; } catch (\Exception $e) { $result['message']=$e->getMessage(); } return json_encode($result);

desmondiazz avatar Oct 07 '18 07:10 desmondiazz

Closing as this seems answered

yash30201 avatar Jun 14 '23 16:06 yash30201