首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NodeJs +异步+ Google身份验证+ passport + aws-ssm

NodeJs +异步+ Google身份验证+ passport + aws-ssm
EN

Stack Overflow用户
提问于 2019-06-20 11:33:00
回答 1查看 165关注 0票数 0

我正在使用nodejs,并使用passport npm模块实现了google-auth。但是我正在从aws:ssm参数获取google api-key和secret-key,就像服务器调用一样。

但问题是,在passport初始化时,我们需要解析api-key和secret-key。我不确定如何在passport初始化之前获取这些密钥,我添加了promise函数,仅用于获取客户端id进行测试。我不确定初始化时如何调用异步调用。我附上了示例代码:

代码语言:javascript
复制
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 }

我在不同的文件中有路由器代码

代码语言:javascript
复制
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('/');
  });

有人能帮我解决这个问题吗?

EN

回答 1

Stack Overflow用户

发布于 2019-06-20 12:10:27

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

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

passport_init.js

代码语言:javascript
复制
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

代码语言:javascript
复制
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));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56678597

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档