在配置数据库之前从Passport-azure-ad获取数据

时间:2018-12-13 15:11:57

标签: javascript node.js azure-active-directory passport.js

var server;
var db;    

passport.use(new OIDCStrategy({
        identityMetadata: config.creds.identityMetadata,
        clientID: config.creds.clientID,
        responseType: config.creds.responseType,
        responseMode: config.creds.responseMode,
        redirectUrl: config.creds.redirectUrl,
        allowHttpForRedirectUrl: config.creds.allowHttpForRedirectUrl,
        clientSecret: config.creds.clientSecret,
        validateIssuer: config.creds.validateIssuer,
        isB2C: config.creds.isB2C,
        issuer: config.creds.issuer,
        passReqToCallback: config.creds.passReqToCallback,
        scope: config.creds.scope,
        loggingLevel: config.creds.loggingLevel,    
        nonceLifetime: config.creds.nonceLifetime,
        nonceMaxAmount: config.creds.nonceMaxAmount,
        useCookieInsteadOfSession: config.creds.useCookieInsteadOfSession,
        cookieEncryptionKeys: config.creds.cookieEncryptionKeys,
        clockSkew: config.creds.clockSkew,
      },
        function (iss, sub, profile, accessToken, refreshToken, done) { 

            const request = require('request');

            const options = {
                url: 'https://graph.microsoft.com/v1.0/me?$select=displayName,extension_db,extension_server',
                headers: {
                    'Authorization': "Bearer " + accessToken,
                    'Content-Type': 'application/json'
                }
            };

            function callback(error, response, body) {
                if (!error && response.statusCode == 200) {
                    console.log('error:', error); // Print the error if one occurred
                    console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
                    console.log('body:', body); 
                    var data = JSON.parse(response.body);
                    server = data.extension_server;
                    db = data.extension_db;
                } else {
                    console.log(response.statusCode);
                }
            }
            request(options, callback);

        if (!profile.oid) {
          return done(new Error("No oid found"), null);
        }
        // asynchronous verification, for effect...
        process.nextTick(function () {
          findByOid(profile.oid, function(err, user) {
            if (err) {
              return done(err);
              }
            if (!user) {
              // "Auto-registration"
                users.push(profile);
              return done(null, profile);
              }

            return done(null, user);
          });
        });
      }
    ));


//config database
var sql = require("mssql");

var config = {
    user: '****',
    password: '*****',
    server: server,
    database: db
};

我想基于我从get请求中收到的字段来配置数据库。我遇到的问题是passport.use发生在配置已设置并且我所有其他代码(包括路由)执行之后。我该怎么做才能使password.use在登录后首先出现?

0 个答案:

没有答案