跳到主内容

添加 Sign in with Apple

本指南介绍如何让用户使用其 Apple ID 登录 Casdoor。


学习目标​

  • 在 Apple 处配置 App ID、Services ID 和密钥。
  • 在 Casdoor 中将 Apple 添加为 OAuth 提供商。

所需条件​

  • 拥有 Apple Developer Program 会员资格的 Apple Developer 账户
  • Casdoor 管理控制台的访问权限(管理员)

配置 Apple​

  1. 创建一个 App ID,或打开现有的 App ID,并为其启用 Sign in with Apple。

    Sign in with Apple capability of the App ID

  2. 创建类型为 Services IDs 的标识符。该标识符将成为 Casdoor 中的 Client ID。

    Services ID registration

  3. 打开 Services ID,启用 Sign in with Apple,然后单击 Configure。

    Services ID configuration

  4. 在 Return URLs 中,输入 Casdoor 上 Apple 提供商页面显示的回调 URL,例如 https://your-casdoor-domain.com/callback。这两个值必须完全匹配。

    Return URLs of the Services ID

  5. 创建一个密钥,为其启用 Sign in with Apple,并将其与你的 App ID 关联。

    Key configuration

  6. 注册该密钥。记下 Key ID 并立即下载 .p8 文件。Apple 仅允许下载一次该文件,因此请妥善保管。

    Key ID and download of the key file

  7. 在 Apple Developer 门户的 Membership 页面上,记下你的 Team ID。

在 Casdoor 中添加提供商​

  1. 在 Casdoor 管理控制台中,前往 Identity > Providers 并添加一个提供商。

  2. 将 Category 设置为 OAuth,将 Type 设置为 Apple。

  3. 填写字段:

    FieldValue
    Client IDThe identifier of the Services ID
    Team IDYour Apple Team ID
    Key IDThe ID of the key
    Key textThe complete content of the .p8 file, including the -----BEGIN and -----END lines
  4. 检查 Casdoor 显示的回调 URL 是否包含在 Services ID 的 Return URLs 中。

    Apple provider in Casdoor

  5. 保存提供商.

用户信息​

Casdoor 从 Apple 签发的 ID 令牌中读取用户 ID 和电子邮件地址。

Apple 仅在用户首次授权时发送一次名字和姓氏:它将其放在发布到 Casdoor /api/callback 端点的表单的 user 字段中。Casdoor 将该名字保留在名为 casdoor_apple_user 的 HttpOnly cookie 中 5 分钟,并将其用作所创建用户的显示名称。对于现有用户,仅当显示名称为空时才会获取该名字。

  • 显示名称格式为 <first-name> <last-name>。如果任一部分是中文,则格式为 <last-name><first-name>,中间无空格。如果只存在其中一部分,Casdoor 将使用该部分。
  • 由于 Apple 不对名字进行签名,Casdoor 仅将其用于显示名称。用户名和电子邮件地址始终来自 ID 令牌。
  • 在后续登录时,Apple 不会发送名字。若要再次发送,用户需在 Apple ID 设置的 Sign in with Apple 下移除该应用,然后重新登录。

下一步​

将提供商添加到应用。请参阅将提供商添加到应用。

另请参阅​