繁体   English   中英

无法验证 ID Token:jwt.split 不是函数

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

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

我收到此错误:

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

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

Google 身份工具包节点

看起来你需要安装一个类似thisthis的 jwt 框架。
我相信您需要服务器的第一个链接,并且可能需要网站的第二个链接(有关网站更多信息)。

在我的场景中,在本地它就像一个魅力,但在 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);

这对我来说已经足够了,希望它可以帮助某人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM