jer*_*710 5 php oauth-2.0 jwt apple-sign-in
我很难弄清楚如何使用Apple登录。文档非常糟糕,失败的响应使我们毫无头绪。亚伦·帕雷基(Aaron Parecki )的文章(https://developer.okta.com/blog/2019/06/04/what-the-heck-is-sign-in-with-apple)确实有所帮助,但我现在似乎卡住了。
首先,我使用/ auth / authorize生成一个登录URL,如下所示:
$_SESSION['state'] = bin2hex(openssl_random_pseudo_bytes(16));
return 'https://appleid.apple.com/auth/authorize?' . http_build_query([
'response_type' => 'code',
'response_mode' => 'form_post',
'client_id' => 'com.my.service.id',
'redirect_uri' => 'https://my.app/redirect'),
'state' => $_SESSION['state'],
'scope' => 'name email',
]);
Run Code Online (Sandbox Code Playgroud)
在尝试了域验证和返回URL后,这使我进入Apple登录页面,并在成功登录后返回到我的redirect_uri。然后,我需要授权使用Guzzle执行的令牌:
$response = (new Client)->request('POST', 'https://appleid.apple.com/auth/token', [
RequestOptions::FORM_PARAMS => [
'grant_type' => 'authorization_code',
'code' => $_POST['code'],
'redirect_uri' => 'https://my.app/redirect',
'client_id' => 'com.my.service.id',
'client_secret' => $this->getClientSecret(),
],
RequestOptions::HEADERS => [
'Accept' => 'application/json'
]
]);
return json_decode($response, true);
Run Code Online (Sandbox Code Playgroud)
客户端机密是使用Firebase php-jwt(https://github.com/firebase/php-jwt)生成的,并且通过jwt.io有效:
$key = openssl_pkey_get_private('file://certificate.p8');
return JWT::encode([
'iss' => 'APPLETEAMID',
'iat' => time(),
'exp' => time() + 3600,
'aud' => 'https://appleid.apple.com',
'sub' => 'com.my.service.id',
], $key, 'ES256', 'certificate-id');
Run Code Online (Sandbox Code Playgroud)
但是,向苹果执行令牌请求将返回400错误,并显示消息“ invalid_client”。我无法弄清楚我的client-id / secret是否错误或重定向中的代码无效。有人可以指出我正确的方向吗?
编辑:请注意,我重写了JWT类,允许使用ES256。有关更多信息,请检查此打开的拉取请求。
Cun*_*ing -4
根据你所说的以及苹果公司在其文档中所说的内容,我认为你误认为苹果公司Identity token的Client Secret.
Apple 的Identity Token令牌可以JWT唯一标识用户,并且是现有 OAuth 2.0 框架“之上”的安全层。
当您在授权服务器中注册/验证客户端时,根据RFC 6749 - OAuth 2.0 授权框架,您很可能会从授权服务器收到一个密钥,该密钥是您的客户端的密钥(即您的服务器的密码com.my.service.id)可用于从获取令牌到使用身份检索资源的每个请求。建议使用该client_id/client_secret对的方法是HTTP Basic Authenticationmethod,并且不鼓励使用 POST 参数。
授权服务器必须支持 HTTP Basic 身份验证方案,用于对获得客户端密码的客户端进行身份验证。
...
不建议使用两个参数在请求正文中包含客户端凭据,并且应仅限于无法直接使用 HTTP 基本身份验证方案(或其他基于密码的 HTTP 身份验证方案)的客户端。参数只能在请求主体中传输,并且不得包含在请求 URI 中。
Identity Token您将以JWT 令牌的形式收到用户的令牌,并且您应该准确提供该令牌且不得篡改,因为该令牌是由 Apple 自己颁发的。但是,您可以检查有效负载(请参阅jwt.io 上的 JWT 简介)以了解 的有效性 ( iat + exp) Identity Token。Identity Token除非您正在为 Apple 授权团队编码,否则不应生成。
最后,我非常强调要彻底阅读 OAuth 规范,并记住这Identity Token是 Apple 向您发送用户 ID 的方式,但 client_id 和 client_secret 是 OAuth 规范中定义的内容。
| 归档时间: |
|
| 查看次数: |
594 次 |
| 最近记录: |