原文链接

作者 | Janak Amarasena

翻译 | 知识小集

来源 | medium

在 WWDC19 大会上,苹果公司推出了一项有意思的内容,即 “Sign In with Apple”。这项由苹果提供的认证服务,可以让开发者允许用户使用 Apple Id 来登录他们的应用程序。

通过浏览 Apple 官方文档[1] 来配置这项功能似乎是一项繁琐的事。所以在这里我将快速指导您完成一些基本的设置:)

登录 Apple 开发者账号。

我们需要获得具有 Sign In with Apple 功能的 App Id。

• 进入 Certificates, Identifiers & Profiles > Identifiers,然后单击 Identifiers 旁边左上角的 + 号;

• 选择 App IDs 并点击继续;

• 在此处输入任意 DescriptionBundle ID(Apple建议使用反向域名样式字符串,如:com.domainname.appname)。向下滚动 Capabilities 项,并确保勾选 Sign In with Apple。 最后,单击“继续”,在下一页中验证详细信息,然后单击 Register

现在我们需要获取一个 Services ID。当您调用 API 来验证用户身份时,这个值也将充当 cliend_id

• 再次进到 Certificates, Identifiers & Profiles > Identifiers,然后单击 Identifiers 旁边左上角的 + 号。

• 这次选择 Services IDs 并点击继续。

• 在此处输入任意 DescriptionBundle ID(Apple建议使用反向域名样式字符串,如:com.domainname.appname)。务必勾选 Sign In with Apple。在这里,您必须单击 “Sign In with Apple” 旁边的 Configure 按钮。

• 单击上一步中的 Configure 按钮将显示一个 Web Authentication Configuration 面板。确保我们之前获得的 App ID 被选为 Primary App ID。接下来,您将必须添加将使用此服务的 Web Domain(不过我没有在我的域上验证 Sign In with Apple 功能,如果可以,最好验证一下)。我使用的是 example-app.com。最后,添加 Return URLs(您可以添加多个),这将是用户在使用 Sign In with Apple 进行身份验证后重定向用户的有效URL(出于快速测试目的,我使用了 example-app.com/redirect)。单…

• 单击 Continue,然后在下一页中验证详细信息并单击 Register

现在我们需要创建一个密钥,用于获取我们的 client_secret,这也是从 Apple 发出令牌请求所必需的。

• 进入 Certificates, Identifiers & Profiles > Keys,然后单击 Keys 旁边左上角的 + 号。

• 提供密钥名称并确保勾选 Sign In with Apple。在这里,我们还必须单击 Configure。在接下来出现的 Configure Key 面板中,选择我们之前在 Choose a Primary App ID 下使用的 App ID,然后单击“保存”。

• 单击 Continue,然后在下一页中验证详细信息并单击 Register

• 下载密钥并将其保存在安全的地方,因为您永远无法再次下载密钥。下载密钥后单击 Done

嗯,这基本上就是所有配置。

我们已经拥有了 client_id,现在我们需要再调用一次 API; 我们将使用刚刚下载的私钥创建的 client_secret

客户端密钥必须是 JWT,根据 Apple 文档[2],我们需要使用带有 P-256 曲线和 SHA-256 哈希算法的椭圆曲线数字签名算法(ECDSA)来加密令牌。完成这项工作的一个简单方法是使用 ruby-jwt[3]。首先检查你是否已经有 Ruby 设置,如果没有,你可以从这里[4]获得它。

以下是我们需要在 JWT 中包含的细节。

