gpt4 book ai didi

javascript - 如何在 Node js 的服务器端验证谷歌身份验证 token ?

转载 作者:IT老高 更新时间:2023-10-28 23:26:52 26 4
gpt4 key购买 nike

我的前端应用程序使用 gmail 帐户进行了身份验证

我在验证成功后检索 id_token 并将其作为 Authorization Header 作为 bearer token 发送。

例如 http://localhost:4000/api

授权持有者token_id

nodejs服务器端,我调用下面的方法来验证token。

exports.verifyUser = function(req, res, next) {
var GoogleAuth = require('google-auth-library');
var auth = new GoogleAuth();
var client = new auth.OAuth2(config.passport.google.clientID, config.passport.google.clientSecret, config.passport.google.callbackURL);
// check header or url parameters or post parameters for token
var token = "";
var tokenHeader = req.headers["authorization"];
var items = tokenHeader.split(/[ ]+/);
if (items.length > 1 && items[0].trim().toLowerCase() == "bearer") {
token = items[1];
}
if (token) {
var verifyToken = new Promise(function(resolve, reject) {
client.verifyIdToken(
token,
config.passport.google.clientID,
function(e, login) {
console.log(e);
if (login) {
var payload = login.getPayload();
var googleId = payload['sub'];
resolve(googleId);
next();
} else {
reject("invalid token");
}
}
)
}).then(function(googleId) {
res.send(googleId);
}).catch(function(err) {
res.send(err);
})
} else {
res.send("Please pass token");
}
}

当我调用上述方法时,我总是得到 Invalid token 响应并出现以下错误。

Error: No pem found for envelope:     {"alg":"RS256","kid":"c1ab5857066442ea01a01601
850770676460a712"}
at OAuth2Client.verifySignedJwtWithCerts (\node_modules\google-auth-libr
ary\lib\auth\oauth2client.js:518:13)
  • 这是验证 token 的正确方法吗?
  • 我是否将 id_token 作为授权承载发送?还是只是为了授权?
  • 如何将 id_token 发送到服务器端?通过网址,标题?
  • 我做错了什么?

非常感谢任何帮助。

最佳答案

OAuth2Client.verifyIdTokenlibrary source 获取一个 idToken 作为参数:

/**
* Verify id token is token by checking the certs and audience
* @param {string} idToken ID Token.
* @param {(string|Array.<string>)} audience The audience to verify against the ID Token
* @param {function=} callback Callback supplying GoogleLogin if successful
*/
OAuth2Client.prototype.verifyIdToken = function(idToken, audience, callback)

您已经传递了整个 header 值 bearer eyJhbGciOiJSUzI1NiIsImtpZCI6ImMxYWI1OD U3MDY2NDQyZWEwMWEwMTYwMTg1MDc3MDY3NjQ2MGE3MTIifQ 所以您必须将 header 值拆分为:

var authorization = req.headers["authorization"];
var items = authorization.split(/[ ]+/);

if (items.length > 1 && items[0].trim() == "Bearer") {
var token = items[1];
console.log(token);
// verify token
}

Is this the right approach to verify token ?

是的,这是验证 token 的正确方法。对于调试,如果您有任何疑问或快速测试,您还可以使用 tokeninfo 端点验证 token :

https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=XYZ123
  • Do I send the id_token as Authorization bearer? Or is it forauthorization only?
  • How do I send the id_token to the sever side? Thruurl, header?

您可以在 Authorization header 中发送 JWT token ,但这可能会导致您拥有 multiple Authorization headers 的用例.最好在正文中进行 URL 编码或嵌入 token 。您可以查看 Google 示例 here

此外,Google 要求以下内容:

  • token 必须通过 HTTPS POST 发送
  • 必须验证 token 完整性

要优化您的代码,您还可以将您的 Google auth 对象移动到您应用根目录下的 app.js 中,而不是每次 token 应该重新定义它得到验证。在 app.js 中:

var app = express();

var GoogleAuth = require('google-auth-library');
var auth = new GoogleAuth();
app.authClient = new auth.OAuth2(config.passport.google.clientID, config.passport.google.clientSecret, config.passport.google.callbackURL);

并在 verifyUser 中从 req.app.authClient 调用它:

req.app.authClient.verifyIdToken(...)

关于javascript - 如何在 Node js 的服务器端验证谷歌身份验证 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42405439/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com