ios - 苹果登录 : How to use it for custom server endpoint authentication?

标签 ios server oauth apple-sign-in

我的用例是,一旦我有用户登录到我的应用程序,当我从我的应用程序到我的自定义服务器进行端点调用时,我使用登录产生的 Oauth token 来验证调用者。例如,我使用 Google Sign In这样。
此方法(例如,使用 Google 登录)具有几个有用的属性:

  • 更新的 token 会在客户端应用程序上自动创建。
  • 我的自定义服务器可以使用 Google 的端点轻松验证 token 的有效性。
  • 初始 token 验证可以在端点请求处理的早期进行——无需访问自定义服务器数据库(如 https://github.com/IBM-Swift/Kitura-Credentials 中的样式)。

  • 我的问题是:鉴于我们被告知必须将 Apple 登录合并到我们的 iOS 应用程序中(如果我们提供通用登录设施),我如何使用我的自定义服务器进行端点身份验证?
    我看到了两种选择,我都不太喜欢。
    首先,我可以让我的客户端应用程序向我的服务器发送 Apple 登录 id_token 并忽略 exp(到期)字段。我可以定期重新生成 id_token(显然是 no more than once a day )并将其发送回我的客户。我不喜欢这个想法,既因为忽略了 token 的到期,也因为需要定期将 token 从服务器发送到客户端。 (我的应用程序使用多个登录系统,这只会造成额外的困难)。
    二、I could have my client send an Apple Sign In refresh token to my server .当然,我的服务器需要最初生成该刷新 token 并将其发送回客户端。我比第一个想法更喜欢这个想法。我在自定义服务器中的初始 token 验证将需要访问其数据库以查找与此 token 匹配的。我通常不能使用 Apple 端点——因为,Apple 显然会再次限制这种验证。
    此外,我不太喜欢我的自定义服务器最多只能每天检查一次 token 有效性的想法。如果用户撤销应用程序的凭据,我希望我的自定义服务器能够相对较快地停止代表用户操作。
    想法?

    2019 年 10 月 5 日——更新到上面的第一个替代方案。实际使用https://developer.apple.com/documentation/signinwithapplerestapi/generate_and_validate_tokens对于刷新 token 验证,我发现它实际上并没有生成更新的 id token 。它正在生成一个访问 token (但 Apple 没有定义它的用途),并正在验证刷新 token 。因此,无法将更新的 id token 发送到客户端 iOS 应用程序。因此,使用第一种替代方法,不能使用 id token 的到期日期。
    2019 年 10 月 10 日——更新:我写了一篇关于这个主题的博客文章——https://medium.com/@crspybits/apple-sign-in-custom-servers-and-an-expiry-conundrum-d1ad63223870
    20 年 8 月 6 日-- 更新:关注博客文章以及可能的前进路径,来自 Apple 的待定详细信息:https://medium.com/@crspybits/part-ii-apple-sign-in-custom-servers-and-an-expiry-conundrum-b3e9735dc079

    最佳答案

    Get the most out of Sign in with Apple in WWDC 2020 ,在 11:30 在他们的演示中,他们引入了服务器到服务器的通知,以使您的服务器能够实时监控用户帐户状态的变化。
    到目前为止,这方面的细节很少。
    ----------------- 更新 (12/23/20) -----------------
    我现在有这些服务器到服务器的通知在我的服务器的测试环境中工作。一些注意事项:

  • 我决定在我的服务器上使用端点,以允许 Apple 向我的服务器发送这些 REST 端点请求。
  • 我将其粘贴到 developer.apple.com > Account > Certificates, Identifiers & Profiles > Identifiers > Select your app identifier > 单击“Sign In with Apple”旁边的“Edit” > Server to Server Notification Endpoint
  • 这个端点实际上是未经授权的。例如,它是由 Apple 制作的,没有 OAuth 凭据访问您的服务器。如何设置将取决于您的服务器。我有办法为未经授权的服务器设置新的端点/路由。
  • 我将客户端和服务器的其他部分设置为允许使用 Apple 登录创建帐户。因此,使用其中一个帐户,我现在开始采取行动,导致 Apple 在我的服务器上调用他们的服务器到服务器通知端点。我想对苹果提出的端点请求的细节进行逆向工程,因为细节很少。
    这提供了一些关于如何导致通知事件发生的想法:
    How to revoke Sign in with Apple credentials for a specific app?
    您可以撤销凭据,但启用和禁用电子邮件中继更容易(因为您可以重复执行此操作)。当然,要做到这一点,您必须首先使用私有(private)/电子邮件中继登录 Apple。
  • 接下来我学到了两件事:

  • a) 在您采取行动(例如,撤销电子邮件中继)后,大约 30 秒内会在您的服务器上访问服务器到服务器通知端点。我已将各种日志输出添加到我的服务器中,因此可以查看我的服务器日志并看到这种情况发生。
    b) Apple 向您的服务器发出的端点请求具有包含以下格式的 JSON 的正文数据:{"payload" : "-- SNIP -- JWT"}我正在使用以下 Swift 结构对其进行解码。
        struct ApplePayload: Decodable {
            let payload: String // JWT
        }
    
  • 正如苹果在 WWDC 2020 视频 (https://developer.apple.com/videos/play/wwdc2020/10173/) 中指出的那样,body 数据的主要内容是一个 JWT。上面,这是 JSON 中键“payload”的值。
  • 下一步是解码这个 JWT。我只是猜测它会在 Apple Sign In 服务器端进程的其他部分使用与 JWT 相同的解码机制。具体来说,在解码客户端使用 Apple 登录传递给您的服务器的身份 token (JWT)时。请参阅 https://developer.apple.com/documentation/sign_in_with_apple/sign_in_with_apple_rest_api/authenticating_users_with_sign_in_with_apple
    我有一些代码可以进行 JWT 解码,所以我将其分解并放在一个共同的地方:
    https://github.com/SyncServerII/AppleJWTDecoder.git
    将其集成到我对 Apple 服务器到服务器通知请求的服务器端处理中,我发现确实可以以这种方式解码此 JWT。
  • 另一个显而易见的方面是,Apple 在 WWDC 2020 视频中指出的结构在解码后并不是 100% 存在于 JWT 中的结构。具体来说,到目前为止,在我的测试中,至少 events field 不是数组,而是只有一个值。见 https://github.com/SyncServerII/AppleJWTDecoder/blob/main/Sources/AppleJWTDecoder/AppleSignInClaims.swift对于 Swift 结构。

  • 我现在成功解析了 JWT。我服务器上的下一个主要步骤是实际利用服务器中的不同事件类型来采取行动。对我来说,这将涉及两个帐户(不是电子邮件)相关的操作:

    User decided to stop using their Apple Id with your application. And should be treated as a sign-out by the user. E.g., when a user decides to disconnect your application from Settings. (From https://developer.apple.com/videos/play/wwdc2020/10173/)

    Also considered a request from user to "delete their app account" (broader context: "Server to Server Notification Endpoint Sign in with Apple server to server notifications allow you to receive important updates about your users and their accounts. Notifications are sent for each app group when users change mail forwarding preferences, delete their app account, or permanently delete their Apple ID. Each group of apps can have one URL, which must be absolute and include the scheme, host, and path. TLS 1.2 or higher is required to receive notifications. Learn more.") To see these docs, go to: developer.apple.com > Account > Certificates, Identifiers & Profiles > Identifiers > Select your app identifier > Click 'Edit' next to 'Sign In with Apple' > Server to Server Notification Endpoint

    case consentRevoked = "consent-revoked"
    
        
    

    User has asked Apple to delete their Apple Id. The user identifier will now no longer be valid.

    case accountDelete = "account-delete"
    
    我的计划是将这两个事件视为等效 - 并删除我服务器上的用户帐户。然后,我将不得不考虑如何将其传达给我的客户(iOS 应用程序)。它需要知道用户已经删除了他们的帐户。

    关于ios - 苹果登录 : How to use it for custom server endpoint authentication?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58178187/

    相关文章:

    node.js - passport-google-oauth 无法注销用户

    ios - 泛型类型约束与继承

    c - 如何为本地主机服务器同时打开 1000 个客户端进程?

    php - 是否有一种使用 PHP 发布到 Twitter 而无需管理用户登录或获取推文的简单方法?

    iphone - 如何使用 SA_OAuthTwitterEngine 在没有 PIN 的情况下从 iPhone 发布推文?

    node.js - 团结 : Live video streaming

    ios - Twitter 分享无法在设备上运行

    ios - 如何在iOS中从TableView高度的底部到顶部插入动画的TableView行

    iOS 模拟器无法正确刷新

    mysql - 使Linux上的MYSQL可以被远程访问