Skip to content

Protect a WebSocket #62

Answered by frankie567
davidbrochart asked this question in Q&A
Aug 24, 2022 · 1 comments · 3 replies
Discussion options

You must be logged in to vote

Unfortunately, we hit the same limitations around security dependencies and WebSockets: FastAPI assume an HTTP Request in those, so it won't work with a websocket. Besides, error responses are quite different with WebSockets.

The only solution right now would be to:

  • Retrieve the cookie headers from the WebSocket object.
  • Validate it using the validate_access_token method on the Fief client.
  • Re-implement the logic to get the user data and cache it
  • If something goes wrong, close the socket with an adequate status code : await websocket.close(code=status.WS_1008_POLICY_VIOLATION)

I think it would be interesting though to have it natively in the FastAPI integration; so I'll add it to my back…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@davidbrochart
Comment options

@PAzter1101
Comment options

@PAzter1101
Comment options

Answer selected by davidbrochart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants