How to use the passport.socketio.authorize function in passport

To help you get started, we’ve selected a few passport examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dunstad / roboserver / public / js / server / customizeServer.js View on Github external
function main(server, app) {

  // start http/socket.io server code

  var io = require('socket.io')(server);
  var config = require('../config/config');

  var passportSocketIo = require('passport.socketio');
  io.use(passportSocketIo.authorize({
    store: app.get('sessionStore'),
    success: onAuthorizeSuccess,
    fail: onAuthorizeFail,
    secret: config.expressSessionSecret
  }));

  function onAuthorizeSuccess(data, accept){
    console.log('successful connection to socket.io');
    console.dir(data.user)
    accept();
  }

  function onAuthorizeFail(data, message, error, accept){
    console.log('failed connection to socket.io:', message);
    if (error) {throw new Error(message);}
    else {accept(new Error(message));}
github maestrano / webshell-server / app.js View on Github external
});

//===============================================
// Initiate http server
//===============================================
var httpserv;
httpserv = http.createServer(app).listen(opts.port, function() {
    console.log('http on port ' + opts.port);
});

//===============================================
// Socket server
//===============================================
var io = server(httpserv,{path: '/webshell/socket.io'});

io.use(passportSocketIo.authorize({
  cookieParser: cookieParser,
  key:          sessionConfig.key,
  secret:       sessionConfig.secret,
  store:        sessionStore,
  fail: function(data, message, error, accept) {
    if (error) accept(new Error(message));
  },
  success: function(data, accept) {
    console.log("success socket.io auth");
    accept();
  }
}));

io.on('connection', function(socket){
    var request = socket.request;
    console.log((new Date()) + ' Connection accepted.');
github ironbane / IronbaneServerLegacy / src / server / http / server.js View on Github external
store: sessionStore // store sessions in db for "remember me"
            }));
            app.set('view engine', 'html');
            app.set('views', config.get('buildTarget'));
            app.engine('html', require('hogan-express'));
            app.locals.delimiters = '<% %>';
            // Initialize Passport!  Also use passport.session() middleware, to support
            // persistent login sessions (recommended).
            app.use(passport.initialize());
            app.use(passport.session());

            // link socket to passport
            var old_auth;
            old_auth = app.io.get('authorization');

            app.io.set("authorization", require('passport.socketio').authorize({
                passport: passport,
                cookieParser: express.cookieParser,
                key: 'connect.sid',
                secret: config.get('session_secret'),
                store: sessionStore,
                success: function(data, accept) {
                    //console.log('auth success');
                    // this means it was able to pull user data from session
                    return old_auth(data, accept);
                },
                fail: function(data, accept) {
                    //console.log('auth fail', arguments);
                    // this means it does not have user data from session, however, it's ok,
                    // we allow guests
                    return old_auth(data, accept);
                }
github godratio / Blogular / app.js View on Github external
io.configure(function () {
    io.set("authorization", passportSocketIo.authorize({
        key: 'express.sid',       //the cookie where express (or connect) stores its session id.
        secret: 'secret', //the session secret to parse the cookie
        store: sessionStore,     //the session store that express uses
        fail: function (data, accept) {
            accept(null, false);             // second param takes boolean on whether or not to allow handshake
        },
        success: function (data, accept) {
            accept(null, true);
        }
    }));
});
var connectedusers = [];
github gridgain / gridgain / modules / web-console / backend / app / configure.js View on Github external
socketio: (io) => {
            const _onAuthorizeSuccess = (data, accept) => accept();

            const _onAuthorizeFail = (data, message, error, accept) => {
                if (error)
                    accept(new Error(message));

                return accept(new Error(message));
            };

            io.use(passportSocketIo.authorize({
                cookieParser,
                key: 'connect.sid', // the name of the cookie where express/connect stores its session_id
                secret: settings.sessionSecret, // the session_secret to parse the cookie
                store: _sessionStore, // we NEED to use a sessionstore. no memorystore please
                success: _onAuthorizeSuccess, // *optional* callback on success - read more below
                fail: _onAuthorizeFail // *optional* callback on fail/error - read more below
            }));
        }
    };
github hexparrot / mineos-node / webui.js View on Github external
app.use(bodyParser.urlencoded({extended: false}));
app.use(methodOverride());
app.use(compression());
app.use(expressSession({ 
  secret: token,
  key: 'express.sid',
  store: sessionStore,
  resave: false,
  saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());

var io = require('socket.io')(http)
io.use(passportSocketIO.authorize({
  cookieParser: cookieParser,       // the same middleware you registrer in express
  key:          'express.sid',       // the name of the cookie where express/connect stores its session_id
  secret:       token,    // the session_secret to parse the cookie
  store:        sessionStore        // we NEED to use a sessionstore. no memorystore please
}));

function tally(callback) {
  var os = require('os');
  var urllib = require('urllib');
  var child_process = require('child_process');

  var tally_info = {
    sysname: os.type(), 
    release: os.release(), 
    nodename: os.hostname(),
    version: '',
github Tzook / lul / output / lib / socketio / socketio.router.js View on Github external
initDependencies(mongoStore) {
        this.io.use((socket, next) => {
            if (socket.request._query.unicorn) {
                socket.request._query.session_id = socket.request._query.unicorn.split(/s:|\./)[1];
            }
            next();
        });
        this.io.use(passportSocketIo.authorize({
            key: 'unicorn',
            secret: 'UnicornsAreAmazingB0ss',
            store: mongoStore,
            success: this.onAuthorizeSuccess.bind(this),
            fail: this.onAuthorizeFail.bind(this),
        }));
    }
    onAuthorizeSuccess(req, next) {
github sdelements / lets-chat / app / auth / index.js View on Github external
passport.deserializeUser(function(id, done) {
        var User = mongoose.model('User');
        User.findOne({ _id: id }, function(err, user) {
            done(err, user);
        });
    });

    app.use(passport.initialize());
    app.use(passport.session());

    session = _.extend(session, {
        cookieParser: cookieParser,
        passport: passport
    });

    var psiAuth = passportSocketIo.authorize(session);

    app.io.use(function (socket, next) {
        var User = mongoose.model('User');
        if (socket.request._query && socket.request._query.token) {
            User.findByToken(socket.request._query.token, function(err, user) {
                if (err || !user) {
                    return next('Fail');
                }

                socket.request.user = user;
                socket.request.user.loggedIn = true;
                socket.request.user.usingToken = true;
                next();
            });
        } else {
            psiAuth(socket, next);
github polonel / trudesk / node / src / chatServer.js View on Github external
module.exports = function(ws) {
    var _ = require('lodash'),
        usersOnline = {},
        rooms = {},
        chatHistory = {},
        chatHistoryCounter = 1000,
        sockets = [],
        io = require('socket.io')(ws.server);

    io.use(passportSocketIo.authorize({
        cookieParser: cookieparser,
        key: 'connect.sid',
        store: ws.sessionStore,
        secret: 'trudesk$123#SessionKeY!2387',
        success: onAuthorizeSuccess
    }));

    io.sockets.on('connection', function(socket) {
        var totalOnline = _.size(usersOnline);
        utils.sendToAllConnectedClients(io, 'updateUserCount', {count: totalOnline});

        utils.sendToSelf(socket, 'connectingToSocketServer', {
            status: 'online'
        });

        setInterval(function() {
github decrypto-org / blockchain-course / api / ws-server.js View on Github external
const setupWss = async (server, sessionStore) => {
  const io = new Server(server)

  io.use(passportSocketIo.authorize({
    cookieParser: require('cookie-parser'),
    secret: process.env.APP_SECRET || 'blockchain course default session secret',
    store: sessionStore,
    passport
  }))

  io.on('connection', (client) => {
    logger.info(`Web client connected: ID: ${client.id}`)
    const user = { ...client.request.user.dataValues }
    clients[user.id] = { socketID: client.id, user }

    client.on('disconnect', (reason) => {
      logger.info(`Web client disconnected: ${reason}`)
      delete clients[user.id]
    })
  })