代码之家  ›  专栏  ›  技术社区  ›  Au Ris

SFAuthenticationSession如何在Safari中存储与会话相关的cookie

  •  0
  • Au Ris  · 技术社区  · 5 年前

    tl;博士 读最后一段。

    我正在使用AppAuth( https://github.com/openid/AppAuth-iOS )用于处理基于OpenID的用户身份验证的库,我希望通过我的应用程序为其提供SSO体验。我的应用程序的部署目标是iOS 11,这意味着AppAuth在内部使用 SFAuthenticationSession SFAuthenticationSession SFAuthenticationSession 调用完成url(如果成功),从中进行授权 code 密码 通过的令牌POST请求 URLSession 是独立于 access_token 已检索。

    访问令牌 ,但当我离开应用程序并在Safari中打开服务提供商提供的用户配置文件网页时,用户未登录。我用谷歌账户测试了同样的流量( https://accounts.google.com )SSO工作得很好,例如当我打开 https://mail.google.com 在Safari中,我登录了。所以我怀疑我的服务提供商做错了什么。也许他们没有给我提供正确的瞄准镜?但在联系他们之前,我想排除我的任何过错。现在,我最直接的想法是,与会话相关的cookie不会存储在Safari中。我的问题如下。

    我的问题。令牌POST请求独立于 SFAuthenticationSession (不同的用户代理)因此,如果不通过,任何与会话相关的cookie如何存储在设备(Safari)上 SFAuthenticationSession ? 有没有办法调试代码中的cookie存储?

    2 回复  |  直到 5 年前
        1
  •  3
  •   ko la    5 年前

    token endpoint 不需要资源所有者身份验证,与 authorization endpoint ,确实如此。(执行授权代码交换的脚本或后台通道不一定有权访问用户代理中设置的HTTP Cookie,默认情况下,浏览器在跨站点XHR中不包含凭据。使用刷新令牌时,根本不需要与资源所有者交互。)您的URLSession没有从Safari或 SFAuthenticationSession

    至于您的移动Safari体验,请参阅 ASWebAuthenticationSession SFAuthenticationSession 继承国:

    除了会话cookie之外,所有cookie都可以与Safari共享。

    SFAuthenticationSession 也谷歌必须使用持久性cookie,因此会话共享可以与它们协同工作。

    另一方面,即使使用持久cookie,在iOS 11环境中同步cookie jar时也会出现一些不一致的情况,例如: http://www.openradar.me/radar?id=5036182937272320

        2
  •  1
  •   Kerberos    5 年前

    import UIKit
    
    import WebKit
    
    class ViewController: UIViewController, WKNavigationDelegate {
    
    var webView: WKWebView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        let configuration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero,configuration:configuration)
        self.view = webView
    }
    
    override func viewDidAppear(_ animated: Bool) {
        let url = URL(string: "YOUR URL")
    
        let request = URLRequest(url: url!)
    
        webView?.navigationDelegate = self
        webView?.addObserver(self, forKeyPath: "URL", options: [.new, .old], context: nil)
    
        self.webView?.load(request)
    }
    
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    
        if let newValue = change?[.newKey] as? Int, let oldValue = change?[.oldKey] as? Int, newValue != oldValue {
    
            print("NEW",change?[.newKey])
        } else {
            print("OLD",change?[.oldKey])
    
        }
    
        webView?.configuration.websiteDataStore.httpCookieStore.getAllCookies { cookies in
            for cookie in cookies {
                print(cookie)
            }
        }
    }
    }
    

    并检查cookies是否有 expiresDate 所有物否则,您将无法使用SSO。

        3
  •  0
  •   t.ios    4 年前

    如果不想在浏览器中存储cookie,可以通过与的私人浏览会话进行身份验证 ASWebAuthenticationSession prefersEphemeralWebBrowserSession 设置为true。

    https://github.com/openid/AppAuth-iOS/issues/530#issuecomment-628159766

    推荐文章