najamelan / ws_stream_wasm

Wasm convenience API for WebSockets
The Unlicense
95 stars 14 forks source link

Authentication #10

Closed simbleau closed 1 year ago

simbleau commented 1 year ago

I can't find the ability to attach headers on WsMeta::connect, is there no way to do Basic Authentication or Bearer authentication? :)

najamelan commented 1 year ago

I'll look into this tonight after work.

najamelan commented 1 year ago

The library wraps the existing javascript API for WebSockets. From what I gather (I admit I haven't really played with this myself), you can pass a header to the protocols parameter. This takes an Option<Vec<&str>> in rust and you can see an example (in Js) here: https://stackoverflow.com/questions/22383089/is-it-possible-to-use-bearer-authentication-for-websocket-upgrade-requests

Let me know if this solves your issue.

najamelan commented 1 year ago

You can obviously also allow the connection first and implement your authentication manually: https://websockets.readthedocs.io/en/stable/topics/authentication.html

This document states:

Setting a custom HTTP header

This would be the most elegant mechanism, solving all issues with the options discussed above.

Unfortunately, it doesn’t work because the WebSocket API doesn’t support setting custom headers.

najamelan commented 1 year ago

Some more discussion here: https://github.com/whatwg/websockets/issues/16

It looks like what you want does not have great support in websockets. You'll have to do some research to see what's the best solution. If ws_stream_wasm can really be improved here, feel free to post more feedback, but I would like to avoid custom rolling stuff in this library. It's just a rust idiomatic wrapper around the Js API. You can always wrap the WsMeta type in a newtype to give it extra functionality on top.

simbleau commented 1 year ago

Yep, fair call.