如何验证“使用Apple登录”中的代码?

2022-08-30 17:53:47

我正在尝试验证从重定向 Uri 上的“使用 Apple 登录”服务获得的代码。我使用文档中的信息来创建帖子数据并生成“client_secret”。

我得到的回应是:.{"error":"invalid_client"}

我生成“client_secret”的函数可以在下面找到:

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

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
    ];

    $privKey = openssl_pkey_get_private($key);
    if (!$privKey) return false;

    $payload = encode(json_encode($header)).'.'.encode(json_encode($body));
    $signature = '';
    $success = openssl_sign($payloads, $signature, $privKey, OPENSSL_ALGO_SHA256);
    if (!$success) return false;

    return $payload.'.'.encode($signature);
}

我在此示例中的变量:

$kid是我的私钥的标识符。在这个例子中,它是JYJ5GS7N9K。我从这里获得了标识符 https://developer.apple.com/account/resources/authkeys/list

$iss是我开发人员帐户中的团队标识符。在此示例中,它是 WGL33ABCD6。

$sub与“client_id”的值相同。在这个例子中,我的“client_id”是“dev.hanashi.sign-in-with-apple”。我从此处的应用标识符中获取了客户端 ID:https://developer.apple.com/account/resources/identifiers/list

$key是我通过开发人员帐户生成的私钥。密钥的格式如下:

-----BEGIN PRIVATE KEY-----
myrandomgeneratedkeybyappledeveloperaccount
-----END PRIVATE KEY-----

这是发出请求的php代码:

$key = <<<EOD
-----BEGIN PRIVATE KEY-----
myrandomgeneratedkeybyappledeveloperaccount
-----END PRIVATE KEY-----
EOD; // replaced with correct key

$kid = 'JYJ5GS7N9K'; // identifier for private key
$iss = 'WGL33ABCD6'; // team identifier
$sub = 'dev.hanashi.sign-in-with-apple'; // my app id

$jwt = generateJWT($kid, $iss, $sub, $key);

$data = [
    'client_id' => $sub,
    'client_secret' => $jwt,
    'code' => $_POST['code'],
    'grant_type' => 'authorization_code',
    'request_uri' => 'https://myurl.tld/redirect.php'
];
$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, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6');

$serverOutput = curl_exec($ch);

curl_close ($ch);
echo $serverOutput;

我现在从苹果服务器得到响应。我做错了什么?可能是我生成了错误的 JWT 令牌?{"error":"invalid_client"}


答案 1

对我来说,问题是我忘记在Apple开发门户的“服务ID”部分下验证我的域。

您需要下载他们为您提供的密钥,并将其上传到:https://example.com/.well-known/apple-developer-domain-association.txt

该网站不会自动验证,您必须单击验证按钮并在域旁边打勾以确保。在此之后,我不再有问题。invalid_client

enter image description here

更新 2020.07

更改流程后,您只需将域和通信电子邮件添加到:

Certificates, Identifiers & Profiles > More > Configure


答案 2

我多次遇到此错误。以下是我可以找到的原因:

  • 域名验证不正确和无效redirect_uri
  • 客户端 ID 不正确:您的客户端 ID 可能不正确。
  • JWT编码器工作不正常:也许它不支持ES256算法?
  • 请求类型:对于 /auth/authorize,您需要使用 x-www-form-urlencode,否则会出现错误。invalid_client

当我解决这些问题时,我开始出错。以下是我一直在做的步骤:invalid_grant

  • 我通过JWT创建了client_secret
  • 我在 Web 浏览器上手动导航到,https://appleid.apple.com/auth/authorize?response_type=code&state=abcdefg&client_id=com.company.apple-sign-in-abcd&scope=openid&redirect_uri=https://app.com/redirect_uri
  • 授权
  • 它重定向回后端URL(它给出了404,因为它尚未部署)。
  • 我在网址栏中复制了代码参数,当时
  • 复制后,我用 x-www-form-urlencoded 参数 POSTe 了端点:codehttps://appleid.apple.com/auth/token
    • 法典
    • client_id
    • client_secret
    • redirect_uri
    • grant_type=“authorization_code”

如果您丢失几秒钟,则会失效,并且会出现错误。如果您在第二秒内立即复制并粘贴,您将收到回复:codeinvalid_grant

{
    "access_token": "abcdefg",
    "token_type": "Bearer",
    "expires_in": 3600,
    "refresh_token": "abcdefg",
    "id_token": "abcdefghijklmnopqrstu"
}

下一步是使用Apple的公钥解码id_token。


推荐