Zam*_*age 9 javascript api google-authentication node.js google-signin
我的前端应用程序使用gmail帐户进行身份验证.
我在身份验证成功后检索id_token,并将其作为授权标头发送为承载令牌.
授权承载token_id
在nodejs服务器端,我调用以下方法来验证令牌.
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");
}
}
Run Code Online (Sandbox Code Playgroud)
当我调用上面的方法时,我总是得到无效的令牌响应,并出现以下错误.
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)
Run Code Online (Sandbox Code Playgroud)
任何帮助都非常感谢.
OAuth2Client.verifyIdToken
从库源获取参数中的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)
Run Code Online (Sandbox Code Playgroud)
您已经传递了整个标头值,bearer eyJhbGciOiJSUzI1NiIsImtpZCI6ImMxYWI1OD
U3MDY2NDQyZWEwMWEwMTYwMTg1MDc3MDY3NjQ2MGE3MTIifQ
因此必须将标头值拆分为:
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
}
Run Code Online (Sandbox Code Playgroud)
这是验证令牌的正确方法吗?
是的,这是验证令牌的正确方法。对于调试,如果您有任何疑问或进行快速测试,也可以使用tokeninfo端点来验证令牌:
https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=XYZ123
Run Code Online (Sandbox Code Playgroud)
- 我是否将id_token作为授权承载发送?还是仅用于授权?
- 如何将id_token发送到服务器端?通过网址,标题?
您可以在Authorization标头中发送JWT令牌,但这可能会导致您有多个Authorization标头的用例。最好对URL进行编码或将令牌嵌入到主体中。您可以在此处查看Google示例
此外,Google需要满足以下条件:
为了优化代码,您也可以将Google auth
对象移动到app.js
应用程序的根目录,而不必在每次验证令牌时重新定义它。在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);
Run Code Online (Sandbox Code Playgroud)
并从verifyUser
调用req.app.authClient
:
req.app.authClient.verifyIdToken(...)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5808 次 |
最近记录: |