-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: get http credentials from cookie
- Loading branch information
1 parent
501b4ab
commit 68a9871
Showing
1 changed file
with
19 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,26 @@ | ||
from typing import Optional | ||
|
||
from fastapi import Request | ||
from fastapi import HTTPException, Request | ||
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer | ||
from fastapi.security.utils import get_authorization_scheme_param | ||
from starlette.status import HTTP_401_UNAUTHORIZED | ||
|
||
|
||
class HTTPBearerWithCookie(HTTPBearer): | ||
async def __call__(self, request: Request) -> Optional[HTTPAuthorizationCredentials]: | ||
pass | ||
authorization: Optional[str] = request.cookies.get("access_token") | ||
scheme, credentials = get_authorization_scheme_param(authorization) | ||
if not (authorization and scheme and credentials): | ||
if self.auto_error: | ||
raise HTTPException(status_code=HTTP_401_UNAUTHORIZED, detail="Not authenticated") | ||
else: | ||
return None | ||
if scheme.lower() != "bearer": | ||
if self.auto_error: | ||
raise HTTPException( | ||
status_code=HTTP_401_UNAUTHORIZED, | ||
detail="Invalid authentication credentials", | ||
) | ||
else: | ||
return None | ||
return HTTPAuthorizationCredentials(scheme=scheme, credentials=credentials) |