Apple 登录“invalid_client”,使用 PHP 和 openSSL

我正在尝试使用此将 Apple 登录实现到 Android 应用程序中。文档中描述了主要流程:该库在Android端返回一个授权码。此授权代码必须发送到我的后端,然后再将其发送到 Apple 服务器以取回访问令牌。

这里这里所述,为了获得访问令牌,我们需要向 Apple API 发送参数列表、授权码和签名的 JWT。特别是,JWT 需要使用 ES256 算法使用私有 .p8 密钥进行签名,该密钥必须从 Apple 开发人员门户生成和下载。苹果文档

这是我的 PHP 脚本:

<?php


$authorization_code = $_POST('auth_code');


$privateKey = <<<EOD

-----BEGIN PRIVATE KEY-----

my_private_key_downloaded_from_apple_developer_portal (.p8 format)

-----END PRIVATE KEY-----

EOD;


$kid = 'key_id_of_the_private_key'; //Generated in Apple developer Portal

$iss = 'team_id_of_my_developer_profile';

$client_id = 'identifier_setted_in_developer_portal'; //Generated in Apple developer Portal


$signed_jwt = $this->generateJWT($kid, $iss, $client_id, $privateKey);


$data = [

            'client_id' => $client_id,

            'client_secret' => $signed_jwt,

            'code' => $authorization_code,

            'grant_type' => 'authorization_code'

        ];

$ch = curl_init();


curl_setopt($ch, CURLOPT_URL, 'https://appleid.apple.com/auth/token');

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);


$serverOutput = curl_exec($ch);


curl_close ($ch);


var_dump($serverOutput);


function generateJWT($kid, $iss, $sub, $key) {


    $header = [

        'alg' => 'ES256',

        'kid' => $kid

    ];

    $body = [

        'iss' => $iss,

        'iat' => time(),

        'exp' => time() + 3600,

        'aud' => 'https://appleid.apple.com',

        'sub' => $sub

    ];


问题是苹果的回应总是:


{"error":"invalid_client"}

在这里阅读,似乎问题可能与生成对 Apple 不正确的签名的 openSSL 有关(“ OpenSSL 的 ES256 签名结果是 DER 编码的 ASN.1 结构(它的大小超过 64)。(不是原始 R | | S 值) ”)。


有没有办法使用 openSSL 获得正确的签名?


p8 格式是 openssl_sign 和 openssl_pkey_get_private 函数的正确输入吗? (我注意到如果在jwt.io中使用提供的 .p8 密钥来计算签名的 jwt,则它不起作用。)


在 openSSL 文档中,我读到应该提供 pem 密钥,如何将 .p8 转换为 .pem 密钥?


我还尝试了一些 PHP 库,它们基本上使用上述相同的步骤,例如firebase /php-jwt和lcobucci/jwt,但 Apple 的响应仍然是“无效客户端”。


aluckdog
浏览 635回答 1
1回答

胡子哥哥

如here所示,问题实际上出在openSSL生成的签名中。使用 ES256,数字签名是两个无符号整数的串联,表示为 R 和 S,它们是椭圆曲线 (EC) 算法的结果。R的长度|| S 为 64。openssl_sign 函数生成一个签名,该签名是一个 DER 编码的 ASN.1 结构(大小 > 64)。解决方案是将 DER 编码的签名转换为 R 和 S 值的原始串联。在这个库中,存在一个函数“ fromDER ”,它执行这样的转换:&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @param string $der&nbsp; &nbsp; &nbsp;* @param int&nbsp; &nbsp; $partLength&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @return string&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public static function fromDER(string $der, int $partLength)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $hex = unpack('H*', $der)[1];&nbsp; &nbsp; &nbsp; &nbsp; if ('30' !== mb_substr($hex, 0, 2, '8bit')) { // SEQUENCE&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new \RuntimeException();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if ('81' === mb_substr($hex, 2, 2, '8bit')) { // LENGTH > 128&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $hex = mb_substr($hex, 6, null, '8bit');&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $hex = mb_substr($hex, 4, null, '8bit');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if ('02' !== mb_substr($hex, 0, 2, '8bit')) { // INTEGER&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new \RuntimeException();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $Rl = hexdec(mb_substr($hex, 2, 2, '8bit'));&nbsp; &nbsp; &nbsp; &nbsp; $R = self::retrievePositiveInteger(mb_substr($hex, 4, $Rl * 2, '8bit'));&nbsp; &nbsp; &nbsp; &nbsp; $R = str_pad($R, $partLength, '0', STR_PAD_LEFT);&nbsp; &nbsp; &nbsp; &nbsp; $hex = mb_substr($hex, 4 + $Rl * 2, null, '8bit');&nbsp; &nbsp; &nbsp; &nbsp; if ('02' !== mb_substr($hex, 0, 2, '8bit')) { // INTEGER&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new \RuntimeException();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $Sl = hexdec(mb_substr($hex, 2, 2, '8bit'));&nbsp; &nbsp; &nbsp; &nbsp; $S = self::retrievePositiveInteger(mb_substr($hex, 4, $Sl * 2, '8bit'));&nbsp; &nbsp; &nbsp; &nbsp; $S = str_pad($S, $partLength, '0', STR_PAD_LEFT);&nbsp; &nbsp; &nbsp; &nbsp; return pack('H*', $R.$S);&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @param string $data&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @return string&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private static function preparePositiveInteger(string $data)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (mb_substr($data, 0, 2, '8bit') > '7f') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return '00'.$data;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; while ('00' === mb_substr($data, 0, 2, '8bit') && mb_substr($data, 2, 2, '8bit') <= '7f') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $data = mb_substr($data, 2, null, '8bit');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return $data;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @param string $data&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @return string&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private static function retrievePositiveInteger(string $data)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; while ('00' === mb_substr($data, 0, 2, '8bit') && mb_substr($data, 2, 2, '8bit') > '7f') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $data = mb_substr($data, 2, null, '8bit');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return $data;&nbsp; &nbsp; }另一点是应该为 open_ssl_sign 函数提供一个 .pem 密钥。从 Apple 开发人员下载的 .p8 密钥开始,我使用 openSSL 创建了 .pem 密钥:openssl pkcs8 -in AuthKey_KEY_ID.p8 -nocrypt -out AuthKey_KEY_ID.pem下面是我的新generateJWT函数代码,它使用 .pem 密钥和 fromDER 函数来转换 openSSL 生成的签名:&nbsp; &nbsp; function generateJWT($kid, $iss, $sub) {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $header = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'alg' => 'ES256',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'kid' => $kid&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; &nbsp; $body = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'iss' => $iss,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'iat' => time(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'exp' => time() + 3600,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'aud' => 'https://appleid.apple.com',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'sub' => $sub&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; &nbsp; $privKey = openssl_pkey_get_private(file_get_contents('AuthKey_.pem'));&nbsp; &nbsp; &nbsp; &nbsp; if (!$privKey){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $payload = $this->encode(json_encode($header)).'.'.$this->encode(json_encode($body));&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $signature = '';&nbsp; &nbsp; &nbsp; &nbsp; $success = openssl_sign($payload, $signature, $privKey, OPENSSL_ALGO_SHA256);&nbsp; &nbsp; &nbsp; &nbsp; if (!$success) return false;&nbsp; &nbsp; &nbsp; &nbsp; $raw_signature = $this->fromDER($signature, 64);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return $payload.'.'.$this->encode($raw_signature);&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP