NodeJs +异步+ Google身份验证+护照+ AWS-SSM

时间:2019-06-20 03:33:00

标签: javascript node.js asynchronous google-authentication aws-ssm

我正在使用nodejs,并使用通行证npm模块实现了google-auth。 但是我正在从aws:ssm参数(例如服务器调用)中获取Google api键和秘密键。.

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

<!DOCTYPE html>
<html>
<head>
	<title>Login page</title><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
</head>
<body>
	<div class="login-form">
		<h3>Sign In</h3>
		<div class="form-content">
				<label for="username"><b>Username</b></label>
				<input type="text" name="username" placeholder="Enter Username ..." id="usernm" required>
				<label for="password"><b>Enter Password</b></label>
				<input type="password" name="password" placeholder="Enter Password ..." id="passwd" required>
				<button id="submitbut">Login</button>
		</div>	
	</div>
</body>
</html>

我在其他文件中的路由器代码

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 }

有人可以帮助我解决此问题。

1 个答案:

答案 0 :(得分:0)

由于通行证依赖于异步任务,因此不需要(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 / route.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));