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

Per*_*ere 5 oauth cross-domain firebase google-cloud-functions

我使用此示例开发了 oAuth 登录。遇到的第一个问题是如果浏览器中禁用了第三方 cookie(现在默认情况下),则状态 cookie 验证。正如这个答案所建议的,我代理了这些功能。

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

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

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

[Object: null prototype] {}
Run Code Online (Sandbox Code Playgroud)

这是托管重写

"hosting": {
...
"rewrites":  [
  {
    "source": "/redirect",
    "function": "redirect"
  },
  {
    "source": "/token**",
    "function": "token"
  },
  {
    "source": "**",
    "destination": "/index.html"
  }
],
Run Code Online (Sandbox Code Playgroud)

这是重定向功能

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)
  })
})
Run Code Online (Sandbox Code Playgroud)

这是令牌函数

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 })
  }
})    
Run Code Online (Sandbox Code Playgroud)

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

Bri*_*ton 5

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

当 Firebase Hosting 与 Cloud Functions 或 Cloud Run 一起使用时,通常会从传入请求中删除 cookie。这是实现高效 CDN 缓存行为所必需的。仅允许特别命名的 __session cookie 传递到您的应用程序的执行。

来源

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