Skip to content
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

Use access token to authenticate WebSocket connections #200

Merged
merged 4 commits into from
Jan 30, 2017

Conversation

robertknight
Copy link
Member

In order to support clients using an OAuth access token rather than a cookie for authentication everywhere, this PR adds the access token to the URL when connecting to the WebSocket. A backend change (hypothesis/h#4330) will allow the WS server to extract the auth token and use it for authentication.

Since token fetching is asynchronous, this means that Streamer.{connect, reconnect} also now become asynchronous since they have to fetch the token before initiating the connection. They now return a void Promise once auth token has been retrieved and the connection has been initiated.

We are including credentials in the URL rather than a custom header because the browser WebSocket API does not support setting custom headers for the initial handshake request. See hypothesis/product-backlog#154 for further discussion.

@robertknight
Copy link
Member Author

Marked as WIP because this doesn't actually work yet. I'm debugging the backend code to find out why.

@codecov-io
Copy link

codecov-io commented Jan 26, 2017

Codecov Report

Merging #200 into master will increase coverage by 0.23%.

@@            Coverage Diff             @@
##           master     #200      +/-   ##
==========================================
+ Coverage   74.78%   75.02%   +0.23%     
==========================================
  Files         111      112       +1     
  Lines        5585     5665      +80     
  Branches      905      919      +14     
==========================================
+ Hits         4177     4250      +73     
- Misses       1408     1415       +7
Impacted Files Coverage Δ
src/sidebar/streamer.js 93.22% <92.3%> (-1.12%)
src/sidebar/session.js 89.42% <ø> (-0.86%)
src/sidebar/host-config.js 100% <ø> (ø)
src/sidebar/util/url-util.js 100% <ø> (ø)
src/sidebar/oauth-auth.js 96.55% <ø> (ø)
src/sidebar/store.js 91.22% <ø> (+0.15%)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4610489...2522cee. Read the comment docs.

Supply the access token to the WebSocket via a query param.

This method is used to send the token because the WebSocket constructor
does not allow setting custom headers. See
hypothesis/product-backlog#154 for context.

An alternative that was tried initially was embedding a username and
password in the URL via `wss://user:password@host/` syntax but that
turned out not to be supported by IE/Edge and required the server to fail
the initial request with a 401 response.

Fixes hypothesis/product-backlog#126
@robertknight
Copy link
Member Author

Following investigation of the different methods available for authenticating the WS endpoint, this has been updated to send the access token via a query param in the WS URL instead of a username/password in the URL. See Slack discussion

robertknight and others added 2 commits January 30, 2017 11:59
Send a "whoami" request [1] after connecting to query the authenticated
user ID for the WS connection.

This makes it easy to check that authentication for the WS worked as
expected by inspecting the frames of the connection in the devtools.

[1] http://h.readthedocs.io/en/latest/realtime/#whoami
* master:
  Remove auth => session dependency
@sean-roberts
Copy link
Contributor

merged master in

Copy link
Contributor

@sean-roberts sean-roberts left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Tested and auth header sent for 3rd party auth and standard auth - as expected

@sean-roberts sean-roberts merged commit 3b5ba2d into master Jan 30, 2017
@chdorner chdorner deleted the websocket-token-auth branch September 22, 2017 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants