【问题标题】:Google "One Tap" Verification: Wrong number of segments in token谷歌“一键式”验证:令牌中的段数错误
【发布时间】:2020-07-20 09:36:15
【问题描述】:

在我从docs here 获得的站点的 HTML 中

<script src="https://accounts.google.com/gsi/client"></script>

      <div
        id="g_id_onload"
        data-client_id={googleClientID}
        data-login_uri='https://mytestdomain.appspot.com/google'
        data-return_uri={uri}
      ></div>

当网站加载时,它会提示我继续使用我的谷歌帐户,我点击继续,它显示我已登录,webhook 确实被调用,我可以从 cookie 中获取g_csrf_token,然后我尝试使用google-auth-library 进行验证,如here in the docs 所述,该one-tap docs here 链接自one-tap docs here

const { OAuth2Client } = require("google-auth-library");
...
app.post("/google", function(req, res) {
  const idToken = req.cookies.g_csrf_token;
  const audience = process.env.GOOGLE_CLIENT_ID
  const client = new OAuth2Client(audience);
  async function verify() {
    const ticket = await client.verifyIdToken({ idToken, audience });
    const payload = ticket.getPayload();
    const userid = payload["sub"];
    console.log('------------------ userid');
    console.log(userid);
  }
  verify().catch(console.error);
  res.status(200).send({ req });
});

我得到的错误是

Error: Wrong number of segments in token: 87ba1eb4d6261b6b      at OAuth2Client.verifySignedJwtWithCertsAsync (/app/node_modules/google-auth-library/build/src/auth/oauth2client.js:525:19)      at OAuth2Client.verifyIdTokenAsync (/app/node_modules/google-auth-library/build/src/auth/oauth2client.js:391:34)      at processTicksAndRejections (internal/process/task_queues.js:97:5)      at async verify (/app/server.js:71:20)

文档确实建议我们应该将令牌作为 POST 参数以及在 cookie 中获取,我已经检查了 req.body.credentialreq.query.credentialreq.params.credential,但没有找到令牌,来自 @ 987654324@

After an ID token is returned from Google, it's submitted by an HTTP POST method request to your login endpoint with the parameter name credential.

【问题讨论】:

  • 您找到解决方案了吗?我碰到了同一个街区

标签: node.js google-oauth google-authentication


【解决方案1】:

我想通了 - 我们都使用 g_csrf_token cookie 作为 idToken 但这是错误的!

您基本上需要获取request.body.credential(或在您的情况下为req.body.credential),这是一个JWT。那么它应该可以工作了!

【讨论】:

    猜你喜欢
    • 2017-12-31
    • 1970-01-01
    • 2022-10-19
    • 1970-01-01
    • 2020-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多