快速配置 Sign In with Apple

8,812 阅读5分钟

原文链接

作者 | 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_file

token = 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 微信群。