-
-
Notifications
You must be signed in to change notification settings - Fork 6.1k
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
Fix APIKeyCookie to accept websocket #9591
base: master
Are you sure you want to change the base?
Conversation
📝 Docs preview for commit 56beb8f at: https://6475ce858bbc31005a94dcc4--fastapi.netlify.app |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's quite useful PR.
I will be ready to approve it after the author makes the small corrections that I mentioned in the review.
I would also think about making the same with APIKeyQuery
. Code is almost identical.
And.. I think adding the description to this PR will increase chances that it will be accepted soon!
websocket: WebSocket, current_user: User = Depends(get_current_user) | ||
): | ||
await websocket.accept() | ||
await websocket.send_json(current_user.dict()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dict
method is deprecated, use model_dump
async def __call__( | ||
self, | ||
request: Request = None, | ||
websocket: WebSocket = None, | ||
) -> Optional[str]: | ||
api_key = (request or websocket).cookies.get(self.model.name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To get rid of type errors we need to use request: HTTPConnection
. It's common base class for both Request
and WebSocket
, and it has all necessary fields.
So, the code should be:
async def __call__(self, request: HTTPConnection) -> Optional[str]:
api_key = request.cookies.get(self.model.name)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
async def __call__( | |
self, | |
request: Request = None, | |
websocket: WebSocket = None, | |
) -> Optional[str]: | |
api_key = (request or websocket).cookies.get(self.model.name) | |
async def __call__(self, conn: HTTPConnection) -> Optional[str]: | |
api_key = conn.cookies.get(self.model.name) |
async def __call__( | ||
self, | ||
request: Request = None, | ||
websocket: WebSocket = None, | ||
) -> Optional[str]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
async def __call__( | |
self, | |
request: Request = None, | |
websocket: WebSocket = None, | |
) -> Optional[str]: | |
async def __call__(self, conn: HTTPConnection) -> Optional[str]: |
async def __call__( | ||
self, | ||
request: Request = None, | ||
websocket: WebSocket = None, | ||
) -> Optional[str]: | ||
api_key = (request or websocket).cookies.get(self.model.name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
async def __call__( | |
self, | |
request: Request = None, | |
websocket: WebSocket = None, | |
) -> Optional[str]: | |
api_key = (request or websocket).cookies.get(self.model.name) | |
async def __call__(self, conn: HTTPConnection) -> Optional[str]: | |
api_key = conn.cookies.get(self.model.name) |
request: Request = None, | ||
websocket: WebSocket = None, | ||
) -> Optional[str]: | ||
api_key = (request or websocket).cookies.get(self.model.name) | ||
if not api_key: | ||
if self.auto_error: | ||
raise HTTPException( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will work as expected after Starlette 0.37.0 is merged, since we will use the WebSocket Denial Extension.
No description provided.