【问题标题】:Unable to verify the ID Token: jwt.split is not a function无法验证 ID Token:jwt.split 不是函数
【发布时间】:2016-10-29 14:38:00
【问题描述】:

我正在尝试在 Node.js 服务器上验证 Google ID 令牌。

我收到此错误:

Unable to verify the ID Token: jwt.split is not a function

这是我从谷歌官方文档中获取的代码链接:

Google Identity Toolkit Node

【问题讨论】:

  • 从客户端检查 idToken 是否未定义/是否为空

标签: javascript node.js identity gitkit


【解决方案1】:

在我的场景中,在本地它就像一个魅力,但在 AWS lambda 中,它导致了相同的错误报告,所以我最终使用这个 URL 和 Axios(你可以使用任何 HTTP 客户端)来检查令牌的有效性和用户的域:

https://www.googleapis.com/oauth2/v3/tokeninfo?id_token={id_token}

这就是想法:

//googleTokenValidatorService
const ENV = require('../config.json');
const AXIOS = require('axios');
function getToken(token) {
  return AXIOS.get(ENV.GOOGLE_VALIDATION_URL + token)
}

function validate(token){
  return new Promise((resolve, reject) => { 
    getToken(token)
      .then(
        (response) => {
          if(isTokenOk(response.data)){
            resolve(response.data)
          }else{
            reject({status:401, message:"You do not have permission to access this resource."})
          }
        },
        (error) => {
          console.log("--- status  " + error.response.status + " with token")
          console.log(token)
          reject({status:401, message:"Invalid google token."}) 
        }
      )
  })
}

const acceptableHds = new Set(
  ['validDomain1.com', 'validDomain2.com']
);

const acceptableClientIDs = new Set(
  [ENV.CLIENT_ID_WEB,ENV.CLIENT_ID_IOS, ENV.CLIENT_ID_ANDROID]
)

function isTokenOk(payload){
  return payload &&
    new Date(payload.exp * 1000) > new Date() &&
    acceptableClientIDs.has(payload.aud) &&
    acceptableHds.has(payload.hd)
}

module.exports = { 
  validate
}

然后在执行一些动作之前用它来验证:

    //some other file
return googleUserValidator.validate(request.headers.token)
.then(productService.getProductDetails.bind({id:request.pathParams.id}))
                    .then(OK)
                    .catch(SERVER_ERROR);

对我来说已经足够了,希望它对某人有所帮助。

【讨论】:

    【解决方案2】:

    看来您需要安装一个 jwt 框架,例如 thisthis
    我相信您需要服务器的第一个链接,并且可能需要网站的第二个链接(网站上的更多信息here)。

    【讨论】:

    • 我已经安装了google-oauth-jwt和jwt,但仍然存在错误。
    • @Master,你在关注这个快速入门吗? developers.google.com/identity/toolkit/web/quickstart/…
    • 是的,我正在关注它。现在错误更改为:Key must be a buffer?
    • 您找到解决方法了吗?我有同样的错误。如果运气好,请发布答案
    猜你喜欢
    • 1970-01-01
    • 2021-12-21
    • 2018-02-21
    • 1970-01-01
    • 2016-11-07
    • 2013-08-19
    • 1970-01-01
    • 2018-02-25
    • 1970-01-01
    相关资源
    最近更新 更多