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

feat(websocket): add process to authenticate websocket connections #1921

Merged
merged 1 commit into from
Sep 24, 2021

Conversation

ramfox
Copy link
Member

@ramfox ramfox commented Sep 23, 2021

closes #1912

also moves websocket work into its own package

@ramfox ramfox added the feat A code change that adds functionality label Sep 23, 2021
@ramfox ramfox added this to the v0.11.0 milestone Sep 23, 2021
@ramfox ramfox self-assigned this Sep 23, 2021
lib/websocket.go Outdated Show resolved Hide resolved
lib/websocket.go Outdated Show resolved Hide resolved
lib/websocket.go Outdated Show resolved Hide resolved
connID := newID()
SetIDRand(strings.NewReader(randIDStr))

wsh.WSConnectionHandler(mockWebsocketWriterAndRequest())
Copy link
Member Author

Choose a reason for hiding this comment

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

this tests that the wsHandler is setting up a connection & placing it in the conns map.

However, it doesn't set us up to be able to test the wsHandler.read or wsHandler.handleMessage methods. May need to expand on the mockHijacker struct below & create methods that allow us to write on the connection.

lib/websocket.go Outdated Show resolved Hide resolved
lib/websocket.go Outdated Show resolved Hide resolved
lib/websocket.go Outdated Show resolved Hide resolved
lib/websocket.go Outdated Show resolved Hide resolved
lib/websocket.go Outdated Show resolved Hide resolved
lib/websocket.go Outdated Show resolved Hide resolved
lib/websocket.go Outdated Show resolved Hide resolved
lib/websocket.go Outdated Show resolved Hide resolved
lib/websocket.go Outdated Show resolved Hide resolved
lib/websocket.go Outdated Show resolved Hide resolved
Websocket connections need to be scoped so we only send the relevant events to the correct connections.

To accomplish this, we keep track of the profile.ID of each connections using the new `websocket.conn` struct, as well as keeping a map of profile.IDs to connections.

To allow us to authenticate tokens, the `websocket.Handler` must have access to the `key.Store`.

We've added an authentication handshake. Once the connection has been established, the client can request to "subscribe" to a connection, and send over a token as part of the message payload. If that token is valid, we upgrade the connection, and send over a success message. If not, we send over a failure message.

If the client wants to unsubscribe, they send over an "unsubscribe" request. This will remove the association between that connection and a profile.ID. It will not close the connection.

We've also defined a `websocket.message` struct that the websocket expects to receive for communication about the state of the websocket connection (namely the authentication handshake).
@ramfox ramfox marked this pull request as ready for review September 24, 2021 16:56
@ramfox ramfox changed the title wip: add Subscribe & Unsubscribe process to websocket connections feat(websocket): add process to authenticate websocket connections Sep 24, 2021
@ramfox ramfox requested a review from b5 September 24, 2021 17:06
Copy link
Member

@b5 b5 left a comment

Choose a reason for hiding this comment

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

LGTM

@ramfox ramfox merged commit 4e83a0d into master Sep 24, 2021
@ramfox ramfox deleted the ramfox/feat_scoped_websocket branch September 24, 2021 22:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat A code change that adds functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat(lib): scope websocket connections
2 participants