我正在尝试在PHP中解码JWT加密的strying。该字符串有效,因为它在此处完美解码:Working Demo
我正在使用此GITHUB进行解码。
我的代码段是,它从数组中获取谷歌的公钥,效果很好。但解码部分会出现以下错误。
include('JWT.php');
$refresh = false;
if (file_exists('oauthkey')) {
$age = time() - filemtime('oauthkey');
if ($age > 20000)
$refresh = true;
} else
$refresh = true;
if ($refresh) {
$oauthKey = file_get_contents('https://www.googleapis.com/oauth2/v1/certs')
or die('Failed to retrieve google public key.');
$keyFile = fopen('oauthkey', 'w') or die ('Failed to open public key file for writing.');
fwrite($keyFile, $oauthKey);
fclose($keyFile);
} else {
$keyFile = fopen('oauthkey', 'r') or die ('Failed to open public key file for reading.');
$oauthKey = fread($keyFile, 5000) or die ('Failed to read from public key file.');
fclose($keyFile);
}
$oauthKey = json_decode($oauthKey, true); // get key from Google in Array
$jwtstring = 'eyJhbGciOiJS...'; // full long JWT encoded string
$bla = JWT::decode($jwtstring, $oauthKey);
echo print_r($bla);
PHP Notice: Undefined index: 433d0da18366fcdc43301fd1e142294a6209e451 in /home/domain.com/php-jwt-master/Authentication/JWT.php on line 64
PHP Warning: openssl_verify(): supplied key param cannot be coerced into a public key in /home/domain.com/php-jwt-master/Authentication/JWT.php on line 179
PHP Fatal error: Uncaught exception 'DomainException' with message 'OpenSSL unable to verify data: ' in /home/domain.com/php-jwt-master/Authentication/JWT.php:181
Stack trace:
#0 /home/domain.com/php-jwt-master/Authentication/JWT.php(71): JWT::verify('eyJhbGciOiJSUzI...', '+??????0?????SK...', NULL, 'RS256')
#1 /home/domain.com/php-jwt-master/Authentication/testjwt.php(31): JWT::decode('eyJhbGciOiJSUzI...', Array)
#2 {main}
thrown in /home/domain.com/php-jwt-master/Authentication/JWT.php on line 181
答案 0 :(得分:2)
发现了这个问题。
必须在解码语句中添加'false'。
$bla = JWT::decode($jwtstring, $oauthKey, false);