【问题标题】:Invalid Signature - Google JWT API无效签名 - Google JWT API
【发布时间】:2018-06-14 20:13:31
【问题描述】:

我正在尝试签署对 Google 0Auth2 的请求(在 PHP 中) 这是我的代码:

$jwtHeader = $this->base64url_encode(json_encode(array(
                "alg" => "RS256",
                "typ" => "JWT"
                )));
$now = time();
            $jwtClaim = $this->base64url_encode(json_encode(array(
                "iss" => "xxxxxxxxxxxxxxx.xxx.gserviceaccount.com",
                "scope" => "https://www.googleapis.com/auth/prediction",
                "aud" => "https://www.googleapis.com/oauth2/v4/token",
                "iat" => $now, 
                "exp" => $now + 3600,
                )));

$sign = hash_hmac('SHA256', $jwtHeader.".".$jwtClaim, $secret);
$jwtAssertion = $jwtHeader.".".$jwtClaim.".".$sign;

$client = new \GuzzleHttp\Client();
$res = $client->post('https://www.googleapis.com/oauth2/v4/token', ['headers' => ['Content-Type: application/x-www-form-urlencoded'], 'form_params' => ['grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer', 'assertion' => $jwtAssertion]]);
if ($res->getStatusCode() == 200) {
    return $res->getBody()->getContents();
}

//BASE64 METHOD
public function base64url_encode($data) { 
    return rtrim(strtr(base64_encode($data), '+/', '-_'), '='); 
} 

我得到的回应是

{
"error": "invalid_grant",
"error_description": "Invalid JWT Signature."
}

现在已经有几个小时了,仍然遇到同样的错误。有人遇到过这个吗? 如有任何建议,我将不胜感激。

【问题讨论】:

  • 很难说RS256是不对称的,你有公钥设置吗?你确定urn:ietf:params:oauth:grant-type:jwt-bearer 是预期的吗?你能不能也显示 base64url_encode 方法,除了代码看起来不错。
  • 宾果游戏,你不是对 hmac 签名进行 base64 化
  • @LawrenceCherone 感谢您的回复。设置服务帐户后,我从下载的 json 文件中获取了私钥,这里是我的$secreturn:ietf:params:oauth:grant-type:jwt-bearer 是的,这就是文档所说的。 base64url_encode 方法只是一个没有填充的 base64_encode。我将更新问题以包含该方法
  • 我也确实将签名编码为 base64。仍然有同样的错误

标签: php laravel oauth google-api jwt


【解决方案1】:

好吧,为了那些可能遇到同样问题的人。我就是这样解决的:

            $jwtHeader = $this->base64url_encode(json_encode(array(
                "alg" => "RS256",
                "typ" => "JWT"
                )));
            $now = time();
            $jwtClaim = $this->base64url_encode(json_encode(array(
                "iss" => "xxxxxxxxxxxxxxxxxx.xxx.gserviceaccount.com",
                "scope" => "https://www.googleapis.com/auth/prediction",
                "aud" => "https://www.googleapis.com/oauth2/v4/token",
                "iat" => $now, 
                "exp" => $now + 3600,
                )));
            $secret = "-----BEGIN PRIVATE KEY-----\nxxxxxxxxxxxxxxxxxx\n-----END PRIVATE KEY-----\n";

            $binary_signature = "";
            $algo = "SHA256";
            openssl_sign($jwtHeader.".".$jwtClaim, $binary_signature, $secret, $algo);
            $jwtSign = $this->base64url_encode($binary_signature);
            $jwtAssertion = $jwtHeader.".".$jwtClaim.".".$jwtSign;

            $client = new \GuzzleHttp\Client();
            $res = $client->post('https://www.googleapis.com/oauth2/v4/token', ['headers' => ['Content-Type: application/x-www-form-urlencoded; charset=utf-8'], 'form_params' => ['grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer', 'assertion' => $jwtAssertion]]);
            if ($res->getStatusCode() == 200) {
                return $res->getBody()->getContents();
            }

        public function base64url_encode($data) { 
            return rtrim(strtr(base64_encode($data), '+/', '-_'), '='); 
        }

这按预期工作。

【讨论】:

    猜你喜欢
    • 2018-10-04
    • 1970-01-01
    • 2016-12-29
    • 2020-09-01
    • 2020-01-21
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多