Skip to content

Commit

Permalink
feat: get http credentials from cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
johannes-birk committed Aug 11, 2023
1 parent 501b4ab commit 68a9871
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions in_concert/authorization.py
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)

0 comments on commit 68a9871

Please sign in to comment.