--Header--
alg - The encryption algorithm used to encrypt the token. This will be ES256.
kid - The 10 charachter Key ID of the private key you create. You can get it from
Certificates, Identifiers & Profiles > Keys > (click on the key you created).
--Payload--
iss - 10 character Team ID give to you. You can find it here https://developer.apple.com/account/#/membership
iat - Indicates the time at which the token was generated, in terms of the number of seconds since Epoch, in UTC.
exp - Indicates the expiry time of the token expiration, in terms of the number of seconds since Epoch, in UTC. Accroding to the docs the value must not be greater than 15777000 (6 months in seconds) from the Current Unix Time on the server.
aud - The value of which identifies the recipient the JWT is intended for. Since this token is meant for Apple, use https://appleid.apple.com.
sub - The value of which identifies the principal that is the subject of the JWT. Use the same value as client_id as this token is meant for your application.
复制代码

让我们来获取 client_secret

设置 Ruby 后运行命令 sudo gem install jwt 来设置 ruby-jwt

添加必要的详细信息并将以下内容保存为 secret_gen.rb

require "jwt"key_file = "Path to the private key"
team_id = "Your Team ID"
client_id = "The Service ID of the service you created"
key_id = "The Key ID of the private key"
validity_period = 180 # In days. Max 180 (6 months) according to Apple docs.private_key = OpenSSL::PKey::EC.new IO.read key_filetoken = JWT.encode({iss: team_id,iat: Time.now.to_i,exp: Time.now.to_i + 86400 * validity_period,aud: "https://appleid.apple.com",sub: client_id},private_key,"ES256",header_fields={kid: key_id }
)
puts token
复制代码

您可以使用命令 ruby secret_gen.rb 从终端运行 secret_gen.rb 文件,它将为您提供 client_secret

好的......现在我们准备来测试 Sign In with Apple 了:)

添加你的 redirect_uri(应该是我们之前配置的 Return URL)和 client_id 并将其粘贴到浏览器中并按 Enter 键。

https://appleid.apple.com/auth/authorize?response_type=code&redirect_uri=`<redirect_uri>`&client_id=`<client_id>`
复制代码

系统将提示您进行身份验证(我必须为我的 Apple ID 启用双因素身份验证才能继续)。最后,您将被重定向到 redirect_uri 并最终获得 code

在使用上面代码执行所获得的 code,之前使用的 redirect_uriclient_id 以及通过运行 secret_gen.rb 获得的 client_secret 替换以下命令的内容,并在终端中运行以下 cURL 命令。

curl -X POST https://appleid.apple.com/auth/token -d 'grant_type=authorization_code&code=`<code>`&redirect_uri=`<redirect_uri>`&client_id=`<client_id>`&client_secret=`<client_secret>`'
复制代码

运行上述内容后,您应该最终获得访问令牌和 ID 令牌。

如果您对 Sign In with Apple 流程感到疑惑,则可以查看 OIDC Authorization Code flow[5]

如果您有兴趣了解更多关于 Sign In with Apple 的信息,请加入Apple Sign In: A Zero-Code Integration Approach Using WSO2 Identity Server[6]

参考

[1]https://developer.apple.com/sign-in-with-apple/
[2]https://developer.apple.com/documentation/signinwithapplerestapi/generate_and_validate_tokens
[3]https://github.com/jwt/ruby-jwt
[4]https://www.ruby-lang.org/en/downloads/
[5]https://openid.net/specs/openid-connect-core-1_0.html#CodeFlowAuth
[6]https://wso2.com/library/webinars/2019/07/apple-sign-in-a-zero-code-integration-approach-using-wso2-identity-server/

关注我们

欢迎关注我们的公众号:知识小集(ID: zsxjtip),也欢迎加入我们的群组讨论问题。可以加微信 coldlight_hh/wsy9871 进入我们的 iOS/flutter 微信群。

