forked from lightspeed-core/lightspeed-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
32 lines (23 loc) · 1.1 KB
/
Copy pathutils.py
File metadata and controls
32 lines (23 loc) · 1.1 KB
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
"""Authentication utility functions."""
from fastapi import HTTPException
from starlette.datastructures import Headers
from models.responses import UnauthorizedResponse
def extract_user_token(headers: Headers) -> str:
"""Extract the bearer token from an HTTP Authorization header.
Parameters:
headers (Headers): Incoming request headers from which the
Authorization header will be read.
Returns:
str: The bearer token string extracted from the header.
Raises:
HTTPException: If the Authorization header is missing or malformed.
"""
authorization_header = headers.get("Authorization")
if not authorization_header:
response = UnauthorizedResponse(cause="No Authorization header found")
raise HTTPException(**response.model_dump())
scheme_and_token = authorization_header.strip().split()
if len(scheme_and_token) != 2 or scheme_and_token[0].lower() != "bearer":
response = UnauthorizedResponse(cause="No token found in Authorization header")
raise HTTPException(**response.model_dump())
return scheme_and_token[1]