通过应用程序和网站使用 Sign In with Apple 时,我能否拥有相同的受众声明价值?

Can I have the same audience claim value when using Sign In with Apple via an app and website?

我已经在我的 iOS 应用程序中实现了 Sign In with Apple,现在我也在我的网站中实现了它。令我惊讶的是,这两个流最终在 id 令牌中具有不同的受众价值,这是可以预料的吗? iOS 应用程序使用应用程序的包 ID,网络流使用服务 ID 的标识符。

例如,在 iOS 流中,观众是 com.domain.app,而在网络流中,观众是 com.domain.siwa。当我将 id 令牌发送到我执行验证逻辑的服务器时,我现在必须知道 id 令牌的来源,以便我可以使用正确的受众。如果两个流程的受众相同,那就更容易了。那可能吗?还是我应该忘记这一点并使用两个不同的受众价值?

ID 令牌

ID 令牌的受众代表客户端应用程序,因此您将有 2 个不同的受众值。 ID 令牌通常由客户端应用程序验证,例如在移动代码中,但也可以使用 API 来完成工作。

访问令牌

这是 OAuth 标准行为:

  • 不要在 API 请求中将 ID 令牌作为凭据发送 - 请改为发送访问令牌。访问令牌具有指定一个 API 或一组相关 API 的受众,例如 api.mycompany.com.

  • 发布您自己的访问令牌,您可以在其中控制您的 API 需要授权的范围和声明 - Apple 令牌并非设计用于您自己的 APIs

授权服务器 (AS)

发行您自己的令牌的最佳方式是让 AS 为您管理与 Apple 的连接 - 如 this tutorial

摘要

希望这能跨越关键点以备不时之需。旨在将解决方案建立在经过专家多年审查的标准设计模式之上。有时供应商特定指南不能很好地解释这些原则。

观众 aud 对您的 JWT 令牌的声明不同的原因是,正如 Apple 自己明确指出的那样,the audience registered claim identifies the recipient for which the identity token is intended

简而言之,由于受众在字面上是不同的,aud 声明应该并且将永远是不同的

这对 Apple 来说不是独一无二的,实际上取自 RFC7519: JSON Web Token,因此试图使 aud 声明值相同实际上是反对 JWT 规范。

这是 RFC7519 对其的描述:

The "aud" (audience) claim identifies the recipients that the JWT is intended for. Each principal intended to process the JWT MUST identify itself with a value in the audience claim. If the principal processing the claim does not identify itself with a value in the "aud" claim when this claim is present, then the JWT MUST be rejected. In the general case, the "aud" value is an array of case- sensitive strings, each containing a StringOrURI value. In the special case when the JWT has one audience, the "aud" value MAY be a single case-sensitive string containing a StringOrURI value. The interpretation of audience values is generally application specific. Use of this claim is OPTIONAL.

如果需要,我会说 只处理不同的受众价值 但是你使用 aud 声明价值的目的是什么?

如果您试图限制特定的功能,例如,基于特定的“受众”,请随意使用它,但是对业务逻辑的这种声明的使用并不是那么普遍。

其他声明很可能更适合,具体取决于您在此要实现的目标。