簡體   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