【发布时间】:2019-06-20 03:33:00
【问题描述】:
我正在使用 nodejs 并使用护照 npm 模块实现了 google-auth。 但是我正在从 aws:ssm 参数中获取 google api-key 和 secret-key 获取,例如服务器调用..
但问题是我们需要在护照初始化时解析api-key和secret-key.. 我不确定如何在护照初始化之前获取这些密钥 我添加了 Promise 函数,仅用于获取客户端 ID 进行测试。我不确定初始化时如何调用异步调用。 我附上了示例代码:
var passport = require('passport');
var GoogleStrategy = require('passport-google-oauth').OAuthStrategy;
const AWS = require('aws-sdk');
const ssm = new AWS.SSM();
const getClientId = new Promise(function(resolve, reject) {
const params = {
Name: 'xxx',
WithDecryption: false
};
ssm.getParameter(params, function(err, data) {
if (err) {
console.log('-----------', err)
reject(err);
} else {
resolve(data);
}
});
});
var clientid = getClientId();
passport.use(new GoogleStrategy({
consumerKey: clientid, //(needs to fetch from aws-ssm)
consumerSecret: GOOGLE_CONSUMER_SECRET, //(needs to fetch from aws-ssm)
callbackURL: "http://localhost:8080/auth/google/callback"
},
function(token, tokenSecret, profile, done) {
return done(null,profile);
}
));
module.exports { passport : passport }
我在不同文件中的路由器代码
app.get('/auth/google',
passport.authenticate('google', { scope: ['https://www.googleapis.com/auth/plus.login'] }));
app.get('/auth/google/callback',
passport.authenticate('google', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
});
谁能帮我解决这个问题。
【问题讨论】:
标签: javascript node.js asynchronous google-authentication aws-ssm