代理 Firebase 函数的跨域状态 cookie 问题

2024-03-19

我使用开发了一个oAuth登录这个例子 https://github.com/firebase/functions-samples/tree/master/linkedin-auth。遇到的第一个问题是如果浏览器中禁用第三方 cookie(现在默认情况下),则状态 cookie 验证。正如建议的这个答案 https://github.com/firebase/functions-samples/tree/master/linkedin-auth,我代理了这些功能。

因此,我使用托管重写来代理这些功能,因此您位于同一域中,并且第一个重定向功能设置的服务器 cookie 似乎与应用程序位于同一域中。所以这就是发生的事情

  1. 用户被重定向到云功能,该功能设置 cookie 并将用户重定向到第三方身份验证提供商
  2. 用户登录
  3. 用户再次重定向到应用程序,应用程序获取授权码并将用户重定向到令牌函数
  4. token函数尝试读取状态cookie,但根本没有cookie

当我尝试从 token 函数读取 cookie 时

[Object: null prototype] {}

这是托管重写

"hosting": {
...
"rewrites":  [
  {
    "source": "/redirect",
    "function": "redirect"
  },
  {
    "source": "/token**",
    "function": "token"
  },
  {
    "source": "**",
    "destination": "/index.html"
  }
],

这是重定向功能

exports.redirect = functions.https.onRequest((req, res) => {
  cookieParser()(req, res, () => {
    const redirect_uri = `https://${process.env.GCLOUD_PROJECT}.firebaseapp.com/auth.html`
    const state = req.cookies.state || crypto.randomBytes(20).toString('hex')
    const authorizationUri = fedidClient().authorizationCode.authorizeURL({
      redirect_uri: redirect_uri,
      scope: OAUTH_SCOPES,
      state: state,
    })
    res.cookie('state', state.toString(), {
      maxAge: 3600000,
      secure: true,
      httpOnly: true,
    })
    res.redirect(authorizationUri)
  })
})

这是令牌函数

exports.token = functions.https.onRequest((req, res) => {
  const redirect_uri = `https://${process.env.GCLOUD_PROJECT}.firebaseapp.com/auth.html`  
  try {
    return cookieParser()(req, res, async () => {
        if (!req.cookies.state) {
          throw new Error(
            'State cookie not set or expired. Maybe you took too long to authorize. Please try again.'
          )
        }
      const tokenConfig = {
        code: req.query.code,
        redirect_uri: redirect_uri,
        scope: OAUTH_SCOPES,
      }
      const result = await fedidClient().authorizationCode.getToken(tokenConfig)
      const accessToken = fedidClient().accessToken.create(result)

      let user = {}
      await getUserInfo(accessToken)
        .then((result) => result.json())
        .then((json) => (user = json))

      // Create a Firebase account and get the Custom Auth Token.
      const firebaseToken = await createFirebaseAccount(
        user.uid,
        user.displayName,
        user.mail,
        accessToken.token.access_token
      )

      res.jsonp({
        token: firebaseToken,
      })
    })
  } catch (error) {
    return res.status(500).jsonp({ error: error.toString })
  }
})    

为什么cookie没有通过第二个云函数传递?如果禁用重写并启用第三方 cookie,则代码可以正常工作。


您可能无意中发现了 Firebase Hosting 中的缓存功能,该功能会删除除__session.

将 Firebase 托管与 Cloud Functions 或 Cloud 一起使用时 运行时,cookie 通常会从传入请求中剥离。这是 允许高效的 CDN 缓存行为所必需的。只有 特殊命名的 __session cookie 被允许传递到 您的应用程序的执行。

Source https://firebase.google.com/docs/hosting/manage-cache#using_cookies

尝试将您的 cookie 重命名为 __session 并查看是否可以修复该问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

代理 Firebase 函数的跨域状态 cookie 问题 的相关文章

随机推荐

  • 如何从链接到 QTableView 的模型中插入和删除行

    The removeRows 通过删除选定的行来按预期工作 但有一个问题insertRows 由于某种原因 新项目不会出现在所选索引号处 是什么原因导致这个问题呢 from PyQt4 QtCore import from PyQt4 Qt
  • Matplotlib:如何设置当前图形?

    希望这是一个简单的问题 但我目前无法弄清楚 我想使用 matplotlib 显示 2 个数字 然后交互式地使用它们 我用以下方法创建数字 import matplotlib import pylab as pl f1 pl figure f
  • Heroku 零停机时间

    是否可以做类似的事情Github 零停机部署 https github com blog 517 unicorn在 Heroku 上使用 Cedar 堆栈上的 Unicorn 我不完全确定 Heroku 上的重启是如何进行的 以及我们对重启
  • 从我的本地计算机附加文件以在 cq/AEM 中发送邮件

    我正在学习 AEM 并且正在满足一个要求 其中我能够发送电子邮件 但无法添加从我的计算机浏览的附件 要求 有一个用 HTML 制作的表单 可以从其中收集信息 并且有一个浏览按钮 可以从其中上传文件 文件上传后 应立即将包含表单内容和附件的电
  • 使用 selenium webdriver 按类名和标记名查找元素

    有类和标记名 我正在编写下面的硒代码以从下面的代码中查找描述 但它不起作用 WebElement WWdescription driver findElement By className atb delivery accordions c
  • Swift:如何在“String”扩展中添加类方法

    我想在扩展中添加一个类函数 extension String class func test 我收到错误 Class methods are only allowed within classes use static to declare
  • 如何在两点之间填充数组列表?

    我有这段代码 解释 用户输入 initcores 数据和 time 数据 forces 是结果 我想用从 0 到 ttime 的值填充 x 数组 用 y 填充 initcores 到 fcores 并绘制散点图 x 与 y 我有一个问题 如
  • Latex:\bibliographystyle{abbrv} 根据外观排序引用[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我使用 Latex 的 bibliographystyle abbrv 命令作为参考 但引用的顺序并不按照它们在文档中出现的顺序 无论如何
  • MySQLi 准备好的语句返回 false

    我正在尝试使用 MySQLi 在我的数据库上运行多个查询 这是我的代码 stmt mysqli gt prepare SELECT password FROM users WHERE username LIMIT 1 stmt gt bin
  • 无法生成 qdoc 文档

    按照本指南操作http doc snapshot qt project org qdoc qdoc guide conf html http doc snapshot qt project org qdoc qdoc guide conf
  • C++比较C字符串的麻烦

    我编写了以下代码 该代码不起作用 但当我更改它时 第二个代码片段将起作用 int main int argc char argv if argv 1 i This is what does not work Do Something 但如果
  • scafford自动生成crud存储库asp.net5

    您好 我正在使用 Visual Studio 2015 我知道我们可以通过添加新的来生成 CRUD 视图控制器和操作scafford物品 但是代码生成不是很有用 似乎所有数据层都依赖于控制器 所以我的问题是 任何使用脚手架的方法都会生成代码
  • 干燥 if 语句

    我有一个 C 程序 在许多不同的 cpp 文件中 我执行如下操作 if thing1 empty thing2 empty if thing1 property lt thing2 property return func1 else if
  • Windows 上 Symfony 5.0.1 控制台中已弃用的类警告

    执行后php bin console make 实体 用户实体 或php bin console make 迁移我在控制台中收到这些警告 2019 12 13T15 49 53 00 00 info User Deprecated The
  • Angular 2 - 组件内的 formControlName

    我想创建一个可以与 FormBuilder API 一起使用的自定义输入组件 我该如何添加formControlName在组件内部 模板
  • 为什么 Lucene QueryParser 需要分析器

    我是 Lucene 的新手 正在尝试将原始字符串解析为Query使用QueryParser 我想知道为什么QueryParser Parse 方法根本需要分析器参数吗 如果分析与查询有关 那么Analyzer处理常规时应指定Query对象以
  • 如何解码字节对象的字符串表示形式?

    我有一个字符串 其中包含编码字节 str1 b Output file xeb xac xb8 xed x95 xad xeb xb6 x84 xec x84 x9d xlsx Created 我想解码它 但我不能 因为它已经变成了一个字符
  • PHP:$var 和 &$var 有什么区别?

    有什么区别 foreach my array as my value And foreach my array as my value 我可以请您举两个现实世界的例子来说明何时使用其中一种以及何时使用另一种吗 第一个示例创建值的副本 而第二
  • 如何在语义保证下使用 React.memo 或 useMemo?

    的文档useMemo says 您可以依赖 useMemo 作为性能优化 而不是作为语义保证 未来 React 可能会选择 忘记 一些 先前记忆的值并在下次渲染时重新计算它们 例如 为屏外组件释放内存 编写你的代码 以便它 没有 useMe
  • 代理 Firebase 函数的跨域状态 cookie 问题

    我使用开发了一个oAuth登录这个例子 https github com firebase functions samples tree master linkedin auth 遇到的第一个问题是如果浏览器中禁用第三方 cookie 现在