Apple JS 登录:从代码和 id_token 获取用户 ID?
Apple JS sign in: Get user id from code and id_token?
我需要使用 Apple 登录来让用户登录我的网站。所有用户都已经在 iOS 本机应用程序中使用 Apple 注册进行了注册。作为 iOS 注册过程的一部分,Apple 提供了一个用户 ID,这是该特定用户的唯一标识符。我将其保存在我的数据库中以识别用户。
当用户在网站上使用 Apple JS 登录时,如何获得相同的 Apple 用户 ID?
我正在按照此处所述使用 Apple JS https://developer.apple.com/documentation/sign_in_with_apple/sign_in_with_apple_js/configuring_your_webpage_for_sign_in_with_apple 我有 usePopup=false
- 用户单击 Apple 使用 Apple 登录 按钮并通过 Apple 的登录向导。好的
- Apple 向我指定的 redirectURI 发送了一个 POST 请求。好的
- post 请求包含 id_token 和代码。好的
现在,如何获取 Apple 的用户 ID 以识别登录的实际用户?
提供的 id_token 包含解码后的 Apple 用户 ID。 它已使用 JWT 编码。您不需要任何密钥或秘密来对其进行解码。实际上,您可以将 id_token 复制粘贴到此处 https://jwt.io/ 以查看格式及其包含的所有数据。
Apple 用户 ID 是子信息。
// Header
{
"kid": "AIDOPK1",
"alg": "RS256"
}
// Payload
{
"iss": "https://appleid.apple.com",
"aud": [Same as clientId, same as serivce id],
"exp": 1579073561,
"iat": 1579072961,
"sub": [Apple User Identifier],
"c_hash": "Q4ZkNP4SB2f-m9vtLfO0UA",
"email": [EMAIL],
"email_verified": "true",
"is_private_email": "true",
"auth_time": 1579072961
}
关于安全
出于安全原因,您应该在继续使用 user_id 之前验证一些事情(提供并检查状态,检查解码信息)本教程讨论了 https://sarunw.com/posts/sign-in-with-apple-4/.
在Python我安装了pyjwt(https://pypi.org/project/PyJWT/)
import jwt
decoded = jwt.decode(token, audience="<clientid>",options={"verify_signature": False})
# clientid is what you provided in the call to Apple
user_id = decoded["sub"]
clientid 是您在用户点击登录时在调用中提供的内容。这与您在控制台中为您的应用设置的服务 ID 相同。 (如果你到了 id_token 的地步,那么你已经设置好了。)我无法在不提供此信息的情况下找到解码 id-token 的方法,尽管它必须是可能的因为 https://jwt.io/ 网站可以做到。
我需要使用 Apple 登录来让用户登录我的网站。所有用户都已经在 iOS 本机应用程序中使用 Apple 注册进行了注册。作为 iOS 注册过程的一部分,Apple 提供了一个用户 ID,这是该特定用户的唯一标识符。我将其保存在我的数据库中以识别用户。
当用户在网站上使用 Apple JS 登录时,如何获得相同的 Apple 用户 ID?
我正在按照此处所述使用 Apple JS https://developer.apple.com/documentation/sign_in_with_apple/sign_in_with_apple_js/configuring_your_webpage_for_sign_in_with_apple 我有 usePopup=false
- 用户单击 Apple 使用 Apple 登录 按钮并通过 Apple 的登录向导。好的
- Apple 向我指定的 redirectURI 发送了一个 POST 请求。好的
- post 请求包含 id_token 和代码。好的
现在,如何获取 Apple 的用户 ID 以识别登录的实际用户?
提供的 id_token 包含解码后的 Apple 用户 ID。 它已使用 JWT 编码。您不需要任何密钥或秘密来对其进行解码。实际上,您可以将 id_token 复制粘贴到此处 https://jwt.io/ 以查看格式及其包含的所有数据。
Apple 用户 ID 是子信息。
// Header
{
"kid": "AIDOPK1",
"alg": "RS256"
}
// Payload
{
"iss": "https://appleid.apple.com",
"aud": [Same as clientId, same as serivce id],
"exp": 1579073561,
"iat": 1579072961,
"sub": [Apple User Identifier],
"c_hash": "Q4ZkNP4SB2f-m9vtLfO0UA",
"email": [EMAIL],
"email_verified": "true",
"is_private_email": "true",
"auth_time": 1579072961
}
关于安全
出于安全原因,您应该在继续使用 user_id 之前验证一些事情(提供并检查状态,检查解码信息)本教程讨论了 https://sarunw.com/posts/sign-in-with-apple-4/.
在Python我安装了pyjwt(https://pypi.org/project/PyJWT/)
import jwt
decoded = jwt.decode(token, audience="<clientid>",options={"verify_signature": False})
# clientid is what you provided in the call to Apple
user_id = decoded["sub"]
clientid 是您在用户点击登录时在调用中提供的内容。这与您在控制台中为您的应用设置的服务 ID 相同。 (如果你到了 id_token 的地步,那么你已经设置好了。)我无法在不提供此信息的情况下找到解码 id-token 的方法,尽管它必须是可能的因为 https://jwt.io/ 网站可以做到。