Sails.JS + sails-auth + passport-openidconnect

时间:2016-04-21 17:06:24

标签: sails.js passport.js openid-connect

我试图在我的Sails应用程序中实现passport-openidconnect。我已经安装了sails-authpassportpassport-localpassport-httppassport-openidconnect,所有这些都是启动风帆应用所必需的。 I copied the contents of this file获取护照配置,因为当我开始实施时,sails应用程序已经启动。到目前为止,这是我的配置文件:

module.exports.passport = {

    openid_connect: {
        name: 'OpenID Connect',
        protocol: 'oauth2',
        strategy: require('passport-openidconnect').OAuth2Strategy,
        options: {
            clientID: '',
            clientSecret: ''
        }
    }

};

我基于上面提到的config/passport.js文件中的一些默认选项。

我已经搜索过OpenID Connect的设置示例,但到目前为止还没有找到任何内容。有没有人在他们自己的项目中实现这个,可以给我一些指示?谢谢!

1 个答案:

答案 0 :(得分:0)

我已经在船上使用护照,护照为本地,护照为Google / FB / Twitter,但没有sails-auth!

我不知道passport-openID,但这应该差不多了。

首先,您需要在config / http.js

中添加this等护照中间件

然后你必须在config/passport.js中创建不同的策略(以FacebookStrategy为例,它应该

var passport = require('passport')
    , LocalStrategy = require('passport-local').Strategy
    , FacebookStrategy = require('passport-facebook').Strategy

var verifyExtHandler = function (token, tokenSecret, profile, done) {
    checkAuthExt(profile, done);
};

var verifyHandler = function (mail, password, done) {

    checkAuth(mail, password, done);

};

// Passport session setup.
// To support persistent login sessions, Passport needs to be able to
// serialize users into and deserialize users out of the session. Typically,
// this will be as simple as storing the user ID when serializing, and finding
// the user by ID when deserializing.
passport.serializeUser(function (user, done) {
    user.password = null;
    done(null, user);
});

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

// Use the LocalStrategy within Passport.
// Strategies in passport require a `verify` function, which accept
// credentials (in this case, a username and password), and invoke a callback
// with a user object.
passport.use(new LocalStrategy({
    usernameField: 'mail',
    passwordField: 'password'
}, verifyHandler));

// Remplacer les 'XXXXX' par vos clés et 'yourhost.com' par votre nom de domaine
passport.use(new FacebookStrategy({
    clientID: "XXXXXX",
    clientSecret: "XXXXXX",
    callbackURL: "http://yourhost.com/auth/facebook"
}, verifyExtHandler));

您需要配置路由(config / routes.js):

'/auth/facebook': 'AuthController.facebook',
'/auth/facebook/callback': 'AuthController.facebook'

然后在你的控制器中:

facebook: function (req, res) {
    passport.authenticate('facebook', {
        failureRedirect: '/auth/login'
    }, function (err, user) {
        if (err) {
            return console.log(err);
        }
        req.logIn(user, function (err) {
            if (err) {
                console.log(err);
                res.serverError();
                return;
            }

            return res.redirect('/');
        });
    })(req, res);
}

希望有所帮助!