-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path_auth.py
33 lines (29 loc) · 1.06 KB
/
_auth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
__all__ = ('WebSocketOAuth2PasswordBearer',)
from fastapi import WebSocket, status
from fastapi.security import OAuth2PasswordBearer
from fastapi.security.utils import get_authorization_scheme_param
class WebSocketOAuth2PasswordBearer(OAuth2PasswordBearer):
def __init__(
self,
token_url: str,
scheme_name: str | None = None,
scopes: dict[str, str] | None = None,
description: str | None = None,
auto_error: bool = True,
) -> None:
super().__init__(
token_url,
scheme_name,
scopes,
description,
auto_error,
)
async def __call__(self, websocket: WebSocket) -> str | None:
authorization: str = websocket.headers.get('Authorization')
scheme, param = get_authorization_scheme_param(authorization)
if not authorization or scheme.lower() != 'bearer':
if self.auto_error:
await websocket.close(code=status.WS_1008_POLICY_VIOLATION)
else:
return None
return param