我试图
使用从 API 控制台获得的私钥,使用 SHA256withRSA(也称为带有 SHA-256 哈希函数的 RSASASA-PKCS1-V1_5-SIGN)对输入的 UTF-8 表示进行签名。输出将是一个字节数组。
所以让我们把 Header 和 Claim 集合放到数组中
{"alg":"RS256","typ":"JWT"}.
{
"iss":"761326798069-r5mljlln1rd4lrbhg75efgigp36m78j5@developer.gserviceaccount.com",
"scope":"https://www.googleapis.com/auth/prediction",
"aud":"https://accounts.google.com/o/oauth2/token",
"exp":1328554385,
"iat":1328550785
}
JSON Web 签名 (JWS) 是指导为 JWT 生成签名的机制的规范。签名的输入是以下内容的字节数组:
{ Base64url 编码的标头}。{ Base64url 编码的声明集}
所以我建立数组只是为了测试
$seg0 = array(
"alg" => "RS256",
"typ" => "JWT"
);
$seg1 = array(
"iss" => "761326798069-r5mljlln1rd4lrbhg75efgigp36m78j5@developer.gserviceaccount.com",
"scope" => "https://www.googleapis.com/auth/prediction",
"aud" => "https://accounts.google.com/o/oauth2/token",
"exp" => 1328554385,
"iat" => 1328550785
);
$segs = array(
json_encode($seg0),
stripslashes(json_encode($seg1))
);
$segments = array(
rtrim(strtr(base64_encode($segs[0]), '+/', '-_'), '='),
rtrim(strtr(base64_encode($segs[1]), '+/', '-_'), '='),
);
这里是。前 2 个数组编码成功。
Output
eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9
eyJpc3MiOiI3NjEzMjY3OTgwNjktcjVtbGpsbG4xcmQ0bHJiaGc3NWVmZ2lncDM2bTc4ajVAZGV2ZWxvcGVyLmdzZXJ2aWNlYWNjb3VudC5jb20iLCJzY29wZSI6Imh0dHBzOi8vd3d3Lmdvb2dsZWFwaXMuY29tL2F1dGgvcHJlZGljdGlvbiIsImF1ZCI6Imh0dHBzOi8vYWNjb3VudHMuZ29vZ2xlLmNvbS9vL29hdXRoMi90b2tlbiIsImV4cCI6MTMyODU1NDM4NSwiaWF0IjoxMzI4NTUwNzg1fQ
我继续并编码签名
然后必须对签名进行 Base64url 编码。然后将签名与“。”连接起来。字符到输入字符串的 Base64url 表示的末尾。结果是 JWT。它应该是:
{ Base64url 编码的标头}。{ Base64url 编码的声明集}。{ Base64url 编码的签名}
$signature = makeSignedJwt($segments);
//$signature = makeSignedJwt($segs);
echo $signature .'<br /><br />';
$segments[] = rtrim(strtr(base64_encode($signature), '+/', '-_'), '=');
echo '<pre>'; print_r($segments); echo '</pre>';
function makeSignedJwt($segments)
{
$data = implode('.', $segments);
if (!openssl_sign($data, $signature, privateKey, "sha256"))
{
exit("Unable to sign data");
}
return $signature;
}
Output
Array
(
[0] => eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9
[1] => eyJpc3MiOiI3NjEzMjY3OTgwNjktcjVtbGpsbG4xcmQ0bHJiaGc3NWVmZ2lncDM2bTc4ajVAZGV2ZWxvcGVyLmdzZXJ2aWNlYWNjb3VudC5jb20iLCJzY29wZSI6Imh0dHBzOi8vd3d3Lmdvb2dsZWFwaXMuY29tL2F1dGgvcHJlZGljdGlvbiIsImF1ZCI6Imh0dHBzOi8vYWNjb3VudHMuZ29vZ2xlLmNvbS9vL29hdXRoMi90b2tlbiIsImV4cCI6MTMyODU1NDM4NSwiaWF0IjoxMzI4NTUwNzg1fQ
[2] => xFS6iZdJku5RKJ5_XdH3W5A8e9V3wsaFeQhAXoJtuxzW-xvqZq1CdEJJAo60VvK1UFONElVf_pthezEyz-eyWsoRGVZFibUQBaKXLI8eR28eFlaCAKH7bKh820uR7IwuRx4xr8MPmnC8so9u9TEY153gkU6Mz9e--pQPlcLlGY
)
一定是少了什么。。