模仿 chrome 打包应用程序中的 cookie 行为

Imitating cookies behavior in chrome packed app

我正在使用 REST 服务,我无法控制修改。 该服务有 verification/login 和许多 "data-retrive" 个端点。

我被要求创建将使用此 REST 服务的 "Chrome packed app"。

我的限制是: 不能使用沙盒页面。 无法使用网络视图。

问题:

  1. Xhr 请求忽略 "Set-cookie" headers。
  2. 手动设置cookie是不可能的(好像没有cookieapi)。

想法与研究

  1. 我使用 "chrome.socket" 在 google 上搜索了 XHR 实现,没有 https 支持,因为 "chrome.socket" 不支持 TLS。
  2. Forge 是一个 "Chrome-js" 库,它为 "chrome.socket"
  3. 添加了 TLS 支持

问题

  1. 有没有办法修改请求和响应headers?
  2. 有什么方法可以将使用 "chrome.socket" 的 XHR 实现与 Forge 结合起来吗? (我真的不知道协议或原始 XHR 实现)

  3. 实施假 cookie API 来管理来自 headers 的数据是否合理?

您应该考虑使用 Cross-Origin Resource Sharing (CORS) request. There is a good introduction here

特别是,您需要使用 withCredentials:

Standard CORS requests do not send or set any cookies by default. In order to include cookies as part of the request, you need to set the XMLHttpRequest’s .withCredentials property to true: [...]

请注意,您需要服务器配合才能执行此操作:

In order for this to work, the server must also enable credentials by setting the Access-Control-Allow-Credentials response header to “true”.

因为您总是在 Chrome,您可以使用 Fetch API 而不是 XMLHttpRequest2。在这种情况下,您将添加 credentials 选项:

Should you want to make a fetch request with credentials such as cookies, you should set the credentials of the request to “include”.

这两种机制都将 (1) 支持 HTTPS 和 (2) 不透明地管理 cookie,即它们不允许您检查 cookie 数据。