简体   繁体   English

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

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

I am trying to verify Google ID Token on Node.js server.我正在尝试在 Node.js 服务器上验证 Google ID 令牌。

I am getting this error:我收到此错误:

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

Here is the link of code that I am following from Google's official documentation:这是我从谷歌官方文档中获取的代码链接:

Google Identity Toolkit Node Google 身份工具包节点

Looks like you need to install a jwt framework like this or this .看起来你需要安装一个类似thisthis的 jwt 框架。
I believe that you need the first link for the server and possible the second link for the website (more info on the website here ).我相信您需要服务器的第一个链接,并且可能需要网站的第二个链接(有关网站更多信息)。

In my scenario, locally it worked like a charm, but inside AWS lambda, it caused the same error reported, so I've endup using this URL and Axios(you can use any HTTP client) to check the token validity and domain of the user:在我的场景中,在本地它就像一个魅力,但在 AWS lambda 中,它导致了相同的错误报告,所以我最终使用这个 URL 和 Axios(你可以使用任何 HTTP 客户端)来检查令牌的有效性和域用户:

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

This is the idea:这是这样的想法:

//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
}

And then used it to validate before executing some action:然后在执行某些操作之前使用它进行验证:

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

It was enough for me, hope it helps someone.这对我来说已经足够了,希望它可以帮助某人。

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

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