-
Notifications
You must be signed in to change notification settings - Fork 118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Decompose session decrypt / support websockets #268
Comments
Hey @IlyaSemenov I believe this issue should be opened on H3 side actually on how to handle session with the websocket handler. |
exp: import iron from '@hapi/iron';
export default defineWebSocketHandler({
async open(peer) {
const cookie = peer.request?.headers?.get('cookie');
// get nuxt-session from the cookie
const nuxtSession = cookie?.match(/nuxt-session=(.*?)(?:;|$)/)?.[1] || '';
console.log({ nuxtSession });
// Decrypt tokens using Iron
const unsealed = await iron.unseal(
nuxtSession,
process.env.NUXT_SESSION_PASSWORD,
iron.defaults
);
console.log('Parsed session data:', unsealed);
},
async message(peer, message) {
},
close(peer) {
},
});
|
From what I see in https://github.com/unjs/h3/blob/5599d61ae5b1e32d9bfa222ccb90bee475ce675e/src/utils/session.ts#L177-L194, you should be able to import I did not test but this could work I guess: import { unsealSession } from 'h3'
export default defineWebSocketHandler({
async open(peer) {
const cookie = peer.request?.headers?.get('cookie');
// get nuxt-session from the cookie
const nuxtSession = cookie?.match(/nuxt-session=(.*?)(?:;|$)/)?.[1] || '';
// Decrypt tokens using Iron
const unsealed = await unsealSession(
{}, // _event not used
{ password: process.env.NUXT_SESSION_PASSWORD },
nuxtSession
);
console.log('Parsed session data:', unsealed);
},
}); |
Just as an update: |
Although, what about runtime's session config (requiring
PS: I was experimenting for a PR to improve typing |
Alright, with // server/routes/ws.ts
export default defineWebSocketHandler({
async upgrade(request) {
// Make sure the user is authenticated before upgrading the WebSocket connection
await requireUserSession(request)
},
async open(peer) {
const { user } = await requireUserSession(peer)
peer.send(`Hello, ${user.name}!`)
},
message(peer, message) {
peer.send(`Echo: ${message}`)
},
}) Make sure to have the latest h3 version! |
As a developer, I need to authenticate users inside a websocket session. In nuxt (h3),
defineWebSocketHandler
provides the API which doesn't pass the originatingH3Event
, only raw URL and headers.I assumed there would be a low-level function such as
getUserSessionFromHeaders({ ... })
orgetUserSessionFromCookie("....")
but I didn't manage to find one.getUserSession
seems to be simply callingh3.useSession
and the whole machinery seems to expect the full blownH3Event
even though in fact it only needs a string.I believe the documentation should include a recommended recipe for websocket users / other non-h3event authentication needs.
In the meanwhile, I ended up with a quite awkward approach where I have a GET API handler that encrypts the result of
requireUserSession
, then call it on the client side and push the encrypted session to websocket, which then decrypts it (and also handles expiration to prevent replay attacks). This is a lot of redundant code and an extra HTTP request per connection, and definitely could be improved if there were a way to directly decode the rawnuxt-session
cookie content.The text was updated successfully, but these errors were encountered: