-
Beta Was this translation helpful? Give feedback.
Replies: 10 comments 10 replies
-
not compatible, passport.js is express middleware but you don't need it |
Beta Was this translation helpful? Give feedback.
-
How do I authorize users then? |
Beta Was this translation helpful? Give feedback.
-
https://github.com/fastify/fastify-passport Fastify has a port to work with passport.js. Anyway, the problem is actually that I'm migrating from Express. I need something +/- secure and VERY fast. If you can't use passport.js in uWebSockets, I'll migrate to fastify. Even simply very fast ws:// is already cool. |
Beta Was this translation helpful? Give feedback.
-
normally this involves looking at and setting secure http Cookies |
Beta Was this translation helpful? Give feedback.
-
Well... That's not the problem. The problem is that I have more than one authorization strategy. Describing them all would be quite difficult. |
Beta Was this translation helpful? Give feedback.
-
The last time I did this was when I was writing PHP. It was fun and exciting, until I wanted to write something huge and scary... |
Beta Was this translation helpful? Give feedback.
-
If you want to see simple example authorizing websocket request with cookies and jsonwebtoken I did that in other discussion: #112 (comment) app.ws('/',{
upgrade:(res,req,context)=>{
try{req.user=decodeJwtCookie(req,'user')}
catch{return res.writeStatus('401').end()}
res.upgrade({uid:req.user._id},req.getHeader('sec-websocket-key'),req.getHeader('sec-websocket-protocol'),req.getHeader('sec-websocket-extensions'),context)
},
open:ws=>console.log('open-ws',ws.uid)
})
const getCookie=(req,name)=>(req.cookies??=req.getHeader('cookie')).match(getCookie[name]??=new RegExp(`(^|;)\\s*${name}\\s*=\\s*([^;]+)`))?.[2]
const decodeJwtCookie=(req,name)=>require('jsonwebtoken').verify(getCookie(req,name),jwtSecret) // set auth cookie
res.writeHeader('Set-Cookie',`user=${require('jsonwebtoken').sign(user,jwtSecret)}; HttpOnly; Secure; SameSite=Lax; Max-Age=315360000;`) for middleware type patterns see #464 (comment) |
Beta Was this translation helpful? Give feedback.
-
You should go to passport and ask them to port their library to uWS, just like they ported their library to fastify. This has nothing to do with uWS, authentication is an application level protocol atop uWS. We cannot change fundamental base functionality to seamlessly support high level libraries written for something else |
Beta Was this translation helpful? Give feedback.
-
@Doumor You should do this yourself as uWebSockets.js is fastest because does not bundle any unnecessary things. |
Beta Was this translation helpful? Give feedback.
-
I have not yet tried but Passport.js potentially works with the Express compatibility layer I've built recently: https://github.com/endel/uWebSockets-express (Thread about the compatibility layer here: #565) |
Beta Was this translation helpful? Give feedback.
not compatible, passport.js is express middleware but you don't need it