简体   繁体   English

将 Websocket 与 Passport 结合使用

[英]Using Websocket with Passport

How do I access the passport login for the current session when someone opens a WebSocket connection?当有人打开 WebSocket 连接时,如何访问当前会话的通行证登录名?

I've found a nice project express-ws which seems to work beautifully我发现了一个不错的项目express-ws ,它似乎工作得很好

app.ws('/', function(ws, req) {
    ws.on('message', function(msg) {
        console.log('express-ws --- ', msg);
    });
    console.log('socket', req.user); //current user == req.user
});

But how would I go about getting this same information with a plain Websocket connection?但是我将如何通过普通的 Websocket 连接获取相同的信息?

var WebSocketServer = require('ws').Server,
    wss = new WebSocketServer({ port: 3001 });

wss.on('connection', function(socket){
    //Where is the current user????
    console.log('connection');

    socket.on('message', function(message){
        console.log('message received', message);
    });
});

(this second connection does work just fine - but I can't for the life of me find any way to get the logged in info from Passport) (这第二个连接确实工作得很好 - 但我一生都找不到任何方法从 Passport 获取登录信息)

When you're creating your WS server, add a "verifyClient" parameter as follows to grab the session information:在创建 WS 服务器时,添加一个“verifyClient”参数如下以获取会话信息:

  const wss = new (require('ws').Server)({
    server,
    verifyClient: (info, done) => {
      sessionParser(info.req, {}, () => {
        done(info.req.session)
      })
    }
  })

Where sessionParser is your express-session configured object.其中sessionParser是您的express-session配置对象。 This then allows you to access it within the websocket via req.session .这允许您通过req.session在 websocket 中访问它。 If you're using passport, you might want req.session.passport.user ..如果您使用的是护照,您可能需要req.session.passport.user ..

Instead of using req.session.passport.user to access authenticated user id, one can use ws.upgradeReq.session.passport.user .可以使用ws.upgradeReq.session.passport.user而不是使用req.session.passport.user来访问经过身份验证的用户 ID。

app.ws('/', function(ws, req) {
    ws.on('message', function(msg) {
        console.log('express-ws --- ', msg);
    });
    console.log(ws.upgradeReq.session.passport.user);
    /* keys can be found like
    for(var key in ws.upgradeReq.session.passport){
        console.log(key);
    }
    */
});

Maybe not the best solution, but we can drop the WS connection with the following code:也许不是最好的解决方案,但我们可以使用以下代码删除 WS 连接:


const wss = new webSocket.Server({ server, port: 9999})

wss.on('connection', (ws, req) => {

  // get token from url:  ..url..?token=your_JWT_TOKEN
  const params = require('url').parse(req.url, true).query
  const { token } = params

  // add token to headers (could be done on frontend)
  req.headers.authorization = `Bearer ${token}`

  // look for the user with token
  passport.authenticate(
    'jwt',
    {
      session: false
    },
    (err, user, info) => {
      // If no user close websocket
      if (!user) {
        console.log('Websocket disconnected due to invalid token')
        ws.close()
      }
    }
  )(req)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM