【问题标题】:NodeJs + Async + Google auth + passport + aws-ssmNodeJs + 异步 + 谷歌身份验证 + 护照 + aws-ssm
【发布时间】: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


    【解决方案1】:

    由于护照依赖于异步任务,而不是需要passport,您需要将其作为参数从主应用程序传递,然后导出函数。

    您可以等待,直到您获得客户端 ID 和其他所需信息。

    passport_init.js

    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);
        }
      });
    });
    
    // Passport as argument passed from Main Application
    module.exports = async function(passport) {
        var clientid = await 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);
          }
        ));
    }
    

    主要应用:

    app.js/routes.js

    const passport = require('passport');
    
    // Pass passport as argument
    let initPassport = require('./config/passport_init.js');
    initPassport(passport)
        .then(() => {
            console.log('Passport Initialised successfully');
    
            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('/');
              });
    
        })
        .catch(err => console.log(err));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-19
      • 2020-10-14
      • 2013-06-28
      • 2014-08-18
      • 2018-02-13
      • 1970-01-01
      • 2018-12-13
      相关资源
      最近更新 更多