2015-12-02 96 views
1

因此,我使用PAM在我的快速應用中驗證用戶身份。不過,我無法找到正確的文檔和教程,介紹如何實現它並配置會話。使用護照本地和PAM模塊進行身份驗證

下面是PAM故宮模塊調用authenticate-pam

var pam = require('authenticate-pam'); 
pam.authenticate('username', 'password', function(err) { 
    if(err) { 
     console.log(err); 
    } 
    else { 
     console.log("Authenticated!"); 
    } 
    }); 

仍持觀望態度的,我怎麼會寫這個,我知道我想要實現的會議也因此,我還擔心如何我會將它與PAM模塊集成在一起。這裏是我在routes/index.js文件中放置的草稿。

var express = require('express'); 
var passport = require('passport'); 
var router = express.Router(); 
var pam = require('authenticate-pam'); 

// GET Commands ========== 
router.get('/', function(req, res, next) { 
    res.render('login', { title: 'Login', layout: 'base-layouts/login-layout.hbs'}); 
}); 

// POST Commands ========== 
router.post('/', function (req, res) { 
    var usernameCaptured = req.body.username; 
    var passwordCaputured = req.body.password; 
    console.log('Username: ' + usernameCaptured); // For debuging 
    console.log('Password: ' + passwordCaputured); // For debuging 
    pam.authenticate(usernameCaptured, passwordCaptured, function(err) { 
    if(err) { 
     console.log("Not Authenticated"); 
     //Send to login page with error. 
    } 
    else { 
     console.log("Authenticated!"); 
     res.render('dashboard', { title: 'Dashboard', layout: 'base-layouts/dashboard-layout.hbs'}); 
    } 
    }); 
}); 

module.exports = router; 

這就像我已經獲得授權。至於會話,這是我在我的應用程序的app.js(主文件)中放置的內容。

var passport = require('passport'); 
    var LocalStrategy = require('passport-local').BasicStrategy; 
    app.use(passport.initialize()); 
    app.use(passport.session()); 

    //This is what I got from the documentation. It's not fully integrated with the PAM module 
    passport.serializeUser(function(user, done) { 
     done(null, user.id); //Think this integrates with a DB, but I can just sync usernames with a flat file. 
    }); 

    passport.deserializeUser(function(id, done) { 
     done(err, user); 
    }); 

看起來很簡單,但有很多的教程,很難分辨哪些我可以使用。他們中有些人甚至沒有解釋他們在做什麼。希望可以有人幫幫我。

回答

0

試試這個......如果你得到一個SIGTERM,你將不得不以root身份運行。不知道這是什麼解決方案。

var passport   = require('passport'); 
var Strategy   = require('passport-local').Strategy; 
var pam     = require('authenticate-pam'); 

module.exports = function(passport){ 

    // ===================================== 
    // Custom Strategy ===================== 
    // ===================================== 
    passport.use('pam', new Strategy({ 
     usernameField : 'username', 
     passwordField : 'password', 
     passReqToCallback : true 
    }, 
    function(req, username, password, done) { 
     pam.authenticate(username, password, function(err) { 
     if(err) { 
      done(null, false, req.flash('loginMessage', err)); 
     }else{ 
      done(null, username); 
     } 
     }, {serviceName: 'login', remoteHost: 'localhost'}); 
    }) 
); 

    // ===================================== 
    // PASSPORT SERIALIZE ================== 
    // ===================================== 
    passport.serializeUser(function(username, done) { 
     done(null, username); 
    }); 

    // used to deserialize the user 
    passport.deserializeUser(function(username, done) { 
     done(null, username); 
    }); 

}; 
相關問題