【问题标题】:Decoding JWT string from Google fails [php]从 Google 解码 JWT 字符串失败 [php]
【发布时间】:2015-04-14 05:04:33
【问题描述】:

我正在尝试在 PHP 中解码 JWT 加密字符串。该字符串是有效的,因为它在这里完美解码:Working Demo

我正在使用这个GITHUB 进行解码。

我的 sn-p 是,它从 google 获取一个数组中的公钥,效果很好。但是解码部分报错如下。

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

【问题讨论】:

标签: php json google-oauth decode jwt


【解决方案1】:

发现问题。

必须在解码语句中添加“false”。

$bla = JWT::decode($jwtstring, $oauthKey, false);

【讨论】:

    猜你喜欢
    • 2015-04-13
    • 2017-07-12
    • 1970-01-01
    • 2014-02-11
    • 2018-04-16
    • 2021-09-12
    • 1970-01-01
    • 2019-08-15
    • 2018-02-15
    相关资源
    最近更新 更多