快速配置 Sign In with Apple相关推荐

  1. iOS 苹果授权登录(Sign in with Apple)系列之Apple Developer配置篇

    原文 在 iOS13 中,如果 App 提供第三方登录,就必须添加 苹果登录 Sign in with Apple 选项,并要求所有开发者于 2020年4月之前 完成现有应用的更新,否则审核不给通过. ...

  2. Sign in with apple 功能配置好后,测试时显示“未完成注册”,是何原因?

    Sign in with apple 功能配置好后,测试时显示"未完成注册",是何原因? 检查了苹果后台,协议之类都正确,证书也都勾选了sign in with apple功能(不 ...

  3. Sign In with Apple - 使用苹果账号登录你的应用

    编辑:老峰,作者:KANGZUBIN 来源:小专栏<WWDC19 内参> 苹果在 9 月 12 号更新了审核指南,加入 4.8 Sign in with Apple 一条,要求所有使用 第 ...

  4. Apple Sign in with Apple(苹果授权登录PHP)

    Apple Sign in with Apple(苹果授权登录PHP) 文章目录 Apple Sign in with Apple(苹果授权登录PHP) 一.登录Apple Developer 二.创 ...

  5. sign in with Apple,使用Apple授权登录

    软件环境:Xcode 11.4.iOS13+ 创建时间:2020年 03月15号 更新时间:2021年 03月02号 这篇文章都说了什么 使用Apple登录的注意事项 接入原理概览 客户端编码 审核规 ...

  6. Sign In With Apple

    在最新的审核指南中,出现了关于Sign In With Apple 的要求: 4.8 Sign in with Apple Apps that use a third-party or social ...

  7. iOS 苹果授权登录(Sign in with Apple)

    在 iOS13 中,如果 App 提供第三方登录,就必须添加 苹果登录 Sign in with Apple 选项,并要求所有开发者于 2020年4月之前 完成现有应用的更新,否则审核不给通过. iO ...

  8. [SDK]Unity接入Sign in with Apple

    iOS13之后引入了Sign in with Apple,Sign In with Apple是跨平台的,可以支持iOS.macOS.watchOS.tvOS.JS.对iOS上的App而言,若引入第三 ...

  9. Sign in with Apple REST API / Revoke tokens (JAVA)

    Sign in with Apple REST API / Revoke tokens (JAVA) 前言 由于Apple政策, Apple ID登录的用户注销时需要进行Revoke Token. 顺 ...

最新文章

  1. 海啸级后浪!“天才少年”曹原再次连发2篇Nature!
  2. ipp 实现图像空间的转换
  3. 调用ajax_[WEB篇]-AJAX-02-AJAX应用案例
  4. mysql 允许其他主机访问权限_允许其他主机访问本机MySQL
  5. 工作189:配置表头即可
  6. com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused
  7. atitit..代码生成流程图 流程图绘制解决方案 java  c#.net  php v2
  8. 前端传入JSON数组转换对象存入数据库
  9. java高级学习视频下载
  10. python 导入包的路径顺序
  11. Tensorflow之搭建神经网络八股
  12. 【计算机网络】集线器、网桥、交换机、路由器、网关大解析
  13. 王者荣耀8月15日服务器维护,王者荣耀:8月15日更新,10件装备调整,依旧互秒荣耀...
  14. JavaWeb程序设计课后答案
  15. Visual Studio 2019 下Python的开发环境搭建
  16. 2021-01-3 VBA利用企业邮箱自动发送邮件
  17. 计算机快捷方式后缀名,电脑快捷键-文件扩展名详解.doc
  18. 数据库中索引的填充因子
  19. JAVA int类型 获取高低位
  20. 为什么祖传代码被称为“屎山”?

热门文章

  1. Python学习整理(之一)
  2. JavaScript之实现文件上传与下载
  3. 采购管理主要流程有哪些?
  4. react-mobx基础
  5. 计算机研究生怎么研究黑洞,有个计算机黑洞一般的爸妈是怎样一种体验
  6. 世界上为什么有那么多的不幸的人
  7. 最难忘的新年祝福,第一个让大家都惊喜的小程序(有趣、恶搞、好玩)
  8. 文本分类(一) | (9) 项目组织结构
  9. 创建SpringMVC项目的基本步骤
  10. Zynq 【SDK裸机开发之PS】——串口接收缓存