# 使用 Apple 登录

**URL:** <https://meta.discourse.org/t/sign-in-with-apple/122790>\
**Category:** Feature\
**Created:** [2019年七月12日 12:38 UTC](https://meta.discourse.org/t/sign-in-with-apple/122790 "2019-07-12T12:38:45Z")\
**Posts on this page:** 1\
**Showing post:** 40

<div class="post-metadata">

**Author:** ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)\
**Post date:** [2020年八月4日 11:14 UTC](https://meta.discourse.org/t/sign-in-with-apple/122790/40 "2020-08-04T11:14:35Z")

</div>

我已经尝试了 [升级我们插件的分支](https://github.com/discourse/discourse-sign-in-with-apple/commit/6766f81f5ec34a6d07325ff1e3b73cfc34d3bb0e) 以使用最新的 omniauth-apple。（注意：除了升级版本号外，还需要进行其他一些更改）。

tl;dr：问题依然存在

我成功地在沙盒环境中通过一些“黑客”手段使其运行，但它仍然存在一些问题：

1. Apple 在回调时使用 POST 请求。这在 OAuth 实现中并不常见，因为这意味着带有 `samesite=Lax` 属性的 Cookie 将不会随请求发送。这导致 Discourse 在回调期间无法读取会话 Cookie，从而引发 CSRF 错误。

2. 在没有 CSRF 令牌的情况下使用 POST 请求还会触发核心中的另一项安全措施。

3. 当 omniauth gem [获取 JWKs](https://github.com/nhosoya/omniauth-apple/blob/master/lib/omniauth/strategies/apple.rb#L78-L80) 时，我收到了来自 Apple 的 403 错误。我怀疑 `Accept:` 头未正确设置，但尚未验证这一点。

经过所有这些操作后，我终于成功使用 Apple 登录了。您可以在 [https://sandbox.dtaylor.uk](https://sandbox.dtaylor.uk) 上尝试（我会让它保持运行几天，但请勿在其中输入任何敏感信息，因为它是 **不安全的** ）。

然后……电子邮件和姓名仍然只在首次认证时包含。您可以尝试一下：使用 Apple 登录，取消账户创建，然后再次尝试。第二次尝试将缺少您的详细信息。

因此，假设 Apple 短期内不会改变现状……我们该如何解决这个问题？

对于问题 (1) 和 (2)，我认为可以将 Apple 的 POST 请求转换为 GET 请求，而不会影响安全性。当我们在回调中收到 POST 请求时，可以渲染一些 JavaScript 代码，将 `window.location` 设置为 `/auth/apple/callback?code=...&state=...`。此后，它将像其他任何提供商一样正常工作。不过，我认为拦截 POST 请求需要对核心 API 进行一些更改。

对于问题 (3)，我认为通过一些在 omniauth gem 中的小改动可能就能解决。

但我们仍然无法获取姓名和电子邮件，所以我不确定是否值得修复这些其他问题 😢

---

_[View the full topic](https://meta.discourse.org/t/sign-in-with-apple/122790)._
