关于苹果授权,官方文档写的不仔细,但还是要看一下 https://developer.apple.com/sign-in-with-apple/get-started/
后端验证苹果授权用户正确性有两种方式,如下:
- 利用 identity_token 与 user_id 进行校验,匹配成功,即证明 user_id 是正确的唯一标识。
- 利用授权 code,加生成 client_secret,进行授权验证。
一、验证 identity_token 与 user_id (建议使用,方便)
- 安装PHP扩展包(支持php5.6及以上):composer require wubuwei/php-apple-signin
- 使用方式见:https://github.com/wubuwei/php-apple-signin
相比于fork的仓库:增加了抛错文件;修复了苹果验证链接返回参数无序导致的偶尔验证失败的情况。 - 参考链接:
https://developer.apple.com/documentation/sign_in_with_apple
https://blog.csdn.net/u013862108/article/details/102890808
二、验证授权 code
- 生成 client_sercret,用于解密参数,暂时使用 ruby 的文件进行生成,有兴趣可以用PHP 实现一套,下面操作前请先了解这篇:https://developer.okta.com/blog/2019/06/04/what-the-heck-is-sign-in-with-apple#create-a-private-key-for-client-authentication
a. mac 电脑安装 ruby 环境+ ruby-jwt 扩展:brew install ruby 、sudo gem install jwt
b. 配置 client-secret.rb 参数。
c. 执行命令:ruby client-secret.rb 文件 生成 token,即后端解密用的 client_sercret
- 如何解密 code:
https://github.com/aaronpk/sign-in-with-apple-example
此篇可能是网上最广泛的教程,index.php 内的方法可以看到解密 code 后的参数
总结一下
后端验证的目的就是为了保证客户端授权后传给后端的 user_id 为正确的 Apple Id 授权后的唯一标识,然后就可以用这个唯一标识来做账户关联等一系列操作,类似于微信授权后的 open_